Files
ortools-clone/examples/cpp/linear_solver_protocol_buffers.cc

107 lines
4.0 KiB
C++
Raw Normal View History

2013-06-11 14:51:23 +00:00
// Copyright 2010-2013 Google
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string>
#include "base/commandlineflags.h"
#include "base/logging.h"
#include "linear_solver/linear_solver.h"
#include "linear_solver/linear_solver2.pb.h"
namespace operations_research {
void BuildLinearProgrammingMaxExample(MPSolver::OptimizationProblemType type) {
const double kObjCoef[] = {10.0, 6.0, 4.0};
const std::string kVarName[] = {"x1", "x2", "x3"};
const int numVars = 3;
const int kNumConstraints = 3;
const std::string kConstraintName[] = {"c1", "c2", "c3"};
const double kConstraintCoef1[] = {1.0, 1.0, 1.0};
const double kConstraintCoef2[] = {10.0, 4.0, 5.0};
const double kConstraintCoef3[] = {2.0, 2.0, 6.0};
2014-01-08 12:01:58 +00:00
const double* kConstraintCoef[] = {kConstraintCoef1, kConstraintCoef2,
kConstraintCoef3};
const double kConstraintUb[] = {100.0, 600.0, 300.0};
const double infinity = MPSolver::infinity();
new_proto::MPModelProto model_proto;
model_proto.set_name("Max_Example");
// Create variables and objective function
for (int j = 0; j < numVars; ++j) {
new_proto::MPVariableProto* x = model_proto.add_variable();
x->set_name(kVarName[j]); // Could be skipped (optional).
x->set_lower_bound(0.0);
x->set_upper_bound(infinity); // Could be skipped (default value).
2014-01-08 12:01:58 +00:00
x->set_is_integer(false); // Could be skipped (default value).
x->set_objective_coefficient(kObjCoef[j]);
}
model_proto.set_maximize(true);
// Create constraints
for (int i = 0; i < kNumConstraints; ++i) {
new_proto::MPConstraintProto* constraint_proto =
model_proto.add_constraint();
constraint_proto->set_name(kConstraintName[i]); // Could be skipped.
2014-01-08 12:01:58 +00:00
constraint_proto->set_lower_bound(-infinity); // Could be skipped.
constraint_proto->set_upper_bound(kConstraintUb[i]);
for (int j = 0; j < numVars; ++j) {
// These two lines may be skipped when the coefficient is zero.
constraint_proto->add_var_index(j);
constraint_proto->add_coefficient(kConstraintCoef[i][j]);
}
}
new_proto::MPModelRequest model_request;
model_request.mutable_model()->CopyFrom(model_proto);
#if defined(USE_GLPK)
if (type == MPSolver::GLPK_LINEAR_PROGRAMMING) {
model_request.set_solver_type(new_proto::MPModelRequest::GLPK_LINEAR_PROGRAMMING);
}
#endif // USE_GLPK
#if defined(USE_CLP)
if (type == MPSolver::CLP_LINEAR_PROGRAMMING) {
model_request.set_solver_type(new_proto::MPModelRequest::CLP_LINEAR_PROGRAMMING);
}
#endif // USE_CLP
new_proto::MPSolutionResponse solution_response;
MPSolver::SolveWithProto(model_request, &solution_response);
// The problem has an optimal solution.
CHECK_EQ(new_proto::MPSolutionResponse::OPTIMAL, solution_response.status());
2014-01-08 12:01:58 +00:00
LOG(INFO) << "objective = " << solution_response.objective_value();
for (int j = 0; j < numVars; ++j) {
LOG(INFO) << model_proto.variable(j).name() << " = "
<< solution_response.variable_value(j);
2011-09-17 13:33:29 +00:00
}
}
void RunAllExamples() {
#if defined(USE_GLPK)
LOG(INFO) << "----- Running Max Example with GLPK -----";
BuildLinearProgrammingMaxExample(MPSolver::GLPK_LINEAR_PROGRAMMING);
#endif // USE_GLPK
#if defined(USE_CLP)
LOG(INFO) << "----- Running Max Example with Coin LP -----";
BuildLinearProgrammingMaxExample(MPSolver::CLP_LINEAR_PROGRAMMING);
#endif // USE_CLP
}
} // namespace operations_research
2014-01-08 12:01:58 +00:00
int main(int argc, char** argv) {
google::ParseCommandLineFlags( &argc, &argv, true);
operations_research::RunAllExamples();
return 0;
}