<ahref="bop__ls_8h.html">Go to the documentation of this file.</a><divclass="fragment"><divclass="line"><aid="l00001"name="l00001"></a><spanclass="lineno"> 1</span><spanclass="comment">// Copyright 2010-2021 Google LLC</span></div>
<divclass="line"><aid="l00002"name="l00002"></a><spanclass="lineno"> 2</span><spanclass="comment">// Licensed under the Apache License, Version 2.0 (the "License");</span></div>
<divclass="line"><aid="l00003"name="l00003"></a><spanclass="lineno"> 3</span><spanclass="comment">// you may not use this file except in compliance with the License.</span></div>
<divclass="line"><aid="l00004"name="l00004"></a><spanclass="lineno"> 4</span><spanclass="comment">// You may obtain a copy of the License at</span></div>
<divclass="line"><aid="l00008"name="l00008"></a><spanclass="lineno"> 8</span><spanclass="comment">// Unless required by applicable law or agreed to in writing, software</span></div>
<divclass="line"><aid="l00009"name="l00009"></a><spanclass="lineno"> 9</span><spanclass="comment">// distributed under the License is distributed on an "AS IS" BASIS,</span></div>
<divclass="line"><aid="l00010"name="l00010"></a><spanclass="lineno"> 10</span><spanclass="comment">// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></div>
<divclass="line"><aid="l00011"name="l00011"></a><spanclass="lineno"> 11</span><spanclass="comment">// See the License for the specific language governing permissions and</span></div>
<divclass="line"><aid="l00012"name="l00012"></a><spanclass="lineno"> 12</span><spanclass="comment">// limitations under the License.</span></div>
<divclass="line"><aid="l00014"name="l00014"></a><spanclass="lineno"> 14</span><spanclass="comment">// This file defines the needed classes to efficiently perform Local Search in</span></div>
<divclass="line"><aid="l00016"name="l00016"></a><spanclass="lineno"> 16</span><spanclass="comment">// Local Search is a technique used to locally improve an existing solution by</span></div>
<divclass="line"><aid="l00017"name="l00017"></a><spanclass="lineno"> 17</span><spanclass="comment">// flipping a limited number of variables. To be successful the produced</span></div>
<divclass="line"><aid="l00018"name="l00018"></a><spanclass="lineno"> 18</span><spanclass="comment">// solution has to satisfy all constraints of the problem and improve the</span></div>
<divclass="line"><aid="l00021"name="l00021"></a><spanclass="lineno"> 21</span><spanclass="comment">// The class BopLocalSearchOptimizer is the only public interface for Local</span></div>
<divclass="line"><aid="l00022"name="l00022"></a><spanclass="lineno"> 22</span><spanclass="comment">// Search in Bop. For unit-testing purposes this file also contains the four</span></div>
<divclass="line"><aid="l00024"name="l00024"></a><spanclass="lineno"> 24</span><spanclass="comment">// OneFlipConstraintRepairer, SatWrapper and LocalSearchAssignmentIterator.</span></div>
<divclass="line"><aid="l00025"name="l00025"></a><spanclass="lineno"> 25</span><spanclass="comment">// They are implementation details and should not be used outside of bop_ls.</span></div>
<divclass="line"><aid="l00049"name="l00049"></a><spanclass="lineno"> 49</span><spanclass="comment">// This class is used to ease the connection with the SAT solver.</span></div>
<divclass="line"><aid="l00051"name="l00051"></a><spanclass="lineno"> 51</span><spanclass="comment">// TODO(user): remove? the meat of the logic is used in just one place, so I am</span></div>
<divclass="line"><aid="l00052"name="l00052"></a><spanclass="lineno"> 52</span><spanclass="comment">// not sure having this extra layer improve the readability.</span></div>
<divclass="line"><aid="l00057"name="l00057"></a><spanclass="lineno"> 57</span><spanclass="comment">// Returns the current state of the solver propagation trail.</span></div>
<divclass="line"><aid="l00060"name="l00060"></a><spanclass="lineno"> 60</span><spanclass="comment">// Returns true if the problem is UNSAT.</span></div>
<divclass="line"><aid="l00061"name="l00061"></a><spanclass="lineno"> 61</span><spanclass="comment">// Note that an UNSAT problem might not be marked as UNSAT at first because</span></div>
<divclass="line"><aid="l00062"name="l00062"></a><spanclass="lineno"> 62</span><spanclass="comment">// the SAT solver is not able to prove it; After some decisions / learned</span></div>
<divclass="line"><aid="l00063"name="l00063"></a><spanclass="lineno"> 63</span><spanclass="comment">// conflicts, the SAT solver might be able to prove UNSAT and so this will</span></div>
<divclass="line"><aid="l00067"name="l00067"></a><spanclass="lineno"> 67</span><spanclass="comment">// Return the current solver VariablesAssignment.</span></div>
<divclass="line"><aid="l00072"name="l00072"></a><spanclass="lineno"> 72</span><spanclass="comment">// Applies the decision that makes the given literal true and returns the</span></div>
<divclass="line"><aid="l00073"name="l00073"></a><spanclass="lineno"> 73</span><spanclass="comment">// number of decisions to backtrack due to conflicts if any.</span></div>
<divclass="line"><aid="l00074"name="l00074"></a><spanclass="lineno"> 74</span><spanclass="comment">// Two cases:</span></div>
<divclass="line"><aid="l00075"name="l00075"></a><spanclass="lineno"> 75</span><spanclass="comment">// - No conflicts: Returns 0 and fills the propagated_literals with the</span></div>
<divclass="line"><aid="l00076"name="l00076"></a><spanclass="lineno"> 76</span><spanclass="comment">// literals that have been propagated due to the decision including the</span></div>
<divclass="line"><aid="l00077"name="l00077"></a><spanclass="lineno"> 77</span><spanclass="comment">// the decision itself.</span></div>
<divclass="line"><aid="l00078"name="l00078"></a><spanclass="lineno"> 78</span><spanclass="comment">// - Conflicts: Returns the number of decisions to backtrack (the current</span></div>
<divclass="line"><aid="l00079"name="l00079"></a><spanclass="lineno"> 79</span><spanclass="comment">// decision included, i.e. returned value > 0) and fills the</span></div>
<divclass="line"><aid="l00080"name="l00080"></a><spanclass="lineno"> 80</span><spanclass="comment">// propagated_literals with the literals that the conflicts propagated.</span></div>
<divclass="line"><aid="l00081"name="l00081"></a><spanclass="lineno"> 81</span><spanclass="comment">// Note that the decision variable should not be already assigned in SAT.</span></div>
<divclass="line"><aid="l00085"name="l00085"></a><spanclass="lineno"> 85</span><spanclass="comment">// Backtracks the last decision if any.</span></div>
<divclass="line"><aid="l00091"name="l00091"></a><spanclass="lineno"> 91</span><spanclass="comment">// Extracts any new information learned during the search.</span></div>
<divclass="line"><aid="l00094"name="l00094"></a><spanclass="lineno"> 94</span><spanclass="comment">// Returns a deterministic number that should be correlated with the time</span></div>
<divclass="line"><aid="l00095"name="l00095"></a><spanclass="lineno"> 95</span><spanclass="comment">// spent in the SAT wrapper. The order of magnitude should be close to the</span></div>
<divclass="line"><aid="l00096"name="l00096"></a><spanclass="lineno"> 96</span><spanclass="comment">// time in seconds.</span></div>
<divclass="line"><aid="l00107"name="l00107"></a><spanclass="lineno"> 107</span><spanclass="comment">// This class defines a Local Search optimizer. The goal is to find a new</span></div>
<divclass="line"><aid="l00108"name="l00108"></a><spanclass="lineno"> 108</span><spanclass="comment">// solution with a better cost than the given solution by iterating on all</span></div>
<divclass="line"><aid="l00109"name="l00109"></a><spanclass="lineno"> 109</span><spanclass="comment">// assignments that can be reached in max_num_decisions decisions or less.</span></div>
<divclass="line"><aid="l00110"name="l00110"></a><spanclass="lineno"> 110</span><spanclass="comment">// The bop parameter max_number_of_explored_assignments_per_try_in_ls can be</span></div>
<divclass="line"><aid="l00111"name="l00111"></a><spanclass="lineno"> 111</span><spanclass="comment">// used to specify the number of new assignments to iterate on each time the</span></div>
<divclass="line"><aid="l00112"name="l00112"></a><spanclass="lineno"> 112</span><spanclass="comment">// method Optimize() is called. Limiting that parameter allows to reduce the</span></div>
<divclass="line"><aid="l00113"name="l00113"></a><spanclass="lineno"> 113</span><spanclass="comment">// time spent in the Optimize() method at once, and still explore all the</span></div>
<divclass="line"><aid="l00114"name="l00114"></a><spanclass="lineno"> 114</span><spanclass="comment">// reachable assignments (if Optimize() is called enough times).</span></div>
<divclass="line"><aid="l00115"name="l00115"></a><spanclass="lineno"> 115</span><spanclass="comment">// Note that due to propagation, the number of variables with a different value</span></div>
<divclass="line"><aid="l00116"name="l00116"></a><spanclass="lineno"> 116</span><spanclass="comment">// in the new solution can be greater than max_num_decisions.</span></div>
<divclass="line"><aid="l00131"name="l00131"></a><spanclass="lineno"> 131</span><spanclass="comment">// Maximum number of decisions the Local Search can take.</span></div>
<divclass="line"><aid="l00132"name="l00132"></a><spanclass="lineno"> 132</span><spanclass="comment">// Note that there is no limit on the number of changed variables due to</span></div>
<divclass="line"><aid="l00136"name="l00136"></a><spanclass="lineno"> 136</span><spanclass="comment">// A wrapper around the given sat_propagator.</span></div>
<divclass="line"><aid="l00139"name="l00139"></a><spanclass="lineno"> 139</span><spanclass="comment">// Iterator on all reachable assignments.</span></div>
<divclass="line"><aid="l00140"name="l00140"></a><spanclass="lineno"> 140</span><spanclass="comment">// Note that this iterator is only reset when Synchronize() is called, i.e.</span></div>
<divclass="line"><aid="l00141"name="l00141"></a><spanclass="lineno"> 141</span><spanclass="comment">// the iterator continues its iteration of the next assignments each time</span></div>
<divclass="line"><aid="l00142"name="l00142"></a><spanclass="lineno"> 142</span><spanclass="comment">// Optimize() is called until everything is explored or a solution is found.</span></div>
<divclass="line"><aid="l00150"name="l00150"></a><spanclass="lineno"> 150</span><spanclass="comment">// Implementation details. The declarations of those utility classes are in</span></div>
<divclass="line"><aid="l00151"name="l00151"></a><spanclass="lineno"> 151</span><spanclass="comment">// the .h for testing reasons.</span></div>
<divclass="line"><aid="l00154"name="l00154"></a><spanclass="lineno"> 154</span><spanclass="comment">// Maintains some information on a sparse set of integers in [0, n). More</span></div>
<divclass="line"><aid="l00155"name="l00155"></a><spanclass="lineno"> 155</span><spanclass="comment">// specifically this class:</span></div>
<divclass="line"><aid="l00156"name="l00156"></a><spanclass="lineno"> 156</span><spanclass="comment">// - Allows to dynamically add/remove element from the set.</span></div>
<divclass="line"><aid="l00157"name="l00157"></a><spanclass="lineno"> 157</span><spanclass="comment">// - Has a backtracking support.</span></div>
<divclass="line"><aid="l00158"name="l00158"></a><spanclass="lineno"> 158</span><spanclass="comment">// - Maintains the number of elements in the set.</span></div>
<divclass="line"><aid="l00159"name="l00159"></a><spanclass="lineno"> 159</span><spanclass="comment">// - Maintains a superset of the elements of the set that contains all the</span></div>
<divclass="line"><aid="l00166"name="l00166"></a><spanclass="lineno"> 166</span><spanclass="comment">// Prepares the class for integers in [0, n) and initializes the set to the</span></div>
<divclass="line"><aid="l00167"name="l00167"></a><spanclass="lineno"> 167</span><spanclass="comment">// empty one. Note that this run in O(n). Once resized, it is better to call</span></div>
<divclass="line"><aid="l00168"name="l00168"></a><spanclass="lineno"> 168</span><spanclass="comment">// BacktrackAll() instead of this to clear the set.</span></div>
<divclass="line"><aid="l00171"name="l00171"></a><spanclass="lineno"> 171</span><spanclass="comment">// Changes the state of the given integer i to be either inside or outside the</span></div>
<divclass="line"><aid="l00172"name="l00172"></a><spanclass="lineno"> 172</span><spanclass="comment">// set. Important: this should only be called with the opposite state of the</span></div>
<divclass="line"><aid="l00173"name="l00173"></a><spanclass="lineno"> 173</span><spanclass="comment">// current one, otherwise size() will not be correct.</span></div>
<divclass="line"><aid="l00174"name="l00174"></a><spanclass="lineno"> 174</span><spanclass="keywordtype">void</span><aclass="code hl_function"href="classoperations__research_1_1bop_1_1_backtrackable_integer_set.html#aba10677c63ecfdabe3116c4e08c31aa1">ChangeState</a>(IntType i, <spanclass="keywordtype">bool</span> should_be_inside);</div>
<divclass="line"><aid="l00176"name="l00176"></a><spanclass="lineno"> 176</span><spanclass="comment">// Returns the current number of elements in the set.</span></div>
<divclass="line"><aid="l00177"name="l00177"></a><spanclass="lineno"> 177</span><spanclass="comment">// Note that this is not its maximum size n.</span></div>
<divclass="line"><aid="l00180"name="l00180"></a><spanclass="lineno"> 180</span><spanclass="comment">// Returns a superset of the current set of integers.</span></div>
<divclass="line"><aid="l00183"name="l00183"></a><spanclass="lineno"> 183</span><spanclass="comment">// BacktrackOneLevel() backtracks to the state the class was in when the</span></div>
<divclass="line"><aid="l00184"name="l00184"></a><spanclass="lineno"> 184</span><spanclass="comment">// last AddBacktrackingLevel() was called. BacktrackAll() just restore the</span></div>
<divclass="line"><aid="l00185"name="l00185"></a><spanclass="lineno"> 185</span><spanclass="comment">// class to its state just after the last ClearAndResize().</span></div>
<divclass="line"><aid="l00193"name="l00193"></a><spanclass="lineno"> 193</span><spanclass="comment">// Contains the elements whose status has been changed at least once.</span></div>
<divclass="line"><aid="l00197"name="l00197"></a><spanclass="lineno"> 197</span><spanclass="comment">// Used for backtracking. Contains the size_ and the stack_.size() at the time</span></div>
<divclass="line"><aid="l00198"name="l00198"></a><spanclass="lineno"> 198</span><spanclass="comment">// of each call to AddBacktrackingLevel() that is not yet backtracked over.</span></div>
<divclass="line"><aid="l00203"name="l00203"></a><spanclass="lineno"> 203</span><spanclass="comment">// A simple and efficient class to hash a given set of integers in [0, n).</span></div>
<divclass="line"><aid="l00204"name="l00204"></a><spanclass="lineno"> 204</span><spanclass="comment">// It uses O(n) memory and produces a good hash (random linear function).</span></div>
<divclass="line"><aid="l00210"name="l00210"></a><spanclass="lineno"> 210</span><spanclass="comment">// Initializes the NonOrderedSetHasher to hash sets of integer in [0, n).</span></div>
<divclass="line"><aid="l00218"name="l00218"></a><spanclass="lineno"> 218</span><spanclass="comment">// Ignores the given set element in all subsequent hash computation. Note that</span></div>
<divclass="line"><aid="l00219"name="l00219"></a><spanclass="lineno"> 219</span><spanclass="comment">// this will be reset by the next call to Initialize().</span></div>
<divclass="line"><aid="l00222"name="l00222"></a><spanclass="lineno"> 222</span><spanclass="comment">// Returns the hash of the given set. The hash is independent of the set</span></div>
<divclass="line"><aid="l00223"name="l00223"></a><spanclass="lineno"> 223</span><spanclass="comment">// order, but there must be no duplicate element in the set. This uses a</span></div>
<divclass="line"><aid="l00224"name="l00224"></a><spanclass="lineno"> 224</span><spanclass="comment">// simple random linear function which has really good hashing properties.</span></div>
<divclass="line"><aid="l00231"name="l00231"></a><spanclass="lineno"> 231</span><spanclass="comment">// The hash of a set is simply the XOR of all its elements. This allows</span></div>
<divclass="line"><aid="l00232"name="l00232"></a><spanclass="lineno"> 232</span><spanclass="comment">// to compute an hash incrementally or without the need of a vector<>.</span></div>
<divclass="line"><aid="l00235"name="l00235"></a><spanclass="lineno"> 235</span><spanclass="comment">// Returns true if Initialize() has been called with a non-zero size.</span></div>
<divclass="line"><aid="l00243"name="l00243"></a><spanclass="lineno"> 243</span><spanclass="comment">// This class is used to incrementally maintain an assignment and the</span></div>
<divclass="line"><aid="l00244"name="l00244"></a><spanclass="lineno"> 244</span><spanclass="comment">// feasibility of the constraints of a given LinearBooleanProblem.</span></div>
<divclass="line"><aid="l00246"name="l00246"></a><spanclass="lineno"> 246</span><spanclass="comment">// The current assignment is initialized using a feasible reference solution,</span></div>
<divclass="line"><aid="l00247"name="l00247"></a><spanclass="lineno"> 247</span><spanclass="comment">// i.e. the reference solution satisfies all the constraints of the problem.</span></div>
<divclass="line"><aid="l00248"name="l00248"></a><spanclass="lineno"> 248</span><spanclass="comment">// The current assignment is updated using the Assign() method.</span></div>
<divclass="line"><aid="l00250"name="l00250"></a><spanclass="lineno"> 250</span><spanclass="comment">// Note that the current assignment is not a solution in the sense that it</span></div>
<divclass="line"><aid="l00251"name="l00251"></a><spanclass="lineno"> 251</span><spanclass="comment">// might not be feasible, ie. violates some constraints.</span></div>
<divclass="line"><aid="l00253"name="l00253"></a><spanclass="lineno"> 253</span><spanclass="comment">// The assignment can be accessed at any time using Assignment().</span></div>
<divclass="line"><aid="l00254"name="l00254"></a><spanclass="lineno"> 254</span><spanclass="comment">// The set of infeasible constraints can be accessed at any time using</span></div>
<divclass="line"><aid="l00257"name="l00257"></a><spanclass="lineno"> 257</span><spanclass="comment">// Note that this class is reversible, i.e. it is possible to backtrack to</span></div>
<divclass="line"><aid="l00259"name="l00259"></a><spanclass="lineno"> 259</span><spanclass="comment">// levels. Consider for instance variable a, b, c, and d.</span></div>
<divclass="line"><aid="l00260"name="l00260"></a><spanclass="lineno"> 260</span><spanclass="comment">// Method called Assigned after method call</span></div>
<divclass="line"><aid="l00261"name="l00261"></a><spanclass="lineno"> 261</span><spanclass="comment">// 1- Assign({a, b}) a b</span></div>
<divclass="line"><aid="l00262"name="l00262"></a><spanclass="lineno"> 262</span><spanclass="comment">// 2- AddBacktrackingLevel() a b |</span></div>
<divclass="line"><aid="l00263"name="l00263"></a><spanclass="lineno"> 263</span><spanclass="comment">// 3- Assign({c}) a b | c</span></div>
<divclass="line"><aid="l00264"name="l00264"></a><spanclass="lineno"> 264</span><spanclass="comment">// 4- Assign({d}) a b | c d</span></div>
<divclass="line"><aid="l00265"name="l00265"></a><spanclass="lineno"> 265</span><spanclass="comment">// 5- BacktrackOneLevel() a b</span></div>
<divclass="line"><aid="l00266"name="l00266"></a><spanclass="lineno"> 266</span><spanclass="comment">// 6- Assign({c}) a b c</span></div>
<divclass="line"><aid="l00270"name="l00270"></a><spanclass="lineno"> 270</span><spanclass="comment">// Note that the constraint indices used in this class are not the same as</span></div>
<divclass="line"><aid="l00271"name="l00271"></a><spanclass="lineno"> 271</span><spanclass="comment">// the one used in the given LinearBooleanProblem here.</span></div>
<divclass="line"><aid="l00275"name="l00275"></a><spanclass="lineno"> 275</span><spanclass="comment">// When we construct the problem, we treat the objective as one constraint.</span></div>
<divclass="line"><aid="l00276"name="l00276"></a><spanclass="lineno"> 276</span><spanclass="comment">// This is the index of this special "objective" constraint.</span></div>
<divclass="line"><aid="l00279"name="l00279"></a><spanclass="lineno"> 279</span><spanclass="comment">// Sets a new reference solution and reverts all internal structures to their</span></div>
<divclass="line"><aid="l00280"name="l00280"></a><spanclass="lineno"> 280</span><spanclass="comment">// initial state. Note that the reference solution has to be feasible.</span></div>
<divclass="line"><aid="l00283"name="l00283"></a><spanclass="lineno"> 283</span><spanclass="comment">// Behaves exactly like SetReferenceSolution() where the passed reference</span></div>
<divclass="line"><aid="l00284"name="l00284"></a><spanclass="lineno"> 284</span><spanclass="comment">// is the current assignment held by this class. Note that the current</span></div>
<divclass="line"><aid="l00285"name="l00285"></a><spanclass="lineno"> 285</span><spanclass="comment">// assignment must be feasible (i.e. IsFeasible() is true).</span></div>
<divclass="line"><aid="l00288"name="l00288"></a><spanclass="lineno"> 288</span><spanclass="comment">// Assigns all literals. That updates the assignment, the constraint values,</span></div>
<divclass="line"><aid="l00289"name="l00289"></a><spanclass="lineno"> 289</span><spanclass="comment">// and the infeasible constraints.</span></div>
<divclass="line"><aid="l00290"name="l00290"></a><spanclass="lineno"> 290</span><spanclass="comment">// Note that the assignment of those literals can be reverted thanks to</span></div>
<divclass="line"><aid="l00291"name="l00291"></a><spanclass="lineno"> 291</span><spanclass="comment">// AddBacktrackingLevel() and BacktrackOneLevel().</span></div>
<divclass="line"><aid="l00292"name="l00292"></a><spanclass="lineno"> 292</span><spanclass="comment">// Note that a variable can't be assigned twice, even for the same literal.</span></div>
<divclass="line"><aid="l00295"name="l00295"></a><spanclass="lineno"> 295</span><spanclass="comment">// Adds a new backtracking level to specify the state that will be restored</span></div>
<divclass="line"><aid="l00296"name="l00296"></a><spanclass="lineno"> 296</span><spanclass="comment">// by BacktrackOneLevel().</span></div>
<divclass="line"><aid="l00297"name="l00297"></a><spanclass="lineno"> 297</span><spanclass="comment">// See the example in the class comment.</span></div>
<divclass="line"><aid="l00300"name="l00300"></a><spanclass="lineno"> 300</span><spanclass="comment">// Backtracks internal structures to the previous level defined by</span></div>
<divclass="line"><aid="l00301"name="l00301"></a><spanclass="lineno"> 301</span><spanclass="comment">// AddBacktrackingLevel(). As a consequence the state will be exactly as</span></div>
<divclass="line"><aid="l00302"name="l00302"></a><spanclass="lineno"> 302</span><spanclass="comment">// before the previous call to AddBacktrackingLevel().</span></div>
<divclass="line"><aid="l00303"name="l00303"></a><spanclass="lineno"> 303</span><spanclass="comment">// Note that backtracking the initial state has no effect.</span></div>
<divclass="line"><aid="l00307"name="l00307"></a><spanclass="lineno"> 307</span><spanclass="comment">// This returns the list of literal that appear in exactly all the current</span></div>
<divclass="line"><aid="l00308"name="l00308"></a><spanclass="lineno"> 308</span><spanclass="comment">// infeasible constraints (ignoring the objective) and correspond to a flip in</span></div>
<divclass="line"><aid="l00309"name="l00309"></a><spanclass="lineno"> 309</span><spanclass="comment">// a good direction for all the infeasible constraint. Performing this flip</span></div>
<divclass="line"><aid="l00310"name="l00310"></a><spanclass="lineno"> 310</span><spanclass="comment">// may repair the problem without any propagations.</span></div>
<divclass="line"><aid="l00312"name="l00312"></a><spanclass="lineno"> 312</span><spanclass="comment">// Important: The returned reference is only valid until the next</span></div>
<divclass="line"><aid="l00316"name="l00316"></a><spanclass="lineno"> 316</span><spanclass="comment">// Returns true if there is no infeasible constraint in the current state.</span></div>
<divclass="line"><aid="l00319"name="l00319"></a><spanclass="lineno"> 319</span><spanclass="comment">// Returns the *exact* number of infeasible constraints.</span></div>
<divclass="line"><aid="l00320"name="l00320"></a><spanclass="lineno"> 320</span><spanclass="comment">// Note that PossiblyInfeasibleConstraints() will potentially return a larger</span></div>
<divclass="line"><aid="l00321"name="l00321"></a><spanclass="lineno"> 321</span><spanclass="comment">// number of constraints.</span></div>
<divclass="line"><aid="l00326"name="l00326"></a><spanclass="lineno"> 326</span><spanclass="comment">// Returns a superset of all the infeasible constraints in the current state.</span></div>
<divclass="line"><aid="l00331"name="l00331"></a><spanclass="lineno"> 331</span><spanclass="comment">// Returns the number of constraints of the problem, objective included,</span></div>
<divclass="line"><aid="l00332"name="l00332"></a><spanclass="lineno"> 332</span><spanclass="comment">// i.e. the number of constraint in the problem + 1.</span></div>
<divclass="line"><aid="l00335"name="l00335"></a><spanclass="lineno"> 335</span><spanclass="comment">// Returns the value of the var in the assignment.</span></div>
<divclass="line"><aid="l00336"name="l00336"></a><spanclass="lineno"> 336</span><spanclass="comment">// As the assignment is initialized with the reference solution, if the</span></div>
<divclass="line"><aid="l00337"name="l00337"></a><spanclass="lineno"> 337</span><spanclass="comment">// variable has not been assigned through Assign(), the returned value is</span></div>
<divclass="line"><aid="l00338"name="l00338"></a><spanclass="lineno"> 338</span><spanclass="comment">// the value of the variable in the reference solution.</span></div>
<divclass="line"><aid="l00344"name="l00344"></a><spanclass="lineno"> 344</span><spanclass="comment">// Returns the lower bound of the constraint.</span></div>
<divclass="line"><aid="l00349"name="l00349"></a><spanclass="lineno"> 349</span><spanclass="comment">// Returns the upper bound of the constraint.</span></div>
<divclass="line"><aid="l00354"name="l00354"></a><spanclass="lineno"> 354</span><spanclass="comment">// Returns the value of the constraint. The value is computed using the</span></div>
<divclass="line"><aid="l00355"name="l00355"></a><spanclass="lineno"> 355</span><spanclass="comment">// variable values in the assignment. Note that a constraint is feasible iff</span></div>
<divclass="line"><aid="l00356"name="l00356"></a><spanclass="lineno"> 356</span><spanclass="comment">// its value is between its two bounds (inclusive).</span></div>
<divclass="line"><aid="l00361"name="l00361"></a><spanclass="lineno"> 361</span><spanclass="comment">// Returns true if the given constraint is currently feasible.</span></div>
<divclass="line"><aid="l00371"name="l00371"></a><spanclass="lineno"> 371</span><spanclass="comment">// This is lazily called by PotentialOneFlipRepairs() once.</span></div>
<divclass="line"><aid="l00374"name="l00374"></a><spanclass="lineno"> 374</span><spanclass="comment">// This is used by PotentialOneFlipRepairs(). It encodes a ConstraintIndex</span></div>
<divclass="line"><aid="l00375"name="l00375"></a><spanclass="lineno"> 375</span><spanclass="comment">// together with a "repair" direction depending on the bound that make a</span></div>
<divclass="line"><aid="l00376"name="l00376"></a><spanclass="lineno"> 376</span><spanclass="comment">// constraint infeasible. An "up" direction means that the constraint activity</span></div>
<divclass="line"><aid="l00377"name="l00377"></a><spanclass="lineno"> 377</span><spanclass="comment">// is lower than the lower bound and we need to make the activity move up to</span></div>
<divclass="line"><aid="l00378"name="l00378"></a><spanclass="lineno"> 378</span><spanclass="comment">// fix the infeasibility.</span></div>
<divclass="line"><aid="l00385"name="l00385"></a><spanclass="lineno"> 385</span><spanclass="comment">// Over constrains the objective cost by the given delta. This should only be</span></div>
<divclass="line"><aid="l00386"name="l00386"></a><spanclass="lineno"> 386</span><spanclass="comment">// called on a feasible reference solution and a fully backtracked state.</span></div>
<divclass="line"><aid="l00389"name="l00389"></a><spanclass="lineno"> 389</span><spanclass="comment">// Local structure to represent the sparse matrix by variable used for fast</span></div>
<divclass="line"><aid="l00390"name="l00390"></a><spanclass="lineno"> 390</span><spanclass="comment">// update of the constraint values.</span></div>
<divclass="line"><aid="l00409"name="l00409"></a><spanclass="lineno"> 409</span><spanclass="comment">// This contains the list of variable flipped in assignment_.</span></div>
<divclass="line"><aid="l00410"name="l00410"></a><spanclass="lineno"> 410</span><spanclass="comment">// flipped_var_trail_backtrack_levels_[i-1] is the index in flipped_var_trail_</span></div>
<divclass="line"><aid="l00411"name="l00411"></a><spanclass="lineno"> 411</span><spanclass="comment">// of the first variable flipped after the i-th AddBacktrackingLevel() call.</span></div>
<divclass="line"><aid="l00415"name="l00415"></a><spanclass="lineno"> 415</span><spanclass="comment">// Members used by PotentialOneFlipRepairs().</span></div>
<divclass="line"><aid="l00424"name="l00424"></a><spanclass="lineno"> 424</span><spanclass="comment">// This class is an utility class used to select which infeasible constraint to</span></div>
<divclass="line"><aid="l00425"name="l00425"></a><spanclass="lineno"> 425</span><spanclass="comment">// repair and identify one variable to flip to actually repair the constraint.</span></div>
<divclass="line"><aid="l00426"name="l00426"></a><spanclass="lineno"> 426</span><spanclass="comment">// A constraint 'lb <= sum_i(w_i * x_i) <= ub', with 'lb' the lower bound,</span></div>
<divclass="line"><aid="l00427"name="l00427"></a><spanclass="lineno"> 427</span><spanclass="comment">// 'ub' the upper bound, 'w_i' the weight of the i-th term and 'x_i' the</span></div>
<divclass="line"><aid="l00428"name="l00428"></a><spanclass="lineno"> 428</span><spanclass="comment">// boolean variable appearing in the i-th term, is infeasible for a given</span></div>
<divclass="line"><aid="l00429"name="l00429"></a><spanclass="lineno"> 429</span><spanclass="comment">// assignment iff its value 'sum_i(w_i * x_i)' is outside of the bounds.</span></div>
<divclass="line"><aid="l00430"name="l00430"></a><spanclass="lineno"> 430</span><spanclass="comment">// Repairing-a-constraint-in-one-flip means making the constraint feasible by</span></div>
<divclass="line"><aid="l00431"name="l00431"></a><spanclass="lineno"> 431</span><spanclass="comment">// just flipping the value of one unassigned variable of the current assignment</span></div>
<divclass="line"><aid="l00432"name="l00432"></a><spanclass="lineno"> 432</span><spanclass="comment">// from the AssignmentAndConstraintFeasibilityMaintainer.</span></div>
<divclass="line"><aid="l00433"name="l00433"></a><spanclass="lineno"> 433</span><spanclass="comment">// For performance reasons, the pairs weight / variable (w_i, x_i) are stored</span></div>
<divclass="line"><aid="l00434"name="l00434"></a><spanclass="lineno"> 434</span><spanclass="comment">// in a sparse manner as a vector of terms (w_i, x_i). In the following the</span></div>
<divclass="line"><aid="l00435"name="l00435"></a><spanclass="lineno"> 435</span><spanclass="comment">// TermIndex term_index refers to the position of the term in the vector.</span></div>
<divclass="line"><aid="l00438"name="l00438"></a><spanclass="lineno"> 438</span><spanclass="comment">// Note that the constraint indices used in this class follow the same</span></div>
<divclass="line"><aid="l00439"name="l00439"></a><spanclass="lineno"> 439</span><spanclass="comment">// convention as the one used in the</span></div>
<divclass="line"><aid="l00442"name="l00442"></a><spanclass="lineno"> 442</span><spanclass="comment">// TODO(user): maybe merge the two classes? maintaining this implicit indices</span></div>
<divclass="line"><aid="l00443"name="l00443"></a><spanclass="lineno"> 443</span><spanclass="comment">// convention between the two classes sounds like a bad idea.</span></div>
<divclass="line"><aid="l00453"name="l00453"></a><spanclass="lineno"> 453</span><spanclass="comment">// Returns the index of a constraint to repair. This will always return the</span></div>
<divclass="line"><aid="l00454"name="l00454"></a><spanclass="lineno"> 454</span><spanclass="comment">// index of a constraint that can be repaired in one flip if there is one.</span></div>
<divclass="line"><aid="l00455"name="l00455"></a><spanclass="lineno"> 455</span><spanclass="comment">// Note however that if there is only one possible candidate, it will be</span></div>
<divclass="line"><aid="l00456"name="l00456"></a><spanclass="lineno"> 456</span><spanclass="comment">// returned without checking that it can indeed be repaired in one flip.</span></div>
<divclass="line"><aid="l00457"name="l00457"></a><spanclass="lineno"> 457</span><spanclass="comment">// This is because the later check can be expensive, and is not needed in our</span></div>
<divclass="line"><aid="l00461"name="l00461"></a><spanclass="lineno"> 461</span><spanclass="comment">// Returns the index of the next term which repairs the constraint when the</span></div>
<divclass="line"><aid="l00462"name="l00462"></a><spanclass="lineno"> 462</span><spanclass="comment">// value of its variable is flipped. This method explores terms with an</span></div>
<divclass="line"><aid="l00463"name="l00463"></a><spanclass="lineno"> 463</span><spanclass="comment">// index strictly greater than start_term_index and then terms with an index</span></div>
<divclass="line"><aid="l00464"name="l00464"></a><spanclass="lineno"> 464</span><spanclass="comment">// smaller than or equal to init_term_index if any.</span></div>
<divclass="line"><aid="l00465"name="l00465"></a><spanclass="lineno"> 465</span><spanclass="comment">// Returns kInvalidTerm when no reparing terms are found.</span></div>
<divclass="line"><aid="l00467"name="l00467"></a><spanclass="lineno"> 467</span><spanclass="comment">// Note that if init_term_index == start_term_index, then all the terms will</span></div>
<divclass="line"><aid="l00468"name="l00468"></a><spanclass="lineno"> 468</span><spanclass="comment">// be explored. Both TermIndex arguments can take values in [-1, constraint</span></div>
<divclass="line"><aid="l00474"name="l00474"></a><spanclass="lineno"> 474</span><spanclass="comment">// Returns true if the constraint is infeasible and if flipping the variable</span></div>
<divclass="line"><aid="l00475"name="l00475"></a><spanclass="lineno"> 475</span><spanclass="comment">// at the given index will repair it.</span></div>
<divclass="line"><aid="l00478"name="l00478"></a><spanclass="lineno"> 478</span><spanclass="comment">// Returns the literal formed by the variable at the given constraint term and</span></div>
<divclass="line"><aid="l00479"name="l00479"></a><spanclass="lineno"> 479</span><spanclass="comment">// assigned to the opposite value of this variable in the current assignment.</span></div>
<divclass="line"><aid="l00482"name="l00482"></a><spanclass="lineno"> 482</span><spanclass="comment">// Local structure to represent the sparse matrix by constraint used for fast</span></div>
<divclass="line"><aid="l00491"name="l00491"></a><spanclass="lineno"> 491</span><spanclass="comment">// Sorts the terms of each constraints in the by_constraint_matrix_ to iterate</span></div>
<divclass="line"><aid="l00492"name="l00492"></a><spanclass="lineno"> 492</span><spanclass="comment">// on most promising variables first.</span></div>
<divclass="line"><aid="l00504"name="l00504"></a><spanclass="lineno"> 504</span><spanclass="comment">// This class is used to iterate on all assignments that can be obtained by</span></div>
<divclass="line"><aid="l00505"name="l00505"></a><spanclass="lineno"> 505</span><spanclass="comment">// deliberately flipping 'n' variables from the reference solution, 'n' being</span></div>
<divclass="line"><aid="l00506"name="l00506"></a><spanclass="lineno"> 506</span><spanclass="comment">// smaller than or equal to max_num_decisions.</span></div>
<divclass="line"><aid="l00507"name="l00507"></a><spanclass="lineno"> 507</span><spanclass="comment">// Note that one deliberate variable flip may lead to many other flips due to</span></div>
<divclass="line"><aid="l00508"name="l00508"></a><spanclass="lineno"> 508</span><spanclass="comment">// constraint propagation, those additional flips are not counted in 'n'.</span></div>
<divclass="line"><aid="l00524"name="l00524"></a><spanclass="lineno"> 524</span><spanclass="comment">// Synchronizes the iterator with the problem state, e.g. set fixed variables,</span></div>
<divclass="line"><aid="l00525"name="l00525"></a><spanclass="lineno"> 525</span><spanclass="comment">// set the reference solution. Call this only when a new solution has been</span></div>
<divclass="line"><aid="l00526"name="l00526"></a><spanclass="lineno"> 526</span><spanclass="comment">// found. This will restart the LS.</span></div>
<divclass="line"><aid="l00529"name="l00529"></a><spanclass="lineno"> 529</span><spanclass="comment">// Synchronize the SatWrapper with our current search state. This needs to be</span></div>
<divclass="line"><aid="l00530"name="l00530"></a><spanclass="lineno"> 530</span><spanclass="comment">// called before calls to NextAssignment() if the underlying SatWrapper was</span></div>
<divclass="line"><aid="l00531"name="l00531"></a><spanclass="lineno"> 531</span><spanclass="comment">// used by someone else than this class.</span></div>
<divclass="line"><aid="l00534"name="l00534"></a><spanclass="lineno"> 534</span><spanclass="comment">// Move to the next assignment. Returns false when the search is finished.</span></div>
<divclass="line"><aid="l00537"name="l00537"></a><spanclass="lineno"> 537</span><spanclass="comment">// Returns the last feasible assignment.</span></div>
<divclass="line"><aid="l00542"name="l00542"></a><spanclass="lineno"> 542</span><spanclass="comment">// Returns true if the current assignment has a better solution than the one</span></div>
<divclass="line"><aid="l00543"name="l00543"></a><spanclass="lineno"> 543</span><spanclass="comment">// passed to the last Synchronize() call.</span></div>
<divclass="line"><aid="l00548"name="l00548"></a><spanclass="lineno"> 548</span><spanclass="comment">// Returns a deterministic number that should be correlated with the time</span></div>
<divclass="line"><aid="l00549"name="l00549"></a><spanclass="lineno"> 549</span><spanclass="comment">// spent in the iterator. The order of magnitude should be close to the time</span></div>
<divclass="line"><aid="l00550"name="l00550"></a><spanclass="lineno"> 550</span><spanclass="comment">// in seconds.</span></div>
<divclass="line"><aid="l00556"name="l00556"></a><spanclass="lineno"> 556</span><spanclass="comment">// This is called when a better solution has been found to restore the search</span></div>
<divclass="line"><aid="l00557"name="l00557"></a><spanclass="lineno"> 557</span><spanclass="comment">// to the new "root" node.</span></div>
<divclass="line"><aid="l00563"name="l00563"></a><spanclass="lineno"> 563</span><spanclass="comment">// Internal structure used to represent a node of the search tree during local</span></div>
<divclass="line"><aid="l00574"name="l00574"></a><spanclass="lineno"> 574</span><spanclass="comment">// Applies the decision. Automatically backtracks when SAT detects conflicts.</span></div>
<divclass="line"><aid="l00577"name="l00577"></a><spanclass="lineno"> 577</span><spanclass="comment">// Adds one more decision to repair infeasible constraints.</span></div>
<divclass="line"><aid="l00578"name="l00578"></a><spanclass="lineno"> 578</span><spanclass="comment">// Returns true in case of success.</span></div>
<divclass="line"><aid="l00581"name="l00581"></a><spanclass="lineno"> 581</span><spanclass="comment">// Backtracks and moves to the next decision in the search tree.</span></div>
<divclass="line"><aid="l00584"name="l00584"></a><spanclass="lineno"> 584</span><spanclass="comment">// Looks if the current decisions (in search_nodes_) plus the new one (given</span></div>
<divclass="line"><aid="l00585"name="l00585"></a><spanclass="lineno"> 585</span><spanclass="comment">// by l) lead to a position already present in transposition_table_.</span></div>
<divclass="line"><aid="l00588"name="l00588"></a><spanclass="lineno"> 588</span><spanclass="comment">// Inserts the current set of decisions in transposition_table_.</span></div>
<divclass="line"><aid="l00591"name="l00591"></a><spanclass="lineno"> 591</span><spanclass="comment">// Initializes the given array with the current decisions in search_nodes_ and</span></div>
<divclass="line"><aid="l00592"name="l00592"></a><spanclass="lineno"> 592</span><spanclass="comment">// by filling the other positions with 0.</span></div>
<divclass="line"><aid="l00596"name="l00596"></a><spanclass="lineno"> 596</span><spanclass="comment">// Looks for the next repairing term in the given constraints while skipping</span></div>
<divclass="line"><aid="l00597"name="l00597"></a><spanclass="lineno"> 597</span><spanclass="comment">// the position already present in transposition_table_. A given TermIndex of</span></div>
<divclass="line"><aid="l00598"name="l00598"></a><spanclass="lineno"> 598</span><spanclass="comment">// -1 means that this is the first time we explore this constraint.</span></div>
<divclass="line"><aid="l00611"name="l00611"></a><spanclass="lineno"> 611</span><spanclass="comment">// Temporary vector used by ApplyDecision().</span></div>
<divclass="line"><aid="l00614"name="l00614"></a><spanclass="lineno"> 614</span><spanclass="comment">// For each set of explored decisions, we store it in this table so that we</span></div>
<divclass="line"><aid="l00615"name="l00615"></a><spanclass="lineno"> 615</span><spanclass="comment">// don't explore decisions (a, b) and later (b, a) for instance. The decisions</span></div>
<divclass="line"><aid="l00616"name="l00616"></a><spanclass="lineno"> 616</span><spanclass="comment">// are converted to int32_t, sorted and padded with 0 before beeing inserted</span></div>
<divclass="line"><aid="l00619"name="l00619"></a><spanclass="lineno"> 619</span><spanclass="comment">// TODO(user): We may still miss some equivalent states because it is possible</span></div>
<divclass="line"><aid="l00620"name="l00620"></a><spanclass="lineno"> 620</span><spanclass="comment">// that completely differents decisions lead to exactly the same state.</span></div>
<divclass="line"><aid="l00621"name="l00621"></a><spanclass="lineno"> 621</span><spanclass="comment">// However this is more time consuming to detect because we must apply the</span></div>
<divclass="line"><aid="l00622"name="l00622"></a><spanclass="lineno"> 622</span><spanclass="comment">// last decision first before trying to compare the states.</span></div>
<divclass="line"><aid="l00624"name="l00624"></a><spanclass="lineno"> 624</span><spanclass="comment">// TODO(user): Currently, we only store kStoredMaxDecisions or less decisions.</span></div>
<divclass="line"><aid="l00625"name="l00625"></a><spanclass="lineno"> 625</span><spanclass="comment">// Ideally, this should be related to the maximum number of decision in the</span></div>
<divclass="line"><aid="l00626"name="l00626"></a><spanclass="lineno"> 626</span><spanclass="comment">// LS, but that requires templating the whole LS optimizer.</span></div>
<divclass="line"><aid="l00636"name="l00636"></a><spanclass="lineno"> 636</span><spanclass="comment">// The number of skipped nodes thanks to the transposition table.</span></div>
<divclass="line"><aid="l00639"name="l00639"></a><spanclass="lineno"> 639</span><spanclass="comment">// The overall number of better solution found. And the ones found by the</span></div>
<divclass="ttc"id="aclassoperations__research_1_1_time_limit_html"><divclass="ttname"><ahref="classoperations__research_1_1_time_limit.html">operations_research::TimeLimit</a></div><divclass="ttdoc">A simple class to enforce both an elapsed time limit and a deterministic time limit in the same threa...</div><divclass="ttdef"><b>Definition:</b><ahref="time__limit_8h_source.html#l00106">time_limit.h:106</a></div></div>
<divclass="ttc"id="aclassoperations__research_1_1bop_1_1_assignment_and_constraint_feasibility_maintainer_html_ab75ee4d5d3256f8f0997e7620a0881b7"><divclass="ttname"><ahref="classoperations__research_1_1bop_1_1_assignment_and_constraint_feasibility_maintainer.html#ab75ee4d5d3256f8f0997e7620a0881b7">operations_research::bop::AssignmentAndConstraintFeasibilityMaintainer::reference</a></div><divclass="ttdeci">const BopSolution & reference() const</div><divclass="ttdef"><b>Definition:</b><ahref="bop__ls_8h_source.html#l00342">bop_ls.h:342</a></div></div>
<divclass="ttc"id="aclassoperations__research_1_1bop_1_1_backtrackable_integer_set_html_aba10677c63ecfdabe3116c4e08c31aa1"><divclass="ttname"><ahref="classoperations__research_1_1bop_1_1_backtrackable_integer_set.html#aba10677c63ecfdabe3116c4e08c31aa1">operations_research::bop::BacktrackableIntegerSet::ChangeState</a></div><divclass="ttdeci">void ChangeState(IntType i, bool should_be_inside)</div><divclass="ttdef"><b>Definition:</b><ahref="bop__ls_8cc_source.html#l00135">bop_ls.cc:135</a></div></div>
<divclass="ttc"id="aclassoperations__research_1_1bop_1_1_bop_optimizer_base_html_a24dcbf29c0d6cd766009a182a6484e3b"><divclass="ttname"><ahref="classoperations__research_1_1bop_1_1_bop_optimizer_base.html#a24dcbf29c0d6cd766009a182a6484e3b">operations_research::bop::BopOptimizerBase::name</a></div><divclass="ttdeci">const std::string & name() const</div><divclass="ttdef"><b>Definition:</b><ahref="bop__base_8h_source.html#l00049">bop_base.h:49</a></div></div>
<divclass="ttc"id="aclassoperations__research_1_1bop_1_1_local_search_assignment_iterator_html_a1aecf270882e0c103c53773297b1d96a"><divclass="ttname"><ahref="classoperations__research_1_1bop_1_1_local_search_assignment_iterator.html#a1aecf270882e0c103c53773297b1d96a">operations_research::bop::LocalSearchAssignmentIterator::LastReferenceAssignment</a></div><divclass="ttdeci">const BopSolution & LastReferenceAssignment() const</div><divclass="ttdef"><b>Definition:</b><ahref="bop__ls_8h_source.html#l00538">bop_ls.h:538</a></div></div>
<divclass="ttc"id="aclassoperations__research_1_1bop_1_1_sat_wrapper_html_ad5d7d187cc8a2b70a360d4cede96cfe9"><divclass="ttname"><ahref="classoperations__research_1_1bop_1_1_sat_wrapper.html#ad5d7d187cc8a2b70a360d4cede96cfe9">operations_research::bop::SatWrapper::SatAssignment</a></div><divclass="ttdeci">const sat::VariablesAssignment & SatAssignment() const</div><divclass="ttdef"><b>Definition:</b><ahref="bop__ls_8h_source.html#l00068">bop_ls.h:68</a></div></div>
<divclass="ttc"id="aclassoperations__research_1_1sat_1_1_sat_solver_html_a4793952607a98dfddfbcc17abfabbb4b"><divclass="ttname"><ahref="classoperations__research_1_1sat_1_1_sat_solver.html#a4793952607a98dfddfbcc17abfabbb4b">operations_research::sat::SatSolver::Assignment</a></div><divclass="ttdeci">const VariablesAssignment & Assignment() const</div><divclass="ttdef"><b>Definition:</b><ahref="sat__solver_8h_source.html#l00378">sat_solver.h:378</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>