-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutations.py
92 lines (64 loc) · 3.18 KB
/
mutations.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
# Import the necessary libraries
from random import sample
def block_swap_mutation(individual):
"""
Perform block swap mutation on an individual.
This mutation operator selects two random blocks within each day for each Practical Turn and swaps them.
Parameters:
- individual (list): The individual to be mutated.
Returns:
- individual (list): The mutated individual.
"""
# Iterates over all Practical Turns
for turn_index in range(len(individual)):
# Iterates over all days
for day_index in range(len(individual[0])):
# Chooses randomly 2 blocks from the total number of blocks
block_indexes = sample(range(0, len(individual[0][0])), 2)
# Swaps the two selected blocks in the day's schedule
individual[turn_index][day_index][block_indexes[0]], individual[turn_index][day_index][block_indexes[1]] = (
individual[turn_index][day_index][block_indexes[1]], individual[turn_index][day_index][block_indexes[0]])
return individual
def block_inversion_mutation(individual):
"""
Perform block inversion mutation on an individual.
This mutation operator selects a random range of blocks within each day of each Practical Turn and inverts the order
of those blocks.
Parameters:
- individual (list): The individual to be mutated.
Returns:
- individual (list): The mutated individual.
"""
# Iterates over all Practical Turns
for class_index in range(len(individual)):
# Iterates over all days
for day_index in range(len(individual[0])):
# Chooses randomly 2 blocks from the total number of blocks and ensures they are not consecutive
block_indexes = sample(range(0, len(individual[0][0])), 2)
while abs(block_indexes[0] - block_indexes[1]) == 1:
block_indexes = sample(range(0, len(individual[0][0])), 2)
block_indexes.sort()
# Inverts the order of the blocks within the selected range
individual[class_index][day_index][block_indexes[0]:block_indexes[1]] = (
individual[class_index][day_index][block_indexes[0]:block_indexes[1]][::-1])
return individual
def block_scramble_mutation(individual):
"""
Applies block scramble mutation to an individual. This mutation randomly scrambles the order of blocks
within each day for all Practical Turns.
Parameters:
- individual (list): The individual to be mutated.
Returns:
- list: The mutated individual.
"""
# Iterates over all Practical Turns
for turn_index in range(len(individual)):
# Iterates over all days within the current Practical Turn
for day_index in range(len(individual[0])):
# Gets the blocks for the current day
blocks = individual[turn_index][day_index]
# Scrambles the order of the blocks
scrambled_blocks = sample(blocks, len(blocks))
# Replaces the original blocks with the scrambled blocks
individual[turn_index][day_index] = scrambled_blocks
return individual