-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm_greedy.py
191 lines (143 loc) · 5.16 KB
/
algorithm_greedy.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from __future__ import division
import utils
import stats
import params
import random
def update_avail_time(workers_array):
for worker in workers_array:
worker.ready_time += worker.used_time
stats.new_total_work_time += worker.used_time
worker.used_time = 0
def reset_used_time(workers_array):
for worker in workers_array:
worker.used_time = 0 #maybe this is not necessary since we never use "used_time" before setting it again
def is_step_fully_scheduled(step):
for skills in step.skills:
if skills[1] > 0:
return False
return True
def allocate_jobs_prio_wrap(tasks_array, workers_array):
allocate_jobs_prio(tasks_array, workers_array, 1)
allocate_jobs_prio(tasks_array, workers_array, 0)
def allocate_jobs_prio(tasks_array, workers_array, prio):
for task in tasks_array:
if task.task_prio != prio:
continue
for step in task.steps_array:
res = utils.get_step_status(task, step)
if res == 1:
break
if res == 2:
continue
for skill in step.skills:
required_time = skill[1]
temp_workers = []
random.shuffle(workers_array)
for worker in workers_array:
if skill[0] in worker.skills and skill[1] > 0:
worker.used_time = min(worker.get_avail_time_sec(), required_time)
required_time -= worker.used_time
temp_workers.extend([worker])
if required_time == 0:
update_avail_time(temp_workers)
step.finish_time = max(max(w.ready_time for w in temp_workers),step.finish_time)
skill[1] = 0 #skill is allocated, clear its required time
break
if required_time > 0:
reset_used_time(temp_workers) #skill can't be allocated now.
if is_step_fully_scheduled(step) == True:
step.isFullyScheduled = True
stats.fully_scheduled_steps += 1
def allocate_jobs1(tasks_array, workers_array):
for task in tasks_array:
for step in task.steps_array:
res = utils.get_step_status(task, step)
if res == 1:
break
if res == 2:
continue
for skill in step.skills:
required_time = skill[1]
temp_workers = []
random.shuffle(workers_array)
for worker in workers_array:
if skill[0] in worker.skills and skill[1] > 0:
worker.used_time = min(worker.get_avail_time_sec(), required_time)
required_time -= worker.used_time
temp_workers.extend([worker])
if required_time == 0:
update_avail_time(temp_workers)
step.finish_time = max(max(w.ready_time for w in temp_workers),step.finish_time)
skill[1] = 0 #skill is allocated, clear its required time
break
if required_time > 0:
reset_used_time(temp_workers) #skill can't be allocated now.
if is_step_fully_scheduled(step) == True:
step.isFullyScheduled = True
stats.fully_scheduled_steps += 1
def allocate_jobs_skills_no_split(tasks_array, workers_array):
for task in tasks_array:
for step in task.steps_array:
res = utils.get_step_status(task, step)
if res == 1:
break
if res == 2:
continue
for skill in step.skills:
random.shuffle(workers_array)
for worker in workers_array:
if skill[0] in worker.skills and worker.get_avail_time_sec() >= skill[1]:
worker.used_time = skill[1]
update_avail_time([worker])
step.finish_time = max(max(w.ready_time for w in [worker]),step.finish_time)
skill[1] = 0 #skill is allocated, clear its required time
break
if is_step_fully_scheduled(step) == True:
step.isFullyScheduled = True
stats.fully_scheduled_steps += 1
def allocate_jobs_steps_no_split(tasks_array, workers_array):
for task in tasks_array:
for step in task.steps_array:
res = utils.get_step_status(task, step)
if res == 1:
break
if res == 2:
continue
step_skills_array = [x[0] for x in step.skills]
random.shuffle(workers_array)
for worker in workers_array:
if set(step_skills_array).issubset(worker.skills) is True and\
worker.get_avail_time_sec() >= step.total_skills_time:
worker.used_time = step.total_skills_time
update_avail_time([worker])
step.finish_time = max(max(w.ready_time for w in [worker]),step.finish_time)
for skill in step.skills:
skill[1] = 0
step.isFullyScheduled = True
stats.fully_scheduled_steps += 1
break
def sama_algo_steps_no_split(tasks_array, workers_array):
steps_ready_for_allocation = []
for task in tasks_array:
for step in task.steps_array:
res = utils.get_step_status(task, step)
if res == 1:
break
if res == 2:
continue
steps_ready_for_allocation.append(step)
random.shuffle(steps_ready_for_allocation)
for worker in workers_array:
for step in steps_ready_for_allocation:
step_skills_array = [x[0] for x in step.skills]
if set(step_skills_array).issubset(worker.skills) is True and\
worker.get_avail_time_sec() >= step.total_skills_time and \
step.isFullyScheduled == False:
worker.used_time = step.total_skills_time
update_avail_time([worker])
step.finish_time = max(max(w.ready_time for w in [worker]),step.finish_time)
for skill in step.skills:
skill[1] = 0
step.isFullyScheduled = True
stats.fully_scheduled_steps += 1
break