Commit 9d11ce28 authored by David Bommes's avatar David Bommes

implemented CPLEXSolver

refined example small_miqp

git-svn-id: http://www.openflipper.org/svnrepo/CoMISo/trunk@133 1355f012-dd97-4b2f-ae87-10fa9f823a57
parent 5c8ab611
......@@ -46,7 +46,7 @@ public:
// specify a function which has several local minima
// f(x,y)=(x-2y+1)^2 + (x-2)^2
// f(x,y)=(x-2y+1)^2 + (x-5)^2
// number of unknown variables, here x and y = 2
virtual int n_unknowns ( )
......@@ -65,7 +65,7 @@ public:
virtual double eval_f ( const double* _x )
{
double term = _x[0] - 2.0*_x[1] + 1.0;
double term2 = _x[0] - 2.0;
double term2 = _x[0] - 5.0;
return term*term + term2*term2;
}
......@@ -74,7 +74,7 @@ public:
virtual void eval_gradient( const double* _x, double* _g)
{
double term = _x[0] - 2.0*_x[1] + 1.0;
double term2 = _x[0] - 2.0;
double term2 = _x[0] - 5.0;
_g[0] = 2.0*term + 2.0*term2;
_g[1] = -4.0*term;
......@@ -122,11 +122,11 @@ int main(void)
std::cout << "---------- 4) setup constraints..." << std::endl;
std::vector<COMISO::NConstraintInterface*> constraints;
// setup constraint x+y <= 0
// setup constraint x+y <= 6.5
COMISO::LinearConstraint::SVectorNC coeffs(2);
coeffs.coeffRef(0) = 1.0;
coeffs.coeffRef(1) = 1.0;
COMISO::LinearConstraint lc(coeffs, 0, COMISO::LinearConstraint::NC_LESS_EQUAL);
COMISO::LinearConstraint lc(coeffs, -6.5, COMISO::LinearConstraint::NC_LESS_EQUAL);
constraints.push_back(&lc);
......
......@@ -40,113 +40,119 @@ solve(NProblemInterface* _problem,
try
{
// //----------------------------------------------
// // 0. set up environment
// //----------------------------------------------
//
// GRBEnv env = GRBEnv();
// GRBModel model = GRBModel(env);
//
// model.getEnv().set(GRB_DoubleParam_TimeLimit, _time_limit);
//
//
// //----------------------------------------------
// // 1. allocate variables
// //----------------------------------------------
//
// // determine variable types: 0->real, 1->integer, 2->bool
// std::vector<char> vtypes(_problem->n_unknowns(),0);
// for(unsigned int i=0; i<_discrete_constraints.size(); ++i)
// switch(_discrete_constraints[i].second)
// {
// case Integer: vtypes[_discrete_constraints[i].first] = 1; break;
// case Binary : vtypes[_discrete_constraints[i].first] = 2; break;
// default : break;
// }
//
// // GUROBI variables
// std::vector<GRBVar> vars;
// // first all
// for( int i=0; i<_problem->n_unknowns(); ++i)
// switch(vtypes[i])
// {
// case 0 : vars.push_back( model.addVar(-GRB_INFINITY, GRB_INFINITY, 0.0, GRB_CONTINUOUS) ); break;
// case 1 : vars.push_back( model.addVar(-GRB_INFINITY, GRB_INFINITY, 0.0, GRB_INTEGER ) ); break;
// case 2 : vars.push_back( model.addVar(-GRB_INFINITY, GRB_INFINITY, 0.0, GRB_BINARY ) ); break;
// }
//
//
// // Integrate new variables
//----------------------------------------------
// 0. set up environment
//----------------------------------------------
IloModel model(env_);
// model.getEnv().set(GRB_DoubleParam_TimeLimit, _time_limit);
//----------------------------------------------
// 1. allocate variables
//----------------------------------------------
// determine variable types: 0->real, 1->integer, 2->bool
std::vector<char> vtypes(_problem->n_unknowns(),0);
for(unsigned int i=0; i<_discrete_constraints.size(); ++i)
switch(_discrete_constraints[i].second)
{
case Integer: vtypes[_discrete_constraints[i].first] = 1; break;
case Binary : vtypes[_discrete_constraints[i].first] = 2; break;
default : break;
}
// CPLEX variables
std::vector<IloNumVar> vars;
// first all
for( int i=0; i<_problem->n_unknowns(); ++i)
switch(vtypes[i])
{
case 0 : vars.push_back( IloNumVar(env_,-IloInfinity, IloInfinity, IloNumVar::Float) ); break;
case 1 : vars.push_back( IloNumVar(env_, -IloIntMax, IloIntMax, IloNumVar::Int) ); break;
case 2 : vars.push_back( IloNumVar(env_, 0, 1, IloNumVar::Bool) ); break;
}
// Integrate new variables
// model.update();
//
// //----------------------------------------------
// // 2. setup constraints
// //----------------------------------------------
//
// // get zero vector
// std::vector<double> x(_problem->n_unknowns(), 0.0);
//
// for(unsigned int i=0; i<_constraints.size(); ++i)
// {
// if(!_constraints[i]->is_linear())
// std::cerr << "Warning: GUROBISolver received a problem with non-linear constraints!!!" << std::endl;
//
// GRBLinExpr lin_expr;
// NConstraintInterface::SVectorNC gc;
// _constraints[i]->eval_gradient(P(x), gc);
//
// NConstraintInterface::SVectorNC::InnerIterator v_it(gc);
// for(; v_it; ++v_it)
//// lin_expr += v_it.value()*vars[v_it.index()];
// lin_expr = lin_expr + vars[v_it.index()]*v_it.value();
//
// double b = _constraints[i]->eval_constraint(P(x));
//
// switch(_constraints[i]->constraint_type())
// {
// case NConstraintInterface::NC_EQUAL : model.addConstr(lin_expr + b == 0); break;
// case NConstraintInterface::NC_LESS_EQUAL : model.addConstr(lin_expr + b <= 0); break;
// case NConstraintInterface::NC_GREATER_EQUAL : model.addConstr(lin_expr + b >= 0); break;
// }
// }
//----------------------------------------------
// 2. setup constraints
//----------------------------------------------
// get zero vector
std::vector<double> x(_problem->n_unknowns(), 0.0);
for(unsigned int i=0; i<_constraints.size(); ++i)
{
if(!_constraints[i]->is_linear())
std::cerr << "Warning: GUROBISolver received a problem with non-linear constraints!!!" << std::endl;
IloExpr lin_expr(env_);
NConstraintInterface::SVectorNC gc;
_constraints[i]->eval_gradient(P(x), gc);
NConstraintInterface::SVectorNC::InnerIterator v_it(gc);
for(; v_it; ++v_it)
// lin_expr += IloNumExpr(vars[v_it.index()]);
lin_expr += vars[v_it.index()]*v_it.value();
double b = _constraints[i]->eval_constraint(P(x));
switch(_constraints[i]->constraint_type())
{
case NConstraintInterface::NC_EQUAL : model.add(lin_expr + b == 0); break;
case NConstraintInterface::NC_LESS_EQUAL : model.add(lin_expr + b <= 0); break;
case NConstraintInterface::NC_GREATER_EQUAL : model.add(lin_expr + b >= 0); break;
}
}
// model.update();
//
// //----------------------------------------------
// // 3. setup energy
// //----------------------------------------------
//
// if(!_problem->constant_hessian())
// std::cerr << "Warning: GUROBISolver received a problem with non-constant hessian!!!" << std::endl;
//
//----------------------------------------------
// 3. setup energy
//----------------------------------------------
if(!_problem->constant_hessian())
std::cerr << "Warning: CPLEXSolver received a problem with non-constant hessian!!!" << std::endl;
// GRBQuadExpr objective;
//
// // add quadratic part
// NProblemInterface::SMatrixNP H;
// _problem->eval_hessian(P(x), H);
// for( int i=0; i<H.outerSize(); ++i)
// for (NProblemInterface::SMatrixNP::InnerIterator it(H,i); it; ++it)
// objective += 0.5*it.value()*vars[it.row()]*vars[it.col()];
//
//
// // add linear part
// std::vector<double> g(_problem->n_unknowns());
// _problem->eval_gradient(P(x), P(g));
// for(unsigned int i=0; i<g.size(); ++i)
// objective += g[i]*vars[i];
//
// // add constant part
// objective += _problem->eval_f(P(x));
//
IloExpr objective(env_);
//
// add quadratic part
NProblemInterface::SMatrixNP H;
_problem->eval_hessian(P(x), H);
for( int i=0; i<H.outerSize(); ++i)
for (NProblemInterface::SMatrixNP::InnerIterator it(H,i); it; ++it)
objective += 0.5*it.value()*vars[it.row()]*vars[it.col()];
// add linear part
std::vector<double> g(_problem->n_unknowns());
_problem->eval_gradient(P(x), P(g));
for(unsigned int i=0; i<g.size(); ++i)
objective += g[i]*vars[i];
// add constant part
objective += _problem->eval_f(P(x));
model.add(IloMinimize(env_,objective));
// model.set(GRB_IntAttr_ModelSense, 1);
// model.setObjective(objective);
// model.update();
//
//
// //----------------------------------------------
// // 4. solve problem
// //----------------------------------------------
//
//
//----------------------------------------------
// 4. solve problem
//----------------------------------------------
IloCplex cplex(model);
cplex.solve();
// if (solution_input_path_.empty())
// {
// if (!problem_env_output_path_.empty())
......@@ -167,16 +173,24 @@ solve(NProblemInterface* _problem,
// std::cout << "Reading solution from file \"" << solution_input_path_ << "\"." << std::endl;
// }
//
// //----------------------------------------------
// // 5. store result
// //----------------------------------------------
//
// if (solution_input_path_.empty())
// {
// // store computed result
// for(unsigned int i=0; i<vars.size(); ++i)
// x[i] = vars[i].get(GRB_DoubleAttr_X);
// }
//----------------------------------------------
// 5. store result
//----------------------------------------------
// store computed result
for(unsigned int i=0; i<vars.size(); ++i)
x[i] = cplex.getValue(vars[i]);
_problem->store_result(P(x));
/*
if (solution_input_path_.empty())
{
// store computed result
for(unsigned int i=0; i<vars.size(); ++i)
x[i] = vars[i].get(GRB_DoubleAttr_X);
}
*/
// else
// {
// std::cout << "Loading stored solution from \"" << solution_input_path_ << "\"." << std::endl;
......
......@@ -50,7 +50,7 @@ public:
CPLEXSolver();
/// Destructor
~CPLEXSolver() { cplex_env_.end();}
~CPLEXSolver() { env_.end();}
// ********** SOLVE **************** //
bool solve(NProblemInterface* _problem, // problem instance
......@@ -74,7 +74,7 @@ protected:
private:
// CPLEX environment
IloEnv cplex_env_;
IloEnv env_;
// filenames for exporting/importing gurobi solutions
// if string is empty nothing is imported or exported
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment