forked from Asabere/Project-on-Genetic-Algorithm-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutate.py
More file actions
25 lines (23 loc) · 692 Bytes
/
mutate.py
File metadata and controls
25 lines (23 loc) · 692 Bytes
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
import random
#helper function for mutation function
def get_good_candidates(chrom,candidates):
"""
A Helper function for mutation
chrom - (list) - a chromosome
candidates - (list) - list containing candidate nodes
"""
for gene in chrom:
candidates.remove(gene)
return candidates
def mutate(chromosome, Pm):
"""
chromosome - a single chromosome string
Pm - probability of mutation
"""
rnd = random.uniform(0,1)
if rnd < Pm:
ind = random.randint(0,3)
good_candidates = get_good_candidates(chromosome)
new_gene = random.sample(good_candidates)
chromosome[ind] = new_gene
return sorted(chromosome)