Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
compute_infeasible_subsystem_result.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#ifndef OR_TOOLS_MATH_OPT_CPP_COMPUTE_INFEASIBLE_SUBSYSTEM_RESULT_H_
17#define OR_TOOLS_MATH_OPT_CPP_COMPUTE_INFEASIBLE_SUBSYSTEM_RESULT_H_
18
19#include <ostream>
20#include <string>
21
22#include "absl/container/flat_hash_map.h"
23#include "absl/container/flat_hash_set.h"
24#include "absl/status/status.h"
25#include "absl/status/statusor.h"
34#include "ortools/math_opt/infeasible_subsystem.pb.h"
36
38
39// Represents a subset of the constraints (including variable bounds and
40// integrality) of a `Model`.
41//
42// The fields contain `Variable` and Constraint objects which retain pointers
43// back to their associated `Model`. Therefore, a `ModelSubset` should not
44// outlive the `Model` it is in reference to.
46 struct Bounds {
47 static Bounds FromProto(const ModelSubsetProto::Bounds& bounds_proto);
48 ModelSubsetProto::Bounds Proto() const;
49
50 bool empty() const { return !lower && !upper; }
51
52 friend bool operator==(const Bounds& lhs, const Bounds& rhs) {
53 return lhs.lower == rhs.lower && lhs.upper == rhs.upper;
54 }
55 friend bool operator!=(const Bounds& lhs, const Bounds& rhs) {
56 return !(lhs == rhs);
57 }
58
59 bool lower = false;
60 bool upper = false;
61 };
62
63 // Returns the `ModelSubset` equivalent to `proto`.
64 //
65 // Returns an error when `model` does not contain a variable or constraint
66 // associated with an index present in `proto`.
67 static absl::StatusOr<ModelSubset> FromProto(const ModelStorage* model,
68 const ModelSubsetProto& proto);
69
70 // Returns the proto equivalent of this object.
71 //
72 // The caller should use CheckModelStorage() as this function does not check
73 // internal consistency of the referenced variables and constraints.
74 ModelSubsetProto Proto() const;
75
76 // Returns a failure if the `Variable` and Constraints contained in the fields
77 // do not belong to the input expected_storage (which must not be nullptr).
78 absl::Status CheckModelStorage(const ModelStorage* expected_storage) const;
79
80 // True if this object corresponds to the empty subset.
81 bool empty() const;
82
83 // Returns a detailed string description of the contents of the model subset.
84 // (not the component names, use `<<` for that instead).
85 std::string ToString() const;
86
87 absl::flat_hash_map<Variable, Bounds> variable_bounds;
88 absl::flat_hash_set<Variable> variable_integrality;
89 absl::flat_hash_map<LinearConstraint, Bounds> linear_constraints;
90 absl::flat_hash_map<QuadraticConstraint, Bounds> quadratic_constraints;
91 absl::flat_hash_set<SecondOrderConeConstraint> second_order_cone_constraints;
92 absl::flat_hash_set<Sos1Constraint> sos1_constraints;
93 absl::flat_hash_set<Sos2Constraint> sos2_constraints;
94 absl::flat_hash_set<IndicatorConstraint> indicator_constraints;
95};
96
97std::ostream& operator<<(std::ostream& out, const ModelSubset::Bounds& bounds);
98std::ostream& operator<<(std::ostream& out, const ModelSubset& model_subset);
99
101 // Returns the `ComputeInfeasibleSubsystemResult` equivalent to `proto`.
102 //
103 // Returns an error when:
104 // * `model` does not contain a variable or constraint associated with an
105 // index present in `proto.infeasible_subsystem`.
106 // * ValidateComputeInfeasibleSubsystemResultNoModel(result_proto) fails.
107 static absl::StatusOr<ComputeInfeasibleSubsystemResult> FromProto(
108 const ModelStorage* model,
109 const ComputeInfeasibleSubsystemResultProto& result_proto);
110
111 // Returns the proto equivalent of this object.
112 //
113 // The caller should use CheckModelStorage() before calling this function as
114 // it does not check internal consistency of the referenced variables and
115 // constraints.
116 ComputeInfeasibleSubsystemResultProto Proto() const;
117
118 // Returns a failure if this object contains references to a model other than
119 // `expected_storage` (which must not be nullptr).
120 absl::Status CheckModelStorage(const ModelStorage* expected_storage) const;
121
122 // The primal feasibility status of the model, as determined by the solver.
124
125 // An infeasible subsystem of the input model. Set if `feasible` is
126 // kInfeasible, and empty otherwise. The IDs correspond to those constraints
127 // included in the infeasible subsystem. Submessages with `Bounds` values
128 // indicate which side of a potentially ranged constraint are included in the
129 // subsystem: lower bound, upper bound, or both.
131
132 // True if the solver has certified that the returned subsystem is minimal
133 // (the instance is feasible if any additional constraint is removed). Note
134 // that, due to problem transformations MathOpt applies or idiosyncrasies of
135 // the solvers contract, the returned infeasible subsystem may not actually be
136 // minimal.
137 bool is_minimal = false;
138};
139
140std::ostream& operator<<(std::ostream& out,
142
143} // namespace operations_research::math_opt
144
145#endif // OR_TOOLS_MATH_OPT_CPP_COMPUTE_INFEASIBLE_SUBSYSTEM_RESULT_H_
CpModelProto proto
The output proto.
GRBmodel * model
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
std::ostream & operator<<(std::ostream &ostr, const IndicatorConstraint &constraint)
@ kUndetermined
Solver does not claim a status.
absl::Status CheckModelStorage(const ModelStorage *expected_storage) const
static absl::StatusOr< ComputeInfeasibleSubsystemResult > FromProto(const ModelStorage *model, const ComputeInfeasibleSubsystemResultProto &result_proto)
FeasibilityStatus feasibility
The primal feasibility status of the model, as determined by the solver.
friend bool operator==(const Bounds &lhs, const Bounds &rhs)
friend bool operator!=(const Bounds &lhs, const Bounds &rhs)
static Bounds FromProto(const ModelSubsetProto::Bounds &bounds_proto)
absl::flat_hash_map< Variable, Bounds > variable_bounds
absl::flat_hash_set< SecondOrderConeConstraint > second_order_cone_constraints
bool empty() const
True if this object corresponds to the empty subset.
absl::Status CheckModelStorage(const ModelStorage *expected_storage) const
absl::flat_hash_map< LinearConstraint, Bounds > linear_constraints
absl::flat_hash_set< IndicatorConstraint > indicator_constraints
static absl::StatusOr< ModelSubset > FromProto(const ModelStorage *model, const ModelSubsetProto &proto)
absl::flat_hash_map< QuadraticConstraint, Bounds > quadratic_constraints