-
Notifications
You must be signed in to change notification settings - Fork 1
/
MinCostFlow_Demo.cpp
executable file
·275 lines (182 loc) · 6.92 KB
/
MinCostFlow_Demo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <map>
#include <lemon/smart_graph.h>
#include <lemon/network_simplex.h>
#include <lemon/cost_scaling.h>
#include <chrono>
using namespace lemon;
using namespace std;
using namespace std::chrono;
using Weight = long;
using Capacity = long;
using Resource = long;
using Graph = SmartDigraph;
using Node = Graph::Node;
using Arc = Graph::Arc;
template<typename ValueType>
using ArcMap = SmartDigraph::ArcMap<ValueType>;
template<typename ValueType>
using NodeMap = SmartDigraph::NodeMap<ValueType>;
using NS = NetworkSimplex<SmartDigraph, Capacity, Weight>;
using CS = CostScaling<SmartDigraph, Capacity, Weight>;
int nodeNum = 5;
int arcNum = 9;
void readNodeTable(int nodeID[5], long nodeSD[5]);
void readArcTable(int arcFrom[9], int arcTo[9], int arcMin[9], long arcMax[9], long arcWeight[9]);
//template<size_t N>
int main()
{
//------------------------------ Reading Data ---------------------------------------
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//----------- Read Node Table -----------------
int nodeID[nodeNum];
long nodeSD[nodeNum];
readNodeTable(nodeID, nodeSD);
//---------- Check Ingested Node Table --------
// for(int i = 0; i < nodeNum; i++)
// {
// cout << nodeID[i] << " " << nodeSD[i] << endl;
// }
//----------------------------------------------
//---------------------------------------------
//----------- Read Arc Table -----------------
int arcFrom[arcNum];
int arcTo[arcNum];
int arcMin[arcNum];
long arcMax[arcNum];
long arcWeight[arcNum];
readArcTable(arcFrom, arcTo, arcMin, arcMax, arcWeight);
// //---------- Check Ingested Arc Table --------
// for(int i = 0; i < arcNum; i++)
// {
// cout << arcFrom[i] << " " << arcTo[i] << " " << arcMin[i] << " " << arcMax[i] << " " << arcWeight[i] << endl;
// }
// //----------------------------------------------
//---------------------------------------------
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//-----------------------------------------------------------------------------------
//New Line Added
//------------------------------ Consstructing Network Graph --------------------------
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Graph g;
//------------- Constructing Nodes -------------
Node nodes[nodeNum];
NodeMap<Resource> supplyDemands(g);
map<int, Node> nodeIndexMap;
for(int i = 0 ; i < nodeNum ; i++)
{
nodes[i] = g.addNode();
nodeIndexMap[nodeID[i]] = nodes[i];
supplyDemands[nodes[i]] = nodeSD[i];
}
//------------------------------------------
//------------- Constructing Arcs/Edges -------------
Arc arcs[arcNum];
ArcMap<Weight> weights(g);
ArcMap<Resource> lowerBounds(g);
ArcMap<Capacity> capacities(g);
for(int i = 0 ; i < arcNum ; i++)
{
arcs[i] = g.addArc(nodeIndexMap[arcFrom[i]] , nodeIndexMap[arcTo[i]]);
weights[arcs[i]] = arcWeight[i];
lowerBounds[arcs[i]] = arcMin[i];
capacities[arcs[i]] = arcMax[i];
}
//------------------------------------------
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//-----------------------------------------------------------------------------------
//-------------------------- Running Optimization Solver ----------------------------
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
NS solver(g);
solver.lowerMap(lowerBounds).upperMap(capacities).costMap(weights).supplyMap(supplyDemands);
ArcMap<Capacity> flows(g);
NS::ProblemType status = solver.run();
switch (status)
{
case NS::INFEASIBLE:
cerr << "insufficient flow" << endl;
break;
case NS::OPTIMAL:
solver.flowMap(flows);
cout << "Successful Execution !" << endl;
cout << "\n------- Suggested Flows ---------" << endl;
cout << "\n";
for(int i = 0 ; i < arcNum ; i++)
{
if(flows[arcs[i]] != 0)
{
cout << arcFrom[i] << " ----> "<< arcTo[i] << " : " << flows[arcs[i]] << " ; Cost : " << flows[arcs[i]] * weights[arcs[i]] << endl;
}
}
cout << "Total Cost = " << solver.totalCost() << endl;
break;
case NS::UNBOUNDED:
cerr << "infinite flow" << endl;
break;
default:
break;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//-----------------------------------------------------------------------------------
return 0;
}
void readNodeTable(int nodeID[100], long nodeSD[100])
{
fstream nodeColumn1;
fstream nodeColumn2;
string temp;
string nodeIDVals, nodeSDVals;
nodeColumn1.open("Demo_Data/nodeID_demo.csv", ios::in);
nodeColumn2.open("Demo_Data/nodeSD_demo.csv", ios::in);
int rowIndex = 0;
//---------- Serially Reading Data in Node Table -------------
while(nodeColumn1 >> temp && nodeColumn2 >> temp)
{
getline(nodeColumn1, nodeIDVals, ',');
nodeID[rowIndex] = stoi(nodeIDVals);
getline(nodeColumn2, nodeSDVals, ',');
nodeSD[rowIndex] = stol(nodeSDVals);
rowIndex++;
}
// nodeID[0] = 1;
// nodeID[1] = 2;
//-------------------------------------------------------------
}
void readArcTable(int arcFrom[158], int arcTo[158], int arcMin[158], long arcMax[158], long arcWeight[158])
{
fstream arcColumn1;
fstream arcColumn2;
fstream arcColumn3;
fstream arcColumn4;
fstream arcColumn5;
string temp;
string fromIDVals, toIDVals, arcMinVals, arcMaxVals, arcWeightVals;
arcColumn1.open("Demo_Data/arcFrom_demo.csv", ios::in);
arcColumn2.open("Demo_Data/arcTo_demo.csv", ios::in);
arcColumn3.open("Demo_Data/arcMin_demo.csv", ios::in);
arcColumn4.open("Demo_Data/arcMax_demo.csv", ios::in);
arcColumn5.open("Demo_Data/arcWeight_demo.csv", ios::in);
int rowIndex = 0;
//---------- Serially Reading Data from Arc Table -------------
while(arcColumn1 >> temp && arcColumn2 >> temp && arcColumn3 >> temp && arcColumn4 >> temp && arcColumn5 >> temp)
{
getline(arcColumn1, fromIDVals, ',');
arcFrom[rowIndex] = stoi(fromIDVals);
getline(arcColumn2, toIDVals, ',');
arcTo[rowIndex] = stoi(toIDVals);
getline(arcColumn3, arcMinVals, ',');
arcMin[rowIndex] = stoi(arcMinVals);
getline(arcColumn4, arcMaxVals, ',');
arcMax[rowIndex] = stol(arcMaxVals);
getline(arcColumn5, arcWeightVals, ',');
arcWeight[rowIndex] = stol(arcWeightVals);
rowIndex++;
}
//--------------------------------------------------------------
// arcMin[0] = 0;
// arcMax[0] = 1000000L;
}