Aleph-w  1.9
General library for algorithms and data structures
tpl_net_sup_dem.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 #line 620 "reducciones.nw"
28 # ifndef TPL_NET_SUP_DEM_H
29 # define TPL_NET_SUP_DEM_H
30 
31 # include <tpl_netgraph.H>
32 
33 namespace Aleph {
34 
35 #line 586 "reducciones.nw"
36 
40  template <typename Node_Info, typename F_Type = long>
41 class Net_Sup_Dem_Node : public Net_Node<Node_Info, F_Type>
42 {
43 public:
45  typedef F_Type Flow_Type;
46 
47  Flow_Type supply_flow;
48  Flow_Type & get_supply_flow() { return supply_flow; }
50 
51  Net_Sup_Dem_Node() : supply_flow(0) { /* empty */ }
52 
55  Net_Sup_Dem_Node(const Node_Info & node_info)
56  : Net_Node<Node_Info, F_Type>(node_info), supply_flow(0) { /* empty */ }
57 
61  : Net_Node<Node_Info, F_Type>(*node), supply_flow(node->supply_flow) { /* empty */ }
62 };
63 
64 #line 629 "reducciones.nw"
65 
81  template <class NodeT = Net_Sup_Dem_Node<Empty_Class, double>,
82  class ArcT = Net_Arc<Empty_Class, double> >
83 class Net_Sup_Dem_Graph : public Net_Graph<NodeT, ArcT>
84 {
85 public:
89  typedef ArcT Arc;
91  typedef NodeT Node;
93  typedef typename Arc::Flow_Type Flow_Type;
95  typedef typename Node::Node_Type Node_Type;
97  typedef typename Arc::Arc_Type Arc_Type;
98 
99 
100 #line 675 "reducciones.nw"
101 
111 Node * insert_node(const Node_Type & node_info, const Flow_Type & supply = 0)
112 {
113  Node * p = Net_Class::insert_node(node_info);
114  p->supply_flow = supply;
115  return p;
116 }
126 Node * insert_node(const Flow_Type & supply = 0)
127 {
128  return insert_node(Node_Type(), supply);
129 }
130 #line 719 "reducciones.nw"
131 private:
132 
133  Node * super_source;
134  Node * super_sink;
135 
136 public:
137  Net_Sup_Dem_Graph() : super_source(nullptr), super_sink(nullptr) { /* empty */ }
139  bool exist_aux_net() const
140  {
141  return super_source != nullptr or super_sink != nullptr;
142  }
143 
144 #line 739 "reducciones.nw"
145 typedef Net_Sup_Dem_Graph Aux_Net;
147 
170 {
171  if (exist_aux_net())
172  throw std::domain_error("Auxiliar net is already computed");
173  if (this->residual_net)
174  throw std::domain_error("Residual net has been already computed");
175 
176  super_source = insert_node(); // fuente auxiliar
177  super_sink = insert_node(); // sumidero auxiliar
178  // recorrer nodos y atar arcos a fuente o sumidero según signo provisión
179  for (Node_Iterator<Net_Sup_Dem_Graph> it(*this); it.has_curr(); it.next_ne())
180  {
181  Node * p = it.get_curr_ne();
182  if (p->supply_flow > 0) // ¿nodo proveedor?
183  {
184  if (p->out_cap < p->supply_flow)
185  throw std::range_error(gnu::autosprintf("Supply flow in node at %p is "
186  "greater than out capacity", p));
187  insert_arc(super_source, p, p->supply_flow);
188  }
189  else if (p->supply_flow < 0) // ¿nodo demandante?
190  {
191  if (p->in_cap < -p->supply_flow)
192  throw std::range_error(gnu::autosprintf("Supply flow in node at %p is "
193  "smaller than in capacity", p));
194  insert_arc(p, super_sink, -p->supply_flow);
195  }
196  }
197  if (this->get_out_degree(super_source) == 0)
198  {
199  remove_node(super_source);
200  super_source = nullptr;
201  }
202  if (this->get_in_degree(super_sink) == 0)
203  {
204  remove_node(super_sink);
205  super_sink = nullptr;
206  }
207  return this;
208 }
209 #line 825 "reducciones.nw"
210  Net_Sup_Dem_Graph * get_aux_net() { return exist_aux_net() ? this : nullptr; }
211 #line 833 "reducciones.nw"
212 
219 bool is_feasible() const
220 {
221  if (not exist_aux_net())
222  throw std::domain_error("Auxiliar net has not been computed");
223  for (Node_Iterator<Net_Sup_Dem_Graph> it(*this); it.has_curr(); it.next_ne())
224  {
225  Node * p = it.get_curr_ne();
226  Flow_Type & supply_flow = p->supply_flow;
227  if (supply_flow == 0)
228  continue;
229  if (supply_flow > 0 and p->out_flow < supply_flow)
230  return false;
231  else if (supply_flow < 0 and p->in_flow < -supply_flow)
232  return true;
233  }
234  return true;
235 }
236 
252 void non_feasible_nodes(DynDlist<Node *> & supply_list, DynDlist<Node *> & demand_list)
253 {
254  for (Node_Iterator<Net_Sup_Dem_Graph> it(*this); it.has_curr(); it.next_ne())
255  {
256  Node * p = it.get_curr_ne();
257  Flow_Type & supply_flow = p->supply_flow;
258  if (supply_flow == 0)
259  continue;
260  if (supply_flow > 0 and p->out_flow < supply_flow)
261  supply_list.append(p);
262  else if (supply_flow < 0 and p->in_flow < -supply_flow)
263  demand_list.append(p);
264  }
265 }
266 #line 895 "reducciones.nw"
267 
280 void set_supply_flow(Node * p, const Flow_Type & supply)
281 {
282  if (supply > 0 and p->out_cap < supply)
283  throw std::range_error(gnu::autosprintf("Supply flow in node at %p is "
284  "greater than out capacity", p));
285  else if (supply < 0 and p->in_cap < -supply)
286  throw std::range_error(gnu::autosprintf("Supply flow in node at %p is "
287  "smaller than in capacity", p));
288  p->supply_flow = supply;
289 }
290 #line 922 "reducciones.nw"
291 void free_aux_net()
293 {
294  if (not exist_aux_net())
295  throw std::domain_error("Auxiliar net has not been computed");
296 
297  if (this->residual_net)
298  {
299  this->unmake_residual_net();
300  this->unmake_super_nodes();
301  }
302  if (super_source != nullptr)
303  {
304  remove_node(super_source);
305  super_source = nullptr;
306  }
307  if (super_sink != nullptr)
308  {
309  remove_node(super_sink);
310  super_sink = nullptr;
311  }
312 }
313 #line 664 "reducciones.nw"
314 };
315 
316 } // end namespace Aleph
317 
318 # endif // TPL_NET_SUP_DEM_H
Definition: tpl_net_sup_dem.H:83
F_Type Flow_Type
Tipo que representa el flujo.
Definition: tpl_net_sup_dem.H:45
NodeT Node
Tipo de node.
Definition: tpl_net_sup_dem.H:91
Net_Sup_Dem_Node(Net_Sup_Dem_Node *node)
Definition: tpl_net_sup_dem.H:60
Definition: tpl_dynDlist.H:51
Definition: tpl_net_sup_dem.H:41
Definition: tpl_net.H:216
void non_feasible_nodes(DynDlist< Node *> &supply_list, DynDlist< Node *> &demand_list)
Definition: tpl_net_sup_dem.H:252
Node * insert_node(const Node_Type &node_info, const Flow_Type &supply=0)
Definition: tpl_net_sup_dem.H:111
Node::Node_Type Node_Type
Tipo de atributo que almacena un nodo.
Definition: tpl_net_sup_dem.H:95
Definition: tpl_agraph.H:43
Definition: ah-comb.H:35
Node * insert_node(const Flow_Type &supply=0)
Definition: tpl_net_sup_dem.H:126
NodeInfo Node_Type
The node.
Definition: graph-dry.H:233
Net_Sup_Dem_Graph * compute_aux_net()
Definition: tpl_net_sup_dem.H:169
Net_Sup_Dem_Node(const Node_Info &node_info)
Definition: tpl_net_sup_dem.H:55
void set_supply_flow(Node *p, const Flow_Type &supply)
Definition: tpl_net_sup_dem.H:280
Net_Graph< NodeT, ArcT > Net_Class
La clase de red tradicional.
Definition: tpl_net_sup_dem.H:87
bool is_feasible() const
Definition: tpl_net_sup_dem.H:219
Flow_Type & get_supply_flow()
Retorna el valor de provisión del nodo.
Definition: tpl_net_sup_dem.H:49
ArcT Arc
Tipo de arco.
Definition: tpl_net_sup_dem.H:89
Arc::Flow_Type Flow_Type
Tipo que representa la capacidad y el flujo.
Definition: tpl_net_sup_dem.H:93
T & append(const T &item)
Definition: tpl_dynDlist.H:149
bool exist_aux_net() const
Retorna true si la red auxiliar ya ha sido creada.
Definition: tpl_net_sup_dem.H:139
Arc::Arc_Type Arc_Type
Tipo de atributo que almacena un arco.
Definition: tpl_net_sup_dem.H:97

Leandro Rabindranath León