Flowlessly  0.1
Minimum cost maximum cost solver
 All Classes Functions Pages
graph.h
1 // The Flowlessly project
2 // Copyright (c) 2013-2016 Ionel Gog <ionel.gog@cl.cam.ac.uk>
3 
4 #ifndef FLOWLESSLY_GRAPH_H
5 #define FLOWLESSLY_GRAPH_H
6 
7 #include <gflags/gflags.h>
8 #include <stdint.h>
9 #include <cstdio>
10 #include <set>
11 #include <string>
12 #include <vector>
13 
14 #include "graphs/node.h"
15 #include "misc/statistics.h"
16 
17 namespace flowlessly {
18 
19 using namespace std; // NOLINT
20 
21 // Forward declarations.
22 class AdjacencyMapGraph;
23 
24 struct Arc {
25  Arc() {}
26 
27  Arc(uint32_t src_id, uint32_t dst_id, bool fwd, bool running,
28  int32_t capacity, int32_t cap_lower_bound, int64_t cst, Arc* rev_arc)
29  : src_node_id(src_id), dst_node_id(dst_id), is_alive(true), is_fwd(fwd),
30  is_running(running), residual_cap(capacity), min_flow(cap_lower_bound),
31  cost(cst), reverse_arc(rev_arc) {
32  }
33 
34  Arc(const Arc& arc) : src_node_id(arc.src_node_id),
35  dst_node_id(arc.dst_node_id), is_alive(arc.is_alive), is_fwd(arc.is_fwd),
36  is_running(arc.is_running), residual_cap(arc.residual_cap),
37  min_flow(arc.min_flow), cost(arc.cost), reverse_arc(arc.reverse_arc) {
38  }
39 
40  explicit Arc(Arc* old_arc)
41  : src_node_id(old_arc->src_node_id),
42  dst_node_id(old_arc->dst_node_id),
43  is_alive(old_arc->is_alive),
44  is_fwd(old_arc->is_fwd),
45  is_running(old_arc->is_running),
46  residual_cap(old_arc->residual_cap),
47  min_flow(old_arc->min_flow),
48  cost(old_arc->cost) {
49  }
50 
51  void CopyArc(const Arc& src_arc) {
52  src_node_id = src_arc.src_node_id;
53  dst_node_id = src_arc.dst_node_id;
54  is_alive = src_arc.is_alive;
55  is_fwd = src_arc.is_fwd;
56  is_running = src_arc.is_running;
57  residual_cap = src_arc.residual_cap;
58  min_flow = src_arc.min_flow;
59  cost = src_arc.cost;
60  // NOTE: It doesn't copy the pointer to the reverse_arc
61  // because the new arc will likely point to a different address.
62  }
63 
64  // NOTE: There can be multiple arcs between a pair of nodes. However,
65  // there can't be multiple arcs with the same cost. We use src_node_id,
66  // dst_node_id and cost to uniquely identify arcs.
67  uint32_t src_node_id;
68  uint32_t dst_node_id;
69  bool is_alive;
70  bool is_fwd;
71  bool is_running;
72  int32_t residual_cap;
73  uint32_t min_flow; // lower flow bound.
74  int64_t cost;
75  // Reduced cost in all algorithms is:
76  // cost - potential[src_node_id] + potential[dst_node_id].
77  Arc *reverse_arc;
78 };
79 
80 class Graph {
81  public:
82  Graph() : max_node_id_(0), num_arcs_(0), sink_node_(0),
83  potentials_(10000000, 0) {}
84  Graph(uint32_t max_node_id, uint32_t num_arcs, uint32_t sink_node,
85  const set<uint32_t>& source_nodes) : max_node_id_(max_node_id),
86  num_arcs_(num_arcs), sink_node_(sink_node), source_nodes_(source_nodes),
87  potentials_(10000000, 0) {
88  }
89  virtual ~Graph() {}
90 
91  virtual Arc* AddArc(uint32_t src_node_id, uint32_t dst_node_id,
92  uint32_t min_flow, int32_t capacity,
93  int64_t cost, int32_t type) = 0;
94 
102  virtual uint32_t AddNode(int32_t supply, int64_t potential,
103  NodeType type, bool first_scheduling_iteration) = 0;
104  virtual void AddNode(uint32_t node_id, int32_t supply, int64_t potential,
105  NodeType type, bool first_scheduling_iteration) = 0;
106 
116  virtual void ChangeArc(Arc* arc, uint32_t new_min_flow, int32_t new_capacity,
117  int64_t new_cost, int32_t new_type) = 0;
118 
131  virtual void ChangeArc(uint32_t src_node_id, uint32_t dst_node_id,
132  uint32_t new_min_flow, int32_t new_capacity,
133  int64_t new_cost, int32_t new_type, bool is_multi_arc,
134  int64_t old_cost) = 0;
135 
136  virtual void GetMachinePUs(uint32_t machine_node_id,
137  set<uint32_t>* pu_ids) = 0;
138 
142  virtual Arc* GetRandomArc(uint32_t node_id) = 0;
143 
147  virtual int64_t GetTotalCost() = 0;
148 
155  virtual bool IsEpsOptimal(int64_t eps) = 0;
156 
163  virtual bool IsFeasible() = 0;
164 
168  virtual bool IsInTopologicalOrder(const vector<uint32_t>& node_ids) = 0;
169 
170  virtual void InitializeGraph() = 0;
171 
172  void ReadGraph(FILE* graph_file, bool first_scheduling_iteration,
173  bool* end_of_scheduling);
174 
182  virtual void RemoveArc(Arc* arc) = 0;
183 
190  virtual void RemoveNode(uint32_t node_id) = 0;
191 
196  virtual void ScaleDownCosts(int64_t scale_down) = 0;
197 
203  virtual void ScaleUpCosts(int64_t scale_up) = 0;
204 
209  virtual void WriteAssignments(FILE* out_file) = 0;
210 
214  void WriteAssignments(const string& out_file_name);
215 
216 
220  virtual void WriteFlowGraph(FILE* out_graph_file) = 0;
221 
226  void WriteFlowGraph(const string& out_graph_file);
227 
232  void WriteGraph(const string& out_graph_file);
233 
239  virtual void WriteGraph(FILE* out_graph_file) = 0;
240 
241  inline uint32_t get_max_node_id() const {
242  return max_node_id_;
243  }
244 
245  inline uint32_t get_sink_node() {
246  return sink_node_;
247  }
248 
249  inline set<uint32_t>& get_source_nodes() {
250  return source_nodes_;
251  }
252  inline vector<int64_t>& get_potentials() {
253  return potentials_;
254  }
255 
256  protected:
257  uint32_t max_node_id_;
258  uint32_t num_arcs_;
259  uint32_t sink_node_;
260  set<uint32_t> source_nodes_;
261  vector<int64_t> potentials_;
262 
263  private:
264  void ReadAddNode(char* new_node_line, FILE* graph_file,
265  bool first_scheduling_iteration);
266 };
267 
268 } // namespace flowlessly
269 
270 #endif // FLOWLESSLY_GRAPH_H
Definition: graph.h:80
Definition: graph.h:24