21#include "absl/algorithm/container.h"
22#include "absl/container/flat_hash_map.h"
23#include "absl/status/status.h"
24#include "absl/status/statusor.h"
25#include "absl/types/span.h"
26#include "google/protobuf/map.h"
43template <
typename SparseVectorProtoType>
44absl::Status CheckSparseVectorProto(
const SparseVectorProtoType& vec) {
47 return absl::OkStatus();
50template <
typename Key>
51absl::StatusOr<absl::flat_hash_map<Key, BasisStatus>> BasisVectorFromProto(
52 const ModelStorage*
const model,
53 const SparseBasisStatusVector& basis_proto) {
54 using IdType =
typename Key::IdType;
55 absl::flat_hash_map<Key, BasisStatus> map;
56 map.reserve(basis_proto.ids_size());
57 for (
const auto& [
id, basis_status_proto_int] :
MakeView(basis_proto)) {
58 const auto basis_status_proto =
59 static_cast<BasisStatusProto
>(basis_status_proto_int);
60 const std::optional<BasisStatus> basis_status =
62 if (!basis_status.has_value()) {
64 <<
"basis status not specified for id " << id;
66 map[Key(
model, IdType(
id))] = *basis_status;
71template <
typename Key>
72SparseDoubleVectorProto MapToProto(
73 const absl::flat_hash_map<Key, double>& id_map) {
74 using IdType =
typename Key::IdType;
75 std::vector<std::pair<IdType, double>> sorted_entries;
76 sorted_entries.reserve(id_map.size());
77 for (
const auto& [k, v] : id_map) {
78 sorted_entries.emplace_back(k.typed_id(), v);
80 absl::c_sort(sorted_entries);
81 SparseDoubleVectorProto result;
82 for (
const auto& [
id, val] : sorted_entries) {
83 result.add_ids(
id.
value());
84 result.add_values(val);
89template <
typename Key>
90SparseBasisStatusVector BasisMapToProto(
91 const absl::flat_hash_map<Key, BasisStatus>& basis_map) {
92 using IdType =
typename Key::IdType;
93 std::vector<std::pair<IdType, BasisStatus>> sorted_entries;
94 sorted_entries.reserve(basis_map.size());
95 for (
const auto& [k, v] : basis_map) {
96 sorted_entries.emplace_back(k.typed_id(), v);
98 absl::c_sort(sorted_entries);
99 SparseBasisStatusVector result;
100 for (
const auto& [
id, val] : sorted_entries) {
101 result.add_ids(
id.
value());
107absl::Status VariableIdsExist(
const ModelStorage*
const model,
108 const absl::Span<const int64_t> ids) {
109 for (
const int64_t
id : ids) {
110 if (!
model->has_variable(VariableId(
id))) {
112 <<
"no variable with id " <<
id <<
" exists";
115 return absl::OkStatus();
118absl::Status LinearConstraintIdsExist(
const ModelStorage*
const model,
119 const absl::Span<const int64_t> ids) {
120 for (
const int64_t
id : ids) {
121 if (!
model->has_linear_constraint(LinearConstraintId(
id))) {
123 <<
"no linear constraint with id " <<
id <<
" exists";
126 return absl::OkStatus();
129absl::Status QuadraticConstraintIdsExist(
const ModelStorage*
const model,
130 const absl::Span<const int64_t> ids) {
131 for (
const int64_t
id : ids) {
132 if (!
model->has_constraint(QuadraticConstraintId(
id))) {
134 <<
"no quadratic constraint with id " <<
id <<
" exists";
137 return absl::OkStatus();
143 const ModelStorage*
const model,
144 const SparseDoubleVectorProto& vars_proto) {
151 const ModelStorage*
model,
const SparseInt32VectorProto& vars_proto) {
159 return MapToProto(variable_values);
162absl::StatusOr<absl::flat_hash_map<Objective, double>>
164 const ModelStorage*
const model,
165 const google::protobuf::Map<int64_t, double>& aux_obj_proto) {
166 absl::flat_hash_map<Objective, double> result;
167 for (
const auto [raw_id,
value] : aux_obj_proto) {
168 const AuxiliaryObjectiveId id(raw_id);
169 if (!
model->has_auxiliary_objective(
id)) {
171 <<
"no auxiliary objective with id " << raw_id <<
" exists";
179 const absl::flat_hash_map<Objective, double>& aux_obj_values) {
180 google::protobuf::Map<int64_t, double> result;
181 for (
const auto& [objective,
value] : aux_obj_values) {
182 CHECK(objective.id().has_value())
183 <<
"encountered primary objective in auxiliary objective value map";
184 result[objective.id().value()] =
value;
190 const ModelStorage*
const model,
191 const SparseDoubleVectorProto& lin_cons_proto) {
199 return MapToProto(linear_constraint_values);
202absl::StatusOr<absl::flat_hash_map<QuadraticConstraint, double>>
204 const ModelStorage*
const model,
205 const SparseDoubleVectorProto& quad_cons_proto) {
212 const absl::flat_hash_map<QuadraticConstraint, double>&
213 quadratic_constraint_values) {
214 return MapToProto(quadratic_constraint_values);
218 const ModelStorage*
const model,
219 const SparseBasisStatusVector& basis_proto) {
222 return BasisVectorFromProto<Variable>(
model, basis_proto);
227 return BasisMapToProto(basis_values);
231 const ModelStorage*
const model,
232 const SparseBasisStatusVector& basis_proto) {
235 return BasisVectorFromProto<LinearConstraint>(
model, basis_proto);
240 return BasisMapToProto(basis_values);
#define RETURN_IF_ERROR(expr)
static Objective Auxiliary(const ModelStorage *storage, AuxiliaryObjectiveId id)
Returns an object that refers to an auxiliary objective of the model.
An object oriented wrapper for quadratic constraints in ModelStorage.
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::Status CheckIdsAndValuesSize(const SparseVectorView< T > &vector_view, absl::string_view value_name="values")
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)
std::optional< typename EnumProto< P >::Cpp > EnumFromProto(P proto_value)
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.
SparseVectorView< T > MakeView(absl::Span< const int64_t > ids, const Collection &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.
Enum< E >::Proto EnumToProto(std::optional< E > value)
absl::StatusOr< VariableMap< BasisStatus > > VariableBasisFromProto(const ModelStorage *const model, const SparseBasisStatusVector &basis_proto)
absl::Status CheckIdsRangeAndStrictlyIncreasing(absl::Span< const int64_t > ids)
StatusBuilder InvalidArgumentErrorBuilder()