OR-Tools  8.2
simple_glop_program.cc
Go to the documentation of this file.
1// Copyright 2010-2018 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14// Minimal example to call the GLOP solver.
15// [START program]
16// [START import]
17#include <iostream>
18
22// [END import]
23
27 // Create the variables x and y.
28 ColIndex col_x = lp.FindOrCreateVariable("x");
29 lp.SetVariableBounds(col_x, 0.0, 1.0);
30 ColIndex col_y = lp.FindOrCreateVariable("y");
31 lp.SetVariableBounds(col_y, 0.0, 2.0);
32
33 // Create linear constraint: 0 <= x + y <= 2.
34 RowIndex row_r1 = lp.FindOrCreateConstraint("r1");
35 lp.SetConstraintBounds(row_r1, 0.0, 2.0);
36 lp.SetCoefficient(row_r1, col_x, 1);
37 lp.SetCoefficient(row_r1, col_y, 1);
38
39 // Create objective function: 3 * x + y.
40 lp.SetObjectiveCoefficient(col_x, 3);
41 lp.SetObjectiveCoefficient(col_y, 1);
43
44 lp.CleanUp();
45
46 std::cout << "Number of variables = " << lp.num_variables() << std::endl;
47 std::cout << "Number of constraints = " << lp.num_constraints() << std::endl;
48
49 LPSolver solver;
50 GlopParameters parameters;
51 parameters.set_provide_strong_optimal_guarantee(true);
53
54 ProblemStatus status = solver.Solve(lp);
55 if (status == ProblemStatus::OPTIMAL) {
56 std::cout << "Optimal solution found !" << std::endl;
57 // The objective value of the solution.
58 std::cout << "Optimal objective value = " << solver.GetObjectiveValue()
59 << std::endl;
60 // The value of each variable in the solution.
61 const DenseRow& values = solver.variable_values();
62 std::cout << "Solution:" << std::endl
63 << "x = " << values[col_x] << std::endl
64 << ", y = " << values[col_y] << std::endl;
65 return EXIT_SUCCESS;
66 } else {
67 return EXIT_FAILURE;
68 }
69}
70} // namespace operations_research::glop
71
72int main(int argc, char** argv) {
74}
75// [END program]
Fractional GetObjectiveValue() const
Definition: lp_solver.cc:476
const DenseRow & variable_values() const
Definition: lp_solver.h:100
ABSL_MUST_USE_RESULT ProblemStatus Solve(const LinearProgram &lp)
Definition: lp_solver.cc:121
void SetParameters(const GlopParameters &parameters)
Definition: lp_solver.cc:113
void SetVariableBounds(ColIndex col, Fractional lower_bound, Fractional upper_bound)
Definition: lp_data.cc:248
void SetCoefficient(RowIndex row, ColIndex col, Fractional value)
Definition: lp_data.cc:316
ColIndex FindOrCreateVariable(const std::string &variable_id)
Definition: lp_data.cc:204
void SetConstraintBounds(RowIndex row, Fractional lower_bound, Fractional upper_bound)
Definition: lp_data.cc:308
RowIndex FindOrCreateConstraint(const std::string &constraint_id)
Definition: lp_data.cc:217
void SetObjectiveCoefficient(ColIndex col, Fractional value)
Definition: lp_data.cc:325
void SetMaximizationProblem(bool maximize)
Definition: lp_data.cc:342
SatParameters parameters
int main(int argc, char **argv)