27 # ifndef TPL_MAXFLOW_MINCOST_H 28 # define TPL_MAXFLOW_MINCOST_H 30 # include <Bellman_Ford.H> 32 # include <generate_graph.H> 34 using namespace Aleph;
38 template <
typename Node_Info = Empty_Class>
55 template <
typename Arc_Info = Empty_Class,
typename F_Type =
double>
63 using Flow_Type = F_Type;
75 noexcept(std::is_nothrow_copy_assignable<Arc_Info>::value)
103 template <
class NodeT = Net_Cost_Node<Empty_Class>,
104 class ArcT = Net_Cost_Arc<Empty_Class,
double>>
111 zip(this->
arcs(), net.
arcs()).for_each([] (
const auto & p)
114 auto asrc = p.second;
115 atgt->cost = asrc->cost;
137 using Node_Type =
typename Node::Node_Type;
140 using Arc_Type =
typename Arc::Arc_Type;
161 Arc * a = Net::insert_arc(src_node, tgt_node, cap, 0, Arc_Type());
166 template <
typename ... Args>
167 Arc * emplace_arc(Node * src_node, Node *
tgt_node,
171 auto a = Net::insert_arc(src_node, tgt_node, cap, cost, Arc_Type(args...));
179 Arc * a = Net::insert_arc(src_node, tgt_node, Arc_Type());
199 Arc * a = it.get_curr_ne();
200 total += a->flow_cost();
209 Flow_Type cap_sum = 0, flow_sum = 0, cost_sum = 0;
212 Arc * a = it.get_curr_ne();
217 return make_tuple(cap_sum, flow_sum, cost_sum);
223 Flow_Type cap_sum = 0, flow_sum = 0, cost_sum = 0;
226 Arc * a = it.get_curr_ne();
231 return make_tuple(cap_sum, flow_sum, cost_sum);
240 Res_Filt(
typename Net::Node *) noexcept {}
242 Res_Filt() noexcept {}
244 bool operator () (
typename Net::Arc * a)
const noexcept
246 return a->cap > a->flow;
263 static void set_zero(
typename Net::Arc * a) noexcept
265 a->cap = numeric_limits<Distance_Type>::max();
272 template <
typename Ftype>
273 struct Res_Arc :
public Net_Cost_Arc<Empty_Class, Ftype>
278 bool is_residual =
false;
279 Res_Arc * img =
nullptr;
283 template <
typename Ftype>
290 template <
class Res_Net>
typename Res_Net::Arc *
291 create_residual_arc(Res_Net & residual_net,
292 typename Res_Net::Node * src,
293 typename Res_Net::Node * tgt,
294 const typename Res_Net::Flow_Type
cap,
295 const typename Res_Net::Flow_Type
flow,
296 const typename Res_Net::Flow_Type
cost)
298 assert(flow <= cap and cost >= 0);
300 auto arc = residual_net.insert_arc(src, tgt, cap, cost);
301 auto rarc = residual_net.insert_arc(tgt, src, cap, -cost);
303 arc->is_residual =
false;
307 rarc->is_residual =
true;
309 rarc->flow = arc->cap - arc->flow;
311 assert(arc->cap == cap and arc->flow == flow and arc->cost == cost);
312 assert(rarc->cap == cap and rarc->flow == cap - flow and rarc->cost == -cost);
323 build_residual_net(
const Net & net,
328 throw std::domain_error(
"Network is not single source and single sink");
333 for (
typename Net::Node_Iterator it(net); it.has_curr(); it.next_ne())
335 auto p = it.get_curr_ne();
337 map_nodes<Net, Rnet>(p, q);
343 auto ga = it.get_curr_ne();
344 auto gsrc = (
typename Net::Node*) ga->src_node;
345 auto gtgt = (
typename Net::Node*) ga->tgt_node;
347 auto rsrc = mapped_node<Net, Rnet>(gsrc);
348 auto rtgt = mapped_node<Net, Rnet>(gtgt);
350 create_residual_arc(rnet, rsrc, rtgt, ga->cap, ga->flow, ga->cost);
354 assert(check_residual_net(rnet));
359 template <
class Res_Net>
360 bool check_residual_net(
const Res_Net & net)
363 net.all_arcs([] (
typename Res_Net::Arc * a) {
return a->img->img == a; });
366 template <
class Res_Net>
367 void cancel_cycle(
const Res_Net &,
const Path<Res_Net> & path)
370 using Ftype =
typename Res_Net::Flow_Type;
373 Ftype slack = std::numeric_limits<Ftype>::max();
376 assert(a->cap -a->flow > 0);
377 slack = std::min(slack, a->cap - a->flow);
384 assert(img->img == a);
385 assert(a->cap == img->cap);
394 template <
class Net>
static 398 arcs.
for_each([] (std::pair<void*, void*> p)
400 auto a = (
typename Rnet::Arc *) p.first;
401 auto ra = (
typename Net::Arc *) p.second;
437 tuple<size_t, double>
439 double it_factor = 0.4,
442 Max_Flow_Algo <Net> () (net);
450 typename Rnet::Node * source = build_residual_net(net, rnet, arcs_map);
456 tuple<Path<Rnet>,
size_t> cycle =
457 BF(rnet).search_negative_cycle(source, it_factor, step);
462 it_factor = ((double) get<1>(cycle))/net.vsize();
463 cancel_cycle(rnet, get<0>(cycle));
466 tuple<Path<Rnet>,
size_t> cycle =
467 BF(rnet).search_negative_cycle(it_factor, step);
470 cancel_cycle(rnet, get<0>(cycle));
476 return make_tuple(count, it_factor);
482 struct Max_Flow_Min_Cost_By_Cycle_Canceling
484 tuple<size_t, double> operator () (Net & net,
485 double it_factor = 0.4,
488 return max_flow_min_cost_by_cycle_canceling<Net, Max_Flow_Algo>
489 (net, it_factor, step);
494 void print(
const Net & net, ostream & out)
497 net.nodes().for_each([&i] (
typename Net::Node* p) {
NODE_COUNTER(p) = i++; });
501 void operator () (
const Net &,
typename Net::Node * p, ostream & out)
503 out <<
"label = \"(" << p->get_info() <<
"," <<
NODE_COUNTER(p) <<
")\"";
509 void operator () (
const Net &,
typename Net::Arc * a, ostream & out)
511 out <<
"label = \"" << a->flow <<
"/" << a->cap <<
"/" << a->cost <<
"\"";
526 void operator () (
const Net &,
typename Net::Node * p, ostream & out)
528 out <<
"label = \"(" << p->get_info() <<
"," <<
NODE_COUNTER(p) <<
")\"";
534 void operator () (
const Net &,
typename Net::Arc * a, ostream & out)
536 out <<
"label = \"" << a->flow <<
"/" << a->cap <<
"/" << a->cost <<
"\"";
538 out <<
" color = red";
550 typename Net::Flow_Type potential;
554 template <
class Net>
static inline 555 void init_simplex_info(Net & )
563 using Feasible_Tree = tuple<DynList<typename Net::Arc*>,
565 DynList<typename Net::Arc*>>;
568 template <
class Net>
static inline 569 Feasible_Tree<Net> build_feasible_spanning_tree(
const Net & net)
571 using Arc =
typename Net::Arc;
573 for (
typename Net::Arc_Iterator it(net); it.has_curr(); it.next_ne())
575 auto a = it.get_curr_ne();
578 else if (a->flow == a->cap)
583 return make_tuple(std::move(empty), std::move(full), std::move(partial));
587 template <
class Net>
static inline 588 DynList<typename Net::Arc*> get_partial_arcs(
const Net & net)
590 DynList<typename Net::Arc*> ret;
591 for (
typename Net::Arc_Iterator it(net); it.has_curr(); it.next_ne())
593 auto a = it.get_curr_ne();
594 if (a->flow > 0 and a->flow < a->cap)
601 # define POTENTIAL(p) (NODE_COUNTER(p)) 605 typename Net::Flow_Type rcost(
typename Net::Node * src,
606 typename Net::Node * tgt)
610 auto a = it.get_curr_ne();
611 if (a->tgt_node == tgt)
612 return a->cost - (POTENTIAL(src) - POTENTIAL(tgt));
614 AH_ERROR(
"Arc not found");
619 typename Net::Flow_Type rcost(
typename Net::Arc * a)
621 using Node =
typename Net::Node*;
623 (POTENTIAL((Node*)a->src_node) - POTENTIAL((Node*)a->tgt_node));
633 # endif // TPL_MAXFLOW_MINCOST_H Flow_Type flow
valor de flujo
Definition: tpl_net.H:92
Flow_Type & get_cost(Arc *a) noexcept
Retorna una referencia modificable al coste de un arco.
Definition: tpl_netcost.H:143
Definition: tpl_graph.H:1225
bool is_single_source() const noexcept
Retorna true si la red es de un solo fuente.
Definition: tpl_net.H:326
Definition: tpl_agraph.H:301
virtual Arc * insert_arc(Node *src_node, Node *tgt_node, const Flow_Type &cap, const Flow_Type &cost)
Definition: tpl_netcost.H:158
Flow_Type cap
valor de capacidad
Definition: tpl_net.H:89
Pair * insert(const Key &key, const Data &data)
Definition: tpl_dynMapTree.H:112
#define NODE_COOKIE(p)
Definition: aleph-graph.H:333
Container< __Graph_Node *> nodes() const
Definition: graph-dry.H:2091
void for_each(Operation &operation) noexcept(noexcept(operation))
Definition: htlist.H:589
Definition: tpl_dynMapTree.H:62
Definition: tpl_net.H:216
Flow_Type flow_cost() const noexcept
Retorna el coste del flujo circulante.
Definition: tpl_netcost.H:69
auto in_pars(Node *p) noexcept
retorna una tripleta de capacidad, flujo y costo saliente
Definition: tpl_netcost.H:221
void * tgt_node
Please don't use.
Definition: graph-dry.H:280
#define NODE_COUNTER(p)
Definition: aleph-graph.H:311
Definition: tpl_netcost.H:56
Container< T > arcs_map(GT &g, std::function< T(typename GT::Arc *)> transformation, SA sa=SA())
Definition: tpl_graph.H:1459
const Flow_Type & flow_cost(Arc *a) const noexcept
Retorna una referencia al coste de un arco:
Definition: tpl_netcost.H:146
NodeT Node
Tipo de node.
Definition: tpl_net.H:230
Definition: generate_graph.H:630
Definition: tpl_graph.H:53
Definition: tpl_agraph.H:43
DynList< typename GT::Arc * > arcs(typename GT::Node *p, SA sa=SA())
Definition: tpl_graph.H:1893
typename Arc::Flow_Type Flow_Type
Tipo que representa la capacidad y el flujo.
Definition: tpl_net.H:233
ArcT Arc
Tipo de arco.
Definition: tpl_net.H:227
Definition: Bellman_Ford.H:53
Flow_Type cost
coste por unidad de flujo (negativo si el arco es residual)
Definition: tpl_netcost.H:66
Container< Arc * > arcs() const
Definition: graph-dry.H:2109
Definition: tpl_graph.H:1796
Node * insert_node(const Node_Type &node_info)
Definition: tpl_net.H:481
Definition: tpl_net.H:1530
bool is_empty() const noexcept
Return true if this (as header node) is empty.
Definition: dlink.H:192
void for_each_arc(Operation op=Operation()) const noexcept(noexcept(op))
Definition: tpl_graph.H:3265
bool is_single_sink() const noexcept
Retorna true si la red es de un solo sumidero.
Definition: tpl_net.H:329
bool is_empty() const noexcept
Return true if the path is empty.
Definition: tpl_graph.H:2758
Flow_Type flow_cost() const noexcept
Usado por algorÃtmica interna. NO UTILIZAR.
Definition: tpl_netcost.H:194
typename GT::Out_Iterator __Out_Iterator
Definition: tpl_graph.H:1780
virtual Arc * insert_arc(Node *src_node, Node *tgt_node)
Usado por algorÃtmica interna. NO UTILIZAR.
Definition: tpl_netcost.H:177
tuple< size_t, double > max_flow_min_cost_by_cycle_canceling(Net &net, double it_factor=0.4, size_t step=10)
Definition: tpl_netcost.H:438
Definition: tpl_netcost.H:105
bool is_cycle() const noexcept
Return true if this is a cycle.
Definition: tpl_graph.H:3098
typename GT::In_Iterator __In_Iterator
Definition: tpl_graph.H:1788
Node * get_source() const
Retorna un nodo fuente de la red.
Definition: tpl_net.H:466
T & append(const T &item)
Definition: htlist.H:1471
NodeT Node
Tipo de node.
Definition: tpl_netcost.H:131
auto out_pars(Node *p) noexcept
retorna una tripleta de capacidad, flujo y costo saliente
Definition: tpl_netcost.H:207