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