109#ifndef __ADS_Reoptimization
110 #define __ADS_Reoptimization
132#include <type_traits>
133#include <unordered_map>
149template<
class R ,
class W >
152 std::span< const GraphEvent< W > > s )
157 { r.outcome() } -> std::convertible_to< int >;
189template<
class W , ReoptPolicy P = RatioPolicy >
199 [[nodiscard]]
static constexpr unsigned mask(
void ) {
210 void apply( std::span< const Event > s ) {
212 f_q.insert(
f_q.end() , s.begin() , s.end() );
228 [[nodiscard]] std::size_t
pending(
void )
const {
return(
f_q.size() ); }
244 [[nodiscard]]
long long byCause(
int c )
const {
258 requires requires( P & p ) { p.rho = 0.0; }
267 std::vector< Event >
take(
void ) {
277 s.
size = std::size_t( m > 0 ? m : 0 );
283 [[nodiscard]]
bool tooMany( std::size_t k ,
long long m ) {
289 [[nodiscard]]
bool fresh(
bool ready ) {
328template< Graph G , ReoptPolicy P = RatioPolicy >
399 return( f_R.d[ v ] );
419 int repair( std::vector< int > & S ,
ReoptStats & s );
421 static constexpr int REPAIRED = 0;
422 static constexpr int CYCLE = 1;
423 static constexpr int ABANDONED = 2;
426 void buildTree(
void );
432 void link(
int x ,
int p );
435 void unlink(
int x );
438 void setParent(
int v ,
int u );
441 void buildIn(
void );
444 void applyIn( std::span< const Event > C );
448 void forEachIn(
int x , F && f )
const;
453 bool f_ready =
false;
455 std::vector< int > f_child;
456 std::vector< int > f_next;
457 std::vector< int > f_prev;
458 std::vector< int > f_pops;
459 std::vector< char > f_inA;
463 std::vector< std::vector< std::pair< int , Weight > > > f_in;
473template< Graph G , ReoptPolicy P = RatioPolicy >
528 [[nodiscard]] std::vector< WeightedEdge< Weight > >
forest(
void );
568 bool operator==(
const Heavy & h )
const {
569 return( ( has == h.has ) && ( node == h.node ) );
578 Heavy operator()(
const Heavy & a ,
const Heavy & b )
const {
587 return( a.node < b.node ? b : a );
592 static std::uint64_t key(
int u ,
int v ) {
595 return( ( std::uint64_t( std::uint32_t( u ) ) << 32 ) |
596 std::uint32_t( v ) );
600 static std::pair< int , int > ends( std::uint64_t k ) {
601 return( std::pair< int , int >(
int( k >> 32 ) ,
int( k & 0xffffffffu ) ) );
605 void scratch(
bool fromGraph );
611 void addTree( std::uint64_t k );
614 void removeTree( std::uint64_t k );
640 bool replacement(
int u ,
int v , std::uint64_t & best );
661 bool f_ready =
false;
664 std::unordered_map< std::uint64_t , Rec > f_E;
676 Inc & inc(
int a ,
int b );
678 std::vector< std::vector< Inc > > f_inc;
680 std::vector< std::vector< int > > f_adjF;
681 std::vector< std::uint64_t > f_F;
682 LinkCutTree< Heavy , Heavier > f_lct;
684 std::vector< int > f_vid;
686 std::vector< std::uint64_t > f_edgeOf;
688 std::vector< int > f_free;
690 std::vector< int > f_mark;
693 std::set< std::pair< Weight , std::uint64_t > > f_off;
695 static constexpr bool SUBTRACT =
requires(
Weight a ,
Weight b ) {
696 { a - b } -> std::convertible_to< Weight >;
698 double f_replAvg = 0;
702 NeumaierSum< Weight > f_total;
704 std::size_t f_changes = 0;
710 void refreshTotal( std::size_t changes );
713 [[nodiscard]]
bool compensated(
void )
const;
717 [[nodiscard]]
bool totalHolds(
void )
const;
738 requires( E & x ,
FlowNetwork & net ,
int s ,
int t ,
742 x.scratch( net , s , t , how )
743 } -> std::convertible_to< long long >;
745 x.warm( net , s , t , how , v )
746 } -> std::convertible_to< long long >;
755 return( net.
maxFlow( s , t , how ) );
760 return( v + net.
maxFlow( s , t , how ) );
767template< Graph G , ReoptPolicy P = RatioPolicy ,
768 FlowEngine E = AugmentEngine >
769 requires std::is_integral_v< typename G::Weight >
809 [[nodiscard]]
long long value(
void ) {
815 [[nodiscard]]
const std::vector< bool > &
minCut(
void ) {
829 static std::uint64_t key(
int u ,
int v ) {
830 return( ( std::uint64_t( std::uint32_t( u ) ) << 32 ) |
831 std::uint32_t( v ) );
835 void scratch(
void );
838 [[nodiscard]]
long long cutCapacity(
void )
const;
844 bool f_ready =
false;
848 std::unordered_map< std::uint64_t , int > f_arc;
849 long long f_value = 0;
850 std::vector< bool > f_S;
862template< Graph G , ReoptPolicy P = RatioPolicy >
863 requires std::is_arithmetic_v< typename G::Weight >
906 const int j = f_A.column( i );
907 return( j < 0 ? -1 : f_k + j );
913 return( f_A.total() );
918 [[nodiscard]] P &
policy(
void ) {
return( f_A.policy() ); }
920 [[nodiscard]]
const P &
policy(
void )
const {
return( f_A.policy() ); }
947#include "ADS/graph/impl/Reoptimization.tpp"
The assignment problem, beyond the book: given the costs of rows and columns (the workers and the ...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The link-cut trees of Sleator and Tarjan, beyond the book: a forest of rooted trees on the nodes tha...
The maximum flow of section 15.2 in a network , with integer capacities, and the minimum cut.
The minimum spanning tree of an unoriented connected graph with weights on the edges (section 14....
A GRAPH that tells its changes, beyond the book: the wrapper Observable< G , O... > routes the change...
The shortest paths of chapter 11 from a node of an oriented GRAPH whose edges have a cost [see Grap...
Weight total(void)
the total cost of the assignment
Definition Reoptimization.h:911
typename G::Weight Weight
the type of the weights
Definition Reoptimization.h:869
const P & policy(void) const
the recompute policy
Definition Reoptimization.h:920
AssignmentObserver(const G &g, int k)
the assignment of the rows of , computed at the first query; a pair with no edge is forbidden
P & policy(void)
the recompute policy, that of the Assignment, which weighs the events only through the rows and the c...
Definition Reoptimization.h:918
Assignment< Weight > & assignment(void)
the assignment, up to date
Definition Reoptimization.h:896
void solve(void)
brings the assignment up to date
int partner(int i)
the column, in , of the row , if none
Definition Reoptimization.h:904
ReoptStats reoptStats(std::size_t k) const
the statistics of a query after more events, for the policy: the events queued and the costs
an assignment of least cost of rows to columns, kept optimal after the changes of the costs,...
Definition Assignment.h:151
the queue of the events, the recompute policy P [see ReoptPolicy] and the counts that the observers s...
Definition Reoptimization.h:191
int f_outcome
Definition Reoptimization.h:315
void setRecomputeRatio(double ratio)
from scratch when the events coalesced exceed times the edges: of the policy
Definition Reoptimization.h:257
GraphEvent< W > Event
the type of the events
Definition Reoptimization.h:195
int outcome(void) const
how the last solve() with events to apply, or with no solution yet, did it [see Reopt]
Definition Reoptimization.h:232
long long byCause(int c) const
the solves of each cause so far
Definition Reoptimization.h:244
int cause(void) const
why: the cause of the last outcome [see Reopt]
Definition Reoptimization.h:235
P f_policy
Definition Reoptimization.h:313
void apply(std::span< const Event > s)
queues the events of , unless Rebuilt came
Definition Reoptimization.h:210
bool f_stale
Definition Reoptimization.h:314
void notify(const Event &e)
apply( e ), for the Observable
Definition Reoptimization.h:216
const P & policy(void) const
the recompute policy
Definition Reoptimization.h:252
void notify(std::span< const Event > s)
apply( s ), for the Observable
Definition Reoptimization.h:218
long long count(int outcome) const
the solves of each outcome so far
Definition Reoptimization.h:238
void done(int cause, double w)
the cause of a solve() counted and told to the policy with the work
Definition Reoptimization.h:307
static constexpr unsigned mask(void)
all the kinds of event
Definition Reoptimization.h:199
bool tooMany(std::size_t k, long long m)
whether the policy wants events on edges from scratch
Definition Reoptimization.h:283
std::vector< Event > take(void)
the queue coalesced, emptied
Definition Reoptimization.h:267
long long f_count[3]
Definition Reoptimization.h:317
static ReoptStats stats(std::size_t k, long long m)
the statistics of events on edges
Definition Reoptimization.h:274
void tally(int cause)
the cause of a solve() counted, not told to the policy
Definition Reoptimization.h:298
long long f_byCause[Reopt::CAUSES]
Definition Reoptimization.h:318
int f_cause
Definition Reoptimization.h:316
void on(const mod::Rebuilt &) noexcept
forgets the queue, and takes no event until the next query, which computes the solution from scratch
Definition Reoptimization.h:222
void apply(const Event &e)
queues , unless Rebuilt came since the last query
Definition Reoptimization.h:204
bool fresh(bool ready)
whether a solve from scratch is due before any event: no solution yet, or Rebuilt; the queue then emp...
Definition Reoptimization.h:289
P Policy
the type of the policy
Definition Reoptimization.h:196
std::vector< Event > f_q
Definition Reoptimization.h:312
P & policy(void)
the recompute policy
Definition Reoptimization.h:250
std::size_t pending(void) const
the events waiting for the next query
Definition Reoptimization.h:228
a network of flow with integer capacities
Definition MaxFlow.h:126
long long maxFlow(int s, int t, Augment how=Augment::DINIC, long long *paths=nullptr)
the maximum flow from to , starting from the flow there is, after the excesses and the deficits left...
void solve(void)
brings the flow up to date
const std::vector< bool > & minCut(void)
the side of of a minimum cut
Definition Reoptimization.h:815
long long value(void)
the value of the maximum flow
Definition Reoptimization.h:809
MaxFlowObserver(const G &g, int s, int t, Augment how=Augment::DINIC)
the flow from to in , computed by how at the first query
ReoptStats reoptStats(std::size_t k) const
the statistics of a query after more events, for the policy: the events queued and the edges
typename G::Weight Weight
the type of the weights
Definition Reoptimization.h:775
Weight distance(int v)
the distance of , infinity() if not reachable
Definition Reoptimization.h:397
void solve(void)
brings the tree up to date
const ShortestPathTree< Weight > & tree(void)
the tree, up to date; its order is that of the last computation from scratch
Definition Reoptimization.h:391
ReoptStats reoptStats(std::size_t k) const
the statistics of a query after more events, for the policy: the events queued and the edges
ShortestPathsObserver(const G &g, int r, Frontier how=Frontier::HEAP)
the shortest paths from in , computed by how at the first query
typename G::Weight Weight
the type of the weights
Definition Reoptimization.h:334
SpanningForestObserver(const G &g)
the forest of , computed by Kruskal at the first query; if the policy is a RatioPolicy
void solve(void)
brings the forest up to date
bool inForest(int u, int v)
whether is in the forest
Weight weight(void)
the total weight of the forest, kept as it changes when the weights subtract, after the query,...
typename G::Weight Weight
the type of the weights
Definition Reoptimization.h:479
ReoptStats reoptStats(std::size_t k) const
the statistics of a query after more events, for the policy: the events queued and the edges
std::vector< WeightedEdge< Weight > > forest(void)
the edges of the forest, up to date, in no given order
the engines of the solves of a MaxFlowObserver, objects that the observer keeps: scratch( net ,...
Definition Reoptimization.h:737
an observer of the events of weights W: the kinds that it wants, and the two ways of getting them
Definition Observable.h:298
a solution that follows the events of weights W
Definition Reoptimization.h:150
The GRAPH of chapter 9 and its realizations.
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
#define ADS_COLD_PATH
a function of a rare path: never inlined, laid out away from the hot code
Definition Portable.h:107
Augment
the way of increasing the flow
Definition MaxFlow.h:113
@ DINIC
a blocking flow on the levels of a visit in breadth
Definition MaxFlow.h:116
std::variant< NodeInserted, NodeDeleted, EdgeInserted< W >, EdgeDeleted< W >, WeightChanged< W > > GraphEvent
an event, whose kind is its index in the variant, the variant of GraphModifications< W >
Definition Observable.h:237
std::vector< GraphEvent< W > > coalesce(std::span< const GraphEvent< W > > E)
the net effect of the events , i.e., the nodes inserted, the edges in the order of their first event ...
Frontier
the structure of the prototype
Definition ShortestPaths.h:135
@ HEAP
a priority queue on a binary heap: Johnson
Definition ShortestPaths.h:137
constexpr int NIL
no node
Definition Visit.h:128
the engine of ADS, both solves by the augmentations how of FlowNetwork::maxFlow() [see MaxFlow....
Definition Reoptimization.h:752
static long long warm(FlowNetwork &net, int s, int t, Augment how, long long v)
the value of a maximum flow from the flow of net, of value
Definition Reoptimization.h:758
static long long scratch(FlowNetwork &net, int s, int t, Augment how)
the value of a maximum flow from the zero flow
Definition Reoptimization.h:754
static constexpr unsigned ALL
all of them
Definition Observable.h:250
what a recompute policy reads: the work is a count of elementary operations of the observer (nodes,...
Definition Modification.h:208
std::size_t events
the net events to apply
Definition Modification.h:209
std::size_t size
the size of the instance
Definition Modification.h:210
static constexpr int outcomeOf(int c)
the outcome of the cause
Definition Modification.h:199
static constexpr int SCRATCH_BY_EVENT
from scratch, Rebuilt,
Definition Modification.h:191
static constexpr int SCRATCH
from scratch
Definition Modification.h:184
static constexpr int CAUSES
the number of the causes
Definition Modification.h:196
anything may have changed: forget and recompute
Definition Modification.h:708