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