-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchromosome.lua
278 lines (226 loc) · 7.45 KB
/
chromosome.lua
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
chromosome = {}
chromosome.possible_actions = {"move", "turnLeft", "turnRight"}
--[[
Chromosome is:
{
true = { -- this path is chosen if there is food in front of the ant
transitions = {Natural}, -- automata transitions, indexes are states,
first state has index 1
actions = {String} -- actions on transitions
},
false = {
transitions = {Natural},
actions = {String}
}
}
interp. a chromosome representing an algorithm
Examples:
c1 = {}
c1[true] = {
transitions = {1},
actions = {"move"}
}
c1[false] = {
transitions = {1},
actions = {"move"}
}
c2 = {}
c2[true] = {
transitions = {2, 3, 4, 1},
actions = {"move", "turnLeft", "move", "turnRight"}
}
c2[true] = {
transitions = {2, 3, 4, 1},
actions = {"move", "turnLeft", "move", "turnRight"}
}
--]]
function chromosome:validate(chromosome)
assert(chromosome)
assert(chromosome[true])
assert(chromosome[false])
assert(chromosome[true].transitions)
assert(chromosome[true].actions)
assert(chromosome[false].transitions)
assert(chromosome[false].actions)
assert(#chromosome[true].transitions == #chromosome[true].actions)
assert(#chromosome[false].transitions == #chromosome[false].actions)
assert(#chromosome[true].transitions == #chromosome[false].actions)
end
local function generate_random_chromosome(size)
if not size then
size = CONFIG.INITIAL_MAX_CHROMOSOME_SIZE
end
local cs = {}
for _, key in ipairs({true, false}) do
cs[key] = {}
local slice = cs[key]
slice.transitions = {}
slice.actions = {}
for i = 1, size do
table.insert(slice.actions, chromosome.possible_actions[math.random(#chromosome.possible_actions)])
table.insert(slice.transitions, math.random(size))
end
end
chromosome:validate(cs)
return cs
end
-- mix two species into new specie
local function mix(s1, s2)
local cs = {}
for _, key in ipairs({true, false}) do
cs[key] = {}
local slice = cs[key]
slice.transitions = {}
slice.actions = {}
for i = 1, CONFIG.INITIAL_MAX_CHROMOSOME_SIZE do
table.insert(slice.actions, math.random() > 0.5 and
s1[key].actions[i] or s2[key].actions[i])
table.insert(slice.transitions, math.random() > 0.5 and
s1[key].transitions[i] or s2[key].transitions[i])
end
end
chromosome:validate(cs)
return cs
end
local function mutateInitialState(s)
local shift = math.random(CONFIG.INITIAL_MAX_CHROMOSOME_SIZE)
for _, key in ipairs({true, false}) do
local newStates = {}
for i = 1, CONFIG.INITIAL_MAX_CHROMOSOME_SIZE do
newStates[i] = s[key][(i + shift) % CONFIG.INITIAL_MAX_CHROMOSOME_SIZE + 1]
end
end
end
local function mutateActionOnTransition(s)
local input = math.random() > 0.5
s[input].actions[math.random(#s[input].actions)] = chromosome.possible_actions[math.random(#chromosome.possible_actions)]
end
local function mutateTransition(s)
local input = math.random() > 0.5
local newDirection = math.random() > 0.5
s[input].transitions[math.random(#s[input].transitions)] = s[newDirection].transitions[math.random(#s[newDirection].transitions)]
end
local function mutateTransitionCondition(s)
local index = math.random(#s[true].transitions)
local trans = s[true].transitions[index]
s[true].transitions[index] = s[false].transitions[index]
s[false].transitions[index] = trans
end
local function mutate(s)
local choice = math.random(4)
if (choice == 1) then
mutateInitialState(s)
elseif (choice == 2) then
mutateActionOnTransition(s)
elseif (choice == 3) then
mutateTransition(s)
elseif (choice == 4) then
mutateTransitionCondition(s)
end
end
local function generate(species)
if (species) then
local children = {}
local toAdd = CONFIG.POPULATION - #species
assert(toAdd >= 0)
while (toAdd > 0) do
local s1 = species[math.random(#species)]
local s2 = math.random() < CONFIG.OUTSIDER_BREED_CHANCE
and generate_random_chromosome()
or species[math.random(#species)]
if (s1 ~= s2) then
table.insert(children, mix(s1, s2))
toAdd = toAdd - 1
end
end
for _, s in ipairs(children) do
if (math.random() < CONFIG.MUTATION_CHANCE) then
mutate(s)
end
end
for _, child in ipairs(children) do
table.insert(species, child)
end
else
local population = {}
for i = 1, CONFIG.POPULATION do
table.insert(population, generate_random_chromosome())
end
return population
end
end
---------------------------------------
-- export functions
---------------------------------------
-- Return a short alias for the given action
local function nameAction(action)
if (action == "move") then
return "M"
elseif (action == "turnLeft") then
return "L"
elseif (action == "turnRight") then
return "R"
end
end
-- Convert an action to number
local function actionToNumber(action)
if (action == "move") then
return 100
elseif (action == "turnLeft") then
return 0
elseif (action == "turnRight") then
return 1000
end
end
-- Export given chromosome to dot format
function chromosome:getDot(s)
local str = "digraph G {\n"
for i = 1, #s[true].transitions do
str = str .. string.format(' %d -> %d [label = "T/%s"]\n', i, s[true].transitions[i],
nameAction(s[true].actions[i]))
str = str .. string.format(' %d -> %d [label = "F/%s"]\n', i, s[false].transitions[i],
nameAction(s[false].actions[i]))
end
str = str .. "}"
return str
end
-- Export given chromosome to gene string format
function chromosome:getGene(s)
local str = ""
for i = 1, #s[true].transitions do
str = str .. s[true].transitions[i] .. nameAction(s[true].actions[i])
str = str .. s[false].transitions[i] .. nameAction(s[false].actions[i]) .. " "
end
return str
end
-- Export given chromosome to a 2d vector
function chromosome:get2dVector(s)
local big_vector = {}
for i = 1, #s[true].transitions do
table.insert(big_vector, s[true].transitions[i])
table.insert(big_vector, actionToNumber(s[true].actions[i]))
table.insert(big_vector, s[false].transitions[i])
table.insert(big_vector, actionToNumber(s[false].actions[i]))
end
if not (chromosome.vector_weights) then
chromosome.vector_weights = {}
for key = 1, 2 do
chromosome.vector_weights[key] = {}
for i = 1, #big_vector do
table.insert(chromosome.vector_weights[key], math.random())
end
end
end
local small_vector = {}
for key = 1, 2 do
for i = 1, #big_vector do
small_vector[key] = small_vector[key] or 0 + big_vector[i] * chromosome.vector_weights[key][i]
end
end
-- normalize it
local mag = small_vector[1] + small_vector[2]
small_vector[1] = small_vector[1] / mag
small_vector[2] = small_vector[2] / mag
return small_vector
end
return generate