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

97 lines
3.6 KiB
C++
Raw Permalink Normal View History

2025-01-10 11:35:44 +01:00
// Copyright 2010-2025 Google LLC
// 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.
2025-02-25 16:03:40 +01:00
#include <limits>
#include <string>
2022-02-25 09:47:52 +01:00
#include "ortools/base/init_google.h"
#include "ortools/base/logging.h"
#include "ortools/linear_solver/linear_solver.pb.h"
2025-02-25 16:03:40 +01:00
#include "ortools/linear_solver/solve_mp_model.h"
namespace operations_research {
2025-02-25 16:03:40 +01:00
void BuildLinearProgrammingMaxExample(MPModelRequest::SolverType type) {
2020-10-22 23:36:58 +02:00
const double kObjCoef[] = {10.0, 6.0, 4.0};
const std::string kVarName[] = {"x1", "x2", "x3"};
const int numVars = 3;
const int kNumConstraints = 3;
2020-10-22 23:36:58 +02:00
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};
2020-10-29 14:25:39 +01:00
const double* kConstraintCoef[] = {kConstraintCoef1, kConstraintCoef2,
2020-10-22 23:36:58 +02:00
kConstraintCoef3};
const double kConstraintUb[] = {100.0, 600.0, 300.0};
2025-02-25 16:03:40 +01:00
const double infinity = std::numeric_limits<double>::infinity();
MPModelProto model_proto;
model_proto.set_name("Max_Example");
// Create variables and objective function
for (int j = 0; j < numVars; ++j) {
2020-10-29 14:25:39 +01:00
MPVariableProto* x = model_proto.add_variable();
2020-10-22 23:36:58 +02:00
x->set_name(kVarName[j]); // Could be skipped (optional).
x->set_lower_bound(0.0);
2020-10-22 23:36:58 +02:00
x->set_upper_bound(infinity); // Could be skipped (default value).
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) {
2020-10-29 14:25:39 +01:00
MPConstraintProto* constraint_proto = model_proto.add_constraint();
2020-10-22 23:36:58 +02:00
constraint_proto->set_name(kConstraintName[i]); // Could be skipped.
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]);
}
}
MPModelRequest model_request;
*model_request.mutable_model() = model_proto;
2025-02-25 16:03:40 +01:00
model_request.set_solver_type(type);
2025-02-25 16:03:40 +01:00
const MPSolutionResponse solution_response = SolveMPModel(model_request);
// The problem has an optimal solution.
CHECK_EQ(MPSOLVER_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() {
2018-06-08 16:40:43 +02:00
#if defined(USE_GLOP)
2014-07-09 11:09:30 +00:00
LOG(INFO) << "----- Running Max Example with GLOP -----";
2025-02-25 16:03:40 +01:00
BuildLinearProgrammingMaxExample(MPModelRequest::GLOP_LINEAR_PROGRAMMING);
2020-10-22 23:36:58 +02:00
#endif // USE_GLOP
2018-06-08 16:40:43 +02:00
#if defined(USE_CLP)
LOG(INFO) << "----- Running Max Example with Coin LP -----";
2025-02-25 16:03:40 +01:00
BuildLinearProgrammingMaxExample(MPModelRequest::CLP_LINEAR_PROGRAMMING);
2020-10-22 23:36:58 +02:00
#endif // USE_CLP
}
2020-10-22 23:36:58 +02:00
} // namespace operations_research
2020-10-29 14:25:39 +01:00
int main(int argc, char** argv) {
2022-02-25 09:47:52 +01:00
InitGoogle(argv[0], &argc, &argv, true);
operations_research::RunAllExamples();
2018-11-07 09:52:37 +01:00
return EXIT_SUCCESS;
}