forked from martinventer/virtual_creatures
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EVC1.py
253 lines (216 loc) · 6.91 KB
/
EVC1.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
import numpy as np
import matplotlib.pyplot as plt
from shapely.geometry import LineString, MultiLineString, Point
from descartes.patch import PolygonPatch
from tqdm import tqdm
from scipy.spatial.transform import Rotation
def next_point(angle, vec, dist, point):
"""
Takes the current point and vector and finds the next point and vector
Parameters
----------
angle : float
the angle of rotation
vec : array like
the current 2d vector
dist : float
the projection distance for the next point
point : array like
the 2d starting point for the projection
Returns
-------
point : array
the projected point in 2d
vec : array
the updated vector
"""
r = Rotation.from_euler('z', angle, degrees=True)
vec = np.append(vec, [0])
vec = r.apply(vec)[:2]
vec = dist * (vec / np.linalg.norm(vec))
point = point + vec
return point, vec
def draw_phenotype(phenotype):
"""
Sketch out the worm.
Returns
-------
"""
fig = plt.figure(1, figsize=(5, 5), dpi=180)
ax = fig.add_subplot(111)
line = LineString(phenotype)
dilated = line.buffer(0.5)
patch1 = PolygonPatch(dilated, facecolor='#99ccff', edgecolor='#6699cc')
ax.add_patch(patch1)
x, y = line.xy
plt.axis('equal')
ax.plot(x, y, color='#999999')
plt.show()
class Vermiculus(object):
"""
Makes a worm like creature
"""
def __init__(self,
instance_number,
starting_point=np.array([0, 0]),
starting_vector=np.array([0, 1]),
dna_length=60,
unit_length=1.0,
unit_width=1.0,
):
"""
Initialise a worm
Parameters
----------
starting_point : array like
a 2d vector containing the strarting point for the worm
instance_number: int
the index for the instance of the worm
"""
self.instance_number = instance_number
self.starting_point = starting_point
self.starting_vector = starting_vector
self.dna_length = dna_length
self.unit_length = unit_length
self.unit_width = unit_width
self.proteins = {"F": 0.0,
"L": 45,
"R": -45}
self.amino_acids = list(self.proteins.keys())
self.amino_probabilities = [0.4, 0.3, 0.3]
self.phenotype = [self.starting_point]
self.dna = self.transcribe_dna()
self.topology = None
self.topology_dilated = None
self.area = None
def transcribe_dna(self):
"""
Build a DNA string for the worm that is N elements long by selecting
from the available amino acids
Returns
-------
A string N elements long
"""
return ''.join(np.random.choice(self.amino_acids,
p=self.amino_probabilities
) for _ in range(self.dna_length))
def _translate_dna(self):
"""
read the DNA sequence and translate it into the a series of segments.
Returns
-------
"""
cv = self.starting_vector
cp = self.starting_point
line_cords = [cp]
for acid in self.dna:
cp, cv = next_point(self.proteins[acid],
cv,
self.unit_length,
cp)
line_cords.append(cp)
self.phenotype = line_cords
def build_topology(self):
"""
Translate the phenotype to the topology
Returns
-------
"""
self._translate_dna()
self.topology = LineString(self.phenotype)
self.topology_dilated = self.topology.buffer(self.unit_width)
self.area = self.topology_dilated.area
class Alga(Vermiculus):
"""
Makes a root like structure
"""
def __init__(self,
instance_number,
starting_point=np.array([0, 0]),
starting_vector=np.array([0, 1]),
dna_length=60,
unit_length=1.0,
unit_width=1.0,
):
"""
Initialise a worm
Parameters
----------
starting_point : array like
a 2d vector containing the strarting point for the worm
instance_number: int
the index for the instance of the worm
"""
self.instance_number = instance_number
self.starting_point = starting_point
self.starting_vector = starting_vector
self.dna_length = dna_length
self.unit_length = unit_length
self.unit_width = unit_width
self.proteins = {"F": 0.0,
"L": 45,
"R": -45,
"B": 0.0}
self.amino_acids = list(self.proteins.keys())
self.amino_probabilities = [0.4, 0.3, 0.3, 0.0]
self.phenotype = [self.starting_point]
self.dna = self.transcribe_dna()
self.topology = None
self.topology_dilated = None
self.area = None
def transcribe_dna(self):
"""
Build a DNA string for the worm that is N elements long by selecting
from the available amino acids
Returns
-------
A string N elements long
"""
return ''.join(np.random.choice(self.amino_acids,
p=self.amino_probabilities
) for _ in range(self.dna_length))
def _translate_dna(self):
"""
read the DNA sequence and translate it into the a series of segments.
Returns
-------
"""
cv = self.starting_vector
cp = self.starting_point
points = [cp]
for acid in self.dna:
cp, cv = next_point(self.proteins[acid],
cv,
self.unit_length,
cp)
points.append(cp)
self.phenotype = points
def build_topology(self):
"""
Translate the phenotype to the topology
Returns
-------
"""
self._translate_dna()
self.topology = LineString(self.phenotype)
self.topology_dilated = self.topology.buffer(self.unit_width)
self.area = self.topology_dilated.area
def scan_population(num):
areas = []
for i in tqdm(range(num)):
worm = Vermiculus(i)
worm.build_topology()
areas.append(worm.area)
plt.hist(areas, bins=50)
def make_one_worm():
worm = Vermiculus(1)
worm.build_topology()
draw_phenotype(worm.phenotype)
def make_one_alga():
duckweed = Alga(1)
duckweed.build_topology()
draw_phenotype(duckweed.phenotype)
if __name__ == "__main__":
# scan_population(5000)
# make_one_worm()
make_one_alga()