-
Notifications
You must be signed in to change notification settings - Fork 0
/
hill_climber.py
94 lines (76 loc) · 2.99 KB
/
hill_climber.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
import preparation
import fitness_function
import random
import csv
def random_schedule(rand_schedule, slot_list):
""" Assigns random slot to every session
"""
copy_list = list(slot_list)
for session in rand_schedule:
slot = random.choice(copy_list)
session.slot = slot
copy_list.remove(slot)
return rand_schedule
def switch(session_1, session_2):
""" Switches slots from 2 sessions
"""
bucket = session_1.slot
session_1.slot = session_2.slot
session_2.slot = bucket
def prep_table(schedule):
""" Prepares html table
"""
table_data = []
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
blocks = ["09:00", "11:00", "13:00", "15:00"]
for i in range(len(schedule)):
if schedule[i].course == schedule[i-1].course and schedule[i].type == schedule[i-1].type and schedule[i].slot.id > schedule[i-1].slot.id:
bucket = schedule[i]
schedule[i] = schedule[i-1]
schedule[i-1] = bucket
for session in schedule:
day = days[session.slot.day]
block = blocks[session.slot.block]
row = [session.course, session.type, day, block, session.slot.room]
table_data.append(row)
return table_data
def hill_climber(data, prep_schedule, slot_list, course_list):
""" Climbs the hill stops when he is stuck
"""
schedule = random_schedule(prep_schedule, slot_list)
old_fitness, old_capacity_minus, old_student_minus, old_sequence_minus = fitness_function.main(schedule, course_list)
new_fitness = 0
# one step
while True:
tries = 0
# one trie
while True:
tries += 1
session_1 = random.choice(schedule)
session_2 = random.choice(schedule)
switch(session_1, session_2)
new_fitness, new_capacity_minus, new_student_minus, new_sequence_minus = fitness_function.main(schedule, course_list)
if new_fitness > old_fitness:
break
else:
switch(session_1, session_2)
if tries > 500:
return False
old_fitness = new_fitness
old_capacity_minus = new_capacity_minus
old_student_minus = new_student_minus
old_sequence_minus = new_sequence_minus
print "Total Fitness: " + str(old_fitness)
print "Capacity Conflict: " + str(old_capacity_minus)
print "Student Conflict: " + str(old_student_minus)
print "Sequence Conflict" + str(old_sequence_minus)
if old_student_minus == 0 and old_capacity_minus == 0 and old_sequence_minus == 0:
table_data = prep_table(schedule)
return table_data, old_capacity_minus, old_student_minus, old_sequence_minus
def main(data):
prep_schedule, slot_list, course_list = preparation.main(data)
for i in range(100):
table_data = hill_climber(data, prep_schedule, slot_list, course_list)
if table_data != False:
return table_data
# main()