<ahref="linear__relaxation_8h.html">Go to the documentation of this file.</a><divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> <spanclass="comment">// Copyright 2010-2021 Google LLC</span></div>
<divclass="line"><aname="l00002"></a><spanclass="lineno"> 2</span> <spanclass="comment">// Licensed under the Apache License, Version 2.0 (the "License");</span></div>
<divclass="line"><aname="l00003"></a><spanclass="lineno"> 3</span> <spanclass="comment">// you may not use this file except in compliance with the License.</span></div>
<divclass="line"><aname="l00004"></a><spanclass="lineno"> 4</span> <spanclass="comment">// You may obtain a copy of the License at</span></div>
<divclass="line"><aname="l00008"></a><spanclass="lineno"> 8</span> <spanclass="comment">// Unless required by applicable law or agreed to in writing, software</span></div>
<divclass="line"><aname="l00009"></a><spanclass="lineno"> 9</span> <spanclass="comment">// distributed under the License is distributed on an "AS IS" BASIS,</span></div>
<divclass="line"><aname="l00010"></a><spanclass="lineno"> 10</span> <spanclass="comment">// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></div>
<divclass="line"><aname="l00011"></a><spanclass="lineno"> 11</span> <spanclass="comment">// See the License for the specific language governing permissions and</span></div>
<divclass="line"><aname="l00012"></a><spanclass="lineno"> 12</span> <spanclass="comment">// limitations under the License.</span></div>
<divclass="line"><aname="l00034"></a><spanclass="lineno"> 34</span> <spanclass="comment">// If the given IntegerVariable is fully encoded (li <=> var == xi), adds to the</span></div>
<divclass="line"><aname="l00035"></a><spanclass="lineno"> 35</span> <spanclass="comment">// constraints vector the following linear relaxation of its encoding:</span></div>
<divclass="line"><aname="l00036"></a><spanclass="lineno"> 36</span> <spanclass="comment">// - Sum li == 1</span></div>
<divclass="line"><aname="l00037"></a><spanclass="lineno"> 37</span> <spanclass="comment">// - Sum li * xi == var</span></div>
<divclass="line"><aname="l00038"></a><spanclass="lineno"> 38</span> <spanclass="comment">// Note that all the literal (li) of the encoding must have an IntegerView,</span></div>
<divclass="line"><aname="l00039"></a><spanclass="lineno"> 39</span> <spanclass="comment">// otherwise this function just does nothing.</span></div>
<divclass="line"><aname="l00041"></a><spanclass="lineno"> 41</span> <spanclass="comment">// Returns false, if the relaxation couldn't be added because this variable</span></div>
<divclass="line"><aname="l00042"></a><spanclass="lineno"> 42</span> <spanclass="comment">// was not fully encoded or not all its associated literal had a view.</span></div>
<divclass="line"><aname="l00046"></a><spanclass="lineno"> 46</span> <spanclass="comment">// When the set of (li <=> var == xi) do not cover the full domain of xi, we</span></div>
<divclass="line"><aname="l00047"></a><spanclass="lineno"> 47</span> <spanclass="comment">// do something a bit more involved. Let min/max the min and max value of the</span></div>
<divclass="line"><aname="l00048"></a><spanclass="lineno"> 48</span> <spanclass="comment">// domain of var that is NOT part of the encoding. We add:</span></div>
<divclass="line"><aname="l00049"></a><spanclass="lineno"> 49</span> <spanclass="comment">// - Sum li <= 1</span></div>
<divclass="line"><aname="l00050"></a><spanclass="lineno"> 50</span> <spanclass="comment">// - (Sum li * xi) + (1 - Sum li) * min <= var</span></div>
<divclass="line"><aname="l00051"></a><spanclass="lineno"> 51</span> <spanclass="comment">// - var <= (Sum li * xi) + (1 - Sum li) * max</span></div>
<divclass="line"><aname="l00053"></a><spanclass="lineno"> 53</span> <spanclass="comment">// Note that if it turns out that the partial encoding is full, this will just</span></div>
<divclass="line"><aname="l00054"></a><spanclass="lineno"> 54</span> <spanclass="comment">// use the same encoding as AppendFullEncodingRelaxation(). Any literal that</span></div>
<divclass="line"><aname="l00055"></a><spanclass="lineno"> 55</span> <spanclass="comment">// do not have an IntegerView will be skipped, there is no point adding them</span></div>
<divclass="line"><aname="l00056"></a><spanclass="lineno"> 56</span> <spanclass="comment">// to the LP if they are not used in any other constraint, the relaxation will</span></div>
<divclass="line"><aname="l00057"></a><spanclass="lineno"> 57</span> <spanclass="comment">// have the same "power" without them.</span></div>
<divclass="line"><aname="l00061"></a><spanclass="lineno"> 61</span> <spanclass="comment">// This is a different relaxation that use a partial set of literal li such that</span></div>
<divclass="line"><aname="l00062"></a><spanclass="lineno"> 62</span> <spanclass="comment">// (li <=> var >= xi). In which case we use the following encoding:</span></div>
<divclass="line"><aname="l00063"></a><spanclass="lineno"> 63</span> <spanclass="comment">// - li >= l_{i+1} for all possible i. Note that the xi need to be sorted.</span></div>
<divclass="line"><aname="l00064"></a><spanclass="lineno"> 64</span> <spanclass="comment">// - var >= min + l0 * (x0 - min) + Sum_{i>0} li * (xi - x_{i-1})</span></div>
<divclass="line"><aname="l00065"></a><spanclass="lineno"> 65</span> <spanclass="comment">// - and same as above for NegationOf(var) for the upper bound.</span></div>
<divclass="line"><aname="l00067"></a><spanclass="lineno"> 67</span> <spanclass="comment">// Like for AppendPartialEncodingRelaxation() we skip any li that do not have</span></div>
<divclass="line"><aname="l00068"></a><spanclass="lineno"> 68</span> <spanclass="comment">// an integer view.</span></div>
<divclass="line"><aname="l00073"></a><spanclass="lineno"> 73</span> <spanclass="comment">// Adds linearization of different types of constraints.</span></div>
<divclass="line"><aname="l00079"></a><spanclass="lineno"> 79</span> <spanclass="comment">// Adds linearization of no overlap constraints.</span></div>
<divclass="line"><aname="l00080"></a><spanclass="lineno"> 80</span> <spanclass="comment">// It adds an energetic equation linking the duration of all potential tasks to</span></div>
<divclass="line"><aname="l00081"></a><spanclass="lineno"> 81</span> <spanclass="comment">// the actual span of the no overlap constraint.</span></div>
<divclass="line"><aname="l00087"></a><spanclass="lineno"> 87</span> <spanclass="comment">// Adds linearization of cumulative constraints.The second part adds an</span></div>
<divclass="line"><aname="l00088"></a><spanclass="lineno"> 88</span> <spanclass="comment">// energetic equation linking the duration of all potential tasks to the actual</span></div>
<divclass="line"><aname="l00089"></a><spanclass="lineno"> 89</span> <spanclass="comment">// max span * capacity of the cumulative constraint.</span></div>
<divclass="line"><aname="l00095"></a><spanclass="lineno"> 95</span> <spanclass="comment">// Adds linearization of int max constraints. This can also be used to linearize</span></div>
<divclass="line"><aname="l00096"></a><spanclass="lineno"> 96</span> <spanclass="comment">// int min with negated variables.</span></div>
<divclass="line"><aname="l00102"></a><spanclass="lineno"> 102</span> <spanclass="comment">// Adds linearization of int max constraints. Returns a vector of z vars such</span></div>
<divclass="line"><aname="l00105"></a><spanclass="lineno"> 105</span> <spanclass="comment">// Consider the Lin Max constraint with d expressions and n variables in the</span></div>
<divclass="line"><aname="l00106"></a><spanclass="lineno"> 106</span> <spanclass="comment">// form: target = max {exprs[l] = Sum (wli * xi + bl)}. l in {1,..,d}.</span></div>
<divclass="line"><aname="l00107"></a><spanclass="lineno"> 107</span> <spanclass="comment">// Li = lower bound of xi</span></div>
<divclass="line"><aname="l00108"></a><spanclass="lineno"> 108</span> <spanclass="comment">// Ui = upper bound of xi.</span></div>
<divclass="line"><aname="l00109"></a><spanclass="lineno"> 109</span> <spanclass="comment">// Let zl be in {0,1} for all l in {1,..,d}.</span></div>
<divclass="line"><aname="l00110"></a><spanclass="lineno"> 110</span> <spanclass="comment">// The target = exprs[l] when zl = 1.</span></div>
<divclass="line"><aname="l00112"></a><spanclass="lineno"> 112</span> <spanclass="comment">// The following is a valid linearization for Lin Max.</span></div>
<divclass="line"><aname="l00113"></a><spanclass="lineno"> 113</span> <spanclass="comment">// target >= exprs[l], for all l in {1,..,d}</span></div>
<divclass="line"><aname="l00114"></a><spanclass="lineno"> 114</span> <spanclass="comment">// target <= Sum_i(wki * xi) + Sum_l((Nkl + bl) * zl), for all k in {1,..,d}</span></div>
<divclass="line"><aname="l00115"></a><spanclass="lineno"> 115</span> <spanclass="comment">// Where Nkl is a large number defined as:</span></div>
<divclass="line"><aname="l00117"></a><spanclass="lineno"> 117</span> <spanclass="comment">// = Sum (max corner difference for variable i, target expr k, max expr l)</span></div>
<divclass="line"><aname="l00118"></a><spanclass="lineno"> 118</span> <spanclass="comment">// Reference: "Strong mixed-integer programming formulations for trained neural</span></div>
<divclass="line"><aname="l00119"></a><spanclass="lineno"> 119</span> <spanclass="comment">// networks" by Ross Anderson et. (https://arxiv.org/pdf/1811.01988.pdf).</span></div>
<divclass="line"><aname="l00120"></a><spanclass="lineno"> 120</span> <spanclass="comment">// TODO(user): Support linear expression as target.</span></div>
<divclass="line"><aname="l00125"></a><spanclass="lineno"> 125</span> <spanclass="comment">// Appends linear constraints to the relaxation. This also handles the</span></div>
<divclass="line"><aname="l00126"></a><spanclass="lineno"> 126</span> <spanclass="comment">// relaxation of linear constraints with enforcement literals.</span></div>
<divclass="line"><aname="l00127"></a><spanclass="lineno"> 127</span> <spanclass="comment">// A linear constraint lb <= ax <= ub with enforcement literals {ei} is relaxed</span></div>
<divclass="line"><aname="l00128"></a><spanclass="lineno"> 128</span> <spanclass="comment">// as following.</span></div>
<divclass="line"><aname="l00131"></a><spanclass="lineno"> 131</span> <spanclass="comment">// Where implied_lb and implied_ub are trivial lower and upper bounds of the</span></div>
<divclass="ttc"id="aclassoperations__research_1_1sat_1_1_model_html"><divclass="ttname"><ahref="classoperations__research_1_1sat_1_1_model.html">operations_research::sat::Model</a></div><divclass="ttdoc">Class that owns everything related to a particular optimization model.</div><divclass="ttdef"><b>Definition:</b><ahref="sat_2model_8h_source.html#l00038">sat/model.h:38</a></div></div>
<divclass="ttc"id="anamespaceoperations__research_1_1sat_html_a1d3cc11409a085647b7fb94b62a82ae5"><divclass="ttname"><ahref="namespaceoperations__research_1_1sat.html#a1d3cc11409a085647b7fb94b62a82ae5">operations_research::sat::AppendLinearConstraintRelaxation</a></div><divclass="ttdeci">void AppendLinearConstraintRelaxation(const ConstraintProto &constraint_proto, const int linearization_level, const Model &model, LinearRelaxation *relaxation)</div><divclass="ttdef"><b>Definition:</b><ahref="linear__relaxation_8cc_source.html#l00812">linear_relaxation.cc:812</a></div></div>
<divclass="ttc"id="anamespaceoperations__research_1_1sat_html_a5564bcb1461ad3cbce7f744c3fb58c6b"><divclass="ttname"><ahref="namespaceoperations__research_1_1sat.html#a5564bcb1461ad3cbce7f744c3fb58c6b">operations_research::sat::AppendFullEncodingRelaxation</a></div><divclass="ttdeci">bool AppendFullEncodingRelaxation(IntegerVariable var, const Model &model, LinearRelaxation *relaxation)</div><divclass="ttdef"><b>Definition:</b><ahref="linear__relaxation_8cc_source.html#l00035">linear_relaxation.cc:35</a></div></div>
<divclass="ttc"id="anamespaceoperations__research_1_1sat_html_a69d42878d739b511b1abd4843592cdb3"><divclass="ttname"><ahref="namespaceoperations__research_1_1sat.html#a69d42878d739b511b1abd4843592cdb3">operations_research::sat::AppendMaxRelaxation</a></div><divclass="ttdeci">void AppendMaxRelaxation(IntegerVariable target, const std::vector< IntegerVariable >&vars, int linearization_level, Model *model, LinearRelaxation *relaxation)</div><divclass="ttdef"><b>Definition:</b><ahref="linear__relaxation_8cc_source.html#l00641">linear_relaxation.cc:641</a></div></div>
<divclass="ttc"id="anamespaceoperations__research_1_1sat_html_a94739087f86f5e1b7679d5371d4d9f4b"><divclass="ttname"><ahref="namespaceoperations__research_1_1sat.html#a94739087f86f5e1b7679d5371d4d9f4b">operations_research::sat::TryToLinearizeConstraint</a></div><divclass="ttdeci">void TryToLinearizeConstraint(const CpModelProto &model_proto, const ConstraintProto &ct, Model *model, int linearization_level, LinearRelaxation *relaxation)</div><divclass="ttdef"><b>Definition:</b><ahref="linear__relaxation_8cc_source.html#l00325">linear_relaxation.cc:325</a></div></div>
<divclass="ttc"id="anamespaceoperations__research_1_1sat_html_aa490b08ec514e9c567ff098d53353db1"><divclass="ttname"><ahref="namespaceoperations__research_1_1sat.html#aa490b08ec514e9c567ff098d53353db1">operations_research::sat::AppendPartialEncodingRelaxation</a></div><divclass="ttdeci">void AppendPartialEncodingRelaxation(IntegerVariable var, const Model &model, LinearRelaxation *relaxation)</div><divclass="ttdef"><b>Definition:</b><ahref="linear__relaxation_8cc_source.html#l00112">linear_relaxation.cc:112</a></div></div>
<divclass="ttc"id="anamespaceoperations__research_1_1sat_html_acc7b292198fce358615d0295cd1c3078"><divclass="ttname"><ahref="namespaceoperations__research_1_1sat.html#acc7b292198fce358615d0295cd1c3078">operations_research::sat::AppendNoOverlapRelaxation</a></div><divclass="ttdeci">void AppendNoOverlapRelaxation(const CpModelProto &model_proto, const ConstraintProto &ct, int linearization_level, Model *model, LinearRelaxation *relaxation)</div><divclass="ttdef"><b>Definition:</b><ahref="linear__relaxation_8cc_source.html#l00626">linear_relaxation.cc:626</a></div></div>
<divclass="ttc"id="anamespaceoperations__research_1_1sat_html_ae32ce362cc268798a2365882c1de7894"><divclass="ttname"><ahref="namespaceoperations__research_1_1sat.html#ae32ce362cc268798a2365882c1de7894">operations_research::sat::AppendCumulativeRelaxation</a></div><divclass="ttdeci">void AppendCumulativeRelaxation(const CpModelProto &model_proto, const ConstraintProto &ct, int linearization_level, Model *model, LinearRelaxation *relaxation)</div><divclass="ttdef"><b>Definition:</b><ahref="linear__relaxation_8cc_source.html#l00607">linear_relaxation.cc:607</a></div></div>
<divclass="ttc"id="anamespaceoperations__research_1_1sat_html_aff14a4092356fceea657393b7b234b7f"><divclass="ttname"><ahref="namespaceoperations__research_1_1sat.html#aff14a4092356fceea657393b7b234b7f">operations_research::sat::AppendPartialGreaterThanEncodingRelaxation</a></div><divclass="ttdeci">void AppendPartialGreaterThanEncodingRelaxation(IntegerVariable var, const Model &model, LinearRelaxation *relaxation)</div><divclass="ttdef"><b>Definition:</b><ahref="linear__relaxation_8cc_source.html#l00187">linear_relaxation.cc:187</a></div></div>
<divclass="ttc"id="anamespaceoperations__research_html"><divclass="ttname"><ahref="namespaceoperations__research.html">operations_research</a></div><divclass="ttdoc">Collection of objects used to extend the Constraint Solver library.</div><divclass="ttdef"><b>Definition:</b><ahref="dense__doubly__linked__list_8h_source.html#l00021">dense_doubly_linked_list.h:21</a></div></div>