-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossovers.py
243 lines (173 loc) · 9.53 KB
/
crossovers.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
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
# Import the necessary libraries
from random import random, randint
def uniform_day_crossover(parent1, parent2):
"""
Perform uniform crossover on two parent individuals at the day level.
Each day's schedule is swapped between parents with a 50% probability.
Parameters:
- parent1 (list): The first parent individual.
- parent2 (list): The second parent individual.
Returns:
- offspring1 (list): The first offspring generated from the parents.
- offspring2 (list): The second offspring generated from the parents.
"""
# Initialize empty lists to store the offspring
offspring1 = []
offspring2 = []
# Ensure both parents have the same number of Practical Turns
if len(parent1) == len(parent2):
for turn_ in range(len(parent1)):
# Ensure both parents have the same number of days for the current Practical Turn
if len(parent1[turn_]) == len(parent2[turn_]):
# Initialize empty lists to store the daily schedules for each Practical Turn in the offspring
class_schedule1 = []
class_schedule2 = []
for day in range(len(parent1[turn_])):
# With a 50% probability, swap the daily schedules between parents
if random() < 0.5:
class_schedule1.append(parent1[turn_][day][:])
class_schedule2.append(parent2[turn_][day][:])
else:
class_schedule1.append(parent2[turn_][day][:])
class_schedule2.append(parent1[turn_][day][:])
# Add the Practical Turn schedules to the offspring
offspring1.append(class_schedule1)
offspring2.append(class_schedule2)
else:
print("Parents must have the same number of days for each Practical Turn")
else:
print("Parents must have the same number of Practical Turns")
return offspring1, offspring2
def uniform_block_crossover(parent1, parent2):
"""
Perform uniform crossover on two parent individuals at the block level.
Each block within a day is swapped between parents with a 50% probability.
Parameters:
- parent1 (list): The first parent individual.
- parent2 (list): The second parent individual.
Returns:
- offspring1 (list): The first offspring generated from the parents.
- offspring2 (list): The second offspring generated from the parents.
"""
# Initialize empty lists to store the offspring
offspring1 = []
offspring2 = []
# Ensure both parents have the same number of Practical Turns
if len(parent1) == len(parent2):
for turn_idx in range(len(parent1)):
# Ensure both parents have the same number of days for the current Practical Turn
if len(parent1[turn_idx]) == len(parent2[turn_idx]):
# Initialize empty lists to store the daily schedules for each Practical Turn in the offspring
class_schedule1 = []
class_schedule2 = []
for day_idx in range(len(parent1[turn_idx])):
# Ensure both parents have the same number of blocks for the current day
if len(parent1[turn_idx][day_idx]) == len(parent2[turn_idx][day_idx]):
# Initialize empty lists to store the block schedules for each day in the offspring
day_schedule1 = []
day_schedule2 = []
for block_idx in range(len(parent1[turn_idx][day_idx])):
# With a 50% probability, swap the blocks between parents
if random() < 0.5:
day_schedule1.append(parent1[turn_idx][day_idx][block_idx][:])
day_schedule2.append(parent2[turn_idx][day_idx][block_idx][:])
else:
day_schedule1.append(parent2[turn_idx][day_idx][block_idx][:])
day_schedule2.append(parent1[turn_idx][day_idx][block_idx][:])
# Add the day schedules to the class schedules
class_schedule1.append(day_schedule1)
class_schedule2.append(day_schedule2)
else:
print(f"Parents must have the same number of blocks for each day in each Practical Turn")
# Add the class schedules to the offspring
offspring1.append(class_schedule1)
offspring2.append(class_schedule2)
else:
print("Parents must have the same number of days for each Practical Turn")
else:
print("Parents must have the same number of Practical Turns")
return offspring1, offspring2
def single_point_day_crossover(parent1, parent2):
"""
Perform single-point crossover on two parent individuals at the day level.
A random crossover point is selected, and the days are swapped between parents at that point.
Parameters:
- parent1 (list): The first parent individual.
- parent2 (list): The second parent individual.
Returns:
- offspring1 (list): The first offspring generated from the parents.
- offspring2 (list): The second offspring generated from the parents.
"""
# Initialize empty lists to store the offspring
offspring1 = []
offspring2 = []
# Iterate over each Practical Turn
for turn_index in range(len(parent1)):
# Select a random crossover point for the current Practical Turn
xo_point = randint(1, len(parent1[turn_index]) - 1)
# Initialize empty lists to store the daily schedules for each Practical Turn in the offspring
class_schedule1 = []
class_schedule2 = []
# Before the crossover point, copy days from parent1 to offspring1 and from parent2 to offspring2
for day_index in range(xo_point):
class_schedule1.append(parent1[turn_index][day_index][:])
class_schedule2.append(parent2[turn_index][day_index][:])
# After the crossover point, copy days from parent2 to offspring1 and from parent1 to offspring2
for day_index in range(xo_point, len(parent1[turn_index])):
class_schedule1.append(parent2[turn_index][day_index][:])
class_schedule2.append(parent1[turn_index][day_index][:])
# Add the class schedules to the offspring
offspring1.append(class_schedule1)
offspring2.append(class_schedule2)
return offspring1, offspring2
def single_point_block_crossover(parent1, parent2):
"""
Perform single-point crossover on two parent individuals at the block level.
A random crossover point is selected for each day, and the blocks are swapped between parents at that point.
Parameters:
- parent1 (list): The first parent individual.
- parent2 (list): The second parent individual.
Returns:
- offspring1 (list): The first offspring generated from the parents.
- offspring2 (list): The second offspring generated from the parents.
"""
# Initialize empty lists to store the offspring
offspring1 = []
offspring2 = []
# Iterate over each Practical Turn
for turn_index in range(len(parent1)):
# Initialize empty lists to store the daily schedules for each Practical Turn in the offspring
class_schedule1 = []
class_schedule2 = []
# Iterate over each day
for day_index in range(len(parent1[turn_index])):
# Select a random crossover point for the current day
xo_point = randint(1, len(parent1[turn_index][day_index]) - 1)
# Initialize empty lists to store the block schedules for each day in the offspring
day_schedule1 = []
day_schedule2 = []
# Iterate over each block
for block_index in range(len(parent1[turn_index][day_index])):
# Before the crossover point, copy blocks from parent1 to offspring1 and from parent2 to offspring2
if block_index < xo_point:
day_schedule1.append(parent1[turn_index][day_index][block_index])
day_schedule2.append(parent2[turn_index][day_index][block_index])
# After the crossover point, copy blocks from parent2 to offspring1 and from parent1 to offspring2
else:
day_schedule1.append(parent2[turn_index][day_index][block_index])
day_schedule2.append(parent1[turn_index][day_index][block_index])
# Add the day schedules to the Practical Turn schedules
class_schedule1.append(day_schedule1)
class_schedule2.append(day_schedule2)
# Add the Practical Turn schedules to the offspring
offspring1.append(class_schedule1)
offspring2.append(class_schedule2)
return offspring1, offspring2
def uniform_day_crossover_named(parent1, parent2):
return uniform_day_crossover(parent1, parent2)
def uniform_block_crossover_named(parent1, parent2):
return uniform_block_crossover(parent1, parent2)
def single_point_day_crossover_named(parent1, parent2):
return single_point_day_crossover(parent1, parent2)
def single_point_block_crossover_named(parent1, parent2):
return single_point_block_crossover(parent1, parent2)