-
Notifications
You must be signed in to change notification settings - Fork 3
/
NetworkP2MPWeighted.java
373 lines (304 loc) · 11.5 KB
/
NetworkP2MPWeighted.java
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package dccast;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.TreeMap;
import java.util.StringJoiner;
import java.util.ArrayList;
import java.util.LinkedList;
/**
*
* @author Mohammad Noormohammadpour
*/
// the following three imports are provided by Dimitri Watel: https://github.com/mouton5000/DSTAlgoEvaluation
import graphTheory.algorithms.steinerProblems.steinerArborescenceApproximation.GFLACAlgorithm;
import graphTheory.graph.DirectedGraph;
import graphTheory.instances.steiner.classic.SteinerDirectedInstance;
//////////////////////////////////////////////////////////////////////////
public class NetworkP2MPWeighted {
// subclasses below Network
private class timeslot {
private final LinkedHashMap<Long, Double> content
= new LinkedHashMap<>();
private double sum = 0.0;
public double getSpace() {
assert(sum >= 0 && sum <= 1);
return Math.max(0, (1.0 - sum));
}
public double getSum() {
assert(sum >= 0 && sum <= 1);
return Math.min(1.0, sum);
}
public boolean addTraffic(double vol, long reqid) {
assert(sum >= 0);
if(vol <= getSpace())
{
sum = Math.min(1.0, sum + vol);
if(content.containsKey(reqid))
{
content.put(reqid, vol + content.get(reqid));
}
else
{
content.put(reqid, vol);
}
assert(sum >= 0 && sum <= 1);
return true;
}
assert(sum >= 0);
return false;
}
public double getTraffic(long reqid, boolean pull) {
assert(sum >= 0);
if(content.containsKey(reqid))
{
double vol = content.get(reqid);
if(pull)
{
sum = Math.max(0, sum - vol);
content.remove(reqid);
}
assert(sum >= 0);
return vol;
}
else
{
assert(sum >= 0);
return 0;
}
}
public double getTrafficVol(long reqid, double reqvol) {
assert(sum >= 0);
if(content.containsKey(reqid))
{
double vol = content.get(reqid);
if(reqvol >= vol)
{
sum = Math.max(0, sum - vol);
content.remove(reqid);
assert(sum >= 0);
return vol;
}
else
{
sum = Math.max(0, sum - reqvol);
content.put(reqid, vol - reqvol);
assert(sum >= 0);
return reqvol;
}
}
else
{
assert(sum >= 0);
return 0;
}
}
public Long[] getRequests() {
assert(sum >= 0);
return content.keySet().toArray(new Long[content.size()]);
}
}
private class jNode {
LinkedHashMap<jNode, jEdge> next = new LinkedHashMap<>();
int index = 0;
}
private class jEdge {
int id;
int src, dst;
ArrayList<timeslot> t_slot;
double load; // Le on the paper
}
private class jSTEINER_tree {
LinkedList<jEdge> edges = new LinkedList<>();
}
// our networkwide variables
private final LinkedHashMap<Long, LinkedList<Integer>> reqedges =
new LinkedHashMap<>();
private final HashMap<Integer, HashMap<Integer, jEdge>> node_to_edge =
new HashMap<>();
private final HashMap<Integer, HashMap<Long, Double>> schedule =
new HashMap<>();
private final HashMap<Long, Requests_P2MP.Req_P2MP> requests =
new HashMap<>();
private int NODES;
private int[][] LINKS;
private int t_time; // now
private int hor_end = 0;
private jNode[] nodes;
private jEdge[] edges;
private double[] total_link_traffic;
// compile network topology
public void compile(Topology links, int Nodes)
{
NODES = Nodes;
LINKS = links.getLinks();
t_time = 0; // now
total_link_traffic = new double[LINKS.length];
// build the whole network based off the links
nodes = new jNode[NODES];
for(int i = 0; i < NODES; i++) {
nodes[i] = new jNode();
nodes[i].index = i;
}
edges = new jEdge[LINKS.length];
for(int i = 0; i < LINKS.length; i++) {
edges[i] = new jEdge();
edges[i].id = i;
edges[i].src = LINKS[i][0];
edges[i].dst = LINKS[i][1];
edges[i].load = 0;
edges[i].t_slot = new ArrayList<>();
nodes[LINKS[i][0]].next.put(nodes[LINKS[i][1]], edges[i]);
if(!node_to_edge.containsKey(edges[i].src)) {
node_to_edge.put(edges[i].src, new HashMap<>());
}
node_to_edge.get(edges[i].src).put(edges[i].dst, edges[i]);
}
}
public double allocate(Requests_P2MP.Req_P2MP r, TreeMap<Long, Double> req_finishes)
{
return routeTraffic(r, req_finishes);
}
private double routeTraffic(Requests_P2MP.Req_P2MP r, TreeMap<Long, Double> req_finishes)
{
requests.put(r.id, r);
DirectedGraph dg = new DirectedGraph();
for(int i = 0; i < NODES; i++)
dg.addVertice(i);
// Add arcs
for(int[] link : LINKS){
dg.addDirectedEdges(link[0], link[1]);
}
SteinerDirectedInstance sdi = new SteinerDirectedInstance(dg);
sdi.setRoot(r.src); // Set the node 1 as the root of the instance
r.dst.stream().forEach(dst -> {sdi.setRequired(dst);});
HashMap<jEdge, Integer> hm_costs = new HashMap<>();
for(jEdge e : edges){
hm_costs.put(e, (int) (e.load + r.volume)); // We = Le + Vr
sdi.setCost(e.src, e.dst, hm_costs.get(e));
}
// Create an algorithm to solve or approximate the instance
GFLACAlgorithm alg = new GFLACAlgorithm(); // This algorithm was developed by Dimitri Watel and Marc-Antoine Weisser: http://dl.acm.org/citation.cfm?id=3013458
alg.setInstance(sdi);
alg.compute();
// System.out.println("Src: " + r.src + ", Dst: " + r.dst.toString());
// System.out.println("Returned solution : " + alg.getArborescence());
// System.out.println("Cost " + r.id + " : " + alg.getCost());
// System.out.println("Runtime: " + alg.getTime() + " ms");
// validate the tree
jSTEINER_tree selectedTree = new jSTEINER_tree();
selectedTree.edges.clear();
HashSet<Integer> set = new HashSet<>();
HashSet<Integer> set2 = new HashSet<>();
set.add(r.src);
HashSet<Integer> dsts = new HashSet<>(r.dst);
while(!set.isEmpty()){
set2.clear();
alg.getArborescence().stream().forEach(arc -> {
if(set.contains(arc.getInput())){
selectedTree.edges.add(node_to_edge.
get(arc.getInput()).
get(arc.getOutput()));
set2.add(arc.getOutput());
if(dsts.contains(arc.getOutput())){
dsts.remove(arc.getOutput());
}
}
});
set.clear();
set.addAll(set2);
}
assert(dsts.isEmpty()); // all destinations are reachable from source
int what = PathAllocate(selectedTree, r.volume, r.id);
req_finishes.put(r.id, what - r.arrival + 1);
double total_bw = selectedTree.edges.size() * r.volume;
final LinkedList<Integer> lst = new LinkedList<>();
selectedTree.edges.stream().forEach(e -> {
lst.add(e.id);
});
reqedges.put(r.id, lst);
StringJoiner sj = new StringJoiner(",");
selectedTree.edges.stream().forEach((e) -> {
sj.add(String.format("%d->%d", e.src, e.dst));
});
System.out.printf("Job id: [" + r.id + "] -> TREE: [%s], \n\tsrc: %d, dst: %s, demand: %.2f, t_time: %d\n",
sj.toString(), r.src, r.dst.toString(), r.volume, t_time);
return total_bw;
}
// do as late as possible on edges
private int PathAllocate(jSTEINER_tree p, double vol, long id)
{
int t = 0;
while(vol > 0){
double space = vol;
if(t >= hor_end - t_time){
if(hor_end < t_time){
hor_end = t_time;
}
for(jEdge e : edges){
e.t_slot.add(new timeslot());
}
hor_end++;
}
for(jEdge ex : p.edges){
space = Math.min(ex.t_slot.get(t).getSpace(), space);
}
if(space > 0) {
vol -= space;
if(!schedule.containsKey(t + t_time)){
schedule.put(t + t_time, new HashMap<>());
}
schedule.get(t + t_time).put(id, space);
for(jEdge ex : p.edges){
assert(ex.t_slot.get(t).addTraffic(space, id));
ex.load += space;
}
}
t++;
}
return t + t_time;
}
// move one timeslot ahead
public boolean walk(TreeMap<Long, Double> req_demands)
{
print_schedule();
if(hor_end <= t_time){
t_time++;
return false;
}
// update the load field for all edges
for (jEdge edge : edges)
{
double now = edge.t_slot.get(0).getSum();
assert(now <= 1.0 + 1E-9);
edge.load = Math.max(0.0, edge.load - now);
total_link_traffic[edge.id] += now;
// test the allocation for validity & integrity
for(long id:edge.t_slot.get(0).getRequests())
{
assert(req_demands.containsKey(id));
req_demands.put(id, (req_demands.get(id) -
edge.t_slot.get(0).getTraffic(id, false) /
reqedges.get(id).size()));
}
}
t_time++;
for(jEdge e : edges){
e.t_slot.remove(0);
}
return t_time < hor_end;
}
// print the transmission schedule for next slot
public void print_schedule(){
System.out.println("Schedule for time " + t_time + ":");
if(schedule.containsKey(t_time)){
for(Long id : schedule.get(t_time).keySet()){
System.out.printf("- SOURCE: %d, JobID: %d, RATE: %.3f\n",
requests.get(id).src,
id,
schedule.get(t_time).get(id));
}
}
System.out.println();
}
}