Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
sparse_containers.h
Go to the documentation of this file.
1// Copyright 2010-2024 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// IWYU pragma: private, include "ortools/math_opt/cpp/math_opt.h"
15// IWYU pragma: friend "ortools/math_opt/cpp/.*"
16
17#ifndef OR_TOOLS_MATH_OPT_CPP_SPARSE_CONTAINERS_H_
18#define OR_TOOLS_MATH_OPT_CPP_SPARSE_CONTAINERS_H_
19
20#include <cstdint>
21
22#include "absl/container/flat_hash_map.h"
29#include "ortools/math_opt/solution.pb.h"
30#include "ortools/math_opt/sparse_containers.pb.h"
32
34
35// Returns the VariableMap<double> equivalent to `vars_proto`.
36//
37// Requires that (or returns a status error):
38// * vars_proto.ids and vars_proto.values have equal size.
39// * vars_proto.ids is sorted.
40// * vars_proto.ids has elements that are variables in `model` (this implies
41// that each id is in [0, max(int64_t))).
42//
43// Note that the values of vars_proto.values are not checked (it may have NaNs).
44absl::StatusOr<VariableMap<double>> VariableValuesFromProto(
45 const ModelStorage* model, const SparseDoubleVectorProto& vars_proto);
46
47// Returns the VariableMap<int32_t> equivalent to `vars_proto`.
48//
49// Requires that (or returns a status error):
50// * vars_proto.ids and vars_proto.values have equal size.
51// * vars_proto.ids is sorted.
52// * vars_proto.ids has elements that are variables in `model` (this implies
53// that each id is in [0, max(int64_t))).
54absl::StatusOr<VariableMap<int32_t>> VariableValuesFromProto(
55 const ModelStorage* model, const SparseInt32VectorProto& vars_proto);
56
57// Returns the proto equivalent of variable_values.
58SparseDoubleVectorProto VariableValuesToProto(
59 const VariableMap<double>& variable_values);
60
61// Returns an absl::flat_hash_map<Objective, double> equivalent to
62// `aux_obj_proto`.
63//
64// Requires that (or returns a status error):
65// * The keys of `aux_obj_proto` correspond to objectives in `model`.
66//
67// Note that the values of `aux_obj_proto` are not checked (it may have NaNs).
68absl::StatusOr<absl::flat_hash_map<Objective, double>>
70 const ModelStorage* model,
71 const google::protobuf::Map<int64_t, double>& aux_obj_proto);
72
73// Returns the proto equivalent of auxiliary_obj_values.
74//
75// Requires that (or will CHECK-fail):
76// * The keys of `aux_obj_values` all correspond to auxiliary objectives.
77google::protobuf::Map<int64_t, double> AuxiliaryObjectiveValuesToProto(
78 const absl::flat_hash_map<Objective, double>& aux_obj_values);
79
80// Returns the LinearConstraintMap<double> equivalent to `lin_cons_proto`.
81//
82// Requires that (or returns a status error):
83// * lin_cons_proto.ids and lin_cons_proto.values have equal size.
84// * lin_cons_proto.ids is sorted.
85// * lin_cons_proto.ids has elements that are linear constraints in `model`
86// (this implies that each id is in [0, max(int64_t))).
87//
88// Note that the values of lin_cons_proto.values are not checked (it may have
89// NaNs).
90absl::StatusOr<LinearConstraintMap<double>> LinearConstraintValuesFromProto(
91 const ModelStorage* model, const SparseDoubleVectorProto& lin_cons_proto);
92
93// Returns the proto equivalent of linear_constraint_values.
94SparseDoubleVectorProto LinearConstraintValuesToProto(
95 const LinearConstraintMap<double>& linear_constraint_values);
96
97// Returns the absl::flat_hash_map<QuadraticConstraint, double> equivalent to
98// `quad_cons_proto`.
99//
100// Requires that (or returns a status error):
101// * quad_cons_proto.ids and quad_cons_proto.values have equal size.
102// * quad_cons_proto.ids is sorted.
103// * quad_cons_proto.ids has elements that are quadratic constraints in `model`
104// (this implies that each id is in [0, max(int64_t))).
105//
106// Note that the values of quad_cons_proto.values are not checked (it may have
107// NaNs).
108absl::StatusOr<absl::flat_hash_map<QuadraticConstraint, double>>
110 const ModelStorage* model, const SparseDoubleVectorProto& quad_cons_proto);
111
112// Returns the proto equivalent of quadratic_constraint_values.
113SparseDoubleVectorProto QuadraticConstraintValuesToProto(
114 const absl::flat_hash_map<QuadraticConstraint, double>&
115 quadratic_constraint_values);
116
117// Returns the VariableMap<BasisStatus> equivalent to `basis_proto`.
118//
119// Requires that (or returns a status error):
120// * basis_proto.ids and basis_proto.values have equal size.
121// * basis_proto.ids is sorted.
122// * basis_proto.ids has elements that are variables in `model` (this implies
123// that each id is in [0, max(int64_t))).
124// * basis_proto.values does not contain UNSPECIFIED and has valid enum values.
125absl::StatusOr<VariableMap<BasisStatus>> VariableBasisFromProto(
126 const ModelStorage* model, const SparseBasisStatusVector& basis_proto);
127
128// Returns the proto equivalent of basis_values.
129SparseBasisStatusVector VariableBasisToProto(
130 const VariableMap<BasisStatus>& basis_values);
131
132// Returns the LinearConstraintMap<BasisStatus> equivalent to `basis_proto`.
133//
134// Requires that (or returns a status error):
135// * basis_proto.ids and basis_proto.values have equal size.
136// * basis_proto.ids is sorted.
137// * basis_proto.ids has elements that are linear constraints in `model` (this
138// implies that each id is in [0, max(int64_t))).
139// * basis_proto.values does not contain UNSPECIFIED and has valid enum values.
140absl::StatusOr<LinearConstraintMap<BasisStatus>> LinearConstraintBasisFromProto(
141 const ModelStorage* model, const SparseBasisStatusVector& basis_proto);
142
143// Returns the proto equivalent of basis_values.
144SparseBasisStatusVector LinearConstraintBasisToProto(
145 const LinearConstraintMap<BasisStatus>& basis_values);
146
147} // namespace operations_research::math_opt
148
149#endif // OR_TOOLS_MATH_OPT_CPP_SPARSE_CONTAINERS_H_
GRBmodel * model
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
google::protobuf::Map< int64_t, double > AuxiliaryObjectiveValuesToProto(const absl::flat_hash_map< Objective, double > &aux_obj_values)
absl::StatusOr< absl::flat_hash_map< Objective, double > > AuxiliaryObjectiveValuesFromProto(const ModelStorage *const model, const google::protobuf::Map< int64_t, double > &aux_obj_proto)
absl::flat_hash_map< Variable, V > VariableMap
absl::flat_hash_map< LinearConstraint, V > LinearConstraintMap
SparseDoubleVectorProto LinearConstraintValuesToProto(const LinearConstraintMap< double > &linear_constraint_values)
Returns the proto equivalent of linear_constraint_values.
absl::StatusOr< LinearConstraintMap< BasisStatus > > LinearConstraintBasisFromProto(const ModelStorage *const model, const SparseBasisStatusVector &basis_proto)
absl::StatusOr< absl::flat_hash_map< QuadraticConstraint, double > > QuadraticConstraintValuesFromProto(const ModelStorage *const model, const SparseDoubleVectorProto &quad_cons_proto)
absl::StatusOr< LinearConstraintMap< double > > LinearConstraintValuesFromProto(const ModelStorage *const model, const SparseDoubleVectorProto &lin_cons_proto)
absl::StatusOr< VariableMap< double > > VariableValuesFromProto(const ModelStorage *const model, const SparseDoubleVectorProto &vars_proto)
SparseBasisStatusVector VariableBasisToProto(const VariableMap< BasisStatus > &basis_values)
Returns the proto equivalent of basis_values.
SparseBasisStatusVector LinearConstraintBasisToProto(const LinearConstraintMap< BasisStatus > &basis_values)
Returns the proto equivalent of basis_values.
SparseDoubleVectorProto VariableValuesToProto(const VariableMap< double > &variable_values)
Returns the proto equivalent of variable_values.
SparseDoubleVectorProto QuadraticConstraintValuesToProto(const absl::flat_hash_map< QuadraticConstraint, double > &quadratic_constraint_values)
Returns the proto equivalent of quadratic_constraint_values.
absl::StatusOr< VariableMap< BasisStatus > > VariableBasisFromProto(const ModelStorage *const model, const SparseBasisStatusVector &basis_proto)