Repository navigation
Expand file tree
/
Copy pathalgorithms.cpp
More file actions
198 lines (159 loc) · 5.02 KB
/
Copy pathalgorithms.cpp
File metadata and controls
198 lines (159 loc) · 5.02 KB
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
#include <iostream>
#include <string>
#include <vector>
#include <time.h>
using namespace std;
//###########################
// List Scheduling Algorithm
Solution LSA(Instance instance) //instance pass by reference
{
Solution solution(instance);
//references for simplier writing
int nbMachines = instance.nbMachines;
vector<int> & tasks = instance.tasks;
vector<int> & assign = solution.assign;
vector<vector<int> > & schedule = solution.schedule;
vector<int> & machineCharge = solution.machinesCharge;
int & finalTime = solution.finalTime;
//beggining
int minimalCharge = 0;
for(int t=0; t < tasks.size(); ++t){ //for each task
for(int i=0; i < nbMachines ; ++i){ //look for the least charged machine
if(machineCharge[i] == minimalCharge){
assign[t] = i;
schedule[i].push_back(t); //add t in schedule[i]
machineCharge[i] += tasks[t];
minimalCharge = machineCharge[i];
break;
}
}
for(int i=0; i < nbMachines ; ++i){ //looking for the least charged machine
if(machineCharge[i] < minimalCharge)
minimalCharge = machineCharge[i];
}
}
finalTime=0;
for(int i=0; i<machineCharge.size(); ++i){ //looking for the most charged machine
if(machineCharge[i]>finalTime)
finalTime = machineCharge[i];
}
return solution;
}
//###################################
// Largest Processing Time algorithm
Solution LPT(Instance instance)
{
sort (instance.tasks.begin(), instance.tasks.end(), greater<int>());
Solution solution = LSA(instance);
return solution;
}
//######################
// Our custom algorithm
Solution MyAlgo(Instance instance)
{
Solution sol = LPT(instance);
vector<vector<int> > & sched = sol.schedule; //contains the num of the tasks for each machine
vector<int> & tasks = instance.tasks; //contains the durations of the tasks
bool found;
do{
// Getting machines sorted by their charge
vector<int> sortedMachines = sol.machinesCharge;
vector<int> machinesOrder = vector<int>(instance.nbMachines);
sort(sortedMachines.begin(), sortedMachines.end(), greater<int>());
for(int i=0; i<instance.nbMachines; ++i)
{
for(int j=0; j<instance.nbMachines; ++j)
{
if(sortedMachines[i]==sol.machinesCharge[j]){
machinesOrder[i] = j;
sol.machinesCharge[j] *= -1; //so that it won't be seen again
break;
}
}
}
for(int i=0; i<instance.nbMachines; ++i)
sol.machinesCharge[i] *= -1;
// Trying to find 2 tasks to permute
found = false;
int a, b; //num of re-sorted machines
int t1, t2; //num for tasks
int taskLong, taskShort; //duration of 2 selected tasks
int taskLongInd, taskShortInd; //their num in their respective machines
int machineShort; //machine of the smaller selected task
int taskLongNum, taskShortNum; //their num in the original list
int delta; //half the difference between the two watched machines
taskLong = 0;
taskShort = 0;
taskLongInd = -1;
taskShortInd = -1;
taskLongNum = -1;
taskShortNum = -1;
machineShort = -1;
a = machinesOrder[0];
for(int k=instance.nbMachines - 1; k>0; --k)
{
b = machinesOrder[k];
delta = sol.machinesCharge[a] - sol.machinesCharge[b];
delta /= 2; //truncated toward zero
if(delta <= taskLong - taskShort)
break; //there will not be better tasks to be found
for(int i=0; i<sched[a].size(); ++i)
{
t1 = sched[a][i];
for(int j=sched[b].size()-1; j>=0; --j)
{
t2 = sched[b][j];
if(tasks[t1] > tasks[t2])
{
if(tasks[t1] - tasks[t2] <= delta)
{
if(tasks[t1] - tasks[t2] > taskLong - taskShort){
found = true;
taskLong = tasks[t1];
taskLongInd = i; //num in the machine
taskLongNum = t1;
taskShort = tasks[t2];
taskShortInd = j;
taskShortNum = t2;
machineShort = b;
}
break; //we're only interested in the biggest smaller than delta
}
}
else
break; //might be ugly, but for loops are more friendly than while loops
}
}
}
if(found){
b = machineShort; //shorter to write
//updating assign (even it is actually never used, it is at least simple to update...)
sol.assign[taskLongNum] = b;
sol.assign[taskShortNum] = a;
//updating sched[a]
sched[a].erase( sched[a].begin()+taskLongInd );
t1=0;
while(t1 < sched[a].size() and tasks[sched[a][t1]] > taskShort){ //to find where to insert taskShort
t1++;
}
sched[a].insert( sched[a].begin()+t1, taskShortNum);
//updating sched[b]
sched[b].erase( sched[b].begin()+taskShortInd );
t2=0;
while(t2 < sched[b].size() and tasks[sched[b][t2]] > taskLong){
t2++;
}
sched[b].insert( sched[b].begin()+t2, taskLongNum);
//updating machinesCharge
sol.machinesCharge[a] -= taskLong - taskShort;
sol.machinesCharge[b] += taskLong - taskShort;
//updating finalTime
sol.finalTime=0;
for(int i=0; i<sol.machinesCharge.size(); ++i){
if(sol.machinesCharge[i]>sol.finalTime)
sol.finalTime = sol.machinesCharge[i];
}
}
}while(found);
return sol;
}