-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.py
173 lines (141 loc) · 3.58 KB
/
scheduler.py
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
import random
import math
def get_time_vec(sol,etc):
time_vec = [0 for _ in range(len(etc))]
for i in range(len(sol)):
time_vec[sol[i]] += etc[sol[i]][i]
return time_vec
def makespan(sol):
return max(get_time_vec(sol,etc))
def utilization(sol,etc):
m = len(etc)
time_vec = get_time_vec(sol,etc)
mspan = max(time_vec)
mpat = sum(time_vec)/m
return mpat/mspan
def reset(pop):
for i in pop:
i.pop(-1)
return pop
#for GA
def calcAndSort(pop,etc):
for i in pop:
i.append(makespan(i))
pop.sort(key=lambda x:x[-1])
return pop
def p(pop):
for i in pop:
print(i)
def GA(pop,etc):
m = len(etc)
n = len(etc[0])
genes="01234"
newgen=[]
count=1
while count!=len(newgen):
newgen=[]
#generation of makespan for each solution and then sorting(asc)
pop=calcAndSort(pop,etc)
#selection
selected=math.ceil(len(pop)/2)
pop=pop[:selected]
pop=reset(pop)
#crossover
for _ in range(m*2):
parent1=random.choice(pop)
parent2=random.choice(pop)
cp1=random.randint(0,n-1)
cp2=random.randint(0,n-1)
maxCp=max(cp1,cp2)
minCp=min(cp1,cp2)
child1=parent1[:minCp]+parent2[minCp:maxCp]+parent1[maxCp:]
#mutation
mpoint=random.randint(0,n-1)
child1[mpoint]=int(random.choice(genes))
child2=parent2[:minCp]+parent1[minCp:maxCp]+parent2[maxCp:]
newgen.extend([child1,child2])
select=len(newgen)-(count-1)
newgen=newgen[:select]
pop.extend(newgen)
count+=1
return pop[0]
def mkspn_sort(elem):
return elem[-1]
def BFO(pop,etc):
#Setting algorithm constraints
m = len(etc)
n = len(etc[0])
S = len(pop)
ned = 500 #no of elimination steps
nc = 10 #no of chemotaxis steps
ped = 0.2 #probability of bacteria elimination
for df in range(ned):
for i in range(len(pop)):
#tumble and move
for _ in range(nc):
time_vec = get_time_vec(pop[i],etc)
max_node = time_vec.index(max(time_vec))
min_node = time_vec.index(min(time_vec))
tasks = []
for k in range(n):
if pop[i][k] == max_node:
tasks.append(k)
tumbler = tasks[0]
for t in tasks:
if etc[max_node][t]<etc[max_node][tumbler]:
tumbler = t
pop[i][tumbler] = min_node
pop.sort(key = makespan)
#reproduce
pop = pop[:int(math.ceil(len(pop)/2))] + pop[:int(math.ceil(len(pop)/2))]
random.shuffle(pop)
#eliminate and replace
ct = int(len(pop)*ped)
for _ in range(ct):
pop.pop(int(random.random()*len(pop)))
for _ in range(ct):
l = []
for _ in range(len(pop[0])):
l.append(int(random.random()*m))
pop.append(l)
pop.sort(key=makespan)
return pop[0][:-1]
def main():
#loading the speed vector
node_file = open('input/nodes')
mips = list(map(float,node_file.read().strip().split(',')))
m = len(mips)
#loading the task sizes
task_file = open('input/tasks')
mi = list(map(float,task_file.read().strip().split(',')))
n = len(mi)
#generating the ETC Matrix
global etc
etc = [[0 for _ in range(n)] for _ in range(m)]
for i in range(m):
for j in range(n):
etc[i][j] = mi[j]/mips[i]
#generating population
S = m*2
pop = []
for _ in range(S):
l=[]
for _ in range(n):
l.append(int(random.random()*m))
pop.append(l)
pop.sort(key=makespan)
print("Initial makespan:",makespan(pop[0]),"\n")
#running BFO algorithm
sol_bfo = BFO(pop,etc)
print("Solution from BFO algorithm:")
print(sol_bfo)
print("Makespan:",makespan(sol_bfo))
print("Utilization:",utilization(sol_bfo,etc),"\n")
# #running GA algorithm
sol_ga = GA(pop,etc)
print("Solution from GA algorithm:")
print(sol_ga)
print("Makespan:",makespan(sol_ga))
print("Utilization:",utilization(sol_ga,etc),"\n")
if __name__=='__main__':
main()