本文整理汇总了C++中OsiSolverInterface::passInMessageHandler方法的典型用法代码示例。如果您正苦于以下问题:C++ OsiSolverInterface::passInMessageHandler方法的具体用法?C++ OsiSolverInterface::passInMessageHandler怎么用?C++ OsiSolverInterface::passInMessageHandler使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OsiSolverInterface
的用法示例。
在下文中一共展示了OsiSolverInterface::passInMessageHandler方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: memLow
/** Returns a feasible solution to the MINLP
* The heuristic constructs a MIP based approximating all univariate functions appearing in nonlinear constraints
* The linear approximation is obtained by adding inner chords linking pairs of points until covering the range of each variable **/
int
HeuristicInnerApproximation::solution(double &solutionValue, double *betterSolution)
{
if(model_->getNodeCount() || model_->getCurrentPassNumber() > 1) return 0;
if ((model_->getNodeCount()%howOften_)!=0||model_->getCurrentPassNumber()>1)
return 0;
int returnCode = 0; // 0 means it didn't find a feasible solution
OsiTMINLPInterface * nlp = NULL;
if(setup_->getAlgorithm() == B_BB)
nlp = dynamic_cast<OsiTMINLPInterface *>(model_->solver()->clone());
else
nlp = dynamic_cast<OsiTMINLPInterface *>(setup_->nonlinearSolver()->clone());
TMINLP2TNLP* minlp = nlp->problem();
// set tolerances
double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance);
int numberColumns;
int numberRows;
int nnz_jac_g;
int nnz_h_lag;
Ipopt::TNLP::IndexStyleEnum index_style;
minlp->get_nlp_info(numberColumns, numberRows, nnz_jac_g,
nnz_h_lag, index_style);
const Bonmin::TMINLP::VariableType* variableType = minlp->var_types();
const double* x_sol = minlp->x_sol();
double* newSolution = new double [numberColumns];
memcpy(newSolution,x_sol,numberColumns*sizeof(double));
double* new_g_sol = new double [numberRows];
bool feasible = true;
// load the problem to OSI
#ifdef DEBUG_BON_HEURISTIC
cout << "Loading the problem to OSI\n";
#endif
OsiSolverInterface *si = mip_->solver(); // the MIP solver
bool delete_si = false;
if(si == NULL) {
si = new OsiClpSolverInterface;
mip_->setLpSolver(si);
delete_si = true;
}
CoinMessageHandler * handler = model_->messageHandler()->clone();
si->passInMessageHandler(handler);
si->messageHandler()->setLogLevel(2);
#ifdef DEBUG_BON_HEURISTIC
cout << "Loading problem into si\n";
#endif
extractInnerApproximation(*nlp, *si, newSolution, true); // Call the function construncting the inner approximation description
#ifdef DEBUG_BON_HEURISTIC
cout << "problem loaded\n";
cout << "**** Running optimization ****\n";
#endif
mip_->optimize(DBL_MAX, 2, 180); // Optimize the MIP
#ifdef DEBUG_BON_HEURISTIC
cout << "Optimization finished\n";
#endif
if(mip_->getLastSolution()) { // if the MIP solver returns a feasible solution
const double* solution = mip_->getLastSolution();
for (size_t iLCol=0;iLCol<numberColumns;iLCol++) {
newSolution[iLCol] = solution[iLCol];
}
}
else
feasible = false;
if(delete_si) {
delete si;
}
delete handler;
const double* x_l = minlp->x_l();
const double* x_u = minlp->x_u();
const double* g_l = minlp->g_l();
const double* g_u = minlp->g_u();
double primalTolerance = 1.0e-6;
#if 1
if(feasible ) {
std::vector<double> memLow(numberColumns);
std::vector<double> memUpp(numberColumns);
std::copy(minlp->x_l(), minlp->x_l() + numberColumns, memLow.begin());
std::copy(minlp->x_u(), minlp->x_u() + numberColumns, memUpp.begin());
// fix the integer variables and solve the NLP
for (int iColumn=0;iColumn<numberColumns;iColumn++) {
if (variableType[iColumn] != Bonmin::TMINLP::CONTINUOUS) {
double value=floor(newSolution[iColumn]+0.5);
minlp->SetVariableUpperBound(iColumn, value);
minlp->SetVariableLowerBound(iColumn, value);
}
}
//.........这里部分代码省略.........
示例2: constTypes
/** Returns a feasible solution to the MINLP
* The heuristic constructs a MIP based approximating all univariate functions appearing in nonlinear constraints
* The linear approximation is obtained by adding inner chords linking pairs of points until covering the range of each variable **/
int
HeuristicInnerApproximation::solution(double &solutionValue, double *betterSolution)
{
if(model_->getNodeCount() || model_->getCurrentPassNumber() > 1) return 0;
if ((model_->getNodeCount()%howOften_)!=0||model_->getCurrentPassNumber()>1)
return 0;
int returnCode = 0; // 0 means it didn't find a feasible solution
Bonmin::OsiTMINLPInterface * nlp = NULL;
if(setup_->getAlgorithm() == Bonmin::B_BB)
nlp = dynamic_cast<Bonmin::OsiTMINLPInterface *>(model_->solver()->clone());
else
nlp = dynamic_cast<Bonmin::OsiTMINLPInterface *>(setup_->nonlinearSolver()->clone());
Bonmin::TMINLP2TNLP* minlp = nlp->problem();
// set tolerances
//double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance);
int numberColumns;
int numberRows;
int nnz_jac_g;
int nnz_h_lag;
Ipopt::TNLP::IndexStyleEnum index_style;
minlp->get_nlp_info(numberColumns, numberRows, nnz_jac_g,
nnz_h_lag, index_style);
//const Bonmin::TMINLP::VariableType* variableType = minlp->var_types();
const double* x_sol = minlp->x_sol();
double* newSolution = new double [numberColumns];
memcpy(newSolution,x_sol,numberColumns*sizeof(double));
double* new_g_sol = new double [numberRows];
bool feasible = true;
// load the problem to OSI
#ifdef DEBUG_BON_HEURISTIC
std::cout << "Loading the problem to OSI\n";
#endif
OsiSolverInterface *si = mip_->solver(); // the MIP solver
bool delete_si = false;
if(si == NULL) {
si = new OsiClpSolverInterface;
mip_->setLpSolver(si);
delete_si = true;
}
CoinMessageHandler * handler = model_->messageHandler()->clone();
si->passInMessageHandler(handler);
si->messageHandler()->setLogLevel(2);
#ifdef DEBUG_BON_HEURISTIC
std::cout << "Loading problem into si\n";
#endif
extractInnerApproximation(*nlp, *si, newSolution, true); // Call the function construncting the inner approximation description
#ifdef DEBUG_BON_HEURISTIC
std::cout << "problem loaded\n";
std::cout << "**** Running optimization ****\n";
#endif
mip_->optimize(DBL_MAX, 2, time_limit_); // Optimize the MIP
#ifdef DEBUG_BON_HEURISTIC
std::cout << "Optimization finished\n";
#endif
if(mip_->getLastSolution()) { // if the MIP solver returns a feasible solution
const double* solution = mip_->getLastSolution();
std::copy(solution, solution + numberColumns, newSolution);
}
else
feasible = false;
if(delete_si) {
delete si;
}
delete handler;
#if 0 // Set to 1 if you need to test the feasibility of the returned solution
const double* x_l = minlp->x_l();
const double* x_u = minlp->x_u();
const double* g_l = minlp->g_l();
const double* g_u = minlp->g_u();
double primalTolerance = 1.0e-6;
Bonmin::vector<Ipopt::TNLP::LinearityType> constTypes(numberRows);
minlp->get_constraints_linearity(numberRows, constTypes());
feasible = true;
for (int iColumn=0;iColumn<numberColumns;iColumn++) {
double value=newSolution[iColumn];
if(value - x_l[iColumn] < -1e-8|| value - x_u[iColumn] > 1e-8) {
std::cout<<"Solution found infeasible because: "<<std::endl;
std::cout<<"x_l["<<iColumn<<"]= "<<x_l[iColumn]<<" "
<<"x_sol["<<iColumn<<"]= "<<value<<" "
<<"x_u["<<iColumn<<"]= "<<x_u[iColumn]<<std::endl;
feasible = false;
break;
}
//.........这里部分代码省略.........
示例3: matrix
//.........这里部分代码省略.........
objective[i] = beta*(1 - 2*colsol[i]);
}
}
#if 0
// Get dual multipliers and build gradient of the lagrangean
const double * duals = nlp->getRowPrice() + 2 *n;
vector<double> grad(n, 0);
vector<int> indices(n, 0);
tminlp->eval_grad_f(n, x_sol, false, grad());
for(int i = 0 ; i < m ; i++){
if(c_lin[i] == Ipopt::TNLP::LINEAR) continue;
int nnz;
tminlp->eval_grad_gi(n, x_sol, false, i, nnz, indices(), NULL);
tminlp->eval_grad_gi(n, x_sol, false, i, nnz, NULL, grad());
for(int k = 0 ; k < nnz ; k++){
objective[indices[k]] += alpha *duals[i] * grad[k];
}
}
for(int i = 0 ; i < n ; i++){
if(variableType[i] != Bonmin::TMINLP::CONTINUOUS)
objective[i] += alpha * grad[i];
//if(fabs(objective[i]) < 1e-4) objective[i] = 0;
else objective[i] = 0;
}
std::copy(objective.begin(), objective.end(), std::ostream_iterator<double>(std::cout, " "));
std::cout<<std::endl;
#endif
// load the problem to OSI
OsiSolverInterface *si = mip_->solver();
assert(si != NULL);
CoinMessageHandler * handler = model_->messageHandler()->clone();
si->passInMessageHandler(handler);
si->messageHandler()->setLogLevel(1);
si->loadProblem(matrix, model_->solver()->getColLower(), model_->solver()->getColUpper(), objective(),
newRowLower(), newRowUpper());
si->setInteger(idxIntegers(), static_cast<int>(idxIntegers.size()));
si->applyCuts(noGoods);
bool hasFractionnal = true;
while(hasFractionnal){
mip_->optimize(DBL_MAX, 0, 60);
hasFractionnal = false;
#if 0
bool feasible = false;
if(mip_->getLastSolution()) {
const double* solution = mip_->getLastSolution();
std::copy(solution, solution + n, newSolution.begin());
feasible = true;
}
if(feasible) {
// fix the integer variables and solve the NLP
// also add no good cut
CoinPackedVector v;
double lb = 1;
for (int iColumn=0;iColumn<n;iColumn++) {
if (variableType[iColumn] != Bonmin::TMINLP::CONTINUOUS) {
double value=newSolution[iColumn];
if (fabs(floor(value+0.5)-value)>integerTolerance) {
#ifdef DEBUG_BON_HEURISTIC_DIVE_MIP
cout<<"It should be infeasible because: "<<endl;
cout<<"variable "<<iColumn<<" is not integer"<<endl;