-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutations.py
100 lines (84 loc) · 3.97 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
93
94
95
96
97
98
99
100
import random
import player as ply
(min_height, max_height) = (1.3, 2.0)
class Mutation(object):
def __init__(self, pm, weapon_list, boots_list, helmet_list, gloves_list, armor_list):
"""Returns a Mutation object, should not be used from outside (Abstract class)
Parameters
----------
pm : float e [0, 1]
Probability of mutation
weapon_list : list of Weapon
List of weapons available
boots_list : list of Boots
List of boots available
helmet_list : list of Helmet
List of helmets available
gloves_list : list of Gloves
List of gloves available
armor_list : list of Armor
List of armors available
"""
self.pm = pm
self.weapon_list = weapon_list
self.boots_list = boots_list
self.helmet_list = helmet_list
self.gloves_list = gloves_list
self.armor_list = armor_list
self.map = [weapon_list, boots_list, helmet_list, gloves_list, armor_list]
def mutate(self, player):
pass
class SimpleGen(Mutation):
def __init__(self, pm, weapon_list, boots_list, helmet_list, gloves_list, armor_list):
super().__init__(pm, weapon_list, boots_list, helmet_list, gloves_list, armor_list)
def mutate(self, player):
if random.random() < self.pm:
gene = random.randint(0,player.n_genes-1)
player_genes = player.genes()
if gene == 0:
player_genes[gene] = random.uniform(min_height,max_height)
else:
player_genes[gene] = self.map[gene-1][random.randint(0, len(self.map[gene-1]) - 1)]
return ply.Player.new_from_array(player.player_class, player_genes)
return player
class MultiLimited(Mutation):
def __init__(self, pm, M, weapon_list, boots_list, helmet_list, gloves_list, armor_list):
super().__init__(pm, weapon_list, boots_list, helmet_list, gloves_list, armor_list)
self.M = M
def mutate(self, player):
if random.random() < self.pm:
qty = random.randint(1,self.M)
changes = random.sample(range(0,player.n_genes),qty)
player_genes = player.genes()
for gene_i in changes:
if gene_i == 0:
player_genes[gene_i] = random.uniform(min_height,max_height)
else:
player_genes[gene_i] = self.map[gene_i-1][random.randint(0, len(self.map[gene_i-1]) - 1)]
return ply.Player.new_from_array(player.player_class, player_genes)
return player
class MultiUniform(Mutation):
def __init__(self, pm, weapon_list, boots_list, helmet_list, gloves_list, armor_list):
super().__init__(pm, weapon_list, boots_list, helmet_list, gloves_list, armor_list)
def mutate(self, player):
player_genes = player.genes()
for gene_i in range(0,player.n_genes):
if random.random() < self.pm:
if gene_i == 0:
player_genes[gene_i] = random.uniform(min_height,max_height)
else:
player_genes[gene_i] = self.map[gene_i-1][random.randint(0, len(self.map[gene_i-1]) - 1)]
return ply.Player.new_from_array(player.player_class, player_genes)
class Full(Mutation):
def __init__(self, pm, weapon_list, boots_list, helmet_list, gloves_list, armor_list):
super().__init__(pm, weapon_list, boots_list, helmet_list, gloves_list, armor_list)
def mutate(self, player):
if random.random() < self.pm:
player_genes = player.genes()
for gene_i in range(0,player.n_genes):
if gene_i == 0:
player_genes[gene_i] = random.uniform(min_height,max_height)
else:
player_genes[gene_i] = self.map[gene_i-1][random.randint(0, len(self.map[gene_i-1]) - 1)]
return ply.Player.new_from_array(player.player_class, player_genes)
return player