OR-Tools  9.6
pdlp_bridge.h
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 #ifndef OR_TOOLS_MATH_OPT_SOLVERS_PDLP_BRIDGE_H_
15 #define OR_TOOLS_MATH_OPT_SOLVERS_PDLP_BRIDGE_H_
16 
17 #include <cstdint>
18 #include <optional>
19 #include <vector>
20 
21 #include "Eigen/Core"
22 #include "absl/container/flat_hash_map.h"
23 #include "absl/status/statusor.h"
25 #include "ortools/math_opt/model.pb.h"
26 #include "ortools/math_opt/model_parameters.pb.h"
27 #include "ortools/math_opt/sparse_containers.pb.h"
30 
31 namespace operations_research {
32 namespace math_opt {
33 
34 // Builds a PDLP model (QuadraticProgram) from ModelProto, and provides methods
35 // to translate solutions back and forth.
36 //
37 // The primary difference in the models are:
38 // 1. PDLP maps the variable/constraint ids to consecutive indices
39 // [0, 1, ..., n).
40 // 2. PDLP does not support maximization. If the ModelProto is a maximization
41 // problem, the objective is negated (coefficients and offset) before
42 // passing to PDLP. On the way back, the objective value, and all dual
43 // variables/reduced costs (also for rays) must be negated.
44 //
45 // Throughout, it is assumed that the MathOpt protos have been validated, but
46 // no assumption is made on the PDLP output. Any Status errors resulting from
47 // invalid PDLP output use the status code kInternal.
48 class PdlpBridge {
49  public:
50  PdlpBridge() = default;
51  static absl::StatusOr<PdlpBridge> FromProto(const ModelProto& model_proto);
52 
53  const pdlp::QuadraticProgram& pdlp_lp() const { return pdlp_lp_; }
54 
55  // Returns the ids of variables and linear constraints with inverted bounds.
57 
58  // TODO(b/183616124): we need to support the inverse of these methods for
59  // warm start.
60  absl::StatusOr<SparseDoubleVectorProto> PrimalVariablesToProto(
61  const Eigen::VectorXd& primal_values,
62  const SparseVectorFilterProto& variable_filter) const;
63  absl::StatusOr<SparseDoubleVectorProto> DualVariablesToProto(
64  const Eigen::VectorXd& dual_values,
65  const SparseVectorFilterProto& linear_constraint_filter) const;
66  absl::StatusOr<SparseDoubleVectorProto> ReducedCostsToProto(
67  const Eigen::VectorXd& reduced_costs,
68  const SparseVectorFilterProto& variable_filter) const;
70  const SolutionHintProto& solution_hint) const;
71 
72  private:
73  pdlp::QuadraticProgram pdlp_lp_;
74  absl::flat_hash_map<int64_t, int64_t> var_id_to_pdlp_index_;
75  // NOTE: this vector is strictly increasing
76  std::vector<int64_t> pdlp_index_to_var_id_;
77  absl::flat_hash_map<int64_t, int64_t> lin_con_id_to_pdlp_index_;
78  // NOTE: this vector is strictly increasing
79  std::vector<int64_t> pdlp_index_to_lin_con_id_;
80 };
81 
82 } // namespace math_opt
83 } // namespace operations_research
84 
85 #endif // OR_TOOLS_MATH_OPT_SOLVERS_PDLP_BRIDGE_H_
absl::StatusOr< SparseDoubleVectorProto > DualVariablesToProto(const Eigen::VectorXd &dual_values, const SparseVectorFilterProto &linear_constraint_filter) const
Definition: pdlp_bridge.cc:195
const pdlp::QuadraticProgram & pdlp_lp() const
Definition: pdlp_bridge.h:53
static absl::StatusOr< PdlpBridge > FromProto(const ModelProto &model_proto)
Definition: pdlp_bridge.cc:80
InvertedBounds ListInvertedBounds() const
Definition: pdlp_bridge.cc:169
pdlp::PrimalAndDualSolution SolutionHintToWarmStart(const SolutionHintProto &solution_hint) const
Definition: pdlp_bridge.cc:209
absl::StatusOr< SparseDoubleVectorProto > PrimalVariablesToProto(const Eigen::VectorXd &primal_values, const SparseVectorFilterProto &variable_filter) const
Definition: pdlp_bridge.cc:189
absl::StatusOr< SparseDoubleVectorProto > ReducedCostsToProto(const Eigen::VectorXd &reduced_costs, const SparseVectorFilterProto &variable_filter) const
Definition: pdlp_bridge.cc:202
CpModelProto const * model_proto
Collection of objects used to extend the Constraint Solver library.