<ahref="integer__search_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="l00014"></a><spanclass="lineno"> 14</span> <spanclass="comment">// This file contains all the top-level logic responsible for driving the search</span></div>
<divclass="line"><aname="l00015"></a><spanclass="lineno"> 15</span> <spanclass="comment">// of a satisfiability integer problem. What decision we take next, which new</span></div>
<divclass="line"><aname="l00016"></a><spanclass="lineno"> 16</span> <spanclass="comment">// Literal associated to an IntegerLiteral we create and when we restart.</span></div>
<divclass="line"><aname="l00018"></a><spanclass="lineno"> 18</span> <spanclass="comment">// For an optimization problem, our algorithm solves a sequence of decision</span></div>
<divclass="line"><aname="l00019"></a><spanclass="lineno"> 19</span> <spanclass="comment">// problem using this file as an entry point. Note that some heuristics here</span></div>
<divclass="line"><aname="l00020"></a><spanclass="lineno"> 20</span> <spanclass="comment">// still use the objective if there is one in order to orient the search towards</span></div>
<divclass="line"><aname="l00021"></a><spanclass="lineno"> 21</span> <spanclass="comment">// good feasible solution though.</span></div>
<divclass="line"><aname="l00035"></a><spanclass="lineno"> 35</span> <spanclass="comment">// This is used to hold the next decision the solver will take. It is either</span></div>
<divclass="line"><aname="l00036"></a><spanclass="lineno"> 36</span> <spanclass="comment">// a pure Boolean literal decision or correspond to an IntegerLiteral one.</span></div>
<divclass="line"><aname="l00038"></a><spanclass="lineno"> 38</span> <spanclass="comment">// At most one of the two options should be set.</span></div>
<divclass="line"><aname="l00055"></a><spanclass="lineno"> 55</span> <spanclass="comment">// Model struct that contains the search heuristics used to find a feasible</span></div>
<divclass="line"><aname="l00056"></a><spanclass="lineno"> 56</span> <spanclass="comment">// solution to an integer problem.</span></div>
<divclass="line"><aname="l00058"></a><spanclass="lineno"> 58</span> <spanclass="comment">// This is reset by ConfigureSearchHeuristics() and used by</span></div>
<divclass="line"><aname="l00059"></a><spanclass="lineno"> 59</span> <spanclass="comment">// SolveIntegerProblem(), see below.</span></div>
<divclass="line"><aname="l00061"></a><spanclass="lineno"> 61</span> <spanclass="comment">// Decision and restart heuristics. The two vectors must be of the same size</span></div>
<divclass="line"><aname="l00062"></a><spanclass="lineno"> 62</span> <spanclass="comment">// and restart_policies[i] will always be used in conjunction with</span></div>
<divclass="line"><aname="l00067"></a><spanclass="lineno"> 67</span> <spanclass="comment">// Index in the vectors above that indicate the current configuration.</span></div>
<divclass="line"><aname="l00070"></a><spanclass="lineno"> 70</span> <spanclass="comment">// Two special decision functions that are constructed at loading time.</span></div>
<divclass="line"><aname="l00071"></a><spanclass="lineno"> 71</span> <spanclass="comment">// These are used by ConfigureSearchHeuristics() to fill the policies above.</span></div>
<divclass="line"><aname="l00076"></a><spanclass="lineno"> 76</span> <spanclass="comment">// Given a base "fixed_search" function that should mainly control in which</span></div>
<divclass="line"><aname="l00077"></a><spanclass="lineno"> 77</span> <spanclass="comment">// order integer variables are lazily instantiated (and at what value), this</span></div>
<divclass="line"><aname="l00078"></a><spanclass="lineno"> 78</span> <spanclass="comment">// uses the current solver parameters to set the SearchHeuristics class in the</span></div>
<divclass="line"><aname="l00079"></a><spanclass="lineno"> 79</span> <spanclass="comment">// given model.</span></div>
<divclass="line"><aname="l00082"></a><spanclass="lineno"> 82</span> <spanclass="comment">// Callbacks that will be called when the search goes back to level 0.</span></div>
<divclass="line"><aname="l00083"></a><spanclass="lineno"> 83</span> <spanclass="comment">// Callbacks should return false if the propagation fails.</span></div>
<divclass="line"><aname="l00088"></a><spanclass="lineno"> 88</span> <spanclass="comment">// Tries to find a feasible solution to the current model.</span></div>
<divclass="line"><aname="l00090"></a><spanclass="lineno"> 90</span> <spanclass="comment">// This function continues from the current state of the solver and loop until</span></div>
<divclass="line"><aname="l00091"></a><spanclass="lineno"> 91</span> <spanclass="comment">// all variables are instantiated (i.e. the next decision is kNoLiteralIndex) or</span></div>
<divclass="line"><aname="l00092"></a><spanclass="lineno"> 92</span> <spanclass="comment">// a search limit is reached. It uses the heuristic from the SearchHeuristics</span></div>
<divclass="line"><aname="l00093"></a><spanclass="lineno"> 93</span> <spanclass="comment">// class in the model to decide when to restart and what next decision to take.</span></div>
<divclass="line"><aname="l00095"></a><spanclass="lineno"> 95</span> <spanclass="comment">// Each time a restart happen, this increment the policy index modulo the number</span></div>
<divclass="line"><aname="l00096"></a><spanclass="lineno"> 96</span> <spanclass="comment">// of heuristics to act as a portfolio search.</span></div>
<divclass="line"><aname="l00099"></a><spanclass="lineno"> 99</span> <spanclass="comment">// Resets the solver to the given assumptions before calling</span></div>
<divclass="line"><aname="l00104"></a><spanclass="lineno"> 104</span> <spanclass="comment">// Only used in tests. Move to a test utility file.</span></div>
<divclass="line"><aname="l00106"></a><spanclass="lineno"> 106</span> <spanclass="comment">// This configures the model SearchHeuristics with a simple default heuristic</span></div>
<divclass="line"><aname="l00107"></a><spanclass="lineno"> 107</span> <spanclass="comment">// and then call ResetAndSolveIntegerProblem() without any assumptions.</span></div>
<divclass="line"><aname="l00110"></a><spanclass="lineno"> 110</span> <spanclass="comment">// Returns decision corresponding to var at its lower bound.</span></div>
<divclass="line"><aname="l00111"></a><spanclass="lineno"> 111</span> <spanclass="comment">// Returns an invalid literal if the variable is fixed.</span></div>
<divclass="line"><aname="l00114"></a><spanclass="lineno"> 114</span> <spanclass="comment">// If a variable appear in the objective, branch on its best objective value.</span></div>
<divclass="line"><aname="l00122"></a><spanclass="lineno"> 122</span> <spanclass="comment">// This method first tries var <= value. If this does not reduce the domain it</span></div>
<divclass="line"><aname="l00123"></a><spanclass="lineno"> 123</span> <spanclass="comment">// tries var >= value. If that also does not reduce the domain then returns</span></div>
<divclass="line"><aname="l00124"></a><spanclass="lineno"> 124</span> <spanclass="comment">// an invalid literal.</span></div>
<divclass="line"><aname="l00128"></a><spanclass="lineno"> 128</span> <spanclass="comment">// Returns decision corresponding to var <= round(lp_value). If the variable</span></div>
<divclass="line"><aname="l00129"></a><spanclass="lineno"> 129</span> <spanclass="comment">// does not appear in the LP, this method returns an invalid literal.</span></div>
<divclass="line"><aname="l00132"></a><spanclass="lineno"> 132</span> <spanclass="comment">// Returns decision corresponding to var <= best_solution[var]. If no solution</span></div>
<divclass="line"><aname="l00133"></a><spanclass="lineno"> 133</span> <spanclass="comment">// has been found, this method returns a literal with kNoIntegerVariable. This</span></div>
<divclass="line"><aname="l00134"></a><spanclass="lineno"> 134</span> <spanclass="comment">// was suggested in paper: "Solution-Based Phase Saving for CP" (2018) by Emir</span></div>
<divclass="line"><aname="l00135"></a><spanclass="lineno"> 135</span> <spanclass="comment">// Demirovic, Geoffrey Chu, and Peter J. Stuckey.</span></div>
<divclass="line"><aname="l00139"></a><spanclass="lineno"> 139</span> <spanclass="comment">// Decision heuristic for SolveIntegerProblemWithLazyEncoding(). Returns a</span></div>
<divclass="line"><aname="l00140"></a><spanclass="lineno"> 140</span> <spanclass="comment">// function that will return the literal corresponding to the fact that the</span></div>
<divclass="line"><aname="l00141"></a><spanclass="lineno"> 141</span> <spanclass="comment">// first currently non-fixed variable value is <= its min. The function will</span></div>
<divclass="line"><aname="l00142"></a><spanclass="lineno"> 142</span> <spanclass="comment">// return kNoLiteralIndex if all the given variables are fixed.</span></div>
<divclass="line"><aname="l00144"></a><spanclass="lineno"> 144</span> <spanclass="comment">// Note that this function will create the associated literal if needed.</span></div>
<divclass="line"><aname="l00148"></a><spanclass="lineno"> 148</span> <spanclass="comment">// Decision heuristic for SolveIntegerProblemWithLazyEncoding(). Like</span></div>
<divclass="line"><aname="l00149"></a><spanclass="lineno"> 149</span> <spanclass="comment">// FirstUnassignedVarAtItsMinHeuristic() but the function will return the</span></div>
<divclass="line"><aname="l00150"></a><spanclass="lineno"> 150</span> <spanclass="comment">// literal corresponding to the fact that the currently non-assigned variable</span></div>
<divclass="line"><aname="l00151"></a><spanclass="lineno"> 151</span> <spanclass="comment">// with the lowest min has a value <= this min.</span></div>
<divclass="line"><aname="l00156"></a><spanclass="lineno"> 156</span> <spanclass="comment">// Set the first unassigned Literal/Variable to its value.</span></div>
<divclass="line"><aname="l00158"></a><spanclass="lineno"> 158</span> <spanclass="comment">// TODO(user): This is currently quadratic as we scan all variables to find the</span></div>
<divclass="line"><aname="l00159"></a><spanclass="lineno"> 159</span> <spanclass="comment">// first unassigned one. Fix. Note that this is also the case in many other</span></div>
<divclass="line"><aname="l00160"></a><spanclass="lineno"> 160</span> <spanclass="comment">// heuristics and should be fixed.</span></div>
<divclass="line"><aname="l00169"></a><spanclass="lineno"> 169</span> <spanclass="comment">// Combines search heuristics in order: if the i-th one returns kNoLiteralIndex,</span></div>
<divclass="line"><aname="l00170"></a><spanclass="lineno"> 170</span> <spanclass="comment">// ask the (i+1)-th. If every heuristic returned kNoLiteralIndex,</span></div>
<divclass="line"><aname="l00175"></a><spanclass="lineno"> 175</span> <spanclass="comment">// Changes the value of the given decision by 'var_selection_heuristic'. We try</span></div>
<divclass="line"><aname="l00176"></a><spanclass="lineno"> 176</span> <spanclass="comment">// to see if the decision is "associated" with an IntegerVariable, and if it is</span></div>
<divclass="line"><aname="l00177"></a><spanclass="lineno"> 177</span> <spanclass="comment">// the case, we choose the new value by the first 'value_selection_heuristics'</span></div>
<divclass="line"><aname="l00178"></a><spanclass="lineno"> 178</span> <spanclass="comment">// that is applicable. If none of the heuristics are applicable then the given</span></div>
<divclass="line"><aname="l00179"></a><spanclass="lineno"> 179</span> <spanclass="comment">// decision by 'var_selection_heuristic' is returned.</span></div>
<divclass="line"><aname="l00186"></a><spanclass="lineno"> 186</span> <spanclass="comment">// Changes the value of the given decision by 'var_selection_heuristic'</span></div>
<divclass="line"><aname="l00187"></a><spanclass="lineno"> 187</span> <spanclass="comment">// according to various value selection heuristics. Looks at the code to know</span></div>
<divclass="line"><aname="l00188"></a><spanclass="lineno"> 188</span> <spanclass="comment">// exactly what heuristic we use.</span></div>
<divclass="line"><aname="l00193"></a><spanclass="lineno"> 193</span> <spanclass="comment">// Returns the BooleanOrIntegerLiteral advised by the underliying SAT solver.</span></div>
<divclass="line"><aname="l00196"></a><spanclass="lineno"> 196</span> <spanclass="comment">// Gets the branching variable using pseudo costs and combines it with a value</span></div>
<divclass="line"><aname="l00197"></a><spanclass="lineno"> 197</span> <spanclass="comment">// for branching.</span></div>
<divclass="line"><aname="l00200"></a><spanclass="lineno"> 200</span> <spanclass="comment">// Returns true if the number of variables in the linearized part represent</span></div>
<divclass="line"><aname="l00201"></a><spanclass="lineno"> 201</span> <spanclass="comment">// a large enough proportion of all the problem variables.</span></div>
<divclass="line"><aname="l00204"></a><spanclass="lineno"> 204</span> <spanclass="comment">// A restart policy that restarts every k failures.</span></div>
<divclass="line"><aname="l00207"></a><spanclass="lineno"> 207</span> <spanclass="comment">// A restart policy that uses the underlying sat solver's policy.</span></div>
<divclass="line"><aname="l00210"></a><spanclass="lineno"> 210</span> <spanclass="comment">// Concatenates each input_heuristic with a default heuristic that instantiate</span></div>
<divclass="line"><aname="l00211"></a><spanclass="lineno"> 211</span> <spanclass="comment">// all the problem's Boolean variables, into a new vector.</span></div>
<divclass="line"><aname="l00217"></a><spanclass="lineno"> 217</span> <spanclass="comment">// Specialized search that will continuously probe Boolean variables and bounds</span></div>
<divclass="line"><aname="l00218"></a><spanclass="lineno"> 218</span> <spanclass="comment">// of integer variables.</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_a304417ca7c3964cc928b771620b2dc53"><divclass="ttname"><ahref="namespaceoperations__research_1_1sat.html#a304417ca7c3964cc928b771620b2dc53">operations_research::sat::AtMinValue</a></div><divclass="ttdeci">IntegerLiteral AtMinValue(IntegerVariable var, IntegerTrail *integer_trail)</div><divclass="ttdef"><b>Definition:</b><ahref="integer__search_8cc_source.html#l00040">integer_search.cc:40</a></div></div>
<divclass="ttc"id="anamespaceoperations__research_1_1sat_html_a3f4df9da99787316bfb5029b7a6b92b7"><divclass="ttname"><ahref="namespaceoperations__research_1_1sat.html#a3f4df9da99787316bfb5029b7a6b92b7">operations_research::sat::GreaterOrEqualToMiddleValue</a></div><divclass="ttdeci">IntegerLiteral GreaterOrEqualToMiddleValue(IntegerVariable var, IntegerTrail *integer_trail)</div><divclass="ttdef"><b>Definition:</b><ahref="integer__search_8cc_source.html#l00060">integer_search.cc:60</a></div></div>
<divclass="ttc"id="anamespaceoperations__research_1_1sat_html_a46cb4c07c4971a99724693260c92fd5b"><divclass="ttname"><ahref="namespaceoperations__research_1_1sat.html#a46cb4c07c4971a99724693260c92fd5b">operations_research::sat::SplitAroundGivenValue</a></div><divclass="ttdeci">IntegerLiteral SplitAroundGivenValue(IntegerVariable var, IntegerValue value, Model *model)</div><divclass="ttdef"><b>Definition:</b><ahref="integer__search_8cc_source.html#l00071">integer_search.cc:71</a></div></div>
<divclass="ttc"id="anamespaceoperations__research_1_1sat_html_a4713f8fb7e1ccddece18a374886ad866"><divclass="ttname"><ahref="namespaceoperations__research_1_1sat.html#a4713f8fb7e1ccddece18a374886ad866">operations_research::sat::UnassignedVarWithLowestMinAtItsMinHeuristic</a></div><divclass="ttdeci">std::function< BooleanOrIntegerLiteral()> UnassignedVarWithLowestMinAtItsMinHeuristic(const std::vector< IntegerVariable >&vars, Model *model)</div><divclass="ttdef"><b>Definition:</b><ahref="integer__search_8cc_source.html#l00169">integer_search.cc:169</a></div></div>
<divclass="ttc"id="anamespaceoperations__research_1_1sat_html_a71fa416b44768076a0e7dd7777ab433d"><divclass="ttname"><ahref="namespaceoperations__research_1_1sat.html#a71fa416b44768076a0e7dd7777ab433d">operations_research::sat::ChooseBestObjectiveValue</a></div><divclass="ttdeci">IntegerLiteral ChooseBestObjectiveValue(IntegerVariable var, Model *model)</div><divclass="ttdef"><b>Definition:</b><ahref="integer__search_8cc_source.html#l00048">integer_search.cc:48</a></div></div>
<divclass="ttc"id="anamespaceoperations__research_1_1sat_html_a872297a32bd1f4a91bbcebd1c47b3751"><divclass="ttname"><ahref="namespaceoperations__research_1_1sat.html#a872297a32bd1f4a91bbcebd1c47b3751">operations_research::sat::SplitDomainUsingBestSolutionValue</a></div><divclass="ttdeci">IntegerLiteral SplitDomainUsingBestSolutionValue(IntegerVariable var, Model *model)</div></div>
<divclass="ttc"id="anamespaceoperations__research_1_1sat_html_ac0774a1df651b83339b00fee0bde1cd8"><divclass="ttname"><ahref="namespaceoperations__research_1_1sat.html#ac0774a1df651b83339b00fee0bde1cd8">operations_research::sat::SplitAroundLpValue</a></div><divclass="ttdeci">IntegerLiteral SplitAroundLpValue(IntegerVariable var, Model *model)</div><divclass="ttdef"><b>Definition:</b><ahref="integer__search_8cc_source.html#l00099">integer_search.cc:99</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>