221using namespace Aleph;
225 cout <<
"Usage: " <<
prog <<
" [--compare] [--network <logistics|assignment|transportation|all>] [--help]\n";
226 cout <<
"\nIf no flags are given, all demos are executed.\n";
231 for (
int i = 1; i <
argc; ++i)
232 if (std::strcmp(
argv[i], flag) == 0)
239 for (
int i = 1; i + 1 <
argc; ++i)
240 if (std::strcmp(
argv[i],
opt) == 0)
275 cout <<
"\n" << string(60,
'=') <<
endl;
276 cout <<
"Comparison: Cycle Canceling vs Network Simplex (Logistics Network)" <<
endl;
284 cout <<
"Cycle canceling:\n";
285 cout <<
" Max flow: " << flow <<
"\n";
287 cout <<
" Cycles cancelled: " <<
get<0>(r) <<
"\n";
295 cout <<
"Network simplex:\n";
296 cout <<
" Max flow: " << flow <<
"\n";
298 cout <<
" Pivots: " << pivots <<
"\n";
315 for (
auto it = net.
get_arc_it(); it.has_curr(); it.next())
317 auto* arc = it.get_curr();
325 total_flow += arc->flow;
327 cout <<
" " <<
setw(8) << left << src->get_info()
328 <<
" -> " <<
setw(8) << left << tgt->get_info()
329 << right <<
" : " <<
setw(4) << arc->flow
330 <<
" / " <<
setw(4) << arc->cap
331 <<
" @ $" << arc->cost <<
endl;
334 cout <<
"\nTotal flow: " << total_flow <<
endl;
343 cout <<
"\n" << string(60,
'=') <<
endl;
344 cout <<
"Example 2: Assignment Problem" <<
endl;
347 cout <<
"\nProblem: Assign 3 workers to 3 jobs minimizing total cost." <<
endl;
350 cout <<
"Worker1: $9 $2 $7" <<
endl;
351 cout <<
"Worker2: $6 $4 $3" <<
endl;
352 cout <<
"Worker3: $5 $8 $1" <<
endl;
362 cout <<
"\nOptimal assignment:" <<
endl;
366 for (
auto it = result.assignments.get_it(); it.has_curr(); it.next())
368 auto [
w, j] = it.get_curr();
369 cout <<
" Worker" << (
w+1) <<
" -> Job" << (j+1)
370 <<
" (cost = $" <<
costs[
w][j] <<
")" <<
endl;
372 cout <<
"\nMinimum total cost: $" << result.total_cost <<
endl;
376 cout <<
"No feasible assignment found." <<
endl;
385 cout <<
"\n" << string(60,
'=') <<
endl;
386 cout <<
"Example 3: Transportation Problem" <<
endl;
389 cout <<
"\nProblem: Ship goods from 2 warehouses to 3 stores." <<
endl;
391 vector<double>
supply = {30, 20};
392 vector<double> demand = {15, 20, 15};
402 cout <<
"\nOptimal shipments:" <<
endl;
403 for (
size_t i = 0; i < result.shipments.size(); ++i)
405 cout <<
" Warehouse" << (i+1) <<
": ";
406 for (
size_t j = 0; j < result.shipments[i].size(); ++j)
407 cout <<
setw(6) << result.shipments[i][j] <<
" ";
410 cout <<
"\nMinimum cost: $" << result.total_cost <<
endl;
414 cout <<
"No feasible solution found." <<
endl;
423 cout <<
"\n" << string(60,
'=') <<
endl;
424 cout <<
"Example 1: Min-Cost Max-Flow" <<
endl;
431 cout <<
"\n--- Computing Min-Cost Max-Flow ---" <<
endl;
440 cout <<
"\n*** Results ***" <<
endl;
441 cout <<
"Maximum flow: " << flow <<
endl;
448 cout <<
"=== Minimum Cost Maximum Flow ===" <<
endl;
449 cout <<
"Optimize flow networks with costs per unit\n" <<
endl;
467 if (
argc == 1 ||
sel ==
"all" ||
sel ==
"logistics")
469 if (
argc == 1 ||
sel ==
"all" ||
sel ==
"assignment")
471 if (
argc == 1 ||
sel ==
"all" ||
sel ==
"transportation")
474 if (!(
argc == 1 ||
sel ==
"all" ||
sel ==
"logistics" ||
sel ==
"assignment" ||
sel ==
"transportation"))
476 cout <<
"Unknown --network value: " <<
sel <<
"\n";
481 cout <<
"\n" << string(60,
'=') <<
endl;
486Min-Cost Max-Flow Problem:
488 Given: Network with capacities and per-unit costs
489 Find: Flow maximizing total flow at minimum cost
492 - Cycle Canceling: Simple, cancel negative-cost cycles
493 - Network Simplex: Efficient, maintains spanning tree
496 - Transportation: Ship goods at minimum cost
497 - Assignment: Match entities minimizing total cost
498 - Supply Chain: Optimize logistics networks
WeightedDigraph::Node Node
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
constexpr size_t get_num_arcs() const noexcept
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
CostNet build_simple_network()
Build a simple logistics network.
void demo_mincost_maxflow()
Demonstrate min-cost max-flow.
void demo_assignment_problem()
Demonstrate the assignment problem.
void print_cost_network(CostNet &net, const string &title)
Print network with flows and costs.
static void usage(const char *prog)
void demo_transportation_problem()
Demonstrate the transportation problem.
static void demo_compare_algorithms_on_logistics()
static bool has_flag(int argc, char *argv[], const char *flag)
static const char * get_opt_value(int argc, char *argv[], const char *opt)
Main namespace for Aleph-w library functions.
std::tuple< size_t, double > max_flow_min_cost_by_cycle_canceling(Net &net, double it_factor=0.4, size_t step=10)
Compute maximum flow at minimum cost using cycle canceling.
size_t max_flow_min_cost_by_network_simplex(Net &net)
Compute maximum flow at minimum cost using Network Simplex.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Arc type for maximum flow minimum cost networks.
Capacitated flow network with costs associated to arcs.
Flow_Type flow_cost() const
Compute the total cost of flow circulating through the network.
virtual Arc * insert_arc(Node *src_node, Node *tgt_node, const Flow_Type &cap, const Flow_Type &__cost)
Create and insert an arc in a flow network with costs.
Flow_Type get_out_flow(Node *node) const noexcept
Return total outgoing flow of node.
Node * insert_node(const Node_Type &node_info)
Insert a new node by copying node_info.
bool is_source(Node *node) const noexcept
Return true if node is a source.
Node * get_source() const
Return an arbitrary source node.
Advanced minimum cost flow algorithms.
Maximum flow minimum cost network algorithms.