ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Assignment.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File Assignment.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
76/*--------------------------------------------------------------------------*/
77/*----------------------------- DEFINITIONS --------------------------------*/
78/*--------------------------------------------------------------------------*/
79
80#ifndef __ADS_Assignment
81 #define __ADS_Assignment
82/* self-identification: #endif constant */
83
84/*--------------------------------------------------------------------------*/
85/*------------------------------ INCLUDES ----------------------------------*/
86/*--------------------------------------------------------------------------*/
87
88#include "ADS/Contract.h"
89#include "ADS/Modification.h"
91
92#include <concepts>
93#include <cstddef>
94#include <type_traits>
95#include <utility>
96#include <variant>
97#include <vector>
98
99// the solve from scratch on the MinCostAssignment of LEMON with
100// ADS_WITH_LEMON, where LEMON has it (abLEMON), unless ADS_ASSIGNMENT_LEMON
101// is 0 (the benchmarks keep the code of ADS)
102#if defined( ADS_WITH_LEMON ) && ! defined( ADS_ASSIGNMENT_LEMON ) && \
103 __has_include( <lemon/bipartite_matching.h> )
104 #define ADS_ASSIGNMENT_LEMON 1
105#endif
106#if defined( ADS_ASSIGNMENT_LEMON ) && ADS_ASSIGNMENT_LEMON
107 #include <lemon/bipartite_matching.h>
108 #include <lemon/smart_graph.h>
109#endif
110
111/*--------------------------------------------------------------------------*/
112/*--------------------------- NAMESPACE ------------------------------------*/
113/*--------------------------------------------------------------------------*/
114
115namespace ADS
116{
117
118/*--------------------------------------------------------------------------*/
119/*--------------------------- CLASS Assignment -----------------------------*/
120/*--------------------------------------------------------------------------*/
121
133template< class E , class W >
135 std::default_initializable< E > &&
136 requires( E & x , int n , const std::vector< W > & C , bool all ,
137 const std::vector< std::pair< int , int > > & D ,
138 std::vector< int > & col , std::vector< W > & u ,
139 std::vector< W > & v )
140{
141 { x.solve( n , C , all , D , col , u , v ) } -> std::convertible_to< int >;
142 };
143
147template< class W = long long , ReoptPolicy P = RatioPolicy , class E = void >
148 requires std::is_arithmetic_v< W > &&
149 ( std::is_void_v< E > || AssignmentEngine< E , W > )
151{
152
153/*--------------------------------------------------------------------------*/
154 public:
155 using Weight = W;
156
160 Assignment( int n , std::vector< W > C );
161
163 [[nodiscard]] int n( void ) const { return( f_n ); }
164
167 [[nodiscard]] W cost( int i , int j ) const;
168
172 void setCost( int i , int j , W c );
173
176 void setRow( int i , const std::vector< W > & R );
177
180 void setColumn( int j , const std::vector< W > & K );
181
205 void solve( void );
206
210 [[nodiscard]] int column( int i );
211
214 [[nodiscard]] int row( int j );
215
218 [[nodiscard]] bool perfect( void );
219
221 [[nodiscard]] W total( void );
222
226 [[nodiscard]] W rowPotential( int i );
227
230 [[nodiscard]] W columnPotential( int j );
231
235 [[nodiscard]] int outcome( void ) const { return( f_outcome ); }
236
238 [[nodiscard]] long long count( int outcome ) const {
239 return( f_count[ outcome ] );
240 }
241
243 [[nodiscard]] int cause( void ) const { return( f_cause ); }
244
247 [[nodiscard]] long long byCause( int c ) const {
248 ADS_PRE( ( c > 0 ) && ( c < Reopt::CAUSES ) );
249 return( f_byCause[ c ] );
250 }
251
254 [[nodiscard]] P & policy( void ) { return( f_policy ); }
256 [[nodiscard]] const P & policy( void ) const { return( f_policy ); }
257
259 [[nodiscard]] long long augmentations( void ) const { return( f_aug ); }
260
261/*--------------------------------------------------------------------------*/
262 private:
264 [[nodiscard]] W reduced( int i , int j ) const {
265 return( f_C[ std::size_t( i ) * std::size_t( f_n ) + std::size_t( j ) ] -
266 f_u[ i ] - f_v[ j ] );
267 }
268
270 [[nodiscard]] bool finite( int i , int j ) const {
271 return( f_C[ std::size_t( i ) * std::size_t( f_n ) + std::size_t( j ) ] !=
272 infinity< W >() );
273 }
274
276 void freeRow( int i );
277
280 bool engineSolve( void );
281
282#if defined( ADS_ASSIGNMENT_LEMON ) && ADS_ASSIGNMENT_LEMON
285 bool lemonScratch( void );
286#endif
287
318 bool augment( int i0 );
319
321 [[nodiscard]] bool optimal( void ) const;
322
323 int f_n;
324 std::vector< W > f_C;
325 std::vector< W > f_u;
326 std::vector< W > f_v;
327 std::vector< int > f_col;
328 std::vector< int > f_row;
329 std::vector< char > f_dirtyR;
330 std::vector< char > f_dirtyC;
331 std::vector< int > f_DR;
332 std::vector< int > f_DC;
333 std::vector< std::pair< int , int > > f_cells;
335 std::vector< W > f_dist;
336 std::vector< int > f_pred;
337 std::vector< char > f_scanned;
338 std::vector< int > f_order;
339 bool f_ready = false;
340 bool f_pending = false;
341 int f_outcome = 0;
342 int f_cause = Reopt::SCRATCH_BY_EVENT;
343 long long f_count[ 3 ] = { 0 , 0 , 0 };
344 long long f_byCause[ Reopt::CAUSES ] = {};
345 P f_policy{};
348 std::conditional_t< std::is_void_v< E > , std::monostate , E > f_engine{};
349 long long f_aug = 0;
350
351 }; // end( class Assignment )
352
353/*--------------------------------------------------------------------------*/
354
355} // end( namespace ADS )
356
357/*--------------------------------------------------------------------------*/
358/*-------------------------- IMPLEMENTATION --------------------------------*/
359/*--------------------------------------------------------------------------*/
360
361#include "ADS/graph/impl/Assignment.tpp"
362
363/*--------------------------------------------------------------------------*/
364
365#endif /* Assignment.h included */
366
367/*--------------------------------------------------------------------------*/
368/*------------------------- End File Assignment.h --------------------------*/
369/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The typed modifications, beyond the book: the vocabulary by which a structure tells the observers att...
The shortest paths of chapter 11 from a node of an oriented GRAPH whose edges have a cost [see Grap...
W rowPotential(int i)
the potential of the row , a solution of the dual with those of the columns
void setCost(int i, int j, W c)
the cost of set to , worked at the next query
long long byCause(int c) const
the solves of each cause so far
Definition Assignment.h:247
void solve(void)
brings the assignment up to date
void setRow(int i, const std::vector< W > &R)
the costs of the row set to
int column(int i)
the column of the row , if none (no perfect matching of finite costs)
long long count(int outcome) const
the solves of each outcome so far
Definition Assignment.h:238
W Weight
the type of the costs
Definition Assignment.h:155
bool perfect(void)
whether every row has a column, i.e., a perfect matching of finite costs exists
int row(int j)
the row of the column , if none
Assignment(int n, std::vector< W > C)
the assignment of the costs , the row in , computed at the first query
void setColumn(int j, const std::vector< W > &K)
the costs of the column set to
int outcome(void) const
how the last solve() with changes, or with no assignment yet, did it [see Reopt of Modification....
Definition Assignment.h:235
W columnPotential(int j)
the potential of the column
long long augmentations(void) const
the augmentations so far
Definition Assignment.h:259
P & policy(void)
the recompute policy, whose abandon() decides the scratch of line 2 [see RatioPolicy]
Definition Assignment.h:254
int n(void) const
the number of rows, and of columns
Definition Assignment.h:163
int cause(void) const
why: the cause of the last outcome [see Reopt]
Definition Assignment.h:243
W cost(int i, int j) const
the cost of the row and the column
const P & policy(void) const
the recompute policy
Definition Assignment.h:256
W total(void)
the total cost of the pairs of the assignment
the engines of the solves of an Assignment, objects that it keeps: solve( n , C , all ,...
Definition Assignment.h:134
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
constexpr D infinity(void)
the infinite distance of the type D
Definition ShortestPaths.h:146
Definition ADT.h:96
static constexpr int SCRATCH_BY_EVENT
from scratch, Rebuilt,
Definition Modification.h:191
static constexpr int CAUSES
the number of the causes
Definition Modification.h:196