-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfitness_functions.py
528 lines (477 loc) · 23.3 KB
/
fitness_functions.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
import numpy as np
import torch
import torch.nn as nn
from typing import List, Any
import sys
from policies import RNN, MLP, SymMLP
from vectors_to_blocks import *
def getch():
""" Allows to input values without pressing enter """
import termios
import sys
import tty
def _getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
return _getch()
def fitness_MLP(evolved_parameters: np.array, generator_init_params: dict, restricted_flag: bool) -> float:
"""
For an interactive evolution, where each structure is presented to the human.
Query the fitness of a structure generated by a given policy network (MLP), whose parameters are given in argument.
input:
evolved_parameters: parameters of a policy network p, element of the population
generator_init_params: dictionary of parameters given as input of train
restricted_flag: boolean, tells if work with a restricted number of blocks
output:
reward for the network p
"""
# Unload generator init parameters
one_hot_dim = generator_init_params['output_dim']
embedding_dim = generator_init_params['embedding_dim']
offset = generator_init_params['position']
oriented = generator_init_params['oriented']
dimension = generator_init_params['dimension']
n_layers = generator_init_params['n_layers']
bounds = generator_init_params['bounds']
symmetrise = generator_init_params['symmetrise']
min_size = generator_init_params['min_size']
if symmetrise:
neo_bound = 2*bounds[0]
else:
neo_bound = bounds[0]
# Initilise policy network
p = MLP(one_hot_dim, embedding_dim, dimension, n_layers)
# Load weights into the policy network
nn.utils.vector_to_parameters(torch.tensor(
evolved_parameters, dtype=torch.float32), p.parameters())
# Generate structure with MLP
blocks, orientations, matter_blocks = p.generate_structure(
generator_init_params)
if matter_blocks > min_size:
# Build it in MInecraft
build_zone(blocks, offset, restricted_flag,
orientations, oriented, dimension)
# Ask Rating Human
print("Rate the creation from 1 to 5:")
reward = float(getch())
print(reward)
# Clean blocks function afterwards: clean all zone
clean_zone([neo_bound, bounds[1], bounds[2]], offset)
else:
reward = 0
scaled_reward = float(max(min(reward, 5), 1)*10)
return scaled_reward # bound reward from 10 to 50
def fitness_MLP_alltogether(evolved_parameters: np.array, generator_init_params: dict, restricted_flag: bool) -> float:
"""
For an interactive evolution, with N(=batch_size) structures compared.
Ask the human to select a structure among N structures generated each by a different policy network (MLP).
input:
evolved_parameters: np.array of size batch_size * N_policy_param(>>), gathering the parameters of the batch_size policy network compared here.
generator_init_params: dictionary of parameters given as input of train
restricted_flag: boolean, tells if work with a restricted number of blocks
output:
rewards: list of size batch_size of rewards for each of these networks.
"""
# Unload generator init parameters
one_hot_dim = generator_init_params['output_dim']
embedding_dim = generator_init_params['embedding_dim']
origin = generator_init_params['position']
batch_size = generator_init_params['choice_batch']
bounds = generator_init_params['bounds']
oriented = generator_init_params['oriented']
dimension = generator_init_params['dimension']
population_size = generator_init_params['population_size']
min_size = generator_init_params['min_size']
symmetrise = generator_init_params['symmetrise']
n_layers = generator_init_params['n_layers']
global_count = 0
local_count = 0
# INITIALISATION of some parameters
blocks_batch = []
spacing = 7 # spacing between 2 structure
# If symmetric structure, like if bound[0] get multiplied by 2
if symmetrise:
neo_bound = 2*bounds[0]
else:
neo_bound = bounds[0]
width_batch = (neo_bound + spacing) * batch_size
# so player can see the different creatures
perspective = np.floor(width_batch/2)
# print("Position player suggested:", origin)
rewards = [0]*population_size
batch_indices = []
# Loop on the whole structure
while global_count < population_size:
while local_count < batch_size and global_count < population_size:
# Initilise new policy network
p = MLP(one_hot_dim, embedding_dim, dimension, n_layers)
# Load weights into the policy network
nn.utils.vector_to_parameters(torch.tensor(
evolved_parameters[global_count, :], dtype=torch.float32), p.parameters())
# Generate structure with MLP
blocks, orientations, matter_blocks = p.generate_structure(
generator_init_params)
global_count += 1
if matter_blocks > min_size: # Then non empty structure, and structure big enough
blocks_batch.append(blocks)
local_count += 1
batch_indices.append(global_count-1)
# Build in Minecraft these different structures aligned
# AS now, never rate the last incomplete batch
if local_count == batch_size:
offset = [origin[0] -
np.floor(width_batch/2), 4, origin[2] - perspective]
# print("Offset", offset)
for i in range(len(blocks_batch)):
build_zone(
blocks_batch[i], offset, restricted_flag, orientations, oriented, dimension)
# redefine first coordinate offset
offset[0] += neo_bound + spacing
# Ask Rating Human
print("Choose one of these structures, from 1 to " +
str(batch_size) + " (west to east) ")
while True:
try:
index = int(getch())
if index not in np.arange(len(batch_indices))+1:
print('Entry not valid, try again. Press 0 to EXIT')
else:
break
except:
print('Entry not valid, try again')
if index == 0:
offset = [
origin[0] - np.floor(width_batch/2), 4, origin[2] - perspective]
full_bounds = [(neo_bound + spacing) *
batch_size, bounds[1], bounds[2]]
clean_zone(full_bounds, offset)
sys.exit("Bye")
# Update rewards of the picked creature
selected_structure = batch_indices[index-1]
# structure batch_size (last one) is global_count-1
rewards[selected_structure] = 50
# CHECK right structure reward:
#print("current_indices:", batch_indices)
#print("selected indices:", selected_structure)
#print("selected blocks", all_blocks[selected_structure])
# RESET variables for after and clean zone
offset = [origin[0] -
np.floor(width_batch/2), 4, origin[2] - perspective]
full_bounds = [(neo_bound + spacing) *
batch_size, bounds[1], bounds[2]]
clean_zone(full_bounds, offset)
local_count = 0
blocks_batch = []
batch_indices = []
return rewards # bound reward from 10 to 50 ?
def fitness_SymMLP(evolved_parameters: np.array, generator_init_params: dict, restricted_flag: bool) -> float:
"""
For an interactive evolution, where each structure is presented to the human.
Query the fitness of a structure generated by a given policy network (MLP), whose parameters are given in argument.
input:
evolved_parameters: parameters of a policy network p, element of the population
generator_init_params: dictionary of parameters given as input of train
restricted_flag: boolean, tells if work with a restricted number of blocks
output:
reward for the network p
"""
# Unload generator init parameters
one_hot_dim = generator_init_params['output_dim']
embedding_dim = generator_init_params['embedding_dim']
offset = generator_init_params['position']
oriented = generator_init_params['oriented']
dimension = generator_init_params['dimension']
bounds = generator_init_params['bounds']
symmetrise = generator_init_params['symmetrise']
min_size = generator_init_params['min_size']
if symmetrise:
neo_bound = 2*bounds[0]
else:
neo_bound = bounds[0]
# Initilise policy network
p = SymMLP(one_hot_dim, embedding_dim, dimension)
# Load weights into the policy network
nn.utils.vector_to_parameters(torch.tensor(
evolved_parameters, dtype=torch.float32), p.parameters())
# Generate structure with MLP
blocks, orientations, matter_blocks = p.generate_structure(
generator_init_params)
if matter_blocks > min_size:
# Build it in MInecraft
build_zone(blocks, offset, restricted_flag,
orientations, oriented, dimension)
# Ask Rating Human
print("Rate the creation from 1 to 5:")
reward = float(getch())
print(reward)
# Clean blocks function afterwards: clean all zone
clean_zone([neo_bound, bounds[1], bounds[2]], offset)
else:
reward = 0
scaled_reward = float(max(min(reward, 5), 1)*10)
print("scaled rewards", scaled_reward)
return scaled_reward # bound reward from 10 to 50
def fitness_SymMLP_alltogether(evolved_parameters: np.array, generator_init_params: dict, restricted_flag: bool) -> float:
"""
For an interactive evolution, with N(=batch_size) structures compared.
Ask the human to select a structure among N structures generated each by a different policy network (MLP).
input:
evolved_parameters: np.array of size batch_size * N_policy_param(>>), gathering the parameters of the batch_size policy network compared here.
generator_init_params: dictionary of parameters given as input of train
restricted_flag: boolean, tells if work with a restricted number of blocks
output:
rewards: list of size batch_size of rewards for each of these networks.
"""
# Unload generator init parameters
one_hot_dim = generator_init_params['output_dim']
embedding_dim = generator_init_params['embedding_dim']
origin = generator_init_params['position']
batch_size = generator_init_params['choice_batch']
bounds = generator_init_params['bounds']
oriented = generator_init_params['oriented']
dimension = generator_init_params['dimension']
population_size = generator_init_params['population_size']
min_size = generator_init_params['min_size']
symmetrise = generator_init_params['symmetrise']
global_count = 0
local_count = 0
# INITIALISATION of some parameters
blocks_batch = []
spacing = 7 # spacing between 2 structure
# If symmetric structure, like if bound[0] get multiplied by 2
if symmetrise:
neo_bound = 2*bounds[0]
else:
neo_bound = bounds[0]
width_batch = (neo_bound + spacing) * batch_size
# so player can see the different creatures
perspective = np.floor(width_batch/2)
# print("Position player suggested:", origin)
rewards = [0]*population_size
batch_indices = []
# Loop on the whole structure
while global_count < population_size:
while local_count < batch_size and global_count < population_size:
# Initilise new policy network
p = SymMLP(one_hot_dim, embedding_dim, dimension)
# Load weights into the policy network
nn.utils.vector_to_parameters(torch.tensor(
evolved_parameters[global_count, :], dtype=torch.float32), p.parameters())
# Generate structure with MLP
blocks, orientations, matter_blocks = p.generate_structure(
generator_init_params)
global_count += 1
if matter_blocks > min_size: # Then non empty structure, and structure big enough
blocks_batch.append(blocks)
local_count += 1
batch_indices.append(global_count-1)
# Build in Minecraft these different structures aligned
# AS now, never rate the last incomplete batch
if local_count == batch_size:
# initial offset #ON THE FLOOR OR PLAYER POSITION ?
offset = [origin[0] -
np.floor(width_batch/2), 4, origin[2] - perspective]
# print("Offset", offset)
for i in range(len(blocks_batch)):
build_zone(
blocks_batch[i], offset, restricted_flag, orientations, oriented, dimension)
# redefine first coordinate offset
offset[0] += neo_bound + spacing
# Ask Rating Human
print("Choose one of these structures, from 1 to " +
str(batch_size) + " (west to east) ")
while True:
try:
index = int(getch())
if index not in np.arange(len(batch_indices))+1:
print('Entry not valid, try again. Press 0 to EXIT')
else:
break
except:
print('Entry not valid, try again')
if index == 0:
offset = [
origin[0] - np.floor(width_batch/2), 4, origin[2] - perspective]
full_bounds = [(neo_bound + spacing) *
batch_size, bounds[1], bounds[2]]
# CLAIRE: TODO: make clean function self-contained
clean_zone(full_bounds, offset)
sys.exit("Bye")
# Update rewards of the picked creature
selected_structure = batch_indices[index-1]
# structure batch_size (last one) is global_count-1
rewards[selected_structure] = 50
# RESET variables for after and clean zone
offset = [origin[0] -
np.floor(width_batch/2), 4, origin[2] - perspective]
full_bounds = [(neo_bound + spacing) *
batch_size, bounds[1], bounds[2]]
clean_zone(full_bounds, offset)
local_count = 0
blocks_batch = []
batch_indices = []
return rewards # bound reward from 10 to 50 ?
def fitness_RNN(evolved_parameters: np.array, generator_init_params: dict, restricted_flag: bool) -> float:
"""
For an interactive evolution, where each structure is presented to the human.
Query the fitness of a structure generated by a given policy network (RNN), whose parameters are given in argument.
input:
evolved_parameters: parameters of a policy network p (RNN), element of the population
generator_init_params: dictionary of parameters given as input of train
restricted_flag: boolean, tells if work with a restricted number of blocks
output:
reward for the network p
"""
# Unload generator init parameters
one_hot_dim = generator_init_params['output_dim']
embedding_dim = generator_init_params['embedding_dim']
hidden_dim = generator_init_params['hidden_dim_RNN']
n_layers = generator_init_params['n_layers_RNN']
if_embedding = generator_init_params['if_embedding']
oriented = generator_init_params['oriented']
offset = generator_init_params['position']
graph = generator_init_params['graph']
dimension = generator_init_params['dimension']
min_size = generator_init_params['min_size']
# Initilise policy network
p = RNN(one_hot_dim, embedding_dim, hidden_dim, n_layers, if_embedding)
# Load weights into the policy network
nn.utils.vector_to_parameters(torch.tensor(
evolved_parameters, dtype=torch.float32), p.parameters())
# BUILD CREATURE STARTING FROM SOS with MAXIMUM SIZE, STOP IF ENCOUNTER EOS
blocks_indices = p.generate_structure(generator_init_params, 0)
phenotype, blocks, positions, orientations = sequence_to_construct(blocks_indices, [], [], [], [], [
[0, 0, 0]], [1], [1], restricted_flag, oriented, graph, dimension) # default direction is north ?
matter_blocks = len(blocks)
if matter_blocks > min_size:
# print("Constructible Sequence:", phenotype)
positions = np.array(positions)
tiled_offset = np.tile(offset, (len(blocks), 1))
new_positions = np.add(positions, tiled_offset) # np.tile
build_from_sequence(blocks, new_positions, orientations)
bounds_structure = [np.amin(np.array(positions), axis=0), np.amax(
np.array(positions), axis=0)]
print("Rate the creation from 1 to 5:")
reward = float(getch())
print(reward)
if len(positions) > 0:
clean_positions(new_positions)
print("***** Thanks for helping me learning ****")
# reward = float(input("Rate the creation from 1 to 5: "))
# Add clean blocks function here
# bound reward from 10 to 50 has to retuzrn list now
return max(min(reward, 5), 1)*10
else:
return 0
def fitness_RNN_alltogether(evolved_parameters: np.array, generator_init_params: dict, restricted_flag: bool) -> float:
"""
For an interactive evolution, with N(=batch_size) structures compared batch after batch.
Ask the human to select a structure among N structures generated each by a different policy network (RNN).
If the structure is empty, will remove it from the proposition
input:
evolved_parameters: np.array of size batch_size * N_policy_param(>>), gathering the parameters of the batch_size policy network compared here.
generator_init_params: dictionary of parameters given as input of train
restricted_flag: boolean, tells if work with a restricted number of blocks
output:
rewards: list of size batch_size of rewards for each of these networks.
"""
# Unload generator init parameters
one_hot_dim = generator_init_params['output_dim']
embedding_dim = generator_init_params['embedding_dim']
hidden_dim = generator_init_params['hidden_dim_RNN']
n_layers = generator_init_params['n_layers_RNN']
if_embedding = generator_init_params['if_embedding']
oriented = generator_init_params['oriented']
# SUGGEST PLAYER BE THERE TO SEE CREATURE
origin = generator_init_params['position']
batch_size = generator_init_params['choice_batch']
graph = generator_init_params['graph']
dimension = generator_init_params['dimension']
population_size = generator_init_params['population_size']
min_size = generator_init_params['min_size']
# CREATURES WOULD BE ALIGNED ON WEST_EAST AXIS (IE FIRST COORDINATE), FROM WEST TO EAST,
positions_batch, orientations_batch, blocks_batch, size_batch, batch_indices, new_positions_batch = [], [], [], [], [], []
local_count, global_count, width_batch, spacing = 0, 0, 0, 5
rewards = [0]*population_size
# Loop on the whole structure
while global_count < population_size:
while local_count < batch_size and global_count < population_size:
# Initilise policy network
p = RNN(one_hot_dim, embedding_dim,
hidden_dim, n_layers, if_embedding)
# Load weights into the policy network
nn.utils.vector_to_parameters(torch.tensor(
evolved_parameters[global_count, :], dtype=torch.float32), p.parameters())
# GENERATE STRUCTURE RNN
blocks_indices = p.generate_structure(generator_init_params, 0)
phenotype, blocks, positions, orientations = sequence_to_construct(blocks_indices, [], [], [], [], [
[0, 0, 0]], [1], [1], restricted_flag, oriented, graph, dimension) # default direction is north ?
matter_blocks = len(blocks)
positions = np.array(positions) # convert into np array
assert(positions.shape[0] == len(blocks))
global_count += 1
if matter_blocks > min_size: # Then non empty structure, and structure big enough
# print("Constructible Sequence:", phenotype)
local_count += 1
batch_indices.append(global_count-1)
positions_batch.append(positions)
orientations_batch.append(orientations)
blocks_batch.append(blocks)
bounds_structure = [
np.amax(positions, axis=0), np.amin(positions, axis=0)]
# only along one dimension
size_structure = [bounds_structure[0]
[0], bounds_structure[1][0]]
size_batch.append(size_structure)
width_batch += spacing + \
bounds_structure[0][0]-bounds_structure[1][0] # width
# Build in Minecraft these different structures aligned
# AS now, never rate the last incomplete batch
if local_count == batch_size:
# so player can see the different creatures
perspective = np.floor(width_batch/2)
# initial offset #ON THE FLOOR OR PLAYER POSITION ?
offset = [origin[0] -
np.floor(width_batch/2), 0, origin[2] - perspective]
# CONSTRUCT AND PLACE STRUCTURES
for i in range(len(blocks_batch)):
# redefine first coordinate offset, according dimension
offset[0] += spacing - size_batch[i][1]
if i > 0:
offset[0] += size_batch[i-1][0]
# UPDATE POSITION depending on offset
tiled_offset = np.tile(offset, (len(blocks_batch[i]), 1))
new_positions = np.add(
positions_batch[i], tiled_offset) # np.tile
new_positions_batch.append(new_positions)
build_from_sequence(
blocks_batch[i], new_positions, orientations_batch[i])
print("Choose one of these structures, from 1 to " +
str(batch_size) + " (west to east) ")
while True:
try:
index = int(getch())
if index not in np.arange(len(batch_indices))+1:
print('Entry not valid, try again. Press 0 to EXIT')
else:
break
except:
print('Entry not valid, try again')
if index == 0:
clean_batch(new_positions_batch)
sys.exit("Bye")
selected_structure = batch_indices[index-1]
# structure batch_size (last one) is global_count-1
rewards[selected_structure] = 50
# ERASE THEN BOARD
clean_batch(new_positions_batch)
# RESET VARIABLES
local_count = 0
positions_batch, orientations_batch, blocks_batch, size_batch, batch_indices, new_positions_batch = [], [], [], [], [], []
return rewards