21 #include "absl/flags/parse.h" 22 #include "absl/flags/usage.h" 23 #include "absl/status/statusor.h" 24 #include "absl/strings/str_cat.h" 25 #include "absl/strings/str_join.h" 26 #include "absl/time/time.h" 31 using ::operations_research::math_opt::LinearConstraint;
32 using ::operations_research::math_opt::LinearExpression;
33 using ::operations_research::math_opt::MathOpt;
34 using ::operations_research::math_opt::Result;
35 using ::operations_research::math_opt::SolveParametersProto;
36 using ::operations_research::math_opt::SOLVER_TYPE_GLOP;
37 using ::operations_research::math_opt::SolveResultProto;
38 using ::operations_research::math_opt::SolveStatsProto;
40 using ::operations_research::math_opt::Variable;
42 constexpr
double kInf = std::numeric_limits<double>::infinity();
53 void SolveSimpleLp() {
54 MathOpt optimizer(SOLVER_TYPE_GLOP,
"Linear programming example");
57 std::vector<Variable> x;
58 for (
int j = 0; j < 3; j++) {
60 optimizer.AddContinuousVariable(0.0,
kInf, absl::StrCat(
"x", j)));
64 std::vector<LinearConstraint> constraints;
65 constraints.push_back(optimizer.AddLinearConstraint(
66 10 * x[0] + 4 * x[1] + 5 * x[2] <= 600,
"c1"));
67 constraints.push_back(optimizer.AddLinearConstraint(
68 2 * x[0] + 2 * x[1] + 6 * x[2] <= 300,
"c2"));
70 constraints.push_back(optimizer.AddLinearConstraint(
Sum(x) <= 100,
"c3"));
73 optimizer.objective().Maximize(10 * x[0] + 6 * x[1] + 4 * x[2]);
75 std::cout <<
"Num variables: " << optimizer.num_variables() << std::endl;
76 std::cout <<
"Num constraints: " << optimizer.num_linear_constraints()
79 const Result result = optimizer.Solve(SolveParametersProto()).value();
82 for (
const auto& warning : result.warnings) {
83 LOG(
ERROR) <<
"Solver warning: " << warning << std::endl;
87 <<
"Failed to find an optimal solution: " << result.termination_detail;
89 std::cout <<
"Problem solved in " << result.solve_time() << std::endl;
90 std::cout <<
"Objective value: " << result.objective_value() << std::endl;
92 std::cout <<
"Variable values: [" 93 << absl::StrJoin(result.variable_values().Values(x),
", ") <<
"]" 95 std::cout <<
"Constraint duals: [" 96 << absl::StrJoin(result.dual_values().Values(constraints),
", ")
98 std::cout <<
"Reduced costs: [" 99 << absl::StrJoin(result.reduced_costs().Values(x),
", ") <<
"]" 101 const SolveStatsProto& stat = result.solve_stats;
102 std::cout <<
"Simplex iterations: " << stat.simplex_iterations() << std::endl;
103 std::cout <<
"Barrier iterations: " << stat.barrier_iterations() << std::endl;
109 int main(
int argc,
char** argv) {
111 absl::ParseCommandLine(argc, argv);
void InitGoogleLogging(const char *argv0)
int main(int argc, char **argv)
LinearExpression Sum(const Iterable &items)