-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.py
More file actions
348 lines (296 loc) · 12.3 KB
/
function.py
File metadata and controls
348 lines (296 loc) · 12.3 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import random
import math
unavailability_constraints = {}
course_capacities = {}
room_capacities = {}
course_teachers = {}
# parse ctt file
def parse_ctt_file(filename):
courses = []
rooms = []
periods = 0
days = 0
periods_per_day = 0
with open(filename, 'r') as file:
lines = file.readlines()
for line in lines:
line = line.strip()
if line.startswith("Days:"):
#get days
days = int(line.split(":")[1].strip())
elif line.startswith("Periods_per_day:"):
#get periods per day
periods_per_day = int(line.split(":")[1].strip())
in_courses = False
in_rooms = False
in_unavailability = False
for line in lines:
line = line.strip()
if line == "COURSES:":
in_courses = True
in_rooms = False
continue
elif line == "ROOMS:":
in_courses = False
in_rooms = True
continue
elif line == "UNAVAILABILITY_CONSTRAINTS:":
in_courses = False
in_rooms = False
in_unavailability = True
continue
elif line.endswith(":"):
in_courses = False
in_rooms = False
continue
if in_courses:
#get courses
course = line.split()
if len(course) >= 2:
course_id, teacher_id,course_capacity = course[0], course[1],course[4]
courses.append(course_id)
course_teachers[course_id] = teacher_id
course_capacities[course_id] = course_capacity
elif in_rooms:
parts = line.split()
if len(parts) >= 1:
#get rooms
capacity = int(parts[1])
rooms.append(parts[0])
room_capacities[parts[0]] = capacity
elif in_unavailability:
# Example: c0001 4 0
parts = line.split()
if len(parts) == 3:
course_id, day, period = parts[0], int(parts[1]), int(parts[2])
if course_id not in unavailability_constraints:
unavailability_constraints[course_id] = []
unavailability_constraints[course_id].append((day, period))
periods = days * periods_per_day
return courses, rooms, periods, days, periods_per_day, unavailability_constraints, course_capacities, room_capacities,course_teachers
# init population
def time_index(day, period, periods_per_day):
return day * periods_per_day + period
def build_valid_assignment_map(courses, rooms, periods, periods_per_day, unavailability_constraints):
assignment_map = {}
for course in courses:
unavailable = unavailability_constraints.get(course, [])
unavailable_indices = set(time_index(d, p,periods_per_day) for d, p in unavailable)
valid = []
for t in range(periods):
if t not in unavailable_indices:
for r in rooms:
valid.append((t, r))
assignment_map[course] = valid
return assignment_map
def initialize_population(courses,population_size,valid_assignment_map):
population = []
for i in range(population_size):
chromosome = {}
for course in courses:
valid_slots = valid_assignment_map.get(course, [])
if valid_slots:
timeslot, room = random.choice(valid_slots)
chromosome[course] = (timeslot, room)
else:
chromosome[course] = (-1, None)
population.append(chromosome)
return population
# fitness
def fitness(chromosome, course_teachers):
penalty = 0
seen = set()
timeslot_courses = {}
for course, (timeslot, room) in chromosome.items():
key = (timeslot, room)
if key in seen:
penalty += 1
seen.add(key)
if (course, timeslot) in unavailability_constraints:
penalty += 1
if room is not None and course is not None:
if int(course_capacities[course]) > int(room_capacities[room]):
penalty += 1
if timeslot not in timeslot_courses:
timeslot_courses[timeslot] = []
timeslot_courses[timeslot].append(course)
for ts, course_list in timeslot_courses.items():
teachers = [course_teachers.get(c) for c in course_list]
for i in range(len(teachers)):
for j in range(i + 1, len(teachers)):
if teachers[i] == teachers[j]:
penalty += 1
return penalty
# ---------- roulette,pick a higher individual. ----------
def roulette_wheel_selection(population, fitnesses):
#Pan
adjusted_fitnesses = [1 / (f + 1) for f in fitnesses]
total_fitness = sum(adjusted_fitnesses)
pick = random.uniform(0, total_fitness)
current = 0
for i, fit in enumerate(adjusted_fitnesses):
current += fit
if current > pick:
return population[i]
return population[-1]
# ---------- generate child ----------
def crossover(parent1, parent2, valid_assignment_map):
point = random.randint(1, len(parent1) - 1)
keys = list(parent1.keys())
#parent is chromosome, which equals population[i]
child1,child2 = {},{}
used1, used2 = set(), set()
for i in keys[:point]:
slot1 = parent1[i]
slot2 = parent2[i]
if slot1 not in used1:
child1[i] = slot1
used1.add(child1[i])
else:
child1[i] = (-1, None)
if slot2 not in used2:
child2[i] = slot2
used2.add(child2[i])
else:
child2[i] = (-1, None)
for j in keys[point:]:
slot1 = parent2[j]
slot2 = parent1[j]
if slot1 not in used1:
child1[j] = slot1
used1.add(child1[j])
elif parent1[j] not in used1:
child1[j] = parent1[j]
used1.add(parent1[j])
else:
child1[j] = (-1, None)
if slot2 not in used2:
child2[j] = slot2
used2.add(child2[j])
elif parent2[j] not in used2:
child2[j] = parent2[j]
used2.add(parent2[j])
else:
child2[j] = (-1, None)
return child1, child2
# ---------- mutate, mutation_rate=0.1----------
def mutate(chromosome, mutation_rate, valid_assignment_map):
used_slots = set(chromosome.values())
for course, (timeslot, room) in chromosome.items():
if random.random() < mutation_rate:
valid_slots = valid_assignment_map.get(course, [])
available = [slot for slot in valid_slots if slot not in used_slots]
if available:
new_slot = random.choice(available)
used_slots.add(new_slot)
chromosome[course] = new_slot
used_slots.remove(chromosome[course])
return chromosome
# ---------- GA main function ----------
def genetic_algorithm(filename, generations, population_size, mutation_rate, hybrid):
#get course,room,timeslot,unavailable
courses, rooms, periods,days, periods_per_day,unavailability_constraints,course_capacities, room_capacities, course_teachers = parse_ctt_file(filename)
print(f"course account: {len(courses)}")
print(f"room account: {len(rooms)}")
print(f"timeslot(periodPerDay * Day): {periods}")
print(f"unavailability_constraints: {unavailability_constraints}")
valid_assignment_map = build_valid_assignment_map(
courses, rooms, periods, periods_per_day, unavailability_constraints
)
population = initialize_population(courses, population_size, valid_assignment_map)
best_solution = None
best_fitness = float('inf')
for generation in range(generations):
fitnesses = [fitness(chromo, course_teachers) for chromo in population]
strong_index = fitnesses.index(min(fitnesses))
gen_best = min(fitnesses)
best_candidate = population[strong_index]
if gen_best == 0:
best_solution = best_candidate
best_fitness = gen_best
print(f"Best solution found at generation {generation}")
return best_solution, best_fitness, courses
#If not = 0, keep the best individual to the next generation
new_population = [best_candidate]
while len(new_population) < population_size:
parent1 = roulette_wheel_selection(population, fitnesses)
parent2 = roulette_wheel_selection(population, fitnesses)
if parent1 != parent2:
child1, child2 = crossover(parent1, parent2, valid_assignment_map)
child1 = mutate(child1, mutation_rate, valid_assignment_map)
child2 = mutate(child2, mutation_rate, valid_assignment_map)
new_population.extend([child1, child2])
#update population
population = new_population[:population_size]
gen_best = min(fitnesses)
if gen_best < best_fitness:
best_fitness = gen_best
best_solution = best_candidate
if hybrid:
print("\nRunning Simulated Annealing for local optimization...\n")
best_solution, best_fitness, courses = simulated_annealing(
filename,
initial_temp=100,
cooling_rate=0.97,
min_temp=0.01,
max_iter=30000,
initial_solution=best_solution,
valid_assignment_map=valid_assignment_map,
best_fitness=best_fitness
)
#print(f"Generation {generation}: Best Solution = {best_solution} : Best Fitness = {best_fitness}")
return best_solution, best_fitness, courses
def simulated_annealing(filename, initial_temp, cooling_rate, min_temp, max_iter, initial_solution = None, valid_assignment_map = None,best_fitness = None):
#Initial solution → Neighborhood generation → Acceptance criteria → Cooling mechanism → Termination condition
courses, rooms, periods,days, periods_per_day,unavailability_constraints,course_capacities, room_capacities,course_teachers = parse_ctt_file(filename)
print(f"course account: {len(courses)}")
print(f"room account: {len(rooms)}")
print(f"timeslot(periodPerDay * Day): {periods}")
print(f"unavailability_constraints: {unavailability_constraints}")
if initial_solution is None:
valid_assignment_map = build_valid_assignment_map(
courses, rooms, periods, periods_per_day, unavailability_constraints
)
current_solution = {}
for course in courses:
valid_slots = valid_assignment_map.get(course, [])
if valid_slots:
current_solution[course] = random.choice(valid_slots)
else:
current_solution[course] = (-1, None)
current_fitness = fitness(current_solution, course_teachers)
if current_fitness == 0:
return current_solution, current_fitness, courses
else:
valid_assignment_map = valid_assignment_map
current_solution = initial_solution.copy()
current_fitness = best_fitness
T = initial_temp
for iteration in range(max_iter):
if T <= min_temp:
break
neighbor = current_solution.copy()
#course = random.choice(courses)
#if valid_assignment_map[course]:
# neighbor[course] = random.choice(valid_assignment_map[course])
course = random.choice(courses)
timeslot, room = current_solution[course]
if random.random() < 0.5:
new_timeslot = (timeslot + random.choice([-1, 1])) % periods
neighbor[course] = (new_timeslot, room)
else:
neighbor[course] = (timeslot, random.choice(rooms))
neighbor_fitness = fitness(neighbor, course_teachers)
delta = neighbor_fitness - current_fitness
#delta < 0 means neighbor better than current
#e(0.01) ≈ 1.01005, e(-0.01) ≈ 0.99
if delta < 0 or random.random() < math.exp(-delta / T):
current_solution = neighbor
current_fitness = neighbor_fitness
if current_fitness == 0:
return current_solution, current_fitness, courses
if iteration % 100 == 0:
T *= cooling_rate
#compare best run time and fitness, different dataset, compare result with other paper, run time, general algorithm
#avg test results
return current_solution, current_fitness, courses