This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindividual.py
105 lines (91 loc) · 3.55 KB
/
individual.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
import os
import settings
import json
import neural_network_representation
import hashlib
import experiment
import pickle # TODO: try cPickle. it might be faster.
class Individual(object):
def __init__(self, genotype, neural_mode, effect):
self.genotype = genotype
self.output_sound = None
self.neural_output_channels = None
self.neural_mode = neural_mode
self.effect = effect
self.nn_representation = None
self.id = None
self.born = None # In which generation was this individual first discovered
self.similarity = None # may be different from the fitness value
def get_id(self):
"""individuals with the same neural network have the same id"""
if self.id is None:
nn_repr = self.get_neural_network_representation()
self.id = hashlib.md5(json.dumps(nn_repr).encode('utf-8')).hexdigest()
return self.id
def get_individual_data_file_path(self):
return os.path.join(
settings.INDIVIDUAL_DATA_DIRECTORY,
experiment.Experiment.folder_name,
'individual_{}.json'.format(self.get_id())
)
def set_fitness(self, fitness):
self.genotype.SetFitness(fitness)
def set_output_sound(self, output_sound):
self.output_sound = output_sound
def set_neural_output(self, neural_output):
self.neural_output_channels = neural_output
def get_neural_network_representation(self):
if self.nn_representation is None:
self.nn_representation = neural_network_representation.get_neural_network_representation(
nn=self.genotype,
neural_mode=self.neural_mode,
effect=self.effect,
is_substrate=False
)
return self.nn_representation
def get_neural_output_representation(self):
return {
'series_standardized': self.neural_output_channels,
'ksmps': settings.HOP_SIZE,
'order': self.effect.parameter_names
}
def get_serialized_representation(self):
"""
Get a serialized representation where data is included directly
:return:
"""
return {
'id': self.get_id(),
'similarity': self.similarity,
'neural_output': self.get_neural_output_representation(),
'output_sound': self.output_sound.get_serialized_representation(),
'neural_network_representation': self.get_neural_network_representation(),
'born': self.born,
'genotype_pickled': pickle.dumps(self.genotype)
}
def get_short_serialized_representation(self):
"""
Get a serialized representation with only references to data files
:return:
"""
return {
'id': self.get_id(),
'similarity': self.similarity,
'fitness': self.genotype.GetFitness()
}
def save(self):
file_path = self.get_individual_data_file_path()
if os.path.exists(file_path):
if settings.VERBOSE:
print('individual {} already exists'.format(self.get_id()))
return
data = self.get_serialized_representation()
with open(file_path, 'w') as outfile:
json.dump(data, outfile)
def delete(self, try_delete_serialized_representation=True):
self.output_sound.delete()
if try_delete_serialized_representation:
try:
os.remove(self.get_individual_data_file_path())
except OSError:
pass