本文整理汇总了C++中OsiSolverInterface::writeLp方法的典型用法代码示例。如果您正苦于以下问题:C++ OsiSolverInterface::writeLp方法的具体用法?C++ OsiSolverInterface::writeLp怎么用?C++ OsiSolverInterface::writeLp使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OsiSolverInterface
的用法示例。
在下文中一共展示了OsiSolverInterface::writeLp方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: lCols
//#############################################################################
bfSol*
MibSHeuristic::getBilevelSolution(const double * sol, double origLower)
{
/*
Find a bilevel feasible solution by solving the LL problem
for a fixed UL solution, given by the UL portion of sol
*/
MibSModel * model = MibSModel_;
OsiSolverInterface * oSolver = model->getSolver();
OsiSolverInterface * lSolver = model->bS_->setUpModel(oSolver, true, sol);
//double uObjSense(model->getSolver()->getObjSense());
int lCols(model->getLowerDim());
int uCols(model->getUpperDim());
int * lColIndices = model->getLowerColInd();
int * uColIndices = model->getUpperColInd();
double etol(etol_);
int tCols(uCols + lCols);
int i(0);
if(0){
lSolver->writeLp("bilevelsolver");
std::cout
<< "Original Lower-level Solution Value: " << origLower << std::endl;
for(i = 0; i < lCols; i++){
std::cout << "lowsol[" << i << "]: " << sol[lColIndices[i]] << std::endl;
}
}
if(0){
dynamic_cast<OsiCbcSolverInterface *>
(lSolver)->getModelPtr()->messageHandler()->setLogLevel(0);
}
else{
dynamic_cast<OsiSymSolverInterface *>
(lSolver)->setSymParam("prep_level", -1);
dynamic_cast<OsiSymSolverInterface *>
(lSolver)->setSymParam("verbosity", -2);
dynamic_cast<OsiSymSolverInterface *>
(lSolver)->setSymParam("max_active_nodes", 1);
}
lSolver->branchAndBound();
if(lSolver->isProvenOptimal()){
double objVal(0.0);
double lowerObj(lSolver->getObjValue());
double * colsol = new double[tCols];
for(i = 0; i < uCols; i++){
colsol[uColIndices[i]] = sol[uColIndices[i]];
}
if(0){
std::cout << "candidate lower solution value: " << origLower << std::endl;
std::cout << "actual lower solution value: " << lowerObj << std::endl;
}
if(fabs(origLower - lowerObj) < etol){
//original solution was bilevel feasible
if(0)
std::cout << "Original solution was bilevel feasible:" << std::endl;
for(i = 0; i < lCols; i++){
if(0){
std::cout << "lowerportion["
<< i << "]: " << sol[lColIndices[i]] << std::endl;
}
colsol[lColIndices[i]] = sol[lColIndices[i]];
}
}
else{
if(0){
std::cout << "Not bilevel feasible." << std::endl;
}
for(i = 0; i < lCols; i++){
if(0){
std::cout << "newportion["
<< i << "]: " << lSolver->getColSolution()[i] << std::endl;
}
colsol[lColIndices[i]] = lSolver->getColSolution()[i];
}
}
for(i = 0; i < tCols; i++)
objVal += colsol[i] * oSolver->getObjCoefficients()[i];
bfSol * bfsol =
new bfSol(objVal, colsol);
delete lSolver;
return bfsol;
}
else{
//.........这里部分代码省略.........
示例2: objSense
//#############################################################################
void
MibSHeuristic::lowerObjHeuristic()
{
/*
optimize wrt to lower-level objective
over current feasible lp feasible region
*/
MibSModel * model = MibSModel_;
OsiSolverInterface * oSolver = model->getSolver();
//OsiSolverInterface * hSolver = new OsiCbcSolverInterface();
OsiSolverInterface* hSolver = new OsiSymSolverInterface();
double objSense(model->getLowerObjSense());
int lCols(model->getLowerDim());
int uCols(model->getUpperDim());
int * lColIndices = model->getLowerColInd();
int * uColIndices = model->getUpperColInd();
double * lObjCoeffs = model->getLowerObjCoeffs();
//int tCols(lCols + uCols);
int tCols(oSolver->getNumCols());
//assert(tCols == oSolver->getNumCols());
hSolver->loadProblem(*oSolver->getMatrixByCol(),
oSolver->getColLower(), oSolver->getColUpper(),
oSolver->getObjCoefficients(),
oSolver->getRowLower(), oSolver->getRowUpper());
int j(0);
for(j = 0; j < tCols; j++){
if(oSolver->isInteger(j))
hSolver->setInteger(j);
}
double * nObjCoeffs = new double[tCols];
int i(0), index(0);
CoinZeroN(nObjCoeffs, tCols);
for(i = 0; i < lCols; i++){
index = lColIndices[i];
nObjCoeffs[index] = lObjCoeffs[i];
}
//MibS objective sense is the opposite of OSI's!
hSolver->setObjSense(objSense);
hSolver->setObjective(nObjCoeffs);
//double cutoff(model->getCutoff());
double cutoff(model->getKnowledgeBroker()->getIncumbentValue());
if(model->getNumSolutions()){
CoinPackedVector objCon;
//double rhs(cutoff * objSense);
//double smlTol(1.0);
double rhs(cutoff);
for(i = 0; i < tCols; i++){
objCon.insert(i, oSolver->getObjCoefficients()[i]
* oSolver->getObjSense());
}
hSolver->addRow(objCon, - hSolver->getInfinity(), rhs);
}
if(0)
hSolver->writeLp("lobjheurstic");
if(0){
dynamic_cast<OsiCbcSolverInterface *>
(hSolver)->getModelPtr()->messageHandler()->setLogLevel(0);
}
else{
dynamic_cast<OsiSymSolverInterface *>
(hSolver)->setSymParam("prep_level", -1);
dynamic_cast<OsiSymSolverInterface *>
(hSolver)->setSymParam("verbosity", -2);
dynamic_cast<OsiSymSolverInterface *>
(hSolver)->setSymParam("max_active_nodes", 1);
}
hSolver->branchAndBound();
if(hSolver->isProvenOptimal()){
double upperObjVal(0.0);
/*****************NEW ******************/
MibSSolution *mibSol = NULL;
OsiSolverInterface * lSolver = model->bS_->setUpModel(hSolver, true);
//.........这里部分代码省略.........
示例3: uObjSense
//#############################################################################
mcSol
MibSHeuristic::solveSubproblem(double beta)
{
/*
optimize wrt to weighted upper-level objective
over current feasible lp feasible region
*/
MibSModel * model = MibSModel_;
OsiSolverInterface * oSolver = model->getSolver();
//OsiSolverInterface * sSolver = new OsiCbcSolverInterface();
OsiSolverInterface* sSolver = new OsiSymSolverInterface();
//sSolver = oSolver->clone();
//OsiSolverInterface * sSolver = tmpSolver;
//OsiSolverInterface * tmpSolver = new OsiSolverInterface(oSolver);
double uObjSense(oSolver->getObjSense());
double lObjSense(model->getLowerObjSense());
int lCols(model->getLowerDim());
int uCols(model->getUpperDim());
int * lColIndices = model->getLowerColInd();
int * uColIndices = model->getUpperColInd();
double * lObjCoeffs = model->getLowerObjCoeffs();
const double * uObjCoeffs = oSolver->getObjCoefficients();
double etol(etol_);
int tCols(uCols + lCols);
assert(tCols == oSolver->getNumCols());
sSolver->loadProblem(*oSolver->getMatrixByCol(),
oSolver->getColLower(), oSolver->getColUpper(),
oSolver->getObjCoefficients(),
oSolver->getRowLower(), oSolver->getRowUpper());
int j(0);
for(j = 0; j < tCols; j++){
if(oSolver->isInteger(j))
sSolver->setInteger(j);
}
double * nObjCoeffs = new double[tCols];
int i(0), index(0);
CoinZeroN(nObjCoeffs, tCols);
/* Multiply the UL columns of the UL objective by beta */
for(i = 0; i < uCols; i++){
index = uColIndices[i];
if(fabs(uObjCoeffs[index]) > etol)
nObjCoeffs[index] = beta * uObjCoeffs[index] * uObjSense;
else
nObjCoeffs[index] = 0.0;
}
/* Multiply the LL columns of the UL objective by beta */
for(i = 0; i < lCols; i++){
index = lColIndices[i];
if(fabs(uObjCoeffs[index]) > etol)
nObjCoeffs[index] = beta* uObjCoeffs[index] * uObjSense;
else
nObjCoeffs[index] = 0.0;
}
/* Add the LL columns of the LL objective multiplied by (1 - beta) */
for(i = 0; i < lCols; i++){
index = lColIndices[i];
if(fabs(lObjCoeffs[i]) > etol)
nObjCoeffs[index] += (1 - beta) * lObjCoeffs[i] * lObjSense;
}
sSolver->setObjective(nObjCoeffs);
//int i(0);
if(0){
for(i = 0; i < sSolver->getNumCols(); i++){
std::cout << "betaobj " << sSolver->getObjCoefficients()[i] << std::endl;
}
}
if(0){
sSolver->writeLp("afterbeta");
//sSolver->writeMps("afterbeta");
}
if(0){
for(i = 0; i < sSolver->getNumCols(); i++){
std::cout << "obj " << sSolver->getObjCoefficients()[i] << std::endl;
std::cout << "upper " << sSolver->getColUpper()[i] << std::endl;
std::cout << "lower " << sSolver->getColLower()[i] << std::endl;
}
}
if(0){
dynamic_cast<OsiCbcSolverInterface *>
(sSolver)->getModelPtr()->messageHandler()->setLogLevel(0);
//.........这里部分代码省略.........
示例4: objVal
//#############################################################################
void
MibSBilevel::checkBilevelFeasiblity(bool isRoot)
{
int cutStrategy =
model_->MibSPar_->entry(MibSParams::cutStrategy);
bool warmStartLL =
model_->MibSPar_->entry(MibSParams::warmStartLL);
int maxThreadsLL =
model_->MibSPar_->entry(MibSParams::maxThreadsLL);
int whichCutsLL =
model_->MibSPar_->entry(MibSParams::whichCutsLL);
int probType =
model_->MibSPar_->entry(MibSParams::bilevelProblemType);
std::string feasCheckSolver =
model_->MibSPar_->entry(MibSParams::feasCheckSolver);
if (warmStartLL && (feasCheckSolver == "SYMPHONY") && solver_){
solver_ = setUpModel(model_->getSolver(), false);
}else{
if (solver_){
delete solver_;
}
solver_ = setUpModel(model_->getSolver(), true);
}
OsiSolverInterface *lSolver = solver_;
//CoinWarmStart * ws = getWarmStart();
//if (ws != NULL){
// lSolver->setWarmStart(ws);
//}
//delete ws;
if(1)
lSolver->writeLp("lowerlevel");
if (feasCheckSolver == "Cbc"){
dynamic_cast<OsiCbcSolverInterface *>
(lSolver)->getModelPtr()->messageHandler()->setLogLevel(0);
}else if (feasCheckSolver == "SYMPHONY"){
//dynamic_cast<OsiSymSolverInterface *>
// (lSolver)->setSymParam("prep_level", -1);
sym_environment *env = dynamic_cast<OsiSymSolverInterface *>
(lSolver)->getSymphonyEnvironment();
if (warmStartLL){
sym_set_int_param(env, "keep_warm_start", TRUE);
if (probType == 1){ //Interdiction
sym_set_int_param(env, "should_use_rel_br", FALSE);
sym_set_int_param(env, "use_hot_starts", FALSE);
sym_set_int_param(env, "should_warmstart_node", TRUE);
sym_set_int_param(env, "sensitivity_analysis", TRUE);
sym_set_int_param(env, "sensitivity_bounds", TRUE);
sym_set_int_param(env, "set_obj_upper_lim", FALSE);
}
}
//Always uncomment for debugging!!
sym_set_int_param(env, "do_primal_heuristic", FALSE);
sym_set_int_param(env, "verbosity", -2);
sym_set_int_param(env, "prep_level", -1);
sym_set_int_param(env, "max_active_nodes", maxThreadsLL);
sym_set_int_param(env, "tighten_root_bounds", FALSE);
sym_set_int_param(env, "max_sp_size", 100);
sym_set_int_param(env, "do_reduced_cost_fixing", FALSE);
if (whichCutsLL == 0){
sym_set_int_param(env, "generate_cgl_cuts", FALSE);
}else{
sym_set_int_param(env, "generate_cgl_gomory_cuts", GENERATE_DEFAULT);
}
if (whichCutsLL == 1){
sym_set_int_param(env, "generate_cgl_knapsack_cuts",
DO_NOT_GENERATE);
sym_set_int_param(env, "generate_cgl_probing_cuts",
DO_NOT_GENERATE);
sym_set_int_param(env, "generate_cgl_clique_cuts",
DO_NOT_GENERATE);
sym_set_int_param(env, "generate_cgl_twomir_cuts",
DO_NOT_GENERATE);
sym_set_int_param(env, "generate_cgl_flowcover_cuts",
DO_NOT_GENERATE);
}
}else if (feasCheckSolver == "CPLEX"){
#ifdef USE_CPLEX
lSolver->setHintParam(OsiDoReducePrint);
lSolver->messageHandler()->setLogLevel(0);
CPXENVptr cpxEnv =
dynamic_cast<OsiCpxSolverInterface*>(lSolver)->getEnvironmentPtr();
assert(cpxEnv);
CPXsetintparam(cpxEnv, CPX_PARAM_SCRIND, CPX_OFF);
CPXsetintparam(cpxEnv, CPX_PARAM_THREADS, maxThreadsLL);
#endif
}
//.........这里部分代码省略.........