OR-Tools  9.2
solution.h
Go to the documentation of this file.
1// Copyright 2010-2021 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#ifndef OR_TOOLS_MATH_OPT_CPP_SOLUTION_H_
15#define OR_TOOLS_MATH_OPT_CPP_SOLUTION_H_
16
17#include <optional>
18
19#include "absl/types/optional.h"
20#include "absl/types/span.h"
22#include "ortools/math_opt/cpp/enums.h" // IWYU pragma: export
25#include "ortools/math_opt/result.pb.h" // IWYU pragma: export
26#include "ortools/math_opt/solution.pb.h"
27
28namespace operations_research {
29namespace math_opt {
30
31// Feasibility of a primal or dual solution as claimed by the solver.
32enum class SolutionStatus {
33 // Solver does not claim a feasibility status.
34 kUndetermined = SOLUTION_STATUS_UNDETERMINED,
35
36 // Solver claims the solution is feasible.
37 kFeasible = SOLUTION_STATUS_FEASIBLE,
38
39 // Solver claims the solution is infeasible.
40 kInfeasible = SOLUTION_STATUS_INFEASIBLE,
41};
42
43MATH_OPT_DEFINE_ENUM(SolutionStatus, SOLUTION_STATUS_UNSPECIFIED);
44
45// Status of a variable/constraint in a LP basis.
46enum class BasisStatus : int8_t {
47 // The variable/constraint is free (it has no finite bounds).
48 kFree = BASIS_STATUS_FREE,
49
50 // The variable/constraint is at its lower bound (which must be finite).
51 kAtLowerBound = BASIS_STATUS_AT_LOWER_BOUND,
52
53 // The variable/constraint is at its upper bound (which must be finite).
54 kAtUpperBound = BASIS_STATUS_AT_UPPER_BOUND,
55
56 // The variable/constraint has identical finite lower and upper bounds.
57 kFixedValue = BASIS_STATUS_FIXED_VALUE,
58
59 // The variable/constraint is basic.
60 kBasic = BASIS_STATUS_BASIC,
61};
62
63MATH_OPT_DEFINE_ENUM(BasisStatus, BASIS_STATUS_UNSPECIFIED);
64
65// A solution to an optimization problem.
66//
67// E.g. consider a simple linear program:
68// min c * x
69// s.t. A * x >= b
70// x >= 0.
71// A primal solution is assignment values to x. It is feasible if it satisfies
72// A * x >= b and x >= 0 from above. In the class PrimalSolution,
73// variable_values is x and objective_value is c * x.
74//
75// For the general case of a MathOpt optimization model, see
76// go/mathopt-solutions for details.
79 const ModelStorage* model,
80 const PrimalSolutionProto& primal_solution_proto);
81
83 double objective_value = 0.0;
84
86};
87
88// A direction of unbounded improvement to an optimization problem;
89// equivalently, a certificate of infeasibility for the dual of the
90// optimization problem.
91//
92// E.g. consider a simple linear program:
93// min c * x
94// s.t. A * x >= b
95// x >= 0
96// A primal ray is an x that satisfies:
97// c * x < 0
98// A * x >= 0
99// x >= 0
100// Observe that given a feasible solution, any positive multiple of the primal
101// ray plus that solution is still feasible, and gives a better objective
102// value. A primal ray also proves the dual optimization problem infeasible.
103//
104// In the class PrimalRay, variable_values is this x.
105//
106// For the general case of a MathOpt optimization model, see
107// go/mathopt-solutions for details.
108struct PrimalRay {
110 const PrimalRayProto& primal_ray_proto);
111
113};
114
115// A solution to the dual of an optimization problem.
116//
117// E.g. consider the primal dual pair linear program pair:
118// (Primal) (Dual)
119// min c * x max b * y
120// s.t. A * x >= b s.t. y * A + r = c
121// x >= 0 y, r >= 0.
122// The dual solution is the pair (y, r). It is feasible if it satisfies the
123// constraints from (Dual) above.
124//
125// Below, y is dual_values, r is reduced_costs, and b * y is objective value.
126//
127// For the general case, see go/mathopt-solutions and go/mathopt-dual (and
128// note that the dual objective depends on r in the general case).
131 const DualSolutionProto& dual_solution_proto);
132
135 std::optional<double> objective_value;
136
138};
139
140// A direction of unbounded improvement to the dual of an optimization,
141// problem; equivalently, a certificate of primal infeasibility.
142//
143// E.g. consider the primal dual pair linear program pair:
144// (Primal) (Dual)
145// min c * x max b * y
146// s.t. A * x >= b s.t. y * A + r = c
147// x >= 0 y, r >= 0.
148// The dual ray is the pair (y, r) satisfying:
149// b * y > 0
150// y * A + r = 0
151// y, r >= 0
152// Observe that adding a positive multiple of (y, r) to dual feasible solution
153// maintains dual feasibility and improves the objective (proving the dual is
154// unbounded). The dual ray also proves the primal problem is infeasible.
155//
156// In the class DualRay, y is dual_values and r is reduced_costs.
157//
158// For the general case, see go/mathopt-solutions and go/mathopt-dual (and
159// note that the dual objective depends on r in the general case).
160struct DualRay {
161 static DualRay FromProto(const ModelStorage* model,
162 const DualRayProto& dual_ray_proto);
163
166};
167
168// A combinatorial characterization for a solution to a linear program.
169//
170// The simplex method for solving linear programs always returns a "basic
171// feasible solution" which can be described combinatorially as a Basis. A
172// basis assigns a BasisStatus for every variable and linear constraint.
173//
174// E.g. consider a standard form LP:
175// min c * x
176// s.t. A * x = b
177// x >= 0
178// that has more variables than constraints and with full row rank A.
179//
180// Let n be the number of variables and m the number of linear constraints. A
181// valid basis for this problem can be constructed as follows:
182// * All constraints will have basis status FIXED.
183// * Pick m variables such that the columns of A are linearly independent and
184// assign the status BASIC.
185// * Assign the status AT_LOWER for the remaining n - m variables.
186//
187// The basic solution for this basis is the unique solution of A * x = b that
188// has all variables with status AT_LOWER fixed to their lower bounds (all
189// zero). The resulting solution is called a basic feasible solution if it
190// also satisfies x >= 0.
191//
192// See go/mathopt-basis for treatment of the general case and an explanation
193// of how a dual solution is determined for a basis.
194struct Basis {
195 // Returns a Basis built from the input indexed_basis, CHECKing that no
196 // values is BASIS_STATUS_UNSPECIFIED. No check is done on other values so
197 // out of bounds values e.g. BasisStatusProto_MAX+1 won't raise an
198 // assertion. See SpaseBasisStatusVectorIsValid().
199 static Basis FromProto(const ModelStorage* model,
200 const BasisProto& basis_proto);
201
204
205 // This is an advanced status. For single-sided LPs it should be equal to the
206 // feasibility status of the associated dual solution. For two-sided LPs it
207 // may be different in some edge cases (e.g. incomplete solves with primal
208 // simplex). For more details see go/mathopt-basis-advanced#dualfeasibility.
210};
211
212// What is included in a solution depends on the kind of problem and solver.
213// The current common patterns are
214// 1. MIP solvers return only a primal solution.
215// 2. Simplex LP solvers often return a basis and the primal and dual
216// solutions associated to this basis.
217// 3. Other continuous solvers often return a primal and dual solution
218// solution that are connected in a solver-dependent form.
219struct Solution {
220 static Solution FromProto(const ModelStorage* model,
221 const SolutionProto& solution_proto);
222 std::optional<PrimalSolution> primal_solution;
223 std::optional<DualSolution> dual_solution;
224 std::optional<Basis> basis;
225};
226
227} // namespace math_opt
228} // namespace operations_research
229
230#endif // OR_TOOLS_MATH_OPT_CPP_SOLUTION_H_
GRBmodel * model
MATH_OPT_DEFINE_ENUM(CallbackEvent, CALLBACK_EVENT_UNSPECIFIED)
Collection of objects used to extend the Constraint Solver library.
VariableMap< BasisStatus > variable_status
Definition: solution.h:203
LinearConstraintMap< BasisStatus > constraint_status
Definition: solution.h:202
static Basis FromProto(const ModelStorage *model, const BasisProto &basis_proto)
Definition: solution.cc:156
LinearConstraintMap< double > dual_values
Definition: solution.h:164
VariableMap< double > reduced_costs
Definition: solution.h:165
static DualRay FromProto(const ModelStorage *model, const DualRayProto &dual_ray_proto)
Definition: solution.cc:148
static DualSolution FromProto(const ModelStorage *model, const DualSolutionProto &dual_solution_proto)
Definition: solution.cc:130
LinearConstraintMap< double > dual_values
Definition: solution.h:133
std::optional< double > objective_value
Definition: solution.h:135
VariableMap< double > variable_values
Definition: solution.h:112
static PrimalRay FromProto(const ModelStorage *model, const PrimalRayProto &primal_ray_proto)
Definition: solution.cc:124
static PrimalSolution FromProto(const ModelStorage *model, const PrimalSolutionProto &primal_solution_proto)
Definition: solution.cc:109
static Solution FromProto(const ModelStorage *model, const SolutionProto &solution_proto)
Definition: solution.cc:171
std::optional< DualSolution > dual_solution
Definition: solution.h:223
std::optional< PrimalSolution > primal_solution
Definition: solution.h:222
std::optional< Basis > basis
Definition: solution.h:224