-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
144 lines (113 loc) · 5.61 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
import genetic
import eel
import json
def generate_schedule_json(population, max_index, max_fitness):
json_string = ''
for session in population[max_index]:
if json_string != '':
json_string += ','
students_str = ''
for student in session.get_project().get_students():
if students_str != '':
students_str += ','
students_str += f'"{student}"'
json_string += f'{{' \
f'"project": {{' \
f'"name": "{session.get_project().get_name()}",' \
f'"supervisor": "{session.get_project().get_supervisor().get_name()}",' \
f'"students": [{students_str}]' \
f'}},' \
f'"day": {session.get_day()},' \
f'"time": {session.get_time()},' \
f'"room": "{session.get_room()}",' \
f'"examiners": [' \
f'"{session.get_examiners()[0].get_name()}",' \
f'"{session.get_examiners()[1].get_name()}"' \
f']}}'
json_string = f'{{' \
f'"schedule": [{json_string}],' \
f'"numberOfConflicts": {int((1 / max_fitness) - 1)}' \
f'}}'
print(json_string)
return json_string
@eel.expose
def optimize_schedule(pop_size=20, iterations=2000, disable_preferences=False, disable_range=False, disable_consecutive_slots=False):
conflicts_disable = [disable_preferences, disable_range, disable_consecutive_slots]
population = genetic.populate(pop_size)
max_fitness, max_index, min_fitness, min_index = genetic.get_max_min_fitness(population, conflicts_disable[0],
conflicts_disable[1], conflicts_disable[2])
print(f'Iteration: 0\nMax Fitness: {max_fitness}\n************')
for i in range(iterations):
if max_fitness == 1:
break
# if iterations >= 1000:
# if i == int(iterations * 0.4):
# conflicts_disable[1] = True
# population = genetic.populate(pop_size)
#
# if i == int(iterations * 0.75):
# conflicts_disable[0] = True
# population = genetic.populate(pop_size)
population = genetic.get_next_gen(population, conflicts_disable[0], conflicts_disable[1], conflicts_disable[2])
new_population = genetic.populate(2)
for chromosome in new_population:
if genetic.get_fitness(chromosome, conflicts_disable[0], conflicts_disable[1], conflicts_disable[2]) > min_fitness:
population.pop(min_index)
population += [chromosome]
max_fitness, max_index, min_fitness, min_index = genetic.get_max_min_fitness(population, conflicts_disable[0],
conflicts_disable[1], conflicts_disable[2])
print(f'Iteration: {i + 1}\nMax Fitness: {max_fitness}\n************')
json_string = generate_schedule_json(population, max_index, max_fitness)
return json_string
__population = []
__conflicts_disable = [False, False, False]
__iteration = 0
__fitness = {
'max': {
'value': 0,
'index': 0
},
'min': {
'value': 0,
'index': 0
},
'avg': 0,
'schedule': None,
'numberOfConflicts': 0
}
@eel.expose
def populate(disable_preferences, disable_range, disable_consecutive_slots, pop_size=20):
global __population, __fitness, __iteration
__population.clear()
__population += genetic.populate(pop_size)
__fitness['max']['value'], __fitness['max']['index'], __fitness['min']['value'], __fitness['min'][
'index'], __fitness['avg'] = genetic.get_max_min_avg_fitness(__population, disable_preferences, disable_range, disable_consecutive_slots)
schedule = json.loads(generate_schedule_json(__population, __fitness['max']['index'], __fitness['max']['value']))
__fitness['schedule'] = schedule['schedule']
__fitness['numberOfConflicts'] = schedule['numberOfConflicts']
print(f'Iteration: {__iteration}\nMax Fitness: {__fitness["max"]["value"]}\n************')
__iteration += 1
return json.dumps(__fitness)
@eel.expose
def get_next_gen(disable_preferences, disable_range, disable_consecutive_slots):
global __population, __fitness, __iteration
new_population = genetic.get_next_gen(
__population, disable_preferences, disable_range, disable_consecutive_slots)
__population.clear()
__population += new_population
new_population = genetic.populate(2)
for chromosome in new_population:
if genetic.get_fitness(chromosome, disable_preferences, disable_range, disable_consecutive_slots) > __fitness['min']['value']:
__population.pop(__fitness['min']['index'])
__population += [chromosome]
__fitness['max']['value'], __fitness['max']['index'], __fitness['min']['value'], __fitness['min'][
'index'], __fitness['avg'] = genetic.get_max_min_avg_fitness(__population, disable_preferences, disable_range, disable_consecutive_slots)
print(f'Iteration: {__iteration}\nMax Fitness: {__fitness["max"]["value"]}\n************')
schedule = json.loads(generate_schedule_json(__population, __fitness['max']['index'], __fitness['max']['value']))
__fitness['schedule'] = schedule['schedule']
__fitness['numberOfConflicts'] = schedule['numberOfConflicts']
__iteration += 1
return json.dumps(__fitness)
eel.init('dist')
# eel.start('index.html', mode='electron')
eel.start('index.html')