This repository has been archived by the owner on Aug 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNetwork.java
461 lines (397 loc) · 11 KB
/
Network.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package net_simplex;
import java.io.*;
import java.util.ArrayList;
public class Network {
int nnodes, narcs, fakearcind;
int[] demand;
Arc[] arcarr;
Simptree tree;
int[] nodeprice;
/**
* the standard constructor, generating a new network
* @param nnodes number of nodes in the networl
* @param narcs number of arcs in the network
*/
public Network(int nnodes, int narcs){
demand=new int[nnodes];
tree=new Simptree(nnodes);
arcarr=new Arc[narcs];
nodeprice= new int[nnodes];
this.nnodes=nnodes;
this.narcs=narcs;
this.fakearcind=this.narcs-this.nnodes+1;
}
/**
* checks if all elements of the network have been initialized
* @return true if the network is initialized
*/
public boolean isInitialized(){
if(nnodes==0 || narcs==0 || demand==null || tree==null || nodeprice==null){
return false;
}
if(!tree.isInitliazied())return false;
for(int i=0; i<arcarr.length;++i){
if(!arcarr[i].isInitialized())return false;
}
return true;
}
/**
* computes the highest cost among arc-costs excluding the arcs that have been artificially added.
* @return the value of the highest cost
*/
public int maxcost() {
int max=0;
for(int i=0; i<this.fakearcind; i++){
if(null!=arcarr[i]){
if(arcarr[i].cost>max)max=arcarr[i].cost;
}
}
return max;
}
/**
* Initializes the tree-solution.
*/
public void initTree() {
this.tree.succ[0]=1;
for(int i=1;i<this.nnodes;++i){
this.tree.depth[i]=1;
this.tree.pred[i]=0;
this.tree.succ[i]=i+1;
this.tree.arcind[i-1]=this.fakearcind+i-1;
}
this.tree.succ[this.nnodes-1]=0;
}
/**
* Generates the arcs between the artificial 0-node and the rest of the nodes and computes the appropriate amount of flow in those arcs for the overall flow to be valid.
* Also compute the nodeprice-array.
*/
public void initFlowPrice() {
int[] tempflow=demand.clone();
for(int i=0; i<this.fakearcind;i++){
tempflow[arcarr[i].startnode]-=arcarr[i].lowerb;
tempflow[arcarr[i].endnode]+=arcarr[i].lowerb;
}
long m = 1+(long)(this.nnodes)*(long)(this.maxcost());
long n = (int)m;
assert(m==n);
int k=(int)m;
for(int i=1; i<nnodes; i++){
if(tempflow[i]<0){
arcarr[this.fakearcind+i-1]=new Arc(0,i,0,Integer.MAX_VALUE,k,-tempflow[i], true);
nodeprice[i]=k;
}
if(tempflow[i]>=0){
arcarr[this.fakearcind+i-1]=new Arc(i,0,0,Integer.MAX_VALUE,k,tempflow[i], true);
nodeprice[i]=-k;
}
}
}
public boolean isValidFlow() {
int[] tempflow=demand.clone();
for(int i=0;i<this.arcarr.length;i++){
tempflow[arcarr[i].startnode]-=arcarr[i].flow;
tempflow[arcarr[i].endnode]+=arcarr[i].flow;
}
for(int i=0;i<this.nnodes;i++){
if(tempflow[i]!=0)return false;
}
return true;
}
/**
* optimizes a mincost-flow network with a valid initialized flow and tree solution using the network simplex algorithm
* @return true if there exists a valid flow with minimal costs.
*/
public Boolean simplex() {
// 3. Optimalitaetstest + pivot-Element finden.
int pivot=findPivot();
while(-1!=pivot){
//find u,v from the pivot
Arc pivotarc = arcarr[pivot];
int u,v=0;
//udirection tells us if we go along the upath from the joint
boolean udirection = true;
int strt= pivotarc.startnode;
int end=pivotarc.endnode;
if(tree.depth[strt]>tree.depth[end]){
u=strt;
v=end;
if(pivotarc.flow==pivotarc.upperb)udirection=false;
}else{
v=strt;
u=end;
if(pivotarc.flow==pivotarc.lowerb)udirection=false;
}
ArrayList<Integer> upath = new ArrayList<Integer>();
ArrayList<Integer> vpath = new ArrayList<Integer>();
upath.add(u);
vpath.add(v);
while(tree.depth[upath.get(upath.size()-1)]!=tree.depth[v]){
upath.add(tree.pred[upath.get(upath.size()-1)]);
}
while(upath.get(upath.size()-1).intValue()!=vpath.get(0).intValue()){
upath.add(tree.pred[upath.get(upath.size()-1)]);
vpath.add(0, tree.pred[vpath.get(0)]);
}
//TODO check changed: corrected order of varcs and simplified for-loop
ArrayList<Arc> uarcs = new ArrayList<Arc>();
for(int i=0;i<upath.size()-1;++i){
uarcs.add(findTreeArc(upath.get(i),upath.get(i+1)));
assert(null!=uarcs.get(uarcs.size()-1));
}
ArrayList<Arc> varcs = new ArrayList<Arc>();
for(int i=0;i<vpath.size()-1;i++){
varcs.add(findTreeArc(vpath.get(i),vpath.get(i+1)));
assert(null!=varcs.get(varcs.size()-1));
}
//TODO check changed: fixed the way f is computed
varcs.add(pivotarc);
int pivotindex = varcs.size()-1;
varcs.addAll(uarcs);
vpath.addAll(upath);
int eps = Integer.MAX_VALUE;
Arc f = null;
int e1=-1,e2=-1,f1=-1,f2=-1;
//determine all forward/backward-arcs and calculate the amount eps which the flow is shifted
boolean[] isforwardarc = new boolean[varcs.size()];
if(udirection){
for(int i=0;i<varcs.size();i++){
//determine whether it is a forward-arc
Arc temp = varcs.get(i);
if(temp.endnode==vpath.get(i).intValue()){
isforwardarc[i]=true;
eps=Math.min(eps, temp.upperb-temp.flow);
}
else{
isforwardarc[i]=false;
eps=Math.min(eps, temp.flow-temp.lowerb);
}
}
}else{
for(int i=0;i<varcs.size();i++){
//determine whether it is a forward-arc
Arc temp = varcs.get(i);
if(temp.endnode==vpath.get(i+1).intValue()){
isforwardarc[i]=true;
eps=Math.min(eps, temp.upperb-temp.flow);
}
else{
isforwardarc[i]=false;
eps=Math.min(eps, temp.flow-temp.lowerb);
}
}
}
//now go through the arc-array again and determine the leaving arc f and f1,f2,e1,e2
if(udirection){
for(int i=0;i<varcs.size();i++){
Arc temp = varcs.get(i);
if((isforwardarc[i] && temp.flow+eps==temp.upperb)||(!isforwardarc[i]&&temp.flow-eps==temp.lowerb)){
f=temp;
if(i>pivotindex){
e2=u;
e1=v;
}else{
e2=v;
e1=u;
}
break;
}
}
}else{
for(int i=varcs.size()-1;i>=0;i--){
Arc temp = varcs.get(i);
if((isforwardarc[i] && temp.flow+eps==temp.upperb)||(!isforwardarc[i]&&temp.flow-eps==temp.lowerb)){
f=temp;
if(i<pivotindex){
e2=v;
e1=u;
}else{
e2=u;
e1=v;
}
break;
}
}
}
if(this.tree.depth[f.startnode]>this.tree.depth[f.endnode]){
f2=f.startnode;
f1=f.endnode;
}else{
f2=f.endnode;
f1=f.startnode;
}
assert(e1!=-1 && e2!=-1 && f1!=-1 && f2!=-1);
int pivotdir;
if(pivotarc.endnode==e2)pivotdir=1;
else pivotdir=-1;
int redcost= pivotarc.cost+this.nodeprice[pivotarc.startnode]-this.nodeprice[pivotarc.endnode];
int y=f2;
nodeprice[y]+=pivotdir*redcost;
while(tree.depth[tree.succ[y]]>tree.depth[f2]){
y=tree.succ[y];
nodeprice[y]+=pivotdir*redcost;
}
//augmentiere den fluss
/*
for(int i=0;i<forwardarcs.size();++i){
forwardarcs.get(i).flow+=eps;
}
for(int i=0;i<backwardarcs.size();++i){
backwardarcs.get(i).flow-=eps;
}*/
for(int i=0;i<varcs.size();++i){
Arc temp=varcs.get(i);
if(isforwardarc[i])temp.flow+=eps;
else temp.flow-=eps;
}
/**
//TODO this is probably garbage
if((e1==f1 && e2==f2)||(e1==f2 && e2==f1)){
pivot=findPivot();
continue;
}
*/
//update succ[]depth[] and pred[]
int[] tempdepth = tree.depth.clone();
int[] temppred = tree.pred.clone();
tempdepth[e2]=tempdepth[e1]+1;
temppred[e2]=e1;
int a=f1;
int b=tree.succ[e1];
int i=e2;
while(tree.succ[a]!=f2){
a=tree.succ[a];
}
int k=i;
while(tree.depth[tree.succ[k]]>tree.depth[i]){
k=tree.succ[k];
tempdepth[k]=tempdepth[tree.pred[k]]+1;
}
int r = tree.succ[k];
while(i!=f2){
int j=i;
i=tree.pred[i];
temppred[i]=j;
tempdepth[i]=tempdepth[j]+1;
tree.succ[k]=i;
k=i;
while(tree.succ[k]!=j){
k=tree.succ[k];
tempdepth[k]=tempdepth[tree.pred[k]]+1;
}
if(tree.depth[r]>tree.depth[i]){
tree.succ[k]=r;
while(tree.depth[tree.succ[k]]>tree.depth[i]){
k=tree.succ[k];
tempdepth[k]=tempdepth[tree.pred[k]]+1;
}
r=tree.succ[k];
}
}
if(e1!=a){
tree.succ[a]=r;
tree.succ[e1]=e2;
tree.succ[k]=b;
}else{
tree.succ[e1]=e2;
tree.succ[k]=r;
}
assert(this.tree.isValidTree());
tree.depth=tempdepth;
tree.pred=temppred;
//abschliessendes update
pivotarc.isInTree=true;
f.isInTree=false;
for(int j=0;j<tree.arcind.length;++j){
Arc temp=arcarr[tree.arcind[j]];
if(f.startnode==temp.startnode && f.endnode==temp.endnode){
tree.arcind[j]=pivot;
break;
}
}
assert(this.isValidFlow());
//update the pivot element
pivot=this.findPivot();
//this.printCosts();
}
for(int i=0;i<this.nnodes-1; ++i){
if(arcarr[this.fakearcind+i].isInTree && arcarr[this.fakearcind+i].flow!=0)return false;
}
return true;
}
private Arc findTreeArc(int i, int j) {
for(int c=0;c<tree.arcind.length;++c){
Arc temparc = arcarr[tree.arcind[c]];
if((temparc.startnode==i && temparc.endnode==j) || (temparc.endnode==i && temparc.startnode==j)){
return temparc;
}
}
return null;
}
public int findPivot(){
// muss hier equal hin? vielleicht ist es besser eine ausgelagerte Methode zu schreiben, die prueft ob a element eines Arc[] ist, anstatt dieser hier
int eps=0;
int pivotindex=-1;
for(int i=0;i<this.arcarr.length;++i){
Arc temp = arcarr[i];
if(temp.isInTree)continue;
int redcost = temp.cost-nodeprice[temp.endnode]+nodeprice[temp.startnode];
if(temp.flow==temp.upperb && redcost > 0 ){
if(eps<redcost){
eps=redcost;
pivotindex=i;
}
}
if(temp.flow==temp.lowerb && redcost < 0 ){
if(eps<-redcost){
eps=-redcost;
pivotindex=i;
}
}
}
return pivotindex;
}
public void printCosts(){
long costs=0;
for (Arc temp: arcarr){
costs+=((long)temp.cost*(long)temp.flow);
}
System.out.println("The total costs of the problem are: " + costs);
}
/**
* prints the solution after the network-optimization
*/
public void printSol() {
// TODO Auto-generated method stub
}
public void writeGraph() throws IOException{
int begin = 0;
int end = 0;
int length = narcs-1;
PrintWriter text = null;
text = new PrintWriter(new BufferedWriter(new FileWriter("graph.gv")));
text.println("digraph G {");
for(int i = 0;i <= length;i++){
begin = arcarr[i].startnode;
end = arcarr[i].endnode;
text.println(begin + " " + "->" + " " + end);
}
text.println("\n");
text.println("}");
text.close();
}
public void writeTree() throws IOException{
int begin = 0;
int end = 0;
PrintWriter text = null;
text = new PrintWriter(new BufferedWriter(new FileWriter("graph.gv")));
text.println("digraph G {");
for(int i = 0;i < this.tree.arcind.length;i++){
begin = arcarr[tree.arcind[i]].startnode;
end = arcarr[tree.arcind[i]].endnode;
text.println(begin + " " + "->" + " " + end);
}
text.println("\n");
text.println("}");
text.close();
}
}