Skip to content

Commit 04abfc2

Browse files
committed
fixed cases where hybrid fn selects invalid slots
1 parent 53dcc25 commit 04abfc2

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

evolve.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,29 @@ def selectAvailableDayAndHour(available, availableDays, session):
6868
if INITIALISATION_METHOD == 1:
6969
return False, False
7070
if INITIALISATION_METHOD == 2:
71-
day = random.choice(availableDays)
72-
hour = random.choice(available[day])
73-
return day, hour
71+
return selectRandomDayAndHour(available, availableDays, session)
72+
73+
74+
def selectRandomDayAndHour(available, availableDays, session):
75+
while True:
76+
day = random.choice(availableDays)
77+
length = session.length
78+
if length == 2:
79+
availableFor2 = [
80+
slot for slot in available[day] if slot != 17]
81+
if availableFor2:
82+
hour = random.choice(availableFor2)
83+
else:
84+
continue
85+
else:
86+
availableFor3 = [
87+
slot for slot in available[day] if slot not in [16, 17]]
88+
if availableFor3:
89+
hour = random.choice(availableFor3)
90+
else:
91+
continue
92+
93+
return day, hour
7494

7595

7696
def generateRandomSchedule():
@@ -106,14 +126,14 @@ def performCrossover(schedule1, schedule2):
106126

107127

108128
def selection(population, size, elite2):
109-
# if INITIALISATION_METHOD == 1:
110-
scores = [max(schedule.fitness, 1) for schedule in population]
111-
# else:
112-
# sortedPopulation = sorted(
113-
# population, key=lambda schedule: schedule.fitness)
114-
# worstFitness = sortedPopulation[0].fitness
115-
# offset = 0 - worstFitness
116-
# scores = [schedule.fitness + offset for schedule in population]
129+
if INITIALISATION_METHOD == 1:
130+
scores = [max(schedule.fitness, 1) for schedule in population]
131+
else:
132+
sortedPopulation = sorted(
133+
population, key=lambda schedule: schedule.fitness)
134+
worstFitness = sortedPopulation[0].fitness
135+
offset = 0 - worstFitness
136+
scores = [schedule.fitness + offset for schedule in population]
117137

118138
selected = random.choices(population, scores, k=size)
119139
selected.extend(elite2)

0 commit comments

Comments
 (0)