Commit 88576660 authored by Hans-Christian Ebke's avatar Hans-Christian Ebke

CoMISo/NSolver: Fixed load/save of GUROBI solutions/models.


git-svn-id: http://www.openflipper.org/svnrepo/CoMISo/trunk@116 1355f012-dd97-4b2f-ae87-10fa9f823a57
parent 8756de5a
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
#include "GUROBISolver.hh" #include "GUROBISolver.hh"
#include <stdexcept>
//== NAMESPACES =============================================================== //== NAMESPACES ===============================================================
namespace COMISO { namespace COMISO {
...@@ -138,6 +140,7 @@ solve(NProblemInterface* _problem, ...@@ -138,6 +140,7 @@ solve(NProblemInterface* _problem,
model.set(GRB_IntAttr_ModelSense, 1); model.set(GRB_IntAttr_ModelSense, 1);
model.setObjective(objective); model.setObjective(objective);
model.update();
//---------------------------------------------- //----------------------------------------------
...@@ -165,8 +168,6 @@ solve(NProblemInterface* _problem, ...@@ -165,8 +168,6 @@ solve(NProblemInterface* _problem,
std::cout << "Reading solution from file \"" << solution_input_path_ << "\"." << std::endl; std::cout << "Reading solution from file \"" << solution_input_path_ << "\"." << std::endl;
} }
model.optimize();
//---------------------------------------------- //----------------------------------------------
// 5. store result // 5. store result
//---------------------------------------------- //----------------------------------------------
...@@ -179,13 +180,22 @@ solve(NProblemInterface* _problem, ...@@ -179,13 +180,22 @@ solve(NProblemInterface* _problem,
} }
else else
{ {
// store loaded result std::cout << "Loading stored solution from \"" << solution_input_path_ << "\"." << std::endl;
GurobiHelper::readSolutionVectorFromSOL(x, solution_input_path_); // store loaded result
const size_t oldSize = x.size();
x.clear();
GurobiHelper::readSolutionVectorFromSOL(x, solution_input_path_);
if (oldSize != x.size()) {
std::cerr << "oldSize != x.size() <=> " << oldSize << " != " << x.size() << std::endl;
throw std::runtime_error("Loaded solution vector doesn't have expected dimension.");
}
} }
_problem->store_result(P(x)); _problem->store_result(P(x));
std::cout << "GUROBI Objective: " << model.get(GRB_DoubleAttr_ObjVal) << std::endl; // ObjVal is only available if the optimize was called.
if (solution_input_path_.empty())
std::cout << "GUROBI Objective: " << model.get(GRB_DoubleAttr_ObjVal) << std::endl;
return true; return true;
} }
catch(GRBException e) catch(GRBException e)
......
...@@ -51,9 +51,9 @@ static void moveConstantTermIntoConstrainedVariable(GRBModel &model) { ...@@ -51,9 +51,9 @@ static void moveConstantTermIntoConstrainedVariable(GRBModel &model) {
//tmpModel.getObjective().addConstant(-constantTerm); //tmpModel.getObjective().addConstant(-constantTerm);
model.getObjective() -= constantTerm; model.getObjective() -= constantTerm;
#if OUTPUT_CONSTANT_AS_CONT #if OUTPUT_CONSTANT_AS_CONT
model.addVar(constantTerm, constantTerm, 1, GRB_CONTINUOUS, "constant"); model.addVar(constantTerm, constantTerm, 1, GRB_CONTINUOUS, "MIQ_synthetic_constant");
#else #else
model.addVar(1, 1, constantTerm, GRB_INTEGER, "constant"); model.addVar(1, 1, constantTerm, GRB_INTEGER, "MIQ_synthetic_constant");
#endif #endif
} }
...@@ -144,7 +144,7 @@ void GurobiHelper::importInitialSolutionIntoModel(GRBModel &model, const std::st ...@@ -144,7 +144,7 @@ void GurobiHelper::importInitialSolutionIntoModel(GRBModel &model, const std::st
//moveConstantTermIntoConstrainedVariable(model); //moveConstantTermIntoConstrainedVariable(model);
const double constantTerm = model.getObjective().getLinExpr().getConstant(); const double constantTerm = model.getObjective().getLinExpr().getConstant();
model.addVar(constantTerm, constantTerm, 0, GRB_CONTINUOUS, "constant"); model.addVar(constantTerm, constantTerm, 0, GRB_CONTINUOUS, "MIQ_synthetic_constant");
model.update(); model.update();
model.read(fileName); model.read(fileName);
...@@ -157,15 +157,15 @@ void GurobiHelper::readSolutionVectorFromSOL(std::vector<double> &out_solution_, ...@@ -157,15 +157,15 @@ void GurobiHelper::readSolutionVectorFromSOL(std::vector<double> &out_solution_,
// throw std::runtime_error("Unable to open file \"" + fileName + "\"."); // throw std::runtime_error("Unable to open file \"" + fileName + "\".");
static const boost::regex commentRe("\\s*#", boost::regex_constants::perl); static const boost::regex commentRe("\\s*#", boost::regex_constants::perl);
static const boost::regex variableRe("\\s*\\S+\\s+([-+]?[0-9]*\\.?[0-9]+(?:[eE][-+]?[0-9]+)?)", boost::regex_constants::perl); static const boost::regex variableRe("\\s*(\\S+)\\s+([-+]?[0-9]*\\.?[0-9]+(?:[eE][-+]?[0-9]+)?)", boost::regex_constants::perl);
std::string line; std::string line;
while (solFile) { while (solFile) {
std::getline(solFile, line); std::getline(solFile, line);
if (boost::regex_search(line, commentRe, boost::match_continuous)) continue; if (boost::regex_search(line, commentRe, boost::match_continuous)) continue;
boost::smatch match; boost::smatch match;
if (boost::regex_search(line, match, variableRe, boost::match_continuous)) { if (boost::regex_search(line, match, variableRe, boost::match_continuous) && match[1] != "MIQ_synthetic_constant") {
out_solution_.push_back(boost::lexical_cast<double>(match[1])); out_solution_.push_back(boost::lexical_cast<double>(match[2]));
} }
} }
} }
......
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