LinearSolver::SolverWithProtocolBuffers is now static

This commit is contained in:
lperron@google.com
2011-11-19 01:58:48 +00:00
parent bac703de15
commit c9de02ac98
3 changed files with 24 additions and 15 deletions

View File

@@ -33,9 +33,9 @@ void BuildLinearProgrammingMaxExample(MPSolver::OptimizationProblemType type) {
kConstraintCoef3};
const double kConstraintUb[] = {100.0, 600.0, 300.0};
MPSolver solver("Max_Example", type);
const double infinity = solver.infinity();
const double infinity = MPSolver::infinity();
MPModelProto model_proto;
model_proto.set_name("Max_Example");
// Create variables and objective function
for (int j = 0; j < numVars; ++j) {
@@ -77,7 +77,7 @@ void BuildLinearProgrammingMaxExample(MPSolver::OptimizationProblemType type) {
#endif // USE_CLP
MPSolutionResponse solution_response;
solver.SolveWithProtocolBuffers(model_request, &solution_response);
MPSolver::SolveWithProtocolBuffers(model_request, &solution_response);
// The problem has an optimal solution.
CHECK_EQ(MPSolver::OPTIMAL, solution_response.result_status());

View File

@@ -599,11 +599,15 @@ void MPSolver::FillSolutionResponse(MPSolutionResponse* response) const {
}
}
// static
void MPSolver::SolveWithProtocolBuffers(const MPModelRequest& model_request,
MPSolutionResponse* response) {
CHECK_NOTNULL(response);
Clear();
const MPSolver::LoadStatus loadStatus = LoadModel(model_request.model());
const MPModelProto& model = model_request.model();
MPSolver solver(model.name(),
static_cast<MPSolver::OptimizationProblemType>(
model_request.problem_type()));
const MPSolver::LoadStatus loadStatus = solver.LoadModel(model);
if (loadStatus != MPSolver::NO_ERROR) {
LOG(WARNING) << "Loading model from protocol buffer failed, "
<< "load status = " << loadStatus;
@@ -611,10 +615,10 @@ void MPSolver::SolveWithProtocolBuffers(const MPModelRequest& model_request,
}
if (model_request.has_time_limit_ms()) {
set_time_limit(model_request.time_limit_ms());
solver.set_time_limit(model_request.time_limit_ms());
}
Solve();
FillSolutionResponse(response);
solver.Solve();
solver.FillSolutionResponse(response);
}
void MPSolver::Clear() {

View File

@@ -161,6 +161,8 @@ class MPVariable;
// This mathematical programming (MP) solver class is the main class
// though which users build and solve problems.
// TODO(user): either get rid of this enum and use the proto's enum instead,
// or add a unit test that verifies that they are in perfect sync.
class MPSolver {
public:
// The type of problems (LP or MIP) that will be solved and the
@@ -196,9 +198,10 @@ class MPSolver {
// The status of loading the problem from a protocol buffer.
enum LoadStatus {
NO_ERROR, // no error has been encountered.
DUPLICATE_VARIABLE_ID, // error: two variables have the same id.
UNKNOWN_VARIABLE_ID // error: a variable has an unknown id.
NO_ERROR = 0, // no error has been encountered.
// Skip value '1' to stay consistent with the .proto.
DUPLICATE_VARIABLE_ID = 2, // error: two variables have the same id.
UNKNOWN_VARIABLE_ID = 3, // error: a variable has an unknown id.
};
// Advanced usage: possible basis status values for a variable and the
@@ -337,10 +340,12 @@ class MPSolver {
// Solves the model encoded by a MPModelRequest protocol buffer and
// fills the solution encoded as a MPSolutionResponse.
// The model is solved by the interface specified in the constructor
// of MPSolver, MPModelRequest.OptimizationProblemType is ignored.
void SolveWithProtocolBuffers(const MPModelRequest& model_request,
MPSolutionResponse* response);
// Note(user): This creates a temporary MPSolver and destroys it at the
// end. If you want to keep the MPSolver alive (for debugging, or for
// incremental solving), you should write another version of this function
// that creates the MPSolver object on the heap and returns it.
static void SolveWithProtocolBuffers(const MPModelRequest& model_request,
MPSolutionResponse* response);
// ----- Misc -----