ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Warm.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------ File Warm.h -------------------------------*/
3/*--------------------------------------------------------------------------*/
96/*--------------------------------------------------------------------------*/
97/*----------------------------- DEFINITIONS --------------------------------*/
98/*--------------------------------------------------------------------------*/
99
100#ifndef __ADS_interop_lemon_Warm
101 #define __ADS_interop_lemon_Warm
102/* self-identification: #endif constant */
103
104/*--------------------------------------------------------------------------*/
105/*------------------------------ INCLUDES ----------------------------------*/
106/*--------------------------------------------------------------------------*/
107
108#if ! defined( ADS_LEMON_SHIM )
109 #error "the warm start of LEMON needs its rewritten headers (ADS_LEMON_SHIM)"
110#endif
111
112#include "ADS/Contract.h"
113#include "ADS/Modification.h"
114#include "ADS/graph/MaxFlow.h"
117
118#include <lemon/core.h>
119#include <lemon/network_simplex.h>
120#include <lemon/preflow.h>
121#include <lemon/smart_graph.h>
122
123#include <cstddef>
124#include <memory>
125#include <utility>
126#include <vector>
127
128/*--------------------------------------------------------------------------*/
129/*--------------------------- NAMESPACE ------------------------------------*/
130/*--------------------------------------------------------------------------*/
131
132namespace ADS::lemon_interop
133{
134
135/*--------------------------------------------------------------------------*/
136/*-------------------------- CLASS PreflowEngine ---------------------------*/
137/*--------------------------------------------------------------------------*/
138
142{
143
144/*--------------------------------------------------------------------------*/
145 public:
148 long long scratch( FlowNetwork & net , int s , int t , Augment ) {
149 build( net , s , t );
150 f_pf->run();
151 ++scratches;
152 return( giveBack( net ) );
153 }
154
157 long long warm( FlowNetwork & net , int s , int t , Augment how ,
158 long long ) {
159 if( ! f_pf || ( net.n() != f_n ) || ( s != f_s ) || ( t != f_t ) )
160 return( scratch( net , s , t , how ) );
161 for( int e = 2 * int( f_arc.size() ) ; e < net.places() ; e += 2 )
162 f_arc.push_back( f_g.addArc( f_g.nodeFromId( net.tail( e ) ) ,
163 f_g.nodeFromId( net.head( e ) ) ) );
164 for( std::size_t k = 0 ; k < f_arc.size() ; ++k ) {
165 f_cap.set( f_arc[ k ] , net.capacity( int( 2 * k ) ) );
166 f_flow.set( f_arc[ k ] , net.flow( int( 2 * k ) ) );
167 }
168 f_pf->runWarm();
169 ++warms;
170 return( giveBack( net ) );
171 }
172
173 int scratches = 0;
174 int warms = 0;
175
176/*--------------------------------------------------------------------------*/
177 private:
178 using G = ::lemon::SmartDigraph;
179 using CM = G::ArcMap< long long >;
180 using PF = ::lemon::Preflow< G , CM >;
181
183 void build( const FlowNetwork & net , int s , int t ) {
184 f_pf.reset();
185 f_g.clear();
186 f_arc.clear();
187 f_n = net.n();
188 f_s = s;
189 f_t = t;
190 f_g.reserveNode( f_n );
191 for( int u = 0 ; u < f_n ; ++u )
192 f_g.addNode();
193 for( int e = 0 ; e < net.places() ; e += 2 ) {
194 f_arc.push_back( f_g.addArc( f_g.nodeFromId( net.tail( e ) ) ,
195 f_g.nodeFromId( net.head( e ) ) ) );
196 f_cap.set( f_arc.back() , net.capacity( e ) );
197 }
198 f_pf = std::make_unique< PF >( f_g , f_cap , f_g.nodeFromId( s ) ,
199 f_g.nodeFromId( t ) );
200 f_pf->flowMap( f_flow );
201 }
202
204 long long giveBack( FlowNetwork & net ) const {
205 std::vector< long long > F( f_arc.size() );
206 for( std::size_t k = 0 ; k < f_arc.size() ; ++k )
207 F[ k ] = f_flow[ f_arc[ k ] ];
208 net.assignFlow( F );
209 return( f_pf->flowValue() );
210 }
211
212 G f_g;
213 CM f_cap{ f_g };
214 CM f_flow{ f_g };
215 std::vector< G::Arc > f_arc;
216 std::unique_ptr< PF > f_pf;
217 int f_n = -1;
218 int f_s = -1;
219 int f_t = -1;
220
221 }; // end( class PreflowEngine )
222
223/*--------------------------------------------------------------------------*/
224/*--------------------- CLASS NetworkSimplexAssignment ---------------------*/
225/*--------------------------------------------------------------------------*/
226
229template< class W = long long >
231{
232
233/*--------------------------------------------------------------------------*/
234 public:
237 int solve( int n , const std::vector< W > & C , bool all ,
238 const std::vector< std::pair< int , int > > & D ,
239 std::vector< int > & col , std::vector< W > & u ,
240 std::vector< W > & v ) {
241 const W INF = infinity< W >();
242 const auto at = [ n ]( int i , int j ) {
243 return( std::size_t( i ) * std::size_t( n ) + std::size_t( j ) );
244 };
245 bool cold = all || ! f_ns || ( n != f_n ) || ! f_ns->warmReady();
246 if( ! cold ) // a pair forbidden or allowed again changes the digraph
247 for( const auto & [ i , j ] : D )
248 if( ( C[ at( i , j ) ] != INF ) != ( f_arc[ at( i , j ) ] >= 0 ) ) {
249 cold = true;
250 break;
251 }
252 typename NS::ProblemType r;
253 if( cold ) {
254 build( n , C );
255 r = f_ns->run();
256 ++scratches;
257 }
258 else {
259 for( const auto & [ i , j ] : D )
260 if( C[ at( i , j ) ] != INF )
261 f_ns->warmCost( f_g.arcFromId( f_arc[ at( i , j ) ] ) ,
262 C[ at( i , j ) ] );
263 r = f_ns->runWarm();
264 ++warms;
265 }
266 if( r != NS::OPTIMAL ) {
267 f_ns.reset(); // a new solve next time
268 return( -1 );
269 }
270 for( int i = 0 ; i < n ; ++i )
271 for( int j = 0 ; j < n ; ++j ) {
272 const int a = f_arc[ at( i , j ) ];
273 if( ( a >= 0 ) && ( f_ns->flow( f_g.arcFromId( a ) ) > 0 ) )
274 col[ std::size_t( i ) ] = j;
275 }
276 for( int i = 0 ; i < n ; ++i ) {
277 u[ std::size_t( i ) ] = -f_ns->potential( f_g.nodeFromId( i ) );
278 v[ std::size_t( i ) ] = f_ns->potential( f_g.nodeFromId( n + i ) );
279 }
280 return( cold ? Reopt::SCRATCH_BY_EVENT : Reopt::WARM );
281 }
282
283 int scratches = 0;
284 int warms = 0;
285
286/*--------------------------------------------------------------------------*/
287 private:
288 using G = ::lemon::SmartDigraph;
289 using NS = ::lemon::NetworkSimplex< G , int , W >;
290
292 void build( int n , const std::vector< W > & C ) {
293 const W INF = infinity< W >();
294 f_ns.reset();
295 f_g.clear();
296 f_n = n;
297 f_arc.assign( std::size_t( n ) * std::size_t( n ) , -1 );
298 for( int k = 0 ; k < 2 * n ; ++k )
299 f_g.addNode();
300 G::ArcMap< W > cost( f_g );
301 G::NodeMap< int > supply( f_g );
302 for( int i = 0 ; i < n ; ++i ) {
303 supply.set( f_g.nodeFromId( i ) , 1 );
304 supply.set( f_g.nodeFromId( n + i ) , -1 );
305 for( int j = 0 ; j < n ; ++j ) {
306 const std::size_t k =
307 std::size_t( i ) * std::size_t( n ) + std::size_t( j );
308 if( C[ k ] == INF )
309 continue;
310 const G::Arc a =
311 f_g.addArc( f_g.nodeFromId( i ) , f_g.nodeFromId( n + j ) );
312 cost.set( a , C[ k ] );
313 f_arc[ k ] = f_g.id( a );
314 }
315 }
316 f_ns = std::make_unique< NS >( f_g );
317 f_ns->costMap( cost ).supplyMap( supply );
318 }
319
320 G f_g;
321 std::vector< int > f_arc;
322 std::unique_ptr< NS > f_ns;
323 int f_n = -1;
324
325 }; // end( class NetworkSimplexAssignment )
326
327/*--------------------------------------------------------------------------*/
328/*------------------------ CLASS MinCostFlowObserver -----------------------*/
329/*--------------------------------------------------------------------------*/
330
334template< class GR , class A = ::lemon::NetworkSimplex< GR > ,
335 class CM = typename GR::template ArcMap< typename A::Cost > ,
336 class UM = typename GR::template ArcMap< typename A::Value > ,
337 class SM = typename GR::template NodeMap< typename A::Value > >
339{
340
341/*--------------------------------------------------------------------------*/
342 public:
343 using Cost = typename A::Cost;
344 using Value = typename A::Value;
345
349 MinCostFlowObserver( const GR & g , const CM & c , const UM & u ,
350 const SM & s )
351 : f_g( g ) , f_c( c ) , f_u( u ) , f_s( s ) , f_a( g ) {
352 load();
353 }
354
356 template< class V >
358 if( ! f_reset )
359 f_a.warmCost( f_g.arcFromId( e.h ) , Cost( e.to ) );
360 }
361
363 template< class V >
365 if( ! f_reset )
366 f_a.warmUpper( f_g.arcFromId( e.h ) , Value( e.to ) );
367 }
368
370 template< class V >
372 if( ! f_reset )
373 f_a.warmSupply( f_g.nodeFromId( e.h ) , Value( e.to ) );
374 }
375
377 template< class K , class V >
379 f_reset = true;
380 }
381
383 template< class K , class V >
384 void on( const mod::Erased< int , K , V > & ) noexcept {
385 f_reset = true;
386 }
387
389 void on( const mod::Cleared & ) noexcept { f_reset = true; }
390
392 void on( const mod::Rebuilt & ) noexcept { f_reset = true; }
393
396 typename A::ProblemType solve( void ) {
397 if( f_reset ) {
398 f_a.reset();
399 load();
400 f_reset = false;
401 }
402 if( f_a.warmReady() ) {
403 ++warms;
404 return( f_a.runWarm() );
405 }
406 ++scratches;
407 return( f_a.run() );
408 }
409
411 [[nodiscard]] const A & algorithm( void ) const { return( f_a ); }
412
413 int scratches = 0;
414 int warms = 0;
415
416/*--------------------------------------------------------------------------*/
417 private:
419 void load( void ) { f_a.costMap( f_c ).upperMap( f_u ).supplyMap( f_s ); }
420
421 const GR & f_g;
422 const CM & f_c;
423 const UM & f_u;
424 const SM & f_s;
425 A f_a;
426 bool f_reset = false;
427
428 }; // end( class MinCostFlowObserver )
429
430/*--------------------------------------------------------------------------*/
431
432} // end( namespace ADS::lemon_interop )
433
434/*--------------------------------------------------------------------------*/
435
436#endif /* Warm.h included */
437
438/*--------------------------------------------------------------------------*/
439/*---------------------------- End File Warm.h -----------------------------*/
440/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The maximum flow of section 15.2 in a network , with integer capacities, and the minimum cut.
The typed modifications, beyond the book: the vocabulary by which a structure tells the observers att...
The solutions that follow the changes of a graph, beyond the book: the observers of an Observable [se...
The shortest paths of chapter 11 from a node of an oriented GRAPH whose edges have a cost [see Grap...
a network of flow with integer capacities
Definition MaxFlow.h:126
void assignFlow(const std::vector< long long > &F)
the flow of each edge set to , e.g., one that another solver computed, and the excesses and the defi...
int n(void) const
the number of nodes
Definition MaxFlow.h:135
int tail(int e) const
the tail of the edge number
Definition MaxFlow.h:165
int head(int e) const
the head of the edge number
Definition MaxFlow.h:172
long long capacity(int e) const
the capacity of the edge number
long long flow(int e) const
the flow on the edge number
int places(void) const
the number of the places of the edges: the edge , an even number, and its residual twin
Definition MaxFlow.h:161
typename A::Value Value
the type of the flows
Definition Warm.h:344
MinCostFlowObserver(const GR &g, const CM &c, const UM &u, const SM &s)
the minimum cost flow of with the costs , the upper bounds and the supplies (the maps below the Ob...
Definition Warm.h:349
void on(const mod::Inserted< int, K, V > &)
an arc or a node inserted: the algorithm is reset at the next solve
Definition Warm.h:378
const A & algorithm(void) const
the algorithm, for its flow, its potentials and its total cost
Definition Warm.h:411
void on(const mod::Cleared &) noexcept
the digraph cleared: the same
Definition Warm.h:389
int scratches
the solves from scratch
Definition Warm.h:413
int warms
the warm ones
Definition Warm.h:414
void on(const mod::Changed< mod::Supply, int, V > &e)
a supply changed, given to the node
Definition Warm.h:371
void on(const mod::Rebuilt &) noexcept
the digraph rebuilt: the same
Definition Warm.h:392
A::ProblemType solve(void)
the flow brought up to date: run() after a reset or at the first call, runWarm() otherwise
Definition Warm.h:396
void on(const mod::Erased< int, K, V > &) noexcept
an arc or a node erased: the same
Definition Warm.h:384
typename A::Cost Cost
the type of the costs
Definition Warm.h:343
void on(const mod::Changed< mod::Cost, int, V > &e)
a cost changed, given to the arc
Definition Warm.h:357
void on(const mod::Changed< mod::Upper, int, V > &e)
an upper bound changed, given to the arc
Definition Warm.h:364
an engine of Assignment on the NetworkSimplex of LEMON, warm after changes of finite costs
Definition Warm.h:231
int scratches
the solves from scratch
Definition Warm.h:283
int warms
the warm ones
Definition Warm.h:284
int solve(int n, const std::vector< W > &C, bool all, const std::vector< std::pair< int, int > > &D, std::vector< int > &col, std::vector< W > &u, std::vector< W > &v)
an optimal assignment of the costs and its potentials [see AssignmentEngine]
Definition Warm.h:237
the engine of MaxFlowObserver on the Preflow of LEMON, with its warm start from the flow of the netwo...
Definition Warm.h:142
int scratches
the solves from scratch
Definition Warm.h:173
long long warm(FlowNetwork &net, int s, int t, Augment how, long long)
the value of a maximum flow of net from its flow, by runWarm() of the Preflow, or from scratch if the...
Definition Warm.h:157
int warms
the warm ones
Definition Warm.h:174
long long scratch(FlowNetwork &net, int s, int t, Augment)
the value of a maximum flow of net from the zero flow, by a new Preflow on a copy of net
Definition Warm.h:148
Augment
the way of increasing the flow
Definition MaxFlow.h:113
constexpr D infinity(void)
the infinite distance of the type D
Definition ShortestPaths.h:146
constexpr int INF
the infinite distance
Definition Visit.h:119
Definition Digraphs.h:89
static constexpr int SCRATCH_BY_EVENT
from scratch, Rebuilt,
Definition Modification.h:191
static constexpr int WARM
by a repair
Definition Modification.h:185
the attribute A of the item of handle changed from from to to
Definition Modification.h:600
V to
the value after
Definition Modification.h:608
H h
< the value
Definition Modification.h:606
the structure emptied
Definition Modification.h:699
the item of handle , key and value erased, told before the removal outside a batch
Definition Modification.h:568
the item of handle , key and value inserted
Definition Modification.h:536
anything may have changed: forget and recompute
Definition Modification.h:708