-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutation.py
More file actions
50 lines (40 loc) · 1.67 KB
/
Copy pathmutation.py
File metadata and controls
50 lines (40 loc) · 1.67 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
"""
CISC455 Final Project
Mutation Module
Joshua Chai-Tang 20119074
"""
# My module imports
import representation
# Python module imports
import random
import copy
"""
Function for handling random mutation of an individual strategy
-randomly determines which mutation forms to use based on given probabilities
-an individual can undergo both micro and macro mutation, but also has a chance of neither
-applies the chosen micro and macro mutation methods
Params:
-individual : Strategy : the individual solution to be mutated
-mutation_rate : float : the chance of mutation occurring, out of 1
-party_size : int : the party size these individuals are designed for
-lower_limit : int : the minimum allowed TPV value (ABSOLUTELY MUST BE GREATER THAN 0)
-upper_limit : int : the maximum allowed TPV value
-debug : boolean : if set to true, will print the entire mutation process (step by step) to the console
Returns:
-Nothing, as the mutated individual's strat attribute is altered directly.
"""
def mutate(individual,mutation_rate,party_size,lower_limit,upper_limit,debug=False):
if debug:
print("Mutating individual:")
print(individual)
# iterate through every TPVL and apply mutations to random TPV's
for i in range(party_size):
for j in range(party_size):
# determine if this TPV should be mutated
if (random.uniform(0,1) < mutation_rate):
# replace the TPV with a random new value
individual.strat[i][j] = random.randint(lower_limit,upper_limit)
if debug:
print("Individual post mutation:")
print(individual)
return