18#include <initializer_list>
22#include "absl/algorithm/container.h"
23#include "absl/status/status.h"
24#include "absl/status/statusor.h"
25#include "google/protobuf/repeated_field.h"
32#include "ortools/math_opt/model_parameters.pb.h"
33#include "ortools/math_opt/solution.pb.h"
34#include "ortools/math_opt/sparse_containers.pb.h"
41using ::google::protobuf::RepeatedField;
53 std::initializer_list<Variable> variables) {
58 const ModelStorage*
const expected_storage)
const {
61 <<
"invalid hint in solution_hints";
65 <<
"invalid initial_basis";
68 <<
"invalid variable_values_filter";
70 <<
"invalid dual_values_filter";
73 <<
"invalid quadratic_dual_values_filter";
75 <<
"invalid reduced_costs_filter";
80 <<
"invalid variable " <<
var <<
" in branching_priorities";
86 <<
"invalid objective " << objective <<
" in objective_parameters";
91 lazy_linear_constraint.storage(),
93 <<
"invalid LinearConstraint " << lazy_linear_constraint
94 <<
" in lazy_linear_constraints";
96 return absl::OkStatus();
100 const ModelStorage* expected_storage)
const {
105 <<
"invalid variable " << v <<
" in variable_values";
111 <<
"invalid constraint " <<
c <<
" in dual_values";
113 return absl::OkStatus();
117 SolutionHintProto
hint;
123absl::StatusOr<ModelSolveParameters::SolutionHint>
125 const Model&
model,
const SolutionHintProto& hint_proto) {
129 _ <<
"failed to parse SolutionHintProto.variable_values");
132 model.storage(), hint_proto.dual_values()),
133 _ <<
"failed to parse SolutionHintProto.dual_values");
135 .variable_values = std::move(variable_values),
136 .dual_values = std::move(dual_values),
142 ObjectiveParametersProto params;
143 if (objective_degradation_absolute_tolerance) {
144 params.set_objective_degradation_absolute_tolerance(
145 *objective_degradation_absolute_tolerance);
147 if (objective_degradation_relative_tolerance) {
148 params.set_objective_degradation_relative_tolerance(
149 *objective_degradation_relative_tolerance);
156 const ObjectiveParametersProto&
proto) {
158 if (
proto.has_objective_degradation_absolute_tolerance()) {
160 proto.objective_degradation_absolute_tolerance();
162 if (
proto.has_objective_degradation_relative_tolerance()) {
164 proto.objective_degradation_relative_tolerance();
171 ModelSolveParametersProto ret;
174 *ret.mutable_quadratic_dual_values_filter() =
183 *ret.add_solution_hints() = solution_hint.Proto();
187 *ret.mutable_branching_priorities()->mutable_ids();
188 RepeatedField<int32_t>& variable_values =
189 *ret.mutable_branching_priorities()->mutable_values();
198 if (objective.id()) {
199 (*ret.mutable_auxiliary_objective_parameters())[*objective.id()] =
202 *ret.mutable_primary_objective_parameters() = params.Proto();
206 RepeatedField<int64_t>& lazy_linear_constraint_ids =
207 *ret.mutable_lazy_linear_constraint_ids();
208 lazy_linear_constraint_ids.Reserve(
212 lazy_linear_constraint_ids.Add(lazy_linear_constraint.id());
214 absl::c_sort(lazy_linear_constraint_ids);
225 _ <<
"invalid variable_values_filter");
229 _ <<
"invalid dual_values_filter");
233 _ <<
"invalid quadratic_dual_values_filter");
237 _ <<
"invalid reduced_costs_filter");
238 if (
proto.has_initial_basis()) {
242 _ <<
"invalid initial_basis");
244 for (
int i = 0; i <
proto.solution_hints_size(); ++i) {
248 _ <<
"invalid solution_hints[" << i <<
"]");
254 _ <<
"invalid branching_priorities");
255 if (
proto.has_primary_objective_parameters()) {
260 for (
const auto& [
id, aux_obj_params_proto] :
261 proto.auxiliary_objective_parameters()) {
262 if (!
model.has_auxiliary_objective(
id)) {
264 <<
"invalid auxiliary_objective_parameters with id: " <<
id
265 <<
", objective not in the model";
271 for (int64_t lin_con :
proto.lazy_linear_constraint_ids()) {
272 if (!
model.has_linear_constraint(lin_con)) {
274 <<
"invalid lazy_linear_constraint with id: " << lin_con
275 <<
", constraint not in the model";
#define RETURN_IF_ERROR(expr)
const ModelStorage * storage() const
static Objective Primary(const ModelStorage *storage)
Returns an object that refers to the primary objective of the model.
static Objective Auxiliary(const ModelStorage *storage, AuxiliaryObjectiveId id)
Returns an object that refers to an auxiliary objective of the model.
CpModelProto proto
The output proto.
absl::Span< const int64_t > variable_ids
std::optional< ModelSolveParameters::SolutionHint > hint
absl::Status CheckModelStorage(const ModelStorage *const storage, const ModelStorage *const expected_storage)
absl::flat_hash_map< Variable, V > VariableMap
MapFilter< KeyType > MakeSkipAllFilter()
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< double > > LinearConstraintValuesFromProto(const ModelStorage *const model, const SparseDoubleVectorProto &lin_cons_proto)
absl::StatusOr< VariableMap< double > > VariableValuesFromProto(const ModelStorage *const model, const SparseDoubleVectorProto &vars_proto)
absl::StatusOr< MapFilter< LinearConstraint > > LinearConstraintFilterFromProto(const Model &model, const SparseVectorFilterProto &proto)
SparseDoubleVectorProto VariableValuesToProto(const VariableMap< double > &variable_values)
Returns the proto equivalent of variable_values.
absl::StatusOr< MapFilter< QuadraticConstraint > > QuadraticConstraintFilterFromProto(const Model &model, const SparseVectorFilterProto &proto)
std::vector< typename Map::key_type > SortedKeys(const Map &map)
absl::StatusOr< MapFilter< Variable > > VariableFilterFromProto(const Model &model, const SparseVectorFilterProto &proto)
In SWIG mode, we don't want anything besides these top-level includes.
StatusBuilder InvalidArgumentErrorBuilder()
static absl::StatusOr< Basis > FromProto(const ModelStorage *model, const BasisProto &basis_proto)
Parameters for an individual objective in a multi-objective model.
std::optional< double > objective_degradation_relative_tolerance
static ObjectiveParameters FromProto(const ObjectiveParametersProto &proto)
std::optional< double > objective_degradation_absolute_tolerance
ObjectiveParametersProto Proto() const
Returns the proto equivalent of this object.
static absl::StatusOr< SolutionHint > FromProto(const Model &model, const SolutionHintProto &hint_proto)
LinearConstraintMap< double > dual_values
absl::Status CheckModelStorage(const ModelStorage *expected_storage) const
SolutionHintProto Proto() const
VariableMap< double > variable_values
absl::Status CheckModelStorage(const ModelStorage *expected_storage) const
MapFilter< QuadraticConstraint > quadratic_dual_values_filter
MapFilter< LinearConstraint > dual_values_filter
The filter that is applied to dual_values of DualSolution and DualRay.
MapFilter< Variable > variable_values_filter
ModelSolveParametersProto Proto() const
ObjectiveMap< ObjectiveParameters > objective_parameters
Parameters for individual objectives in a multi-objective model.
std::optional< Basis > initial_basis
static ModelSolveParameters OnlySomePrimalVariables(const Collection &variables)
VariableMap< int32_t > branching_priorities
static ModelSolveParameters OnlyPrimalVariables()
MapFilter< Variable > reduced_costs_filter
The filter that is applied to reduced_costs of DualSolution and DualRay.
static absl::StatusOr< ModelSolveParameters > FromProto(const Model &model, const ModelSolveParametersProto &proto)
absl::flat_hash_set< LinearConstraint > lazy_linear_constraints
std::vector< SolutionHint > solution_hints
#define OR_ASSIGN_OR_RETURN3(lhs, rexpr, error_expression)