-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathqpso_all.cpp
273 lines (240 loc) · 7.31 KB
/
qpso_all.cpp
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
#include <bits/stdc++.h>
#define NUM_RESOURCES 4
#define P_MAX 1
#define P_MIN 0
using namespace std;
int N = 32;
int M = 1 ; // No of particles
float G = 0.8 ; // Contraction factor
int MAX_ITERS = 1; // Total no of schedules to be generated.
vector<int> succ_list[123]; // Array of lists that stores successors for every vertex.
vector<int> pred_list[123]; // Array of lists that stores predecesors for every vertex.
vector<int> res_req_list[123]; // 2D-Array storing Resource requiremments for all the activities.
int duration[123];
int max_resources[NUM_RESOURCES];
double mBest[123] ; // m-best used in QPSO equations
double gBest_pos[123];
int gBest_cost = INT_MAX;
// Generates a float random number between zero to one.
double getRandom0to1(){
return (double) rand()/RAND_MAX ;
}
void read_file(char*);
class Particle{
public:
double pos[123] ;
double best_pos[123] ;
int best_cost ;
Particle(){
for(int i=0; i<N ; i++)
best_pos[i] = pos[i] = getRandom0to1();
best_cost = INT_MAX ;
}
void performOps();
int evaluateSchedule(bool forward) ;
}particles[123];
class Activity{
public:
int activity_id ;
int start_time ;
int duration ;
int f_time ;
};
// A comparator function for sorting of feasible activities on
// the basis of decreasing priority.
int current_particle_id = 0;
int compare(const int &a, const int &b){
return (particles[current_particle_id].pos[a] > particles[current_particle_id].pos[b]);
}
// Returns activities which are satisfying precedence constraints
// and have not been scheduled for execution. Resource contraints
// are not checked in this function.
vector<int> getFeasibleActs(bool scheduled[], int completed_preds[], vector<int> *dList){
vector<int> feasible_acts;
for(int i=0 ; i<N ; i++){
if(completed_preds[i] == dList[i].size() && !scheduled[i])
feasible_acts.push_back(i);
}
return feasible_acts ;
}
// Reads the files and then performs the required operations.
void executeOnFile(char* filename){
read_file(filename);
int i, iterations = MAX_ITERS ;
while(iterations--){
for(i=0 ; i<N ; i++){
double b = 0 ;
for(int j=0 ; j<M ; j++)
b += particles[i].pos[j];
mBest[i] = b/M ;
}
for(i=0 ; i<M ; i++){
current_particle_id = i ;
particles[i].performOps();
}
}
printf("%d", gBest_cost) ;
}
// Perform operations on particle. Called once in every iterations.
// Operations include: Updating of particle's position.
void Particle::performOps(){
double c1 = getRandom0to1();
double c2 = getRandom0to1();
double u = getRandom0to1();
double r = getRandom0to1();
if(u == 0.0)
u = 0.5 ;
double P[N] ; //Optimization : Allocate dynamically and reuse
double diff[N], z ;
for(int i=0 ; i<N ; i++){
P[i] = (c1*best_pos[i] + c2* gBest_pos[i])/(c1+c2) ;
diff[i] = abs(mBest[i] - pos[i]) ;
diff[i] *= G*log(1/u) ;
if(r<0.5)
pos[i] = P[i] - diff[i] ;
else
pos[i] = P[i] + diff[i] ;
if(pos[i]<P_MIN)
pos[i] = P_MIN ;
else if(pos[i] > P_MAX)
pos[i] = P_MAX ;
}
int cost = min(evaluateSchedule(true), evaluateSchedule(false));
if(best_cost>cost){
best_cost = cost ;
for(int i=0 ; i<N; i++)
best_pos[i] = pos[i] ;
}
if(gBest_cost>cost){
gBest_cost = cost ;
for(int i=0 ; i<N; i++)
gBest_pos[i] = pos[i] ;
}
}
// Fitness function : Evaluates the schedule in the direction given as a parameter
// If true then forward schedule is evaluated else the backward schedule is evaluated.
// Forms the schedule and returns the makespan(duration of the schedule).
int Particle::evaluateSchedule(bool forward){
vector<Activity> sList ;
bool scheduled[N], finished[N] ;
int completed_preds[N];
int time = 0 ;
for(int i=0 ; i<N ; i++){
scheduled[i] = finished[i] = false ;
completed_preds[i] = 0 ;
}
Activity a0 ;
a0.activity_id = 0 ;
a0.start_time = 0 ;
a0.duration = 0 ;
a0.f_time = 0 ;
sList.push_back(a0) ;
scheduled[0] = true ;
int resources_left[NUM_RESOURCES] ; //OPTIMIZATION: This is redundant
for(int i=0; i<NUM_RESOURCES ; i++)
resources_left[i] = max_resources[i] ;
for(int num_finished = 0 ; num_finished<N ; ){
// cout<<"==>Time:"<<time<<" Finished:"<<num_finished<<"\n";
for(int i=0 ; i<sList.size() ; i++){
if(sList[i].f_time == time && !finished[sList[i].activity_id]){
int id = sList[i].activity_id ;
finished[id] = true ;
// cout<<"Activity "<<id<<" finished.\n";
num_finished++ ;
for(int j=0 ; j<NUM_RESOURCES ; j++){
resources_left[j] += res_req_list[id][j];
// cout<<res_req_list[id][j]<<"/"<<resources_left[j]<<"/"<<max_resources[j]<<"\n";
}
if(forward)
for(int j=0 ; j<succ_list[id].size(); j++)
completed_preds[succ_list[id][j]]++ ;
else
for(int j=0 ; j<pred_list[id].size(); j++)
completed_preds[pred_list[id][j]]++ ;
}
}
vector<int> feasible_activities ;
if(forward)
feasible_activities = getFeasibleActs(scheduled, completed_preds, pred_list) ;
else
feasible_activities = getFeasibleActs(scheduled, completed_preds, succ_list) ;
sort( feasible_activities.begin(), feasible_activities.end(), compare ) ; //sort by decreasing priority ---====CHECk
int flag, id;
for (int i=0 ; i<feasible_activities.size() ; i++){
id = feasible_activities[i] ;
flag = 0 ;
for(int j=0; j<4 ;j++)
if(resources_left[j] < res_req_list[id][j])
flag = 1 ;
if(flag == 0){
// Schedule this activity
Activity a1 ;
a1.activity_id = id ;
a1.start_time = time ;
a1.duration = duration[id];
a1.f_time = a1.start_time + a1.duration ;
sList.push_back(a1) ;
scheduled[id] = true ;
if(a1.start_time == a1.f_time)
time-- ;
for(int j=0 ; j<NUM_RESOURCES ; ++j)
resources_left[j] -= res_req_list[id][j] ;
}
}
time++;
}
int cost = 0;
for(int i=0 ; i<sList.size() ; i++){
cost = max(cost, sList[i].f_time) ;
}
return cost ;
}
// You can modify N, M, G in the python file.
int main(int argc, char **argv){
for(int i=0 ; i<N; i++)
gBest_pos[i] = getRandom0to1();
srand((unsigned)time(NULL));
if(argc>=5){
N = atoi(argv[2]);
M = atoi(argv[3]);
MAX_ITERS = atoi(argv[4])/M ;
executeOnFile(argv[1]);
}
}
// File reading function.
void read_file(char* filename){ //Tested: Working perfectly!
FILE *fp = fopen(filename, "r");
char line[100];
int i = 1 ;
int num[10];
while( fgets(line, 79, fp) != NULL ){
istringstream iss(line) ;
// cout<<" "<<line;
if(19<=i && i<N+19){
//Parse successors
iss>>num[0]>>num[1]>>num[2] ;
// cout<<num[0]<<" has "<<num[2]<<" successors\n";
for(int k=0 ; k<num[2] ; k++){
iss>>num[3];
// cout<<" "<<num[3];
succ_list[num[0]-1].push_back(num[3]-1) ;
pred_list[num[3]-1].push_back(num[0]-1) ;
}
// cout<<"\n";
}
else if(N+19+4<=i && i<N+19+4+N){
//Parse resource requirements
iss>>num[0]>>num[1]>>num[5]>>num[1]>>num[2]>>num[3]>>num[4] ;
// printf("Activity: %d R1:%d R2:%d R3:%d R4%d\n", num[0],num[1],num[2],num[3],num[4]);
res_req_list[num[0]-1].push_back(num[1]);
res_req_list[num[0]-1].push_back(num[2]);
res_req_list[num[0]-1].push_back(num[3]);
res_req_list[num[0]-1].push_back(num[4]);
duration[num[0]-1] = num[5] ;
}
else if(i==2*N+19+4+3){
iss>>max_resources[0]>>max_resources[1]>>max_resources[2]>>max_resources[3];
}
i++;
}
}