100#ifndef __ADS_interop_lemon_Warm
101 #define __ADS_interop_lemon_Warm
108#if ! defined( ADS_LEMON_SHIM )
109 #error "the warm start of LEMON needs its rewritten headers (ADS_LEMON_SHIM)"
118#include <lemon/core.h>
119#include <lemon/network_simplex.h>
120#include <lemon/preflow.h>
121#include <lemon/smart_graph.h>
149 build( net , s , t );
152 return( giveBack( net ) );
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 ) ) );
170 return( giveBack( net ) );
178 using G = ::lemon::SmartDigraph;
179 using CM = G::ArcMap< long long >;
180 using PF = ::lemon::Preflow< G , CM >;
183 void build(
const FlowNetwork & net ,
int s ,
int t ) {
190 f_g.reserveNode( f_n );
191 for(
int u = 0 ; u < f_n ; ++u )
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 ) );
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 );
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 ] ];
209 return( f_pf->flowValue() );
215 std::vector< G::Arc > f_arc;
216 std::unique_ptr< PF > f_pf;
229template<
class W =
long long >
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 ) {
242 const auto at = [ n ](
int i ,
int j ) {
243 return( std::size_t( i ) * std::size_t( n ) + std::size_t( j ) );
245 bool cold = all || ! f_ns || ( n != f_n ) || ! f_ns->warmReady();
247 for(
const auto & [ i , j ] : D )
248 if( ( C[ at( i , j ) ] !=
INF ) != ( f_arc[ at( i , j ) ] >= 0 ) ) {
252 typename NS::ProblemType r;
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 ) ] ) ,
266 if( r != NS::OPTIMAL ) {
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;
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 ) );
288 using G = ::lemon::SmartDigraph;
289 using NS = ::lemon::NetworkSimplex< G , int , W >;
292 void build(
int n ,
const std::vector< W > & C ) {
297 f_arc.assign( std::size_t( n ) * std::size_t( n ) , -1 );
298 for(
int k = 0 ; k < 2 * n ; ++k )
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 );
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 );
316 f_ns = std::make_unique< NS >( f_g );
317 f_ns->costMap( cost ).supplyMap( supply );
321 std::vector< int > f_arc;
322 std::unique_ptr< NS > f_ns;
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 > >
351 : f_g( g ) , f_c( c ) , f_u( u ) , f_s( s ) , f_a( g ) {
359 f_a.warmCost( f_g.arcFromId( e.
h ) ,
Cost( e.
to ) );
366 f_a.warmUpper( f_g.arcFromId( e.
h ) ,
Value( e.
to ) );
373 f_a.warmSupply( f_g.nodeFromId( e.
h ) ,
Value( e.
to ) );
377 template<
class K ,
class V >
383 template<
class K ,
class V >
396 typename A::ProblemType
solve(
void ) {
402 if( f_a.warmReady() ) {
404 return( f_a.runWarm() );
411 [[nodiscard]]
const A &
algorithm(
void )
const {
return( f_a ); }
419 void load(
void ) { f_a.costMap( f_c ).upperMap( f_u ).supplyMap( f_s ); }
426 bool f_reset =
false;
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
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