Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
solution.h
Go to the documentation of this file.
1// Copyright 2010-2025 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_SOLUTION_H_
18#define OR_TOOLS_MATH_OPT_CPP_SOLUTION_H_
19
20#include <optional>
21
22#include "absl/container/flat_hash_map.h"
23#include "absl/status/status.h"
24#include "absl/status/statusor.h"
27#include "ortools/math_opt/cpp/enums.h" // IWYU pragma: export
31#include "ortools/math_opt/result.pb.h" // IWYU pragma: export
34
35namespace operations_research {
36namespace math_opt {
37
38// Feasibility of a primal or dual solution as claimed by the solver.
39enum class SolutionStatus {
40 // Solver does not claim a feasibility status.
42
43 // Solver claims the solution is feasible.
45
46 // Solver claims the solution is infeasible.
48};
49
51
52// A solution to an optimization problem.
53//
54// E.g. consider a simple linear program:
55// min c * x
56// s.t. A * x >= b
57// x >= 0.
58// A primal solution is assignment values to x. It is feasible if it satisfies
59// A * x >= b and x >= 0 from above. In the class PrimalSolution,
60// variable_values is x and objective_value is c * x.
61//
62// For the general case of a MathOpt optimization model.
64 // Returns the PrimalSolution equivalent of primal_solution_proto.
65 //
66 // Returns an error when:
67 // * VariableValuesFromProto(primal_solution_proto.variable_values) fails.
68 // * the feasibility_status is not specified.
69 static absl::StatusOr<PrimalSolution> FromProto(
70 ModelStorageCPtr model, const PrimalSolutionProto& primal_solution_proto);
71
72 // Returns the proto equivalent of this.
74
75 // Returns the value for the given `objective`.
76 //
77 // Will CHECK-fail if `objective` has been deleted, or if it is from the is
78 // from the wrong model (however, if the solution has no variables, this CHECK
79 // will not occur due to an implementation detail of the struct).
80 double get_objective_value(Objective objective) const;
81
83 double objective_value = 0.0;
84 absl::flat_hash_map<Objective, double> auxiliary_objective_values;
85
87};
88
89// A direction of unbounded improvement to an optimization problem;
90// equivalently, a certificate of infeasibility for the dual of the
91// optimization problem.
92//
93// E.g. consider a simple linear program:
94// min c * x
95// s.t. A * x >= b
96// x >= 0
97// A primal ray is an x that satisfies:
98// c * x < 0
99// A * x >= 0
100// x >= 0
101// Observe that given a feasible solution, any positive multiple of the primal
102// ray plus that solution is still feasible, and gives a better objective
103// value. A primal ray also proves the dual optimization problem infeasible.
104//
105// In the class PrimalRay, variable_values is this x.
106//
107// For the general case of a MathOpt optimization model.
108struct PrimalRay {
109 // Returns the PrimalRay equivalent of primal_ray_proto.
110 //
111 // Returns an error when
112 // VariableValuesFromProto(primal_ray_proto.variable_values) fails.
113 static absl::StatusOr<PrimalRay> FromProto(
114 ModelStorageCPtr model, const PrimalRayProto& primal_ray_proto);
115
116 // Returns the proto equivalent of this.
117 PrimalRayProto Proto() const;
118
120};
121
122// A solution to the dual of an optimization problem.
123//
124// E.g. consider the primal dual pair linear program pair:
125// (Primal) (Dual)
126// min c * x max b * y
127// s.t. A * x >= b s.t. y * A + r = c
128// x >= 0 y, r >= 0.
129// The dual solution is the pair (y, r). It is feasible if it satisfies the
130// constraints from (Dual) above.
131//
132// Below, y is dual_values, r is reduced_costs, and b * y is objective value.
134 // Returns the DualSolution equivalent of dual_solution_proto.
135 //
136 // Returns an error when any of:
137 // * VariableValuesFromProto(dual_solution_proto.reduced_costs) fails.
138 // * LinearConstraintValuesFromProto(dual_solution_proto.dual_values) fails.
139 // * dual_solution_proto.feasibility_status is not specified.
140 static absl::StatusOr<DualSolution> FromProto(
141 ModelStorageCPtr model, const DualSolutionProto& dual_solution_proto);
142
143 // Returns the proto equivalent of this.
144 DualSolutionProto Proto() const;
145
147
148 // Note: Some solvers only return quadratic constraint duals when a
149 // solver-specific parameter is set
150 // (see go/mathopt-qcqp-dual#solver-specific).
151 absl::flat_hash_map<QuadraticConstraint, double> quadratic_dual_values;
153 std::optional<double> objective_value;
154
156};
157
158// A direction of unbounded improvement to the dual of an optimization
159// problem; equivalently, a certificate of primal infeasibility.
160//
161// E.g. consider the primal dual linear program pair:
162// (Primal) (Dual)
163// min c * x max b * y
164// s.t. A * x >= b s.t. y * A + r = c
165// x >= 0 y, r >= 0.
166// The dual ray is the pair (y, r) satisfying:
167// b * y > 0
168// y * A + r = 0
169// y, r >= 0
170// Observe that adding a positive multiple of (y, r) to dual feasible solution
171// maintains dual feasibility and improves the objective (proving the dual is
172// unbounded). The dual ray also proves the primal problem is infeasible.
173//
174// In the class DualRay, y is dual_values and r is reduced_costs.
175struct DualRay {
176 // Returns the DualRay equivalent of dual_ray_proto.
177 //
178 // Returns an error when either of:
179 // * VariableValuesFromProto(dual_ray_proto.reduced_costs) fails.
180 // * LinearConstraintValuesFromProto(dual_ray_proto.dual_values) fails.
181 static absl::StatusOr<DualRay> FromProto(ModelStorageCPtr model,
182 const DualRayProto& dual_ray_proto);
183
184 // Returns the proto equivalent of this.
185 DualRayProto Proto() const;
186
189};
190
191// A combinatorial characterization for a solution to a linear program.
192//
193// The simplex method for solving linear programs always returns a "basic
194// feasible solution" which can be described combinatorially as a Basis. A
195// basis assigns a BasisStatus for every variable and linear constraint.
196//
197// E.g. consider a standard form LP:
198// min c * x
199// s.t. A * x = b
200// x >= 0
201// that has more variables than constraints and with full row rank A.
202//
203// Let n be the number of variables and m the number of linear constraints. A
204// valid basis for this problem can be constructed as follows:
205// * All constraints will have basis status FIXED.
206// * Pick m variables such that the columns of A are linearly independent and
207// assign the status BASIC.
208// * Assign the status AT_LOWER for the remaining n - m variables.
209//
210// The basic solution for this basis is the unique solution of A * x = b that
211// has all variables with status AT_LOWER fixed to their lower bounds (all
212// zero). The resulting solution is called a basic feasible solution if it
213// also satisfies x >= 0.
214struct Basis {
215 // Returns the equivalent Basis object for basis_proto.
216 //
217 // Returns an error if:
218 // * VariableBasisFromProto(basis_proto.variable_status) fails.
219 // * LinearConstraintBasisFromProto(basis_proto.constraint_status) fails.
220 static absl::StatusOr<Basis> FromProto(ModelStorageCPtr model,
221 const BasisProto& basis_proto);
222
223 // Returns a failure if the referenced variables don't belong to the input
224 // expected_storage (which must not be nullptr).
225 absl::Status CheckModelStorage(ModelStorageCPtr expected_storage) const;
226
227 // Returns the proto equivalent of this object.
228 //
229 // The caller should use CheckModelStorage() as this function does not check
230 // internal consistency of the referenced variables and constraints.
231 BasisProto Proto() const;
232
235
236 // This is an advanced feature used by MathOpt to characterize feasibility of
237 // suboptimal LP solutions (optimal solutions will always have status
238 // SolutionStatus::kFeasible).
239 //
240 // For single-sided LPs it should be equal to the feasibility status of the
241 // associated dual solution. For two-sided LPs it may be different in some
242 // edge cases (e.g. incomplete solves with primal simplex).
243 //
244 // If you are providing a starting basis via
245 // `ModelSolveParameters.initial_basis`, this value is ignored. It is only
246 // relevant for the basis returned by `Solution.basis`, and it is is always
247 // populated in a Basis returned by a call to Solve().
248 std::optional<SolutionStatus> basic_dual_feasibility;
249};
250
251// What is included in a solution depends on the kind of problem and solver.
252// The current common patterns are
253// 1. MIP solvers return only a primal solution.
254// 2. Simplex LP solvers often return a basis and the primal and dual
255// solutions associated to this basis.
256// 3. Other continuous solvers often return a primal and dual solution that
257// are connected in a solver-dependent form.
258struct Solution {
259 // Returns the Solution equivalent of solution_proto.
260 //
261 // Returns an error if FromProto() fails on any field that is not std::nullopt
262 // (see the static FromProto() functions for each field type for details).
263 static absl::StatusOr<Solution> FromProto(
264 ModelStorageCPtr model, const SolutionProto& solution_proto);
265
266 // Returns the proto equivalent of this.
267 SolutionProto Proto() const;
268
269 std::optional<PrimalSolution> primal_solution;
270 std::optional<DualSolution> dual_solution;
271 std::optional<Basis> basis;
272};
273
274} // namespace math_opt
275} // namespace operations_research
276
277#endif // OR_TOOLS_MATH_OPT_CPP_SOLUTION_H_
#define MATH_OPT_DEFINE_ENUM(CppEnum, proto_unspecified_value)
Definition enums.h:325
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
absl::flat_hash_map< Variable, V > VariableMap
absl::Nonnull< const ModelStorage * > ModelStorageCPtr
absl::flat_hash_map< LinearConstraint, V > LinearConstraintMap
SolutionStatus
Feasibility of a primal or dual solution as claimed by the solver.
Definition solution.h:39
@ kUndetermined
Solver does not claim a feasibility status.
Definition solution.h:41
@ kFeasible
Solver claims the solution is feasible.
Definition solution.h:44
@ kInfeasible
Solver claims the solution is infeasible.
Definition solution.h:47
In SWIG mode, we don't want anything besides these top-level includes.
absl::Status CheckModelStorage(ModelStorageCPtr expected_storage) const
Definition solution.cc:200
LinearConstraintMap< BasisStatus > constraint_status
Definition solution.h:233
VariableMap< BasisStatus > variable_status
Definition solution.h:234
std::optional< SolutionStatus > basic_dual_feasibility
Definition solution.h:248
static absl::StatusOr< Basis > FromProto(ModelStorageCPtr model, const BasisProto &basis_proto)
Definition solution.cc:184
DualRayProto Proto() const
Returns the proto equivalent of this.
Definition solution.cc:177
static absl::StatusOr< DualRay > FromProto(ModelStorageCPtr model, const DualRayProto &dual_ray_proto)
Definition solution.cc:163
LinearConstraintMap< double > dual_values
Definition solution.h:187
VariableMap< double > reduced_costs
Definition solution.h:188
LinearConstraintMap< double > dual_values
Definition solution.h:146
static absl::StatusOr< DualSolution > FromProto(ModelStorageCPtr model, const DualSolutionProto &dual_solution_proto)
Definition solution.cc:122
absl::flat_hash_map< QuadraticConstraint, double > quadratic_dual_values
Definition solution.h:151
std::optional< double > objective_value
Definition solution.h:153
DualSolutionProto Proto() const
Returns the proto equivalent of this.
Definition solution.cc:150
PrimalRayProto Proto() const
Returns the proto equivalent of this.
Definition solution.cc:116
VariableMap< double > variable_values
Definition solution.h:119
static absl::StatusOr< PrimalRay > FromProto(ModelStorageCPtr model, const PrimalRayProto &primal_ray_proto)
Definition solution.cc:106
absl::flat_hash_map< Objective, double > auxiliary_objective_values
Definition solution.h:84
PrimalSolutionProto Proto() const
Returns the proto equivalent of this.
Definition solution.cc:82
static absl::StatusOr< PrimalSolution > FromProto(ModelStorageCPtr model, const PrimalSolutionProto &primal_solution_proto)
Definition solution.cc:59
double get_objective_value(Objective objective) const
Definition solution.cc:92
std::optional< PrimalSolution > primal_solution
Definition solution.h:269
std::optional< DualSolution > dual_solution
Definition solution.h:270
SolutionProto Proto() const
Returns the proto equivalent of this.
Definition solution.cc:249
static absl::StatusOr< Solution > FromProto(ModelStorageCPtr model, const SolutionProto &solution_proto)
Definition solution.cc:226