// Copyright 2010-2025 Google LLC // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // IWYU pragma: private, include "ortools/math_opt/cpp/math_opt.h" // IWYU pragma: friend "ortools/math_opt/cpp/.*" #ifndef ORTOOLS_MATH_OPT_CPP_SPARSE_CONTAINERS_H_ #define ORTOOLS_MATH_OPT_CPP_SPARSE_CONTAINERS_H_ #include #include "absl/container/flat_hash_map.h" #include "ortools/base/logging.h" #include "ortools/math_opt/constraints/quadratic/quadratic_constraint.h" #include "ortools/math_opt/cpp/basis_status.h" #include "ortools/math_opt/cpp/linear_constraint.h" #include "ortools/math_opt/cpp/objective.h" #include "ortools/math_opt/cpp/variable_and_expressions.h" #include "ortools/math_opt/solution.pb.h" #include "ortools/math_opt/sparse_containers.pb.h" #include "ortools/math_opt/storage/model_storage.h" namespace operations_research::math_opt { // Returns the VariableMap equivalent to `vars_proto`. // // Requires that (or returns a status error): // * vars_proto.ids and vars_proto.values have equal size. // * vars_proto.ids is sorted. // * vars_proto.ids has elements that are variables in `model` (this implies // that each id is in [0, max(int64_t))). // // Note that the values of vars_proto.values are not checked (it may have NaNs). absl::StatusOr> VariableValuesFromProto( ModelStorageCPtr model, const SparseDoubleVectorProto& vars_proto); // Returns the VariableMap equivalent to `vars_proto`. // // Requires that (or returns a status error): // * vars_proto.ids and vars_proto.values have equal size. // * vars_proto.ids is sorted. // * vars_proto.ids has elements that are variables in `model` (this implies // that each id is in [0, max(int64_t))). absl::StatusOr> VariableValuesFromProto( ModelStorageCPtr model, const SparseInt32VectorProto& vars_proto); // Returns the proto equivalent of variable_values. SparseDoubleVectorProto VariableValuesToProto( const VariableMap& variable_values); // Returns an absl::flat_hash_map equivalent to // `aux_obj_proto`. // // Requires that (or returns a status error): // * The keys of `aux_obj_proto` correspond to objectives in `model`. // // Note that the values of `aux_obj_proto` are not checked (it may have NaNs). absl::StatusOr> AuxiliaryObjectiveValuesFromProto( ModelStorageCPtr model, const google::protobuf::Map& aux_obj_proto); // Returns the proto equivalent of auxiliary_obj_values. // // Requires that (or will CHECK-fail): // * The keys of `aux_obj_values` all correspond to auxiliary objectives. google::protobuf::Map AuxiliaryObjectiveValuesToProto( const absl::flat_hash_map& aux_obj_values); // Returns the LinearConstraintMap equivalent to `lin_cons_proto`. // // Requires that (or returns a status error): // * lin_cons_proto.ids and lin_cons_proto.values have equal size. // * lin_cons_proto.ids is sorted. // * lin_cons_proto.ids has elements that are linear constraints in `model` // (this implies that each id is in [0, max(int64_t))). // // Note that the values of lin_cons_proto.values are not checked (it may have // NaNs). absl::StatusOr> LinearConstraintValuesFromProto( ModelStorageCPtr model, const SparseDoubleVectorProto& lin_cons_proto); // Returns the proto equivalent of linear_constraint_values. SparseDoubleVectorProto LinearConstraintValuesToProto( const LinearConstraintMap& linear_constraint_values); // Returns the absl::flat_hash_map equivalent to // `quad_cons_proto`. // // Requires that (or returns a status error): // * quad_cons_proto.ids and quad_cons_proto.values have equal size. // * quad_cons_proto.ids is sorted. // * quad_cons_proto.ids has elements that are quadratic constraints in `model` // (this implies that each id is in [0, max(int64_t))). // // Note that the values of quad_cons_proto.values are not checked (it may have // NaNs). absl::StatusOr> QuadraticConstraintValuesFromProto( ModelStorageCPtr model, const SparseDoubleVectorProto& quad_cons_proto); // Returns the proto equivalent of quadratic_constraint_values. SparseDoubleVectorProto QuadraticConstraintValuesToProto( const absl::flat_hash_map& quadratic_constraint_values); // Returns the VariableMap equivalent to `basis_proto`. // // Requires that (or returns a status error): // * basis_proto.ids and basis_proto.values have equal size. // * basis_proto.ids is sorted. // * basis_proto.ids has elements that are variables in `model` (this implies // that each id is in [0, max(int64_t))). // * basis_proto.values does not contain UNSPECIFIED and has valid enum values. absl::StatusOr> VariableBasisFromProto( ModelStorageCPtr model, const SparseBasisStatusVector& basis_proto); // Returns the proto equivalent of basis_values. SparseBasisStatusVector VariableBasisToProto( const VariableMap& basis_values); // Returns the LinearConstraintMap equivalent to `basis_proto`. // // Requires that (or returns a status error): // * basis_proto.ids and basis_proto.values have equal size. // * basis_proto.ids is sorted. // * basis_proto.ids has elements that are linear constraints in `model` (this // implies that each id is in [0, max(int64_t))). // * basis_proto.values does not contain UNSPECIFIED and has valid enum values. absl::StatusOr> LinearConstraintBasisFromProto( ModelStorageCPtr model, const SparseBasisStatusVector& basis_proto); // Returns the proto equivalent of basis_values. SparseBasisStatusVector LinearConstraintBasisToProto( const LinearConstraintMap& basis_values); } // namespace operations_research::math_opt #endif // ORTOOLS_MATH_OPT_CPP_SPARSE_CONTAINERS_H_