OR-Tools  9.6
simple_glop_program.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 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 #include <ostream>
19 
20 #include "ortools/glop/lp_solver.h"
23 // [END import]
24 
25 namespace operations_research::glop {
27  LinearProgram lp;
28  // Create the variables x and y.
29  ColIndex col_x = lp.FindOrCreateVariable("x");
30  lp.SetVariableBounds(col_x, 0.0, 1.0);
31  ColIndex col_y = lp.FindOrCreateVariable("y");
32  lp.SetVariableBounds(col_y, 0.0, 2.0);
33 
34  // Create linear constraint: 0 <= x + y <= 2.
35  RowIndex row_r1 = lp.FindOrCreateConstraint("r1");
36  lp.SetConstraintBounds(row_r1, 0.0, 2.0);
37  lp.SetCoefficient(row_r1, col_x, 1);
38  lp.SetCoefficient(row_r1, col_y, 1);
39 
40  // Create objective function: 3 * x + y.
41  lp.SetObjectiveCoefficient(col_x, 3);
42  lp.SetObjectiveCoefficient(col_y, 1);
43  lp.SetMaximizationProblem(true);
44 
45  lp.CleanUp();
46 
47  std::cout << "Number of variables = " << lp.num_variables() << std::endl;
48  std::cout << "Number of constraints = " << lp.num_constraints() << std::endl;
49 
50  LPSolver solver;
51  GlopParameters parameters;
52  parameters.set_provide_strong_optimal_guarantee(true);
53  solver.SetParameters(parameters);
54 
55  ProblemStatus status = solver.Solve(lp);
57  std::cout << "Optimal solution found !" << std::endl;
58  // The objective value of the solution.
59  std::cout << "Optimal objective value = " << solver.GetObjectiveValue()
60  << std::endl;
61  // The value of each variable in the solution.
62  const DenseRow& values = solver.variable_values();
63  std::cout << "Solution:" << std::endl
64  << "x = " << values[col_x] << std::endl
65  << ", y = " << values[col_y] << std::endl;
66  return EXIT_SUCCESS;
67  } else {
68  return EXIT_FAILURE;
69  }
70 }
71 } // namespace operations_research::glop
72 
73 int main(int argc, char** argv) {
75 }
76 // [END program]
const DenseRow & variable_values() const
Definition: lp_solver.h:105
Fractional GetObjectiveValue() const
Definition: lp_solver.cc:508
ABSL_MUST_USE_RESULT ProblemStatus Solve(const LinearProgram &lp)
Definition: lp_solver.cc:136
void SetParameters(const GlopParameters &parameters)
Definition: lp_solver.cc:118
void SetVariableBounds(ColIndex col, Fractional lower_bound, Fractional upper_bound)
Definition: lp_data.cc:250
void SetCoefficient(RowIndex row, ColIndex col, Fractional value)
Definition: lp_data.cc:318
ColIndex FindOrCreateVariable(const std::string &variable_id)
Definition: lp_data.cc:206
void SetConstraintBounds(RowIndex row, Fractional lower_bound, Fractional upper_bound)
Definition: lp_data.cc:310
RowIndex FindOrCreateConstraint(const std::string &constraint_id)
Definition: lp_data.cc:219
void SetObjectiveCoefficient(ColIndex col, Fractional value)
Definition: lp_data.cc:327
void SetMaximizationProblem(bool maximize)
Definition: lp_data.cc:344
SatParameters parameters
absl::Status status
Definition: g_gurobi.cc:41
int main(int argc, char **argv)