-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathea.py
146 lines (118 loc) · 5.27 KB
/
ea.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
import numpy as np
import random
import copy
from random import randint
do_debug=False
class EA():
def __init__(self,etype):
self.etype = etype
def mutate(self,solution,flip_at_least_one,environment, mutation_probability):
"""
Mutate one solution into n
"""
# Create a set of indexes in the solution that we are going to mutate
mutation_indexes = set()
if(do_debug):
print('sol:',solution)
solution_length = len(solution)
if flip_at_least_one:
mutation_indexes.add(np.random.randint(solution_length))
mutation_indexes = mutation_indexes.union(
set(np.where(np.random.random([solution_length]) < mutation_probability)[0]))
# Create the number of mutations that is the same as the number of mutation indexes
num_mutations = len(mutation_indexes)
p_actions=[0,1,2,3]
mutations = [p_actions[randint(0,len(p_actions)-1)] for _ in range(num_mutations)]
# Replace values in the solutions with mutated values
new_solution = np.copy(solution)
new_solution[list(mutation_indexes)] = mutations
return new_solution
def mutateN(self,num_rollout_pop,solution,flip_at_least_one,environment,mutation_probability):
"""
Mutate one solution into n
"""
#print('before mutation: ',solution)
candidate_solutions = []
# Solution here is 2D of rollout_actions x batch_size
for b in range(num_rollout_pop):
# Create a set of indexes in the solution that we are going to mutate
mutation_indexes = set()
solution_length = len(solution)
if flip_at_least_one:
mutation_indexes.add(np.random.randint(solution_length))
mutation_indexes = mutation_indexes.union(
set(np.where(np.random.random([solution_length]) < mutation_probability)[0]))
# Create the number of mutations that is the same as the number of mutation indexes
num_mutations = len(mutation_indexes)
#l = len(environment.get_possible_actions())
#possiveis acoes. Varia de cada env
p_actions=[0,1,2,3]
#print('l: ',l)
mutations = [p_actions[randint(0,len(p_actions)-1)] for _ in range(num_mutations)]
# Replace values in the solutions with mutated values
new_solution = np.copy(solution)
new_solution[list(mutation_indexes)] = mutations
#print('after mutation: ',new_solution)
candidate_solutions.append(new_solution)
return np.stack(candidate_solutions)
def mutateNN(self,environment,solutions,mutation_probability,flip_at_least_one=True):
"""
Mutate N solutions into N
"""
# Solution here is 2D of rollout_actions x batch_size
#print()
for solution in solutions:
# Create a set of indexes in the solution that we are going to mutate
mutation_indexes = set()
solution_length = len(solution)
if flip_at_least_one:
mutation_indexes.add(np.random.randint(solution_length))
mutation_indexes = mutation_indexes.union(
set(np.where(np.random.random([solution_length]) < mutation_probability)[0]))
# Create the number of mutations that is the same as the number of mutation indexes
num_mutations = len(mutation_indexes)
l = len(environment.get_possible_actions())
#Importante!! alterar para cada jogo
possible_actions=[0,1,2,3]
l =len(possible_actions)
#l = len(environment.get_possible_actions())
#possible_actions =environment.get_possible_actions()
mutations = [possible_actions[randint(0,l-1)] for _ in range(num_mutations)]
# Replace values in the solutions with mutated values
if(do_debug):
print('solution before mutation',solution)
solution[list(mutation_indexes)] = mutations
if(do_debug):
print('solution after mutation',solution)
return np.stack(solutions)
def crossover (self,couple, cross_type='point'):
off1=[]
off2=[]
if (cross_type == 'point'):
#int p = gen.nextInt(actions.length - 3) + 1;
p = random.randint(0,len(couple[0]))
#p = len(couple[0])//2
for i in range(len(couple[0])):
if (i < p):
off1.append(couple[0][i])
off2.append(couple[1][i])
else:
off1.append(couple[1][i])
off2.append(couple[0][i])
elif (cross_type == 'uniform'):
#escolhe acoes aleatorias de um dos dois
#if p1==0:
# p2=1
#else:
# p2=0
for i in range(len(couple[0])):
p = random.randint(0,1)
if(p==0):
off1.append(couple[0][i])
off2.append(couple[1][i])
else:
off1.append(couple[1][i])
off2.append(couple[0][i])
else:
return np.asarray(couple)
return np.asarray([off1,off2])