<ahref="min__cost__flow_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-2018 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">// An implementation of a cost-scaling push-relabel algorithm for</span></div>
<divclass="line"><aname="l00015"></a><spanclass="lineno"> 15</span> <spanclass="comment">// the min-cost flow problem.</span></div>
<divclass="line"><aname="l00017"></a><spanclass="lineno"> 17</span> <spanclass="comment">// In the following, we consider a graph G = (V,E) where V denotes the set</span></div>
<divclass="line"><aname="l00018"></a><spanclass="lineno"> 18</span> <spanclass="comment">// of nodes (vertices) in the graph, E denotes the set of arcs (edges).</span></div>
<divclass="line"><aname="l00019"></a><spanclass="lineno"> 19</span> <spanclass="comment">// n = |V| denotes the number of nodes in the graph, and m = |E| denotes the</span></div>
<divclass="line"><aname="l00020"></a><spanclass="lineno"> 20</span> <spanclass="comment">// number of arcs in the graph.</span></div>
<divclass="line"><aname="l00022"></a><spanclass="lineno"> 22</span> <spanclass="comment">// With each arc (v,w) is associated a nonnegative capacity u(v,w)</span></div>
<divclass="line"><aname="l00023"></a><spanclass="lineno"> 23</span> <spanclass="comment">// (where 'u' stands for "upper bound") and a unit cost c(v,w). With</span></div>
<divclass="line"><aname="l00024"></a><spanclass="lineno"> 24</span> <spanclass="comment">// each node v is associated a quantity named supply(v), which</span></div>
<divclass="line"><aname="l00025"></a><spanclass="lineno"> 25</span> <spanclass="comment">// represents a supply of fluid (if >0) or a demand (if <0).</span></div>
<divclass="line"><aname="l00026"></a><spanclass="lineno"> 26</span> <spanclass="comment">// Furthermore, no fluid is created in the graph so</span></div>
<divclass="line"><aname="l00027"></a><spanclass="lineno"> 27</span> <spanclass="comment">// sum_{v in V} supply(v) = 0.</span></div>
<divclass="line"><aname="l00029"></a><spanclass="lineno"> 29</span> <spanclass="comment">// A flow is a function from E to R such that:</span></div>
<divclass="line"><aname="l00030"></a><spanclass="lineno"> 30</span> <spanclass="comment">// a) f(v,w) <= u(v,w) for all (v,w) in E (capacity constraint).</span></div>
<divclass="line"><aname="l00031"></a><spanclass="lineno"> 31</span> <spanclass="comment">// b) f(v,w) = -f(w,v) for all (v,w) in E (flow antisymmetry constraint).</span></div>
<divclass="line"><aname="l00032"></a><spanclass="lineno"> 32</span> <spanclass="comment">// c) sum on v f(v,w) + supply(w) = 0 (flow conservation).</span></div>
<divclass="line"><aname="l00034"></a><spanclass="lineno"> 34</span> <spanclass="comment">// The cost of a flow is sum on (v,w) in E ( f(v,w) * c(v,w) ) [Note:</span></div>
<divclass="line"><aname="l00035"></a><spanclass="lineno"> 35</span> <spanclass="comment">// It can be confusing to beginners that the cost is actually double</span></div>
<divclass="line"><aname="l00036"></a><spanclass="lineno"> 36</span> <spanclass="comment">// the amount that it might seem at first because of flow</span></div>
<divclass="line"><aname="l00039"></a><spanclass="lineno"> 39</span> <spanclass="comment">// The problem to solve: find a flow of minimum cost such that all the</span></div>
<divclass="line"><aname="l00040"></a><spanclass="lineno"> 40</span> <spanclass="comment">// fluid flows from the supply nodes to the demand nodes.</span></div>
<divclass="line"><aname="l00042"></a><spanclass="lineno"> 42</span> <spanclass="comment">// The principles behind this algorithm are the following:</span></div>
<divclass="line"><aname="l00043"></a><spanclass="lineno"> 43</span> <spanclass="comment">// 1/ handle pseudo-flows instead of flows and refine pseudo-flows until an</span></div>
<divclass="line"><aname="l00044"></a><spanclass="lineno"> 44</span> <spanclass="comment">// epsilon-optimal minimum-cost flow is obtained,</span></div>
<divclass="line"><aname="l00045"></a><spanclass="lineno"> 45</span> <spanclass="comment">// 2/ deal with epsilon-optimal pseudo-flows.</span></div>
<divclass="line"><aname="l00047"></a><spanclass="lineno"> 47</span> <spanclass="comment">// 1/ A pseudo-flow is like a flow, except that a node's outflow minus</span></div>
<divclass="line"><aname="l00048"></a><spanclass="lineno"> 48</span> <spanclass="comment">// its inflow can be different from its supply. If it is the case at a</span></div>
<divclass="line"><aname="l00049"></a><spanclass="lineno"> 49</span> <spanclass="comment">// given node v, it is said that there is an excess (or deficit) at</span></div>
<divclass="line"><aname="l00050"></a><spanclass="lineno"> 50</span> <spanclass="comment">// node v. A deficit is denoted by a negative excess and inflow =</span></div>
<divclass="line"><aname="l00052"></a><spanclass="lineno"> 52</span> <spanclass="comment">// (Look at ortools/graph/max_flow.h to see that the definition</span></div>
<divclass="line"><aname="l00053"></a><spanclass="lineno"> 53</span> <spanclass="comment">// of preflow is more restrictive than the one for pseudo-flow in that a preflow</span></div>
<divclass="line"><aname="l00054"></a><spanclass="lineno"> 54</span> <spanclass="comment">// only allows non-negative excesses, i.e., no deficit.)</span></div>
<divclass="line"><aname="l00055"></a><spanclass="lineno"> 55</span> <spanclass="comment">// More formally, a pseudo-flow is a function f such that:</span></div>
<divclass="line"><aname="l00056"></a><spanclass="lineno"> 56</span> <spanclass="comment">// a) f(v,w) <= u(v,w) for all (v,w) in E (capacity constraint).</span></div>
<divclass="line"><aname="l00057"></a><spanclass="lineno"> 57</span> <spanclass="comment">// b) f(v,w) = -f(w,v) for all (v,w) in E (flow antisymmetry constraint).</span></div>
<divclass="line"><aname="l00059"></a><spanclass="lineno"> 59</span> <spanclass="comment">// For each v in E, we also define the excess at node v, the algebraic sum of</span></div>
<divclass="line"><aname="l00060"></a><spanclass="lineno"> 60</span> <spanclass="comment">// all the incoming preflows at this node, added together with the supply at v.</span></div>
<divclass="line"><aname="l00061"></a><spanclass="lineno"> 61</span> <spanclass="comment">// excess(v) = sum on u f(u,v) + supply(v)</span></div>
<divclass="line"><aname="l00063"></a><spanclass="lineno"> 63</span> <spanclass="comment">// The goal of the algorithm is to obtain excess(v) = 0 for all v in V, while</span></div>
<divclass="line"><aname="l00064"></a><spanclass="lineno"> 64</span> <spanclass="comment">// consuming capacity on some arcs, at the lowest possible cost.</span></div>
<divclass="line"><aname="l00066"></a><spanclass="lineno"> 66</span> <spanclass="comment">// 2/ Internally to the algorithm and its analysis (but invisibly to</span></div>
<divclass="line"><aname="l00067"></a><spanclass="lineno"> 67</span> <spanclass="comment">// the client), each node has an associated "price" (or potential), in</span></div>
<divclass="line"><aname="l00068"></a><spanclass="lineno"> 68</span> <spanclass="comment">// addition to its excess. It is formally a function from E to R (the</span></div>
<divclass="line"><aname="l00069"></a><spanclass="lineno"> 69</span> <spanclass="comment">// set of real numbers.). For a given price function p, the reduced</span></div>
<divclass="line"><aname="l00070"></a><spanclass="lineno"> 70</span> <spanclass="comment">// cost of an arc (v,w) is:</span></div>
<divclass="line"><aname="l00072"></a><spanclass="lineno"> 72</span> <spanclass="comment">// (c(v,w) is the cost of arc (v,w).) For those familiar with linear</span></div>
<divclass="line"><aname="l00073"></a><spanclass="lineno"> 73</span> <spanclass="comment">// programming, the price function can be viewed as a set of dual</span></div>
<divclass="line"><aname="l00076"></a><spanclass="lineno"> 76</span> <spanclass="comment">// For a constant epsilon >= 0, a pseudo-flow f is said to be epsilon-optimal</span></div>
<divclass="line"><aname="l00077"></a><spanclass="lineno"> 77</span> <spanclass="comment">// with respect to a price function p if for every residual arc (v,w) in E,</span></div>
<divclass="line"><aname="l00080"></a><spanclass="lineno"> 80</span> <spanclass="comment">// A flow f is optimal if and only if there exists a price function p such that</span></div>
<divclass="line"><aname="l00081"></a><spanclass="lineno"> 81</span> <spanclass="comment">// no arc is admissible with respect to f and p.</span></div>
<divclass="line"><aname="l00083"></a><spanclass="lineno"> 83</span> <spanclass="comment">// If the arc costs are integers, and epsilon < 1/n, any epsilon-optimal flow</span></div>
<divclass="line"><aname="l00084"></a><spanclass="lineno"> 84</span> <spanclass="comment">// is optimal. The integer cost case is handled by multiplying all the arc costs</span></div>
<divclass="line"><aname="l00085"></a><spanclass="lineno"> 85</span> <spanclass="comment">// and the initial value of epsilon by (n+1). When epsilon reaches 1, and</span></div>
<divclass="line"><aname="l00086"></a><spanclass="lineno"> 86</span> <spanclass="comment">// the solution is epsilon-optimal, it means: for all residual arc (v,w) in E,</span></div>
<divclass="line"><aname="l00090"></a><spanclass="lineno"> 90</span> <spanclass="comment">// A node v is said to be *active* if excess(v) > 0.</span></div>
<divclass="line"><aname="l00091"></a><spanclass="lineno"> 91</span> <spanclass="comment">// In this case the following operations can be applied to it:</span></div>
<divclass="line"><aname="l00092"></a><spanclass="lineno"> 92</span> <spanclass="comment">// - if there are *admissible* incident arcs, i.e. arcs which are not saturated,</span></div>
<divclass="line"><aname="l00093"></a><spanclass="lineno"> 93</span> <spanclass="comment">// and whose reduced costs are negative, a PushFlow operation can</span></div>
<divclass="line"><aname="l00094"></a><spanclass="lineno"> 94</span> <spanclass="comment">// be applied. It consists in sending as much flow as both the excess at the</span></div>
<divclass="line"><aname="l00095"></a><spanclass="lineno"> 95</span> <spanclass="comment">// node and the capacity of the arc permit.</span></div>
<divclass="line"><aname="l00096"></a><spanclass="lineno"> 96</span> <spanclass="comment">// - if there are no admissible arcs, the active node considered is relabeled,</span></div>
<divclass="line"><aname="l00097"></a><spanclass="lineno"> 97</span> <spanclass="comment">// This is implemented in Discharge, which itself calls PushFlow and Relabel.</span></div>
<divclass="line"><aname="l00099"></a><spanclass="lineno"> 99</span> <spanclass="comment">// Discharge itself is called by Refine. Refine first saturates all the</span></div>
<divclass="line"><aname="l00100"></a><spanclass="lineno"> 100</span> <spanclass="comment">// admissible arcs, then builds a stack of active nodes. It then applies</span></div>
<divclass="line"><aname="l00101"></a><spanclass="lineno"> 101</span> <spanclass="comment">// Discharge for each active node, possibly adding new ones in the process,</span></div>
<divclass="line"><aname="l00102"></a><spanclass="lineno"> 102</span> <spanclass="comment">// until no nodes are active. In that case an epsilon-optimal flow is obtained.</span></div>
<divclass="line"><aname="l00104"></a><spanclass="lineno"> 104</span> <spanclass="comment">// Optimize iteratively calls Refine, while epsilon > 1, and divides epsilon by</span></div>
<divclass="line"><aname="l00105"></a><spanclass="lineno"> 105</span> <spanclass="comment">// alpha (set by default to 5) before each iteration.</span></div>
<divclass="line"><aname="l00107"></a><spanclass="lineno"> 107</span> <spanclass="comment">// The algorithm starts with epsilon = C, where C is the maximum absolute value</span></div>
<divclass="line"><aname="l00108"></a><spanclass="lineno"> 108</span> <spanclass="comment">// of the arc costs. In the integer case which we are dealing with, since all</span></div>
<divclass="line"><aname="l00109"></a><spanclass="lineno"> 109</span> <spanclass="comment">// costs are multiplied by (n+1), the initial value of epsilon is (n+1)*C.</span></div>
<divclass="line"><aname="l00110"></a><spanclass="lineno"> 110</span> <spanclass="comment">// The algorithm terminates when epsilon = 1, and Refine() has been called.</span></div>
<divclass="line"><aname="l00111"></a><spanclass="lineno"> 111</span> <spanclass="comment">// In this case, a minimum-cost flow is obtained.</span></div>
<divclass="line"><aname="l00113"></a><spanclass="lineno"> 113</span> <spanclass="comment">// The complexity of the algorithm is O(n^2*m*log(n*C)) where C is the value of</span></div>
<divclass="line"><aname="l00114"></a><spanclass="lineno"> 114</span> <spanclass="comment">// the largest arc cost in the graph.</span></div>
<divclass="line"><aname="l00117"></a><spanclass="lineno"> 117</span> <spanclass="comment">// The algorithm is not able to detect the infeasibility of a problem (i.e.,</span></div>
<divclass="line"><aname="l00118"></a><spanclass="lineno"> 118</span> <spanclass="comment">// when a bottleneck in the network prohibits sending all the supplies.)</span></div>
<divclass="line"><aname="l00119"></a><spanclass="lineno"> 119</span> <spanclass="comment">// Worse, it could in some cases loop forever. This is why feasibility checking</span></div>
<divclass="line"><aname="l00120"></a><spanclass="lineno"> 120</span> <spanclass="comment">// is enabled by default (FLAGS_min_cost_flow_check_feasibility=true.)</span></div>
<divclass="line"><aname="l00121"></a><spanclass="lineno"> 121</span> <spanclass="comment">// Feasibility checking is implemented using a max-flow, which has a much lower</span></div>
<divclass="line"><aname="l00122"></a><spanclass="lineno"> 122</span> <spanclass="comment">// complexity. The impact on performance is negligible, while the risk of being</span></div>
<divclass="line"><aname="l00123"></a><spanclass="lineno"> 123</span> <spanclass="comment">// caught in an endless loop is removed. Note that using the feasibility checker</span></div>
<divclass="line"><aname="l00124"></a><spanclass="lineno"> 124</span> <spanclass="comment">// roughly doubles the memory consumption.</span></div>
<divclass="line"><aname="l00126"></a><spanclass="lineno"> 126</span> <spanclass="comment">// The starting reference for this class of algorithms is:</span></div>
<divclass="line"><aname="l00137"></a><spanclass="lineno"> 137</span> <spanclass="comment">// A.V. Goldberg and M. Kharitonov, "On Implementing Scaling Push-Relabel</span></div>
<divclass="line"><aname="l00138"></a><spanclass="lineno"> 138</span> <spanclass="comment">// Algorithms for the Minimum-Cost Flow Problem", Network flows and matching:</span></div>
<divclass="line"><aname="l00139"></a><spanclass="lineno"> 139</span> <spanclass="comment">// First DIMACS implementation challenge, DIMACS Series in Discrete Mathematics</span></div>
<divclass="line"><aname="l00140"></a><spanclass="lineno"> 140</span> <spanclass="comment">// and Theoretical Computer Science, (1993) 12:157-198.</span></div>
<divclass="line"><aname="l00142"></a><spanclass="lineno"> 142</span> <spanclass="comment">// and in:</span></div>
<divclass="line"><aname="l00143"></a><spanclass="lineno"> 143</span> <spanclass="comment">// U. Bunnagel, B. Korte, and J. Vygen. “Efficient implementation of the</span></div>
<divclass="line"><aname="l00148"></a><spanclass="lineno"> 148</span> <spanclass="comment">// We have tried as much as possible in this implementation to keep the</span></div>
<divclass="line"><aname="l00149"></a><spanclass="lineno"> 149</span> <spanclass="comment">// notations and namings of the papers cited above, except for 'demand' or</span></div>
<divclass="line"><aname="l00150"></a><spanclass="lineno"> 150</span> <spanclass="comment">// 'balance' which have been replaced by 'supply', with the according sign</span></div>
<divclass="line"><aname="l00151"></a><spanclass="lineno"> 151</span> <spanclass="comment">// changes to better accommodate with the API of the rest of our tools. A demand</span></div>
<divclass="line"><aname="l00152"></a><spanclass="lineno"> 152</span> <spanclass="comment">// is denoted by a negative supply.</span></div>
<divclass="line"><aname="l00154"></a><spanclass="lineno"> 154</span> <spanclass="comment">// TODO(user): See whether the following can bring any improvements on real-life</span></div>
<divclass="line"><aname="l00160"></a><spanclass="lineno"> 160</span> <spanclass="comment">// An interesting general reference on network flows is:</span></div>
<divclass="line"><aname="l00161"></a><spanclass="lineno"> 161</span> <spanclass="comment">// R. K. Ahuja, T. L. Magnanti, J. B. Orlin, "Network Flows: Theory, Algorithms,</span></div>
<divclass="line"><aname="l00162"></a><spanclass="lineno"> 162</span> <spanclass="comment">// and Applications," Prentice Hall, 1993, ISBN: 978-0136175490,</span></div>
<divclass="line"><aname="l00190"></a><spanclass="lineno"> 190</span> <spanclass="comment">// Different statuses for a solved problem.</span></div>
<divclass="line"><aname="l00191"></a><spanclass="lineno"> 191</span> <spanclass="comment">// We use a base class to share it between our different interfaces.</span></div>
<divclass="line"><aname="l00205"></a><spanclass="lineno"> 205</span> <spanclass="comment">// A simple and efficient min-cost flow interface. This is as fast as</span></div>
<divclass="line"><aname="l00206"></a><spanclass="lineno"> 206</span> <spanclass="comment">// GenericMinCostFlow<ReverseArcStaticGraph>, which is the fastest, but is uses</span></div>
<divclass="line"><aname="l00207"></a><spanclass="lineno"> 207</span> <spanclass="comment">// more memory in order to hide the somewhat involved construction of the</span></div>
<divclass="line"><aname="l00210"></a><spanclass="lineno"> 210</span> <spanclass="comment">// TODO(user): If the need arises, extend this interface to support warm start</span></div>
<divclass="line"><aname="l00211"></a><spanclass="lineno"> 211</span> <spanclass="comment">// and incrementality between solves. Note that this is already supported by the</span></div>
<divclass="line"><aname="l00215"></a><spanclass="lineno"> 215</span> <spanclass="comment">// The constructor takes no size. New node indices will be created lazily by</span></div>
<divclass="line"><aname="l00216"></a><spanclass="lineno"> 216</span> <spanclass="comment">// AddArcWithCapacityAndUnitCost() or SetNodeSupply() such that the set of</span></div>
<divclass="line"><aname="l00217"></a><spanclass="lineno"> 217</span> <spanclass="comment">// valid nodes will always be [0, NumNodes()).</span></div>
<divclass="line"><aname="l00220"></a><spanclass="lineno"> 220</span> <spanclass="comment">// Adds a directed arc from tail to head to the underlying graph with</span></div>
<divclass="line"><aname="l00221"></a><spanclass="lineno"> 221</span> <spanclass="comment">// a given capacity and cost per unit of flow.</span></div>
<divclass="line"><aname="l00222"></a><spanclass="lineno"> 222</span> <spanclass="comment">// * Node indices and the capacity must be non-negative (>= 0).</span></div>
<divclass="line"><aname="l00223"></a><spanclass="lineno"> 223</span> <spanclass="comment">// * The unit cost can take any integer value (even negative).</span></div>
<divclass="line"><aname="l00224"></a><spanclass="lineno"> 224</span> <spanclass="comment">// * Self-looping and duplicate arcs are supported.</span></div>
<divclass="line"><aname="l00225"></a><spanclass="lineno"> 225</span> <spanclass="comment">// * After the method finishes, NumArcs() == the returned ArcIndex + 1.</span></div>
<divclass="line"><aname="l00230"></a><spanclass="lineno"> 230</span> <spanclass="comment">// Sets the supply of the given node. The node index must be non-negative (>=</span></div>
<divclass="line"><aname="l00231"></a><spanclass="lineno"> 231</span> <spanclass="comment">// 0). Nodes implicitly created will have a default supply set to 0. A demand</span></div>
<divclass="line"><aname="l00232"></a><spanclass="lineno"> 232</span> <spanclass="comment">// is modeled as a negative supply.</span></div>
<divclass="line"><aname="l00235"></a><spanclass="lineno"> 235</span> <spanclass="comment">// Solves the problem, and returns the problem status. This function</span></div>
<divclass="line"><aname="l00236"></a><spanclass="lineno"> 236</span> <spanclass="comment">// requires that the sum of all node supply minus node demand is zero and</span></div>
<divclass="line"><aname="l00237"></a><spanclass="lineno"> 237</span> <spanclass="comment">// that the graph has enough capacity to send all supplies and serve all</span></div>
<divclass="line"><aname="l00238"></a><spanclass="lineno"> 238</span> <spanclass="comment">// demands. Otherwise, it will return INFEASIBLE.</span></div>
<divclass="line"><aname="l00243"></a><spanclass="lineno"> 243</span> <spanclass="comment">// Same as Solve(), but does not have the restriction that the supply</span></div>
<divclass="line"><aname="l00244"></a><spanclass="lineno"> 244</span> <spanclass="comment">// must match the demand or that the graph has enough capacity to serve</span></div>
<divclass="line"><aname="l00245"></a><spanclass="lineno"> 245</span> <spanclass="comment">// all the demand or use all the supply. This will compute a maximum-flow</span></div>
<divclass="line"><aname="l00246"></a><spanclass="lineno"> 246</span> <spanclass="comment">// with minimum cost. The value of the maximum-flow will be given by</span></div>
<divclass="line"><aname="l00252"></a><spanclass="lineno"> 252</span> <spanclass="comment">// Returns the cost of the minimum-cost flow found by the algorithm when</span></div>
<divclass="line"><aname="l00253"></a><spanclass="lineno"> 253</span> <spanclass="comment">// the returned Status is OPTIMAL.</span></div>
<divclass="line"><aname="l00256"></a><spanclass="lineno"> 256</span> <spanclass="comment">// Returns the total flow of the minimum-cost flow found by the algorithm</span></div>
<divclass="line"><aname="l00257"></a><spanclass="lineno"> 257</span> <spanclass="comment">// when the returned Status is OPTIMAL.</span></div>
<divclass="line"><aname="l00260"></a><spanclass="lineno"> 260</span> <spanclass="comment">// Returns the flow on arc, this only make sense for a successful Solve().</span></div>
<divclass="line"><aname="l00262"></a><spanclass="lineno"> 262</span> <spanclass="comment">// Note: It is possible that there is more than one optimal solution. The</span></div>
<divclass="line"><aname="l00263"></a><spanclass="lineno"> 263</span> <spanclass="comment">// algorithm is deterministic so it will always return the same solution for</span></div>
<divclass="line"><aname="l00264"></a><spanclass="lineno"> 264</span> <spanclass="comment">// a given problem. However, there is no guarantee of this from one code</span></div>
<divclass="line"><aname="l00265"></a><spanclass="lineno"> 265</span> <spanclass="comment">// version to the next (but the code does not change often).</span></div>
<divclass="line"><aname="l00268"></a><spanclass="lineno"> 268</span> <spanclass="comment">// Accessors for the user given data. The implementation will crash if "arc"</span></div>
<divclass="line"><aname="l00269"></a><spanclass="lineno"> 269</span> <spanclass="comment">// is not in [0, NumArcs()) or "node" is not in [0, NumNodes()).</span></div>
<divclass="line"><aname="l00282"></a><spanclass="lineno"> 282</span> <spanclass="comment">// Applies the permutation in arc_permutation_ to the given arc index.</span></div>
<divclass="line"><aname="l00302"></a><spanclass="lineno"> 302</span> <spanclass="comment">// Generic MinCostFlow that works with StarGraph and all the graphs handling</span></div>
<divclass="line"><aname="l00303"></a><spanclass="lineno"> 303</span> <spanclass="comment">// reverse arcs from graph.h, see the end of min_cost_flow.cc for the exact</span></div>
<divclass="line"><aname="l00304"></a><spanclass="lineno"> 304</span> <spanclass="comment">// types this class is compiled for.</span></div>
<divclass="line"><aname="l00306"></a><spanclass="lineno"> 306</span> <spanclass="comment">// One can greatly decrease memory usage by using appropriately small integer</span></div>
<divclass="line"><aname="l00308"></a><spanclass="lineno"> 308</span> <spanclass="comment">// - For the Graph<> types, i.e. NodeIndexType and ArcIndexType, see graph.h.</span></div>
<divclass="line"><aname="l00309"></a><spanclass="lineno"> 309</span> <spanclass="comment">// - ArcFlowType is used for the *per-arc* flow quantity. It must be signed, and</span></div>
<divclass="line"><aname="l00310"></a><spanclass="lineno"> 310</span> <spanclass="comment">// large enough to hold the maximum arc capacity and its negation.</span></div>
<divclass="line"><aname="l00311"></a><spanclass="lineno"> 311</span> <spanclass="comment">// - ArcScaledCostType is used for a per-arc scaled cost. It must be signed</span></div>
<divclass="line"><aname="l00312"></a><spanclass="lineno"> 312</span> <spanclass="comment">// and large enough to hold the maximum unit cost of an arc times</span></div>
<divclass="line"><aname="l00315"></a><spanclass="lineno"> 315</span> <spanclass="comment">// Note that the latter two are different than FlowQuantity and CostValue, which</span></div>
<divclass="line"><aname="l00316"></a><spanclass="lineno"> 316</span> <spanclass="comment">// are used for global, aggregated values and may need to be larger.</span></div>
<divclass="line"><aname="l00318"></a><spanclass="lineno"> 318</span> <spanclass="comment">// TODO(user): Avoid using the globally defined type CostValue and FlowQuantity.</span></div>
<divclass="line"><aname="l00319"></a><spanclass="lineno"> 319</span> <spanclass="comment">// Also uses the Arc*Type where there is no risk of overflow in more places.</span></div>
<divclass="line"><aname="l00331"></a><spanclass="lineno"> 331</span> <spanclass="comment">// Initialize a MinCostFlow instance on the given graph. The graph does not</span></div>
<divclass="line"><aname="l00332"></a><spanclass="lineno"> 332</span> <spanclass="comment">// need to be fully built yet, but its capacity reservation is used to</span></div>
<divclass="line"><aname="l00333"></a><spanclass="lineno"> 333</span> <spanclass="comment">// initialize the memory of this class.</span></div>
<divclass="line"><aname="l00336"></a><spanclass="lineno"> 336</span> <spanclass="comment">// Returns the graph associated to the current object.</span></div>
<divclass="line"><aname="l00339"></a><spanclass="lineno"> 339</span> <spanclass="comment">// Returns the status of last call to Solve(). NOT_SOLVED is returned if</span></div>
<divclass="line"><aname="l00340"></a><spanclass="lineno"> 340</span> <spanclass="comment">// Solve() has never been called or if the problem has been modified in such a</span></div>
<divclass="line"><aname="l00341"></a><spanclass="lineno"> 341</span> <spanclass="comment">// way that the previous solution becomes invalid.</span></div>
<divclass="line"><aname="l00344"></a><spanclass="lineno"> 344</span> <spanclass="comment">// Sets the supply corresponding to node. A demand is modeled as a negative</span></div>
<divclass="line"><aname="l00354"></a><spanclass="lineno"> 354</span> <spanclass="comment">// Sets the flow for the given arc. Note that new_flow must be smaller than</span></div>
<divclass="line"><aname="l00355"></a><spanclass="lineno"> 355</span> <spanclass="comment">// the capacity of the arc.</span></div>
<divclass="line"><aname="l00358"></a><spanclass="lineno"> 358</span> <spanclass="comment">// Solves the problem, returning true if a min-cost flow could be found.</span></div>
<divclass="line"><aname="l00361"></a><spanclass="lineno"> 361</span> <spanclass="comment">// Checks for feasibility, i.e., that all the supplies and demands can be</span></div>
<divclass="line"><aname="l00362"></a><spanclass="lineno"> 362</span> <spanclass="comment">// matched without exceeding bottlenecks in the network.</span></div>
<divclass="line"><aname="l00363"></a><spanclass="lineno"> 363</span> <spanclass="comment">// If infeasible_supply_node (resp. infeasible_demand_node) are not NULL,</span></div>
<divclass="line"><aname="l00364"></a><spanclass="lineno"> 364</span> <spanclass="comment">// they are populated with the indices of the nodes where the initial supplies</span></div>
<divclass="line"><aname="l00365"></a><spanclass="lineno"> 365</span> <spanclass="comment">// (resp. demands) are too large. Feasible values for the supplies and</span></div>
<divclass="line"><aname="l00366"></a><spanclass="lineno"> 366</span> <spanclass="comment">// demands are accessible through FeasibleSupply.</span></div>
<divclass="line"><aname="l00367"></a><spanclass="lineno"> 367</span> <spanclass="comment">// Note that CheckFeasibility is called by Solve() when the flag</span></div>
<divclass="line"><aname="l00368"></a><spanclass="lineno"> 368</span> <spanclass="comment">// min_cost_flow_check_feasibility is set to true (which is the default.)</span></div>
<divclass="line"><aname="l00372"></a><spanclass="lineno"> 372</span> <spanclass="comment">// Makes the min-cost flow problem solvable by truncating supplies and</span></div>
<divclass="line"><aname="l00373"></a><spanclass="lineno"> 373</span> <spanclass="comment">// demands to a level acceptable by the network. There may be several ways to</span></div>
<divclass="line"><aname="l00374"></a><spanclass="lineno"> 374</span> <spanclass="comment">// do it. In our case, the levels are computed from the result of the max-flow</span></div>
<divclass="line"><aname="l00375"></a><spanclass="lineno"> 375</span> <spanclass="comment">// algorithm run in CheckFeasibility().</span></div>
<divclass="line"><aname="l00376"></a><spanclass="lineno"> 376</span> <spanclass="comment">// MakeFeasible returns false if CheckFeasibility() was not called before.</span></div>
<divclass="line"><aname="l00379"></a><spanclass="lineno"> 379</span> <spanclass="comment">// Returns the cost of the minimum-cost flow found by the algorithm.</span></div>
<divclass="line"><aname="l00382"></a><spanclass="lineno"> 382</span> <spanclass="comment">// Returns the flow on the given arc using the equations given in the</span></div>
<divclass="line"><aname="l00383"></a><spanclass="lineno"> 383</span> <spanclass="comment">// comment on residual_arc_capacity_.</span></div>
<divclass="line"><aname="l00389"></a><spanclass="lineno"> 389</span> <spanclass="comment">// Returns the unscaled cost for the given arc.</span></div>
<divclass="line"><aname="l00392"></a><spanclass="lineno"> 392</span> <spanclass="comment">// Returns the supply at a given node. Demands are modelled as negative</span></div>
<divclass="line"><aname="l00396"></a><spanclass="lineno"> 396</span> <spanclass="comment">// Returns the initial supply at a given node.</span></div>
<divclass="line"><aname="l00399"></a><spanclass="lineno"> 399</span> <spanclass="comment">// Returns the largest supply (if > 0) or largest demand in absolute value</span></div>
<divclass="line"><aname="l00400"></a><spanclass="lineno"> 400</span> <spanclass="comment">// (if < 0) admissible at node. If the problem is not feasible, some of these</span></div>
<divclass="line"><aname="l00401"></a><spanclass="lineno"> 401</span> <spanclass="comment">// values will be smaller (in absolute value) than the initial supplies</span></div>
<divclass="line"><aname="l00402"></a><spanclass="lineno"> 402</span> <spanclass="comment">// and demand given as input.</span></div>
<divclass="line"><aname="l00405"></a><spanclass="lineno"> 405</span> <spanclass="comment">// Whether to use the UpdatePrices() heuristic.</span></div>
<divclass="line"><aname="l00408"></a><spanclass="lineno"> 408</span> <spanclass="comment">// Whether to check the feasibility of the problem with a max-flow, prior to</span></div>
<divclass="line"><aname="l00409"></a><spanclass="lineno"> 409</span> <spanclass="comment">// solving it. This uses about twice as much memory, but detects infeasible</span></div>
<divclass="line"><aname="l00410"></a><spanclass="lineno"> 410</span> <spanclass="comment">// problems (where the flow can't be satisfied) and makes Solve() return</span></div>
<divclass="line"><aname="l00411"></a><spanclass="lineno"> 411</span> <spanclass="comment">// INFEASIBLE. If you disable this check, you will spare memory but you must</span></div>
<divclass="line"><aname="l00412"></a><spanclass="lineno"> 412</span> <spanclass="comment">// make sure that your problem is feasible, otherwise the code can loop</span></div>
<divclass="line"><aname="l00417"></a><spanclass="lineno"> 417</span> <spanclass="comment">// Returns true if the given arc is admissible i.e. if its residual capacity</span></div>
<divclass="line"><aname="l00418"></a><spanclass="lineno"> 418</span> <spanclass="comment">// is strictly positive, and its reduced cost strictly negative, i.e., pushing</span></div>
<divclass="line"><aname="l00419"></a><spanclass="lineno"> 419</span> <spanclass="comment">// more flow into it will result in a reduction of the total cost.</span></div>
<divclass="line"><aname="l00423"></a><spanclass="lineno"> 423</span> <spanclass="comment">// Returns true if node is active, i.e., if its supply is positive.</span></div>
<divclass="line"><aname="l00426"></a><spanclass="lineno"> 426</span> <spanclass="comment">// Returns the reduced cost for a given arc.</span></div>
<divclass="line"><aname="l00430"></a><spanclass="lineno"> 430</span> <spanclass="comment">// Returns the first incident arc of a given node.</span></div>
<divclass="line"><aname="l00433"></a><spanclass="lineno"> 433</span> <spanclass="comment">// Checks the consistency of the input, i.e., whether the sum of the supplies</span></div>
<divclass="line"><aname="l00434"></a><spanclass="lineno"> 434</span> <spanclass="comment">// for all nodes is equal to zero. To be used in a DCHECK.</span></div>
<divclass="line"><aname="l00437"></a><spanclass="lineno"> 437</span> <spanclass="comment">// Checks whether the result is valid, i.e. whether for each arc,</span></div>
<divclass="line"><aname="l00439"></a><spanclass="lineno"> 439</span> <spanclass="comment">// (A solution is epsilon-optimal if ReducedCost(arc) >= -epsilon.)</span></div>
<divclass="line"><aname="l00440"></a><spanclass="lineno"> 440</span> <spanclass="comment">// To be used in a DCHECK.</span></div>
<divclass="line"><aname="l00443"></a><spanclass="lineno"> 443</span> <spanclass="comment">// Checks that the cost range fits in the range of int64's.</span></div>
<divclass="line"><aname="l00444"></a><spanclass="lineno"> 444</span> <spanclass="comment">// To be used in a DCHECK.</span></div>
<divclass="line"><aname="l00447"></a><spanclass="lineno"> 447</span> <spanclass="comment">// Checks the relabel precondition (to be used in a DCHECK):</span></div>
<divclass="line"><aname="l00448"></a><spanclass="lineno"> 448</span> <spanclass="comment">// - The node must be active, or have a 0 excess (relaxation for the Push</span></div>
<divclass="line"><aname="l00450"></a><spanclass="lineno"> 450</span> <spanclass="comment">// - The node must have no admissible arcs.</span></div>
<divclass="line"><aname="l00453"></a><spanclass="lineno"> 453</span> <spanclass="comment">// Returns context concatenated with information about a given arc</span></div>
<divclass="line"><aname="l00454"></a><spanclass="lineno"> 454</span> <spanclass="comment">// in a human-friendly way.</span></div>
<divclass="line"><aname="l00457"></a><spanclass="lineno"> 457</span> <spanclass="comment">// Resets the first_admissible_arc_ array to the first incident arc of each</span></div>
<divclass="line"><aname="l00461"></a><spanclass="lineno"> 461</span> <spanclass="comment">// Scales the costs, by multiplying them by (graph_->num_nodes() + 1).</span></div>
<divclass="line"><aname="l00464"></a><spanclass="lineno"> 464</span> <spanclass="comment">// Unscales the costs, by dividing them by (graph_->num_nodes() + 1).</span></div>
<divclass="line"><aname="l00467"></a><spanclass="lineno"> 467</span> <spanclass="comment">// Optimizes the cost by dividing epsilon_ by alpha_ and calling Refine().</span></div>
<divclass="line"><aname="l00470"></a><spanclass="lineno"> 470</span> <spanclass="comment">// Saturates the admissible arcs, i.e., push as much flow as possible.</span></div>
<divclass="line"><aname="l00473"></a><spanclass="lineno"> 473</span> <spanclass="comment">// Pushes flow on a given arc, i.e., consumes flow on</span></div>
<divclass="line"><aname="l00474"></a><spanclass="lineno"> 474</span> <spanclass="comment">// residual_arc_capacity_[arc], and consumes -flow on</span></div>
<divclass="line"><aname="l00475"></a><spanclass="lineno"> 475</span> <spanclass="comment">// residual_arc_capacity_[Opposite(arc)]. Updates node_excess_ at the tail</span></div>
<divclass="line"><aname="l00476"></a><spanclass="lineno"> 476</span> <spanclass="comment">// and head of the arc accordingly.</span></div>
<divclass="line"><aname="l00483"></a><spanclass="lineno"> 483</span> <spanclass="comment">// Price update heuristics as described in A.V. Goldberg, "An Efficient</span></div>
<divclass="line"><aname="l00484"></a><spanclass="lineno"> 484</span> <spanclass="comment">// Implementation of a Scaling Minimum-Cost Flow Algorithm," Journal of</span></div>
<divclass="line"><aname="l00489"></a><spanclass="lineno"> 489</span> <spanclass="comment">// Performs an epsilon-optimization step by saturating admissible arcs</span></div>
<divclass="line"><aname="l00490"></a><spanclass="lineno"> 490</span> <spanclass="comment">// and discharging the active nodes.</span></div>
<divclass="line"><aname="l00493"></a><spanclass="lineno"> 493</span> <spanclass="comment">// Discharges an active node by saturating its admissible adjacent arcs,</span></div>
<divclass="line"><aname="l00494"></a><spanclass="lineno"> 494</span> <spanclass="comment">// if any, and by relabelling it when it becomes inactive.</span></div>
<divclass="line"><aname="l00497"></a><spanclass="lineno"> 497</span> <spanclass="comment">// Part of the Push LookAhead heuristic. When we are about to push on the</span></div>
<divclass="line"><aname="l00498"></a><spanclass="lineno"> 498</span> <spanclass="comment">// in_arc, we check that the head (i.e node here) can accept the flow and</span></div>
<divclass="line"><aname="l00499"></a><spanclass="lineno"> 499</span> <spanclass="comment">// return true if this is the case:</span></div>
<divclass="line"><aname="l00500"></a><spanclass="lineno"> 500</span> <spanclass="comment">// - Returns true if the node excess is < 0.</span></div>
<divclass="line"><aname="l00501"></a><spanclass="lineno"> 501</span> <spanclass="comment">// - Returns true if node is an admissible arc at its current potential.</span></div>
<divclass="line"><aname="l00502"></a><spanclass="lineno"> 502</span> <spanclass="comment">// - If the two conditions above are false, the node can be relabeled. We</span></div>
<divclass="line"><aname="l00503"></a><spanclass="lineno"> 503</span> <spanclass="comment">// do that and return true if the in_arc is still admissible.</span></div>
<divclass="line"><aname="l00506"></a><spanclass="lineno"> 506</span> <spanclass="comment">// Relabels node, i.e., decreases its potential while keeping the</span></div>
<divclass="line"><aname="l00507"></a><spanclass="lineno"> 507</span> <spanclass="comment">// epsilon-optimality of the pseudo flow. See CheckRelabelPrecondition() for</span></div>
<divclass="line"><aname="l00508"></a><spanclass="lineno"> 508</span> <spanclass="comment">// details on the preconditions.</span></div>
<divclass="line"><aname="l00511"></a><spanclass="lineno"> 511</span> <spanclass="comment">// Handy member functions to make the code more compact.</span></div>
<divclass="line"><aname="l00518"></a><spanclass="lineno"> 518</span> <spanclass="comment">// Pointer to the graph passed as argument.</span></div>
<divclass="line"><aname="l00521"></a><spanclass="lineno"> 521</span> <spanclass="comment">// An array representing the supply (if > 0) or the demand (if < 0)</span></div>
<divclass="line"><aname="l00522"></a><spanclass="lineno"> 522</span> <spanclass="comment">// for each node in graph_.</span></div>
<divclass="line"><aname="l00529"></a><spanclass="lineno"> 529</span> <spanclass="comment">// An array representing the residual_capacity for each arc in graph_.</span></div>
<divclass="line"><aname="l00530"></a><spanclass="lineno"> 530</span> <spanclass="comment">// Residual capacities enable one to represent the capacity and flow for all</span></div>
<divclass="line"><aname="l00531"></a><spanclass="lineno"> 531</span> <spanclass="comment">// arcs in the graph in the following manner.</span></div>
<divclass="line"><aname="l00532"></a><spanclass="lineno"> 532</span> <spanclass="comment">// For all arcs, residual_arc_capacity_[arc] = capacity[arc] - flow[arc]</span></div>
<divclass="line"><aname="l00533"></a><spanclass="lineno"> 533</span> <spanclass="comment">// Moreover, for reverse arcs, capacity[arc] = 0 by definition.</span></div>
<divclass="line"><aname="l00534"></a><spanclass="lineno"> 534</span> <spanclass="comment">// Also flow[Opposite(arc)] = -flow[arc] by definition.</span></div>
<divclass="line"><aname="l00542"></a><spanclass="lineno"> 542</span> <spanclass="comment">// Using these facts enables one to only maintain residual_arc_capacity_,</span></div>
<divclass="line"><aname="l00543"></a><spanclass="lineno"> 543</span> <spanclass="comment">// instead of both capacity and flow, for each direct and indirect arc. This</span></div>
<divclass="line"><aname="l00544"></a><spanclass="lineno"> 544</span> <spanclass="comment">// reduces the amount of memory for this information by a factor 2.</span></div>
<divclass="line"><aname="l00545"></a><spanclass="lineno"> 545</span> <spanclass="comment">// Note that the sum of the largest capacity of an arc in the graph and of</span></div>
<divclass="line"><aname="l00546"></a><spanclass="lineno"> 546</span> <spanclass="comment">// the total flow in the graph mustn't exceed the largest 64 bit integer</span></div>
<divclass="line"><aname="l00547"></a><spanclass="lineno"> 547</span> <spanclass="comment">// to avoid errors. CheckInputConsistency() verifies this constraint.</span></div>
<divclass="line"><aname="l00550"></a><spanclass="lineno"> 550</span> <spanclass="comment">// An array representing the first admissible arc for each node in graph_.</span></div>
<divclass="line"><aname="l00553"></a><spanclass="lineno"> 553</span> <spanclass="comment">// A stack used for managing active nodes in the algorithm.</span></div>
<divclass="line"><aname="l00554"></a><spanclass="lineno"> 554</span> <spanclass="comment">// Note that the papers cited above recommend the use of a queue, but</span></div>
<divclass="line"><aname="l00555"></a><spanclass="lineno"> 555</span> <spanclass="comment">// benchmarking so far has not proved it is better.</span></div>
<divclass="line"><aname="l00558"></a><spanclass="lineno"> 558</span> <spanclass="comment">// epsilon_ is the tolerance for optimality.</span></div>
<divclass="line"><aname="l00561"></a><spanclass="lineno"> 561</span> <spanclass="comment">// alpha_ is the factor by which epsilon_ is divided at each iteration of</span></div>
<divclass="line"><aname="l00565"></a><spanclass="lineno"> 565</span> <spanclass="comment">// cost_scaling_factor_ is the scaling factor for cost.</span></div>
<divclass="line"><aname="l00568"></a><spanclass="lineno"> 568</span> <spanclass="comment">// An array representing the scaled unit cost for each arc in graph_.</span></div>
<divclass="line"><aname="l00577"></a><spanclass="lineno"> 577</span> <spanclass="comment">// An array containing the initial excesses (i.e. the supplies) for each</span></div>
<divclass="line"><aname="l00578"></a><spanclass="lineno"> 578</span> <spanclass="comment">// node. This is used to create the max-flow-based feasibility checker.</span></div>
<divclass="line"><aname="l00581"></a><spanclass="lineno"> 581</span> <spanclass="comment">// An array containing the best acceptable excesses for each of the</span></div>
<divclass="line"><aname="l00582"></a><spanclass="lineno"> 582</span> <spanclass="comment">// nodes. These excesses are imposed by the result of the max-flow-based</span></div>
<divclass="line"><aname="l00583"></a><spanclass="lineno"> 583</span> <spanclass="comment">// feasibility checker for the nodes with an initial supply != 0. For the</span></div>
<divclass="line"><aname="l00584"></a><spanclass="lineno"> 584</span> <spanclass="comment">// other nodes, the excess is simply 0.</span></div>
<divclass="line"><aname="l00590"></a><spanclass="lineno"> 590</span> <spanclass="comment">// Number of Relabel() since last UpdatePrices().</span></div>
<divclass="line"><aname="l00593"></a><spanclass="lineno"> 593</span> <spanclass="comment">// A Boolean which is true when feasibility has been checked.</span></div>
<divclass="line"><aname="l00596"></a><spanclass="lineno"> 596</span> <spanclass="comment">// Whether to use the UpdatePrices() heuristic.</span></div>
<divclass="line"><aname="l00599"></a><spanclass="lineno"> 599</span> <spanclass="comment">// Whether to check the problem feasibility with a max-flow.</span></div>
<divclass="line"><aname="l00607"></a><spanclass="lineno"> 607</span> <spanclass="comment">// Default MinCostFlow instance that uses StarGraph.</span></div>
<divclass="line"><aname="l00608"></a><spanclass="lineno"> 608</span> <spanclass="comment">// New clients should use SimpleMinCostFlow if they can.</span></div>