/
DiePaten.java
282 lines (229 loc) · 6.98 KB
/
DiePaten.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
import java.io.IOException;
import java.util.ArrayList;
/**
* Fill this class with your own strategy.
*/
public class DiePaten extends FVSPlayer {
public static final int WHITE = 0, GRAY = 1, BLACK = 2;
private int[] color, minCapacity, parent, queue;
private int first, last, size;
int[][] flow, restCapacity;
public DiePaten() {
// Set your team name here
// Prior to sending the code in you should turn debugging off.
super("DiePaten", true);
size = this.adjacencyMatrix.length;
}
/*
* Note: Available values are int source int sink int[][] adjacencyMatrix
* string[][] capacityMatrix The capacities are encoded as Strings and can
* be parsed by Integer.parseInt() The values in the adjacency matrix are
* either UNSELECTED_EDGE, FLOW_EDGE, CUT_EDGE or NO_EDGE
*
* You have only a limited time to make your choice. The program is aborted
* after the time limit. If you have not sent any reply by that time, you
* don't get any edge. You can send as many replies as you want, only the
* last one counts. If you are done with your calculations, you can send a
* flush to the game controller so that it won't wait unnecessarily long.
*/
// Implement your flow strategy
protected void handle_flow() {
/*
* Start of sample strategy. Replace this with your own code.
*/
System.err.println("flow");
Edge nextEdge = null;
//nextEdge = bottleNecks();
// nextEdge=maxcapacity();
// Send final reply indicating that we won't change our mind any more.
sendReply(nextEdge, true);
}
/**
* method to find the bottlenecks in the graph. every edge is set to 1 before
* @return nextEges, the list with the bottlenecks-edges
*/
private ArrayList<Edge> bottleNecks() {
ArrayList<Edge> nextEdges = null;
int tempMatrix[][] = new int[size][size];
for (int l = 0; l < size; l++) {
for (int k = 0; k < size; k++) {
int temp = Integer.parseInt(capacityMatrix[l][k]);
if (temp > 0 && adjacencyMatrix[l][k] == UNSELECTED_EDGE) {
tempMatrix[l][k] = 1;
} else {
tempMatrix[l][k] = 0;
}
}
}
//tempMatrix, getMincut
nextEdges = minCut();
return nextEdges;
}
private Edge maxcapacity() {
int maxCapacity = 0;
int numNodes = this.capacityMatrix.length;
Edge nextEdge = null;
for (int i = 0; i < numNodes; i++) {
for (int j = 0; j < numNodes; j++) {
if (this.adjacencyMatrix[i][j] == UNSELECTED_EDGE) {
int currentCapacity = Integer
.parseInt(capacityMatrix[i][j]);
if (currentCapacity > maxCapacity) {
maxCapacity = currentCapacity;
nextEdge = new Edge(i, j);
sendReply(nextEdge, false);
}
}
}
}
return nextEdge;
}
// Implement your cut strategy
protected void handle_cut() {
/*
* Start of sample strategy. Replace this with your own code.
*/
System.err.println("cut");
// even poorer strategy: select a random edge
Edge nextEdge = null;
nextEdge = random();
sendReply(nextEdge, true);
}
private Edge random() {
Edge nextEdge = null;
int numNodes = this.capacityMatrix.length;
while (nextEdge == null) {
int i = (int) (numNodes * Math.random());
int j = (int) (numNodes * Math.random());
if (this.adjacencyMatrix[i][j] == UNSELECTED_EDGE)
nextEdge = new Edge(i, j);
}
return nextEdge;
}
/**
* Method to determine the nodes which are in the min-cut-source-set.
* @return source_Set
*/
public ArrayList<Integer> sourceSet() {
ArrayList<Integer> source_Set = new ArrayList<Integer>();
// restCapacity array to string-array
String[][] restCapacityString = new String[restCapacity.length][restCapacity.length];
for(int i = 0; i < restCapacity.length; i++) {
//restCapacityString[i] = new String[ restCapacity[i].length ];
for(int j = 0; j < restCapacity[i].length; j++) {
restCapacityString[i][j] = Integer.toString( restCapacity[i][j] );
}
}
// to compute the max-flow
maxFlow(adjacencyMatrix, capacityMatrix, source, sink);
// to get the source_Set of the global queue-array
maxFlow(adjacencyMatrix, restCapacityString, source, sink);
// put it in source_Set arraylist
for (int i = 0; i < queue.length; i++) {
source_Set.add(queue[i]);
}
return source_Set;
}
public ArrayList<Integer> sinkSet() {
ArrayList<Integer> source_Set = new ArrayList<Integer>();
ArrayList<Integer> sink_Set = new ArrayList<Integer>();
source_Set = sourceSet();
for (int i = 0; i < size; i++) {
sink_Set.add(i);
for (int j = 0; j < source_Set.size(); j++) {
if (source_Set.indexOf(j) == sink_Set.indexOf(i)) {
sink_Set.remove(i);
}
}
}
return sink_Set;
}
/**
* determine the edges from source_set to sink_set
* @return the min-cut
*/
public ArrayList<Edge> minCut() {
ArrayList<Edge> min_Cut = new ArrayList<Edge>();
ArrayList<Integer> source_Set = new ArrayList<Integer>();
ArrayList<Integer> sink_Set = new ArrayList<Integer>();
Edge edge;
source_Set = sourceSet();
sink_Set = sinkSet();
for (int q = 0; q < source_Set.size(); q++) {
for (int s = 0; s < sink_Set.size(); s++) {
if (adjacencyMatrix[source_Set.get(q)][sink_Set.get(s)] == 1 || adjacencyMatrix[source_Set.get(q)][sink_Set.get(s)] == 2) {
edge = new Edge(source_Set.get(q), sink_Set.get(s));
min_Cut.add(edge);
}
}
}
return min_Cut;
}
public int maxFlow(int[][] adjacencyMatrix, String[][] capacityMatrix,
int source, int sink) {
int maxflow = 0;
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
if (adjacencyMatrix[x][y] == CUT_EDGE) {
capacityMatrix[x][y] = "0";
}
}
}
flow = new int[size][size];
restCapacity = new int[size][size];
parent = new int[size];
minCapacity = new int[size];
color = new int[size];
queue = new int[size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
restCapacity[i][j] = Integer.parseInt(capacityMatrix[i][j]);
}
}
while(BFS(source)) {
maxflow += minCapacity[sink];
int v = sink, u;
while(v != source) {
u = parent[v];
flow[u][v] += minCapacity[sink];
flow[v][u] -= minCapacity[sink];
restCapacity[u][v] -= minCapacity[sink];
restCapacity[v][u] += minCapacity[sink];
v = u;
}
}
return maxflow;
}
private boolean BFS(int source) {
boolean augmentedPathExits = false;
for (int i = 0; i < size; i++) {
color[i] = WHITE;
minCapacity[i] = Integer.MAX_VALUE;
}
first = last = 0;
queue[last++] = source;
color[source] = GRAY;
while (first != last) {
int v = queue[first++];
for (int u = 0; u < size; u++) {
if (color[u] == WHITE && restCapacity[v][u] > 0) {
minCapacity[u] = Math.min(minCapacity[v], restCapacity[v][u]);
parent[u] = v;
color[u] = GRAY;
if (u == sink) {
augmentedPathExits = true;
} else {
queue[last++] = u;
}
}
}
}
return augmentedPathExits;
}
// Do not edit!
public static void main(String args[]) throws IOException {
DiePaten p = new DiePaten();
p.connect();
p.mainLoop();
}
}