OR-Tools  9.3
linear_relaxation.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_SAT_LINEAR_RELAXATION_H_
15#define OR_TOOLS_SAT_LINEAR_RELAXATION_H_
16
17#include <vector>
18
19#include "ortools/sat/cp_model.pb.h"
21#include "ortools/sat/cuts.h"
22#include "ortools/sat/integer.h"
25#include "ortools/sat/model.h"
27
28namespace operations_research {
29namespace sat {
30
32 std::vector<LinearConstraint> linear_constraints;
33 std::vector<std::vector<Literal>> at_most_ones;
34 std::vector<CutGenerator> cut_generators;
35};
36
37// Looks at all the encoding literal (li <=> var == value_i) that have a
38// view and add a linear relaxation of their relationship with var.
39//
40// If the encoding is full, we can just add:
41// - Sum li == 1
42// - var == min_value + Sum li * (value_i - min_value)
43//
44// When the set of such encoding literals do not cover the full domain of var,
45// we do something a bit more involved. Let min_not_encoded/max_not_encoded the
46// min and max value of the domain of var that is NOT part of the encoding.
47// We add:
48// - Sum li <= 1
49// - var >= (Sum li * value_i) + (1 - Sum li) * min_not_encoded
50// - var <= (Sum li * value_i) + (1 - Sum li) * max_not_encoded
51//
52// Note of the special case where min_not_encoded == max_not_encoded that kind
53// of reduce to the full encoding, except with a different "rhs" value.
54//
55// We also increment the corresponding counter if we added something. We
56// consider the relaxation "tight" if the encoding was full or if
57// min_not_encoded == max_not_encoded.
58void AppendRelaxationForEqualityEncoding(IntegerVariable var,
59 const Model& model,
60 LinearRelaxation* relaxation,
61 int* num_tight, int* num_loose);
62
63// This is a different relaxation that use a partial set of literal li such that
64// (li <=> var >= xi). In which case we use the following encoding:
65// - li >= l_{i+1} for all possible i. Note that the xi need to be sorted.
66// - var >= min + l0 * (x0 - min) + Sum_{i>0} li * (xi - x_{i-1})
67// - and same as above for NegationOf(var) for the upper bound.
68//
69// Like for AppendRelaxationForEqualityEncoding() we skip any li that do not
70// have an integer view.
72 const Model& model,
73 LinearRelaxation* relaxation);
74
75// Returns a vector of new literals in exactly one relationship.
76// In addition, this create an IntegerView for all these literals and also add
77// the exactly one to the LinearRelaxation.
78std::vector<Literal> CreateAlternativeLiteralsWithView(
79 int num_literals, Model* model, LinearRelaxation* relaxation);
80
81void AppendBoolOrRelaxation(const ConstraintProto& ct, Model* model,
82 LinearRelaxation* relaxation);
83
84void AppendBoolAndRelaxation(const ConstraintProto& ct, Model* model,
85 LinearRelaxation* relaxation);
86
87void AppendAtMostOneRelaxation(const ConstraintProto& ct, Model* model,
88 LinearRelaxation* relaxation);
89
90void AppendExactlyOneRelaxation(const ConstraintProto& ct, Model* model,
91 LinearRelaxation* relaxation);
92
93// Adds linearization of int max constraints. Returns a vector of z vars such
94// that: z_vars[l] == 1 <=> target = exprs[l].
95//
96// Consider the Lin Max constraint with d expressions and n variables in the
97// form: target = max {exprs[l] = Sum (wli * xi + bl)}. l in {1,..,d}.
98// Li = lower bound of xi
99// Ui = upper bound of xi.
100// Let zl be in {0,1} for all l in {1,..,d}.
101// The target = exprs[l] when zl = 1.
102//
103// The following is a valid linearization for Lin Max.
104// target >= exprs[l], for all l in {1,..,d}
105// target <= Sum_i(wki * xi) + Sum_l((Nkl + bl) * zl), for all k in {1,..,d}
106// Where Nkl is a large number defined as:
107// Nkl = Sum_i(max((wli - wki)*Li, (wli - wki)*Ui))
108// = Sum (max corner difference for variable i, target expr k, max expr l)
109// Reference: "Strong mixed-integer programming formulations for trained neural
110// networks" by Ross Anderson et. (https://arxiv.org/pdf/1811.01988.pdf).
111// TODO(user): Support linear expression as target.
112void AppendLinMaxRelaxationPart1(const ConstraintProto& ct, Model* model,
113 LinearRelaxation* relaxation);
114
116 IntegerVariable target, const std::vector<Literal>& alternative_literals,
117 const std::vector<LinearExpression>& exprs, Model* model,
118 LinearRelaxation* relaxation);
119
120// Note: This only works if all affine expressions share the same variable.
121void AppendMaxAffineRelaxation(const ConstraintProto& ct, Model* model,
122 LinearRelaxation* relaxation);
123
124// Appends linear constraints to the relaxation. This also handles the
125// relaxation of linear constraints with enforcement literals.
126// A linear constraint lb <= ax <= ub with enforcement literals {ei} is relaxed
127// as following.
128// lb <= (Sum Negated(ei) * (lb - implied_lb)) + ax <= inf
129// -inf <= (Sum Negated(ei) * (ub - implied_ub)) + ax <= ub
130// Where implied_lb and implied_ub are trivial lower and upper bounds of the
131// constraint.
132void AppendLinearConstraintRelaxation(const ConstraintProto& ct,
133 bool linearize_enforced_constraints,
134 Model* model,
135 LinearRelaxation* relaxation);
136
137void AppendCircuitRelaxation(const ConstraintProto& ct, Model* model,
138 LinearRelaxation* relaxation);
139
140void AppendRoutesRelaxation(const ConstraintProto& ct, Model* model,
141 LinearRelaxation* relaxation);
142
143// Adds linearization of no overlap constraints.
144// It adds an energetic equation linking the duration of all potential tasks to
145// the actual span of the no overlap constraint.
146void AppendNoOverlapRelaxation(const CpModelProto& model_proto,
147 const ConstraintProto& ct, Model* model,
148 LinearRelaxation* relaxation);
149
150// Adds linearization of cumulative constraints.The second part adds an
151// energetic equation linking the duration of all potential tasks to the actual
152// max span * capacity of the cumulative constraint.
153void AppendCumulativeRelaxation(const CpModelProto& model_proto,
154 const ConstraintProto& ct, Model* model,
155 LinearRelaxation* relaxation);
156
157// Cut generators.
158void AddIntProdCutGenerator(const ConstraintProto& ct, int linearization_level,
159 Model* m, LinearRelaxation* relaxation);
160
161void AddAllDiffCutGenerator(const ConstraintProto& ct, Model* m,
162 LinearRelaxation* relaxation);
163
164void AddLinMaxCutGenerator(const ConstraintProto& ct, Model* m,
165 LinearRelaxation* relaxation);
166
167void AddCircuitCutGenerator(const ConstraintProto& ct, Model* m,
168 LinearRelaxation* relaxation);
169
170void AddRoutesCutGenerator(const ConstraintProto& ct, Model* m,
171 LinearRelaxation* relaxation);
172
173void AddCumulativeCutGenerator(const ConstraintProto& ct, Model* m,
174 LinearRelaxation* relaxation);
175
176void AddNoOverlapCutGenerator(const ConstraintProto& ct, Model* m,
177 LinearRelaxation* relaxation);
178
179void AddNoOverlap2dCutGenerator(const ConstraintProto& ct, Model* m,
180 LinearRelaxation* relaxation);
181
182// Adds linearization of different types of constraints.
183void TryToLinearizeConstraint(const CpModelProto& model_proto,
184 const ConstraintProto& ct,
185 int linearization_level, Model* model,
186 LinearRelaxation* relaxation);
187
188// Builds the linear relaxation of a CpModelProto.
190 Model* m);
191
192} // namespace sat
193} // namespace operations_research
194
195#endif // OR_TOOLS_SAT_LINEAR_RELAXATION_H_
Class that owns everything related to a particular optimization model.
Definition: sat/model.h:42
CpModelProto const * model_proto
const Constraint * ct
IntVar * var
Definition: expr_array.cc:1874
GRBmodel * model
void AddCumulativeCutGenerator(const ConstraintProto &ct, Model *m, LinearRelaxation *relaxation)
void AppendLinMaxRelaxationPart1(const ConstraintProto &ct, Model *model, LinearRelaxation *relaxation)
void AppendBoolOrRelaxation(const ConstraintProto &ct, Model *model, LinearRelaxation *relaxation)
void TryToLinearizeConstraint(const CpModelProto &model_proto, const ConstraintProto &ct, int linearization_level, Model *model, LinearRelaxation *relaxation)
void AppendNoOverlapRelaxation(const CpModelProto &model_proto, const ConstraintProto &ct, Model *model, LinearRelaxation *relaxation)
void AppendAtMostOneRelaxation(const ConstraintProto &ct, Model *model, LinearRelaxation *relaxation)
void AppendCumulativeRelaxation(const CpModelProto &model_proto, const ConstraintProto &ct, Model *model, LinearRelaxation *relaxation)
void AddNoOverlapCutGenerator(const ConstraintProto &ct, Model *m, LinearRelaxation *relaxation)
void AppendBoolAndRelaxation(const ConstraintProto &ct, Model *model, LinearRelaxation *relaxation)
void AppendLinearConstraintRelaxation(const ConstraintProto &ct, bool linearize_enforced_constraints, Model *model, LinearRelaxation *relaxation)
void AddNoOverlap2dCutGenerator(const ConstraintProto &ct, Model *m, LinearRelaxation *relaxation)
void AddIntProdCutGenerator(const ConstraintProto &ct, int linearization_level, Model *m, LinearRelaxation *relaxation)
void AddCircuitCutGenerator(const ConstraintProto &ct, Model *m, LinearRelaxation *relaxation)
void AppendMaxAffineRelaxation(const ConstraintProto &ct, Model *model, LinearRelaxation *relaxation)
void AppendExactlyOneRelaxation(const ConstraintProto &ct, Model *model, LinearRelaxation *relaxation)
std::vector< Literal > CreateAlternativeLiteralsWithView(int num_literals, Model *model, LinearRelaxation *relaxation)
void AppendCircuitRelaxation(const ConstraintProto &ct, Model *model, LinearRelaxation *relaxation)
void AddAllDiffCutGenerator(const ConstraintProto &ct, Model *m, LinearRelaxation *relaxation)
void AddLinMaxCutGenerator(const ConstraintProto &ct, Model *m, LinearRelaxation *relaxation)
void AppendRoutesRelaxation(const ConstraintProto &ct, Model *model, LinearRelaxation *relaxation)
void AppendRelaxationForEqualityEncoding(IntegerVariable var, const Model &model, LinearRelaxation *relaxation, int *num_tight, int *num_loose)
void AddRoutesCutGenerator(const ConstraintProto &ct, Model *m, LinearRelaxation *relaxation)
LinearRelaxation ComputeLinearRelaxation(const CpModelProto &model_proto, Model *m)
void AppendLinMaxRelaxationPart2(IntegerVariable target, const std::vector< Literal > &alternative_literals, const std::vector< LinearExpression > &exprs, Model *model, LinearRelaxation *relaxation)
void AppendPartialGreaterThanEncodingRelaxation(IntegerVariable var, const Model &model, LinearRelaxation *relaxation)
Collection of objects used to extend the Constraint Solver library.
std::vector< std::vector< Literal > > at_most_ones
std::vector< LinearConstraint > linear_constraints
std::vector< CutGenerator > cut_generators