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