-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduling.java
359 lines (332 loc) · 12.2 KB
/
Scheduling.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
import java.util.*;
import java.lang.*;
import java.io.*;
public class Scheduling{
public static int min;
public static int alternateSolutions;
public static void main(String... args){
LinkedList<SNode> nodeList = null;
try{
nodeList = readData();
}
catch(IOException e){}
printList(nodeList);
nodeList = runList(nodeList);
analyseList(nodeList);
}
public static void printList(LinkedList<SNode> nodeList){
for(int i=0; i<nodeList.size(); i++){
nodeList.get(i).printNode();
}
}
public static void analyseList(LinkedList<SNode> nodeList){
int maxTime = getMaxFinish(nodeList);
System.out.println("Max duration for the project: " + maxTime);
int maxWorkers = getMaxWorkers(nodeList, false);
System.out.println("Max workers for min duration is: " + maxWorkers);
identifyCriticalPath(nodeList);
int maxWorkersCrit = getMaxWorkers(nodeList, true);
System.out.println("Workers for crit path is: " + maxWorkersCrit);
LinkedList<SNode> bestList = getOptimizeValue(nodeList, maxWorkers);
bestList = addCritPath(nodeList, bestList);
//System.out.println("menor numero de trabalhadores otimizado é " + min + " e a sua lista é");
min = getMaxWorkers(bestList, false);
if(alternateSolutions>0) System.out.println("Existem soluções alternativas");
else System.out.println("Não existem soluções alternativas");
System.out.println("menor numero de trabalhadores otimizado é " + min + " e a sua lista é");
System.out.println();
printList(bestList);
}
private static LinkedList<SNode> addCritPath(LinkedList<SNode> nodeList, LinkedList<SNode> bestList) {
SNode cur;
for(int i=0; i<nodeList.size(); i++){
cur = nodeList.get(i);
if(cur.isCritTask()){
bestList.addLast(cur);
}
}
return bestList;
}
private static int getMaxFinish(LinkedList<SNode> nodeList) {
SNode cur = null;
int max= 0;
for(int i=0; i<nodeList.size(); i++){
cur = nodeList.get(i);
if(cur.getEFinish()>max)
max = cur.getEFinish();
}
SNode.maxDuration = max;
return max;
}
private static void identifyCriticalPath(LinkedList<SNode> nodeList) {
SNode cur = null;
for(int i=0; i<nodeList.size(); i++){
cur = nodeList.get(i);
if(cur.getEStart() == cur.getLStart()){
cur.setCritTask();
}
}
}
private static LinkedList<SNode> getOptimizeValue(LinkedList<SNode> nodeList, int maxWorkers){
PriorityQueue<SNode> pq = getPriorityQueue(nodeList);
LinkedList<SNode> testList = new LinkedList<>();
LinkedList<SNode> bestList = new LinkedList<>();
min = maxWorkers;
bestList = expandPriorityQueue(pq, testList, bestList);
return bestList;
}
private static PriorityQueue<SNode> getPriorityQueue(LinkedList<SNode> nodeList) {
PriorityQueue<SNode> pq = new PriorityQueue<SNode>(new SNodeComparator());
SNode cur;
for(int i = 0; i<nodeList.size(); i++){
cur = nodeList.get(i);
if(!cur.isCritTask())
pq.add(nodeList.get(i));
}
return pq;
}
private static LinkedList<SNode> expandPriorityQueue(PriorityQueue<SNode> pq, LinkedList<SNode> testList, LinkedList<SNode> bestList) {
if(pq.isEmpty()){
int max = getMaxWorkers(testList, false);
if (max>min){
alternateSolutions=0;
return bestList;
}
else if(max == min){
alternateSolutions=1;
return bestList;
}
min = max;
bestList = copyList(testList);
}
else {
SNode atual = pq.poll();
int availableStartDay = getStartDay(testList, atual);
atual.setCurStart(availableStartDay);
while (atual.getCurStart() < atual.getLStart()) {
testList.add(atual);
bestList = expandPriorityQueue(pq, testList, bestList);
testList.remove(atual);
atual.incrementCurStart();
}
pq.add(atual);
}
return bestList;
}
private static LinkedList<SNode> copyList(LinkedList<SNode> testList) {
LinkedList<SNode> clone = new LinkedList<>();
SNode cur;
for (int i=0; i<testList.size(); i++){
cur = testList.get(i);
clone.addLast(cur.copyOf());
}
return clone;
}
private static int getStartDay(LinkedList<SNode> testList, SNode atual) {
int available, curNodeNr;
Boolean isPrecedent;
SNode cur;
HashMap<Integer,Boolean> hm = atual.getPrecedences();
available = atual.getEStart();
for (int i=0; i<testList.size(); i++){
cur = testList.get(i);
curNodeNr = cur.getNodeNR();
isPrecedent = hm.get(curNodeNr);
if(isPrecedent!=null){
if (cur.getCurStart()>available){
available = cur.getCurStart();
}
}
}
return available;
}
private static int getMaxWorkers(LinkedList<SNode> nodeList, Boolean considerOnlyCritPath) {
SNode cur;
int maxTime = SNode.maxDuration;
int[] time = new int[maxTime+1];
for (int i=0; i<nodeList.size(); i++){
cur = nodeList.get(i);
if(considerOnlyCritPath && !cur.isCritTask())
continue;
for(int j = cur.getCurStart(); j<cur.getCurStart()+cur.getDuration(); j++){
time[j] = time[j]+cur.getNrTrab();
}
}
return getMax(time);
}
private static int getMax(int[] time) {
int max = 0;
for(int i=0; i<time.length; i++){
if(time[i]>max){
max = time[i];
}
}
return max;
}
public static LinkedList<SNode> readData() throws IOException{
BufferedReader reader = new BufferedReader(new FileReader("data.txt"));
LinkedList<SNode> nodeList = new LinkedList<SNode>();
HashMap<Integer, Integer> hmAll = new HashMap<Integer, Integer>(30);
String line=null;
String[] parts;
int starterFlag=0;
int nodeNR, duracao, nrTrab, size, nrPrec;
line = reader.readLine();
while (( line = reader.readLine()) != null){
parts = line.split(" ");
size = parts.length;
nrPrec=Integer.parseInt(parts[1]);
nodeNR = Integer.parseInt(parts[0]);
//System.out.println(Integer.parseInt(parts[size]));
HashMap<Integer,Boolean> hm = new HashMap<Integer,Boolean>(6);
for(int i=0; i<nrPrec; i++){
hmAll.put(nodeNR,Integer.parseInt(parts[i+2]));
}
duracao = Integer.parseInt(parts[size-2]);
nrTrab =Integer.parseInt(parts[size-1]);
SNode node = new SNode(nodeNR, hm, duracao, nrTrab);
if (starterFlag==1){
node.addSetEFinish();
starterFlag = 0;
}
nodeList.addLast(node);
}
addPrecedentsFromDescendants(nodeList, hmAll);
return nodeList;
}
private static void addPrecedentsFromDescendants(LinkedList<SNode> nodeList, HashMap<Integer, Integer> hmAll) {
SNode cur;
HashMap<Integer,Boolean> precedences;
for (int i=0;i<nodeList.size();i++){
cur = nodeList.get(i);
for (Map.Entry<Integer, Integer> entry : hmAll.entrySet()) {
if(entry.getValue()==cur.getNodeNR()){
cur.getPrecedences().put(entry.getKey(), true);
}
}
}
int test = 0;
for (int i=0;i<nodeList.size();i++) {
cur = nodeList.get(i);
for (Map.Entry<Integer, Boolean> entry : cur.precedences.entrySet()) {
test = 1;
}
if (test==0){
cur.precedences.put(0,false);
cur.addSetEFinish();
}
test = 0;
}
}
public static LinkedList<SNode> runList(LinkedList<SNode> nodeList){
nodeList = forwardPass(nodeList);
nodeList = backwardPass(nodeList);
return nodeList;
}
private static LinkedList<SNode> backwardPass(LinkedList<SNode> nodeList) {
int[] grauS = getNodeGrau(nodeList);
Queue<SNode> S = getGrau0(grauS, nodeList);
SNode precedent = null;
SNode cur =S.poll();//not initializing as null becuz of while
while(cur!=null){
for (Map.Entry<Integer, Boolean> entry : cur.precedences.entrySet()) {
if(entry.getKey()!=0) {
precedent = nodeList.get(entry.getKey() - 1);
updateLF(cur, precedent);
grauS[precedent.getNodeNR()-1]--;
if (grauS[precedent.getNodeNR()-1] == 0)
S.add(precedent);
}
}
cur = S.poll();
}
return nodeList;
}
private static void updateLF(SNode cur, SNode precedent) {
if(cur.getLStart()<precedent.getLFinish() || precedent.getLFinish()==0){
precedent.setLFinish(cur.getLStart());
precedent.setLStart();
}
}
private static LinkedList<SNode> getGrau0(int[] grauS, LinkedList<SNode> nodeList) {
LinkedList<SNode> queue = new LinkedList<>();
SNode cur = null;
int max = getMaxFinish(nodeList);
for(int i=0; i<grauS.length;i++){
if(grauS[i]==0){
cur = nodeList.get(i);
cur.setLFinish(max);
cur.setLStart();
queue.add(cur);
}
}
return queue;
}
private static int[] getNodeGrau(LinkedList<SNode> nodeList) {
SNode cur = null;
int[] grau = new int[nodeList.size()];
for(int i=0; i< nodeList.size(); i++){
cur = nodeList.get(i);
for (Map.Entry<Integer, Boolean> entry : cur.precedences.entrySet()) {
if(entry.getKey()!=0)
grau[entry.getKey()-1]++;
}
}
return grau;
}
private static LinkedList<SNode> forwardPass(LinkedList<SNode> nodeList) {
SNode cur = getNextNode(nodeList);
while(cur!=null){
nodeList = checkPrecedences(cur, nodeList);
cur = getNextNode(nodeList);
}
return nodeList;
}
public static SNode getNextNode(LinkedList<SNode> nodeList){
for (int i=0; i<nodeList.size(); i++) {
SNode cur = nodeList.get(i);
if(!cur.getPrecedences().containsValue(true) && !cur.getVisited()){//if all precedences value are false than pick it
cur.setVisited();
return cur;
}
}
return null;
}
public static LinkedList<SNode> checkPrecedences(SNode ref, LinkedList<SNode> nodeList){
for(int i=0; i<nodeList.size(); i++){
SNode cur = nodeList.get(i);
boolean hasNode= cur.getPrecedences().containsKey(ref.getNodeNR());
if( hasNode){
updateES(cur, ref);
removePrecedences(cur, ref);
}
}
return nodeList;
}
public static void updateES(SNode cur, SNode ref){//if ES is worse, change it
if(cur.getEStart()<ref.getEFinish()){
cur.setEStart(ref.getEFinish());
cur.addSetEFinish();
}
}
public static void removePrecedences(SNode cur, SNode ref){
cur.getPrecedences().replace(ref.getNodeNR(), false);
}
}
class SNodeComparator implements Comparator<SNode>{
@Override
public int compare(SNode o1, SNode o2) {
int s1 = getSlack(o1);
int s2 = getSlack(o2);
if(s1>s2){
return -1;
}
else if(s1<s2){
return 1;
}
else return 0;
}
private int getSlack(SNode o1) {
return o1.EFinish - o1.LFinish;
}
}