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