OR-Tools  9.6
simple_pdlp_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 // Solves a simple LP using PDLP's direct C++ API.
15 //
16 // Note: The direct API is generally for advanced use cases. It is matrix-based,
17 // that is, you specify the LP using matrices and vectors instead of algebraic
18 // expressions. You can also use PDLP via the algebraic MPSolver API (see
19 // linear_solver/samples/simple_lp_program.cc).
20 #include <cstdint>
21 #include <iostream>
22 #include <limits>
23 #include <optional>
24 #include <vector>
25 
26 #include "Eigen/Core"
27 #include "Eigen/SparseCore"
32 #include "ortools/pdlp/solve_log.pb.h"
33 #include "ortools/pdlp/solvers.pb.h"
34 
35 namespace pdlp = ::operations_research::pdlp;
36 
37 constexpr double kInfinity = std::numeric_limits<double>::infinity();
38 
39 // Returns a small LP:
40 // min 5.5 x_0 - 2 x_1 - x_2 + x_3 - 14 s.t.
41 // 2 x_0 + x_1 + x_2 + 2 x_3 = 12
42 // x_0 + x_2 <= 7
43 // 4 x_0 >= -4
44 // -1 <= 1.5 x_2 - x_3 <= 1
45 // -infinity <= x_0 <= infinity
46 // -2 <= x_1 <= infinity
47 // -infinity <= x_2 <= 6
48 // 2.5 <= x_3 <= 3.5
50  pdlp::QuadraticProgram lp(4, 4);
51  // "<<" is Eigen's syntax for initialization.
52  lp.constraint_lower_bounds << 12, -kInfinity, -4, -1;
53  lp.constraint_upper_bounds << 12, 7, kInfinity, 1;
54  lp.variable_lower_bounds << -kInfinity, -2, -kInfinity, 2.5;
56  const std::vector<Eigen::Triplet<double, int64_t>>
57  constraint_matrix_triplets = {{0, 0, 2}, {0, 1, 1}, {0, 2, 1},
58  {0, 3, 2}, {1, 0, 1}, {1, 2, 1},
59  {2, 0, 4}, {3, 2, 1.5}, {3, 3, -1}};
60  lp.constraint_matrix.setFromTriplets(constraint_matrix_triplets.begin(),
61  constraint_matrix_triplets.end());
62  lp.objective_vector << 5.5, -2, -1, 1;
63  lp.objective_offset = -14;
64  return lp;
65 }
66 
67 int main(int argc, char* argv[]) {
68  InitGoogle(argv[0], &argc, &argv, /*remove_flags=*/true);
69 
70  pdlp::PrimalDualHybridGradientParams params;
71  // Below are some common parameters to modify. Here, we just re-assign the
72  // defaults.
73  params.mutable_termination_criteria()
74  ->mutable_simple_optimality_criteria()
75  ->set_eps_optimal_relative(1.0e-6);
76  params.mutable_termination_criteria()
77  ->mutable_simple_optimality_criteria()
78  ->set_eps_optimal_absolute(1.0e-6);
79  params.mutable_termination_criteria()->set_time_sec_limit(kInfinity);
80  params.set_num_threads(1);
81  params.set_verbosity_level(0);
82  params.mutable_presolve_options()->set_use_glop(false);
83 
84  const pdlp::SolverResult result =
86  const pdlp::SolveLog& solve_log = result.solve_log;
87 
88  if (solve_log.termination_reason() == pdlp::TERMINATION_REASON_OPTIMAL) {
89  std::cout << "Solve successful" << std::endl;
90  } else {
91  std::cout << "Solve not successful. Status: "
92  << pdlp::TerminationReason_Name(solve_log.termination_reason())
93  << std::endl;
94  }
95 
96  // Solutions vectors are always returned. *However*, their interpretation
97  // depends on termination_reason! See primal_dual_hybrid_gradient.h for more
98  // details on what the vectors mean if termination_reason is not
99  // TERMINATION_REASON_OPTIMAL.
100  std::cout << "Primal solution:\n" << result.primal_solution << std::endl;
101  std::cout << "Dual solution:\n" << result.dual_solution << std::endl;
102  std::cout << "Reduced costs:\n" << result.reduced_costs << std::endl;
103 
104  const pdlp::PointType solution_type = solve_log.solution_type();
105  std::cout << "Solution type: " << pdlp::PointType_Name(solution_type)
106  << std::endl;
107  const std::optional<pdlp::ConvergenceInformation> ci =
108  pdlp::GetConvergenceInformation(solve_log.solution_stats(),
109  solution_type);
110  if (ci.has_value()) {
111  std::cout << "Primal objective: " << ci->primal_objective() << std::endl;
112  std::cout << "Dual objective: " << ci->dual_objective() << std::endl;
113  }
114 
115  std::cout << "Iterations: " << solve_log.iteration_count() << std::endl;
116  std::cout << "Solve time (sec): " << solve_log.solve_time_sec() << std::endl;
117 
118  return 0;
119 }
void InitGoogle(const char *usage, int *argc, char ***argv, bool deprecated)
Definition: init_google.h:34
SolverResult PrimalDualHybridGradient(QuadraticProgram qp, const PrimalDualHybridGradientParams &params, const std::atomic< bool > *interrupt_solve, IterationStatsCallback iteration_stats_callback)
std::optional< ConvergenceInformation > GetConvergenceInformation(const IterationStats &stats, PointType candidate_type)
int main(int argc, char *argv[])
constexpr double kInfinity
pdlp::QuadraticProgram SimpleLp()
Eigen::SparseMatrix< double, Eigen::ColMajor, int64_t > constraint_matrix