forked from millebesso/gensched
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
145 lines (110 loc) · 3.87 KB
/
main.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
from random import randint, choice, seed
from math import sqrt
import sys
from datetime import datetime
# TARGET = [0, 0, 1, 2, 3, 5, 5, 5, 6, 3, 1]
TARGET = [0, 5, 10, 15, 15, 20, 15, 15, 10, 10, 25, 30, 20, 10, 20, 25, 10,
10, 0]
MAX_SHIFT_LENGTH = 8
MIN_SHIFT_LENGTH = 3
MUTATION_RATE = 0.25
POPULATION = 1000
GENERATIONS = 100
NEGATIVE_PENALTY_FACTOR = 1
def run():
seed()
start = datetime.now()
schedule = initialize()
output(schedule)
for i in range(GENERATIONS):
population = [copy_schedule(schedule) for x in range(POPULATION)]
mutate_population(population)
current_rms = get_rms(schedule)
new_schedule = get_best_schedule(population)
new_rms = get_rms(new_schedule)
if new_rms < current_rms:
schedule = new_schedule
print(f'Generation {i}')
output(schedule)
if new_rms == 0:
break
elapsed_seconds = (datetime.now() - start).seconds
print('-------------------------')
output(schedule)
print(f'Agents: {len(schedule)}')
print(f'Seconds elapsed: {elapsed_seconds}')
def get_best_schedule(population):
best_rms = float(sys.maxsize)
best_schedule = None
for schedule in population:
current_rms = get_rms(schedule)
if current_rms < best_rms:
best_rms = current_rms
best_schedule = schedule
return best_schedule
def mutate_population(population):
mutations = max(1, int(MUTATION_RATE*len(population[0])))
for schedule in population:
for i in range(mutations):
mutation = choice(['update', 'add', 'remove'])
if mutation == 'update':
schedule[randint(0, len(schedule)-1)] = create_agent_shift()
elif mutation == 'add':
schedule.append(create_agent_shift())
elif mutation == 'remove':
del schedule[len(schedule)-1]
def schedule_update(schedule, position, value):
schedule[position] = value
def output(schedule):
print(get_schedule_as_string(schedule))
print(format_array(get_slot_sum(schedule)))
print(format_array(TARGET))
print(f'RMS: {get_rms(schedule)}')
print()
def copy_schedule(schedule):
return [shift.copy() for shift in schedule]
def initialize():
schedule = []
# Approximating the number of needed agents to twice the number max
# concurrent agents. Would work with 0 too.
agents = max(TARGET)*2
for i in range(agents):
shift = create_agent_shift()
schedule.append(shift)
return schedule
def get_slot_sum(schedule):
sum = [0 for x in range(len(TARGET))]
for shift in schedule:
for i in range(len(TARGET)):
sum[i] += shift[i]
return sum
def create_agent_shift():
slots = len(TARGET)
start = randint(0, slots - MIN_SHIFT_LENGTH)
length = randint(MIN_SHIFT_LENGTH, min(MAX_SHIFT_LENGTH, slots-start))
shift = [
1 if x >= start and x <= start+length-1 else 0 for x in range(slots)]
return shift
def get_schedule_as_string(schedule):
schedule_as_string = ''
for i in range(len(schedule)):
shift_string = format_array(schedule[i])
schedule_as_string += f'{shift_string}\n'
return schedule_as_string
def format_array(array):
format_string = ''.join('{:4}'*(len(array)))
return format_string.format(*array)
def get_rms(schedule):
slot_sum = get_slot_sum(schedule)
square_diff = []
for i in range(len(TARGET)):
if slot_sum[i] < TARGET[i]:
square_diff.append(
(NEGATIVE_PENALTY_FACTOR * (slot_sum[i] - TARGET[i]))**2)
else:
square_diff.append((slot_sum[i] - TARGET[i])**2)
# square_diff = [(slot_sum[i] - TARGET[i])**2 for i in range(len(TARGET))]
mean_square = sum(square_diff) / len(square_diff)
return sqrt(mean_square)
if __name__ == '__main__':
run()