1 """
2 Tests springbot's fitness for specific tasks:
3
4 walk: optimizes maximum horizontal difference of mass center before and after simulation
5 jump: optimizes maximum mass center height achieved compared with initial height
6 equilibrium: optimizes maximum mass center height percentage over body's height
7 height: optimizes maximum aspect ratio of body favoring height
8 swim: optimizes maximium difference of mass center before and after in a liquid enviroment
9 """
10
11
12 from vector import Vector
13 from math import sqrt
14 import gear
15
16 try:
17 import pygame
18 HAS_PYGAME = True
19 except ImportError:
20 HAS_PYGAME = False
21
22
23
24
25
26 -def walk(springbot, width, height, enable_graphics=False, simulation_time=1000):
27 """
28 Rewards maximum horizontal difference of mass center position before and after simulation
29 """
30 springbot['adapted'] = 'walk'
31
32
33 if len(springbot['bloodline'].split('.')) == 1:
34 springbot.centerGround(height)
35
36
37 start_x, start_y = springbot.massCenter()
38
39 if enable_graphics and HAS_PYGAME:
40 ticks = 0
41 screen = pygame.display.get_surface()
42
43
44 for i in xrange(simulation_time):
45 if enable_graphics and HAS_PYGAME:
46 springbot.draw(screen, ticks, track_x=True, extrainfo="evolving for walk")
47 pygame.display.flip()
48 ticks += 1
49
50 for event in pygame.event.get():
51 if event.type == pygame.QUIT or \
52 (event.type == pygame.KEYDOWN and event.key == 27):
53 raise KeyboardInterrupt
54
55 springbot.refresh()
56 springbot.colideWall(height, gear.DOWN)
57
58
59 end_x, end_y = springbot.massCenter()
60
61
62 return abs(end_x - start_x)
63
64
65
66
67
68 -def jump(springbot, width, height, enable_graphics=False, simulation_time=500):
69 """
70 Rewards maximum difference of mass center height achieved
71 """
72 springbot['adapted'] = 'jump'
73
74
75 if len(springbot['bloodline'].split('.')) == 1:
76 springbot.centerGround(height)
77
78
79 start_x, start_y = springbot.massCenter()
80 min_y = max_y = start_y
81
82 if enable_graphics and HAS_PYGAME:
83 ticks = 0
84 screen = pygame.display.get_surface()
85
86
87 for i in xrange(simulation_time):
88 if enable_graphics and HAS_PYGAME:
89 springbot.draw(screen, ticks, extrainfo="evolving for jump")
90 pygame.display.flip()
91 ticks += 1
92
93 for event in pygame.event.get():
94 if event.type == pygame.QUIT or \
95 (event.type == pygame.KEYDOWN and event.key == 27):
96 raise KeyboardInterrupt
97
98
99 cx, cy = springbot.massCenter()
100 min_y = min(min_y, cy)
101 max_y = max(max_y, cy)
102
103 springbot.refresh()
104 springbot.colideWall(height, gear.DOWN)
105
106
107 return abs(max_y - min_y)
108
109
110
111
112
113 -def equilibrium(springbot, width, height, enable_graphics=False, simulation_time=600):
114 """
115 Rewards maximum average mass center height percentage over body's height
116 """
117 springbot['adapted'] = 'equilibrium'
118
119
120 if len(springbot['bloodline'].split('.')) == 1:
121 springbot.centerGround(height)
122
123
124 eq_ratio = 0
125
126
127 height_av = 0
128
129 if enable_graphics and HAS_PYGAME:
130 ticks = 0
131 screen = pygame.display.get_surface()
132
133
134 for i in xrange(simulation_time):
135 if enable_graphics and HAS_PYGAME:
136 springbot.draw(screen, ticks, extrainfo="evolving for equilibrium")
137 pygame.display.flip()
138 ticks += 1
139
140 for event in pygame.event.get():
141 if event.type == pygame.QUIT or \
142 (event.type == pygame.KEYDOWN and event.key == 27):
143 raise KeyboardInterrupt
144
145
146 cx, cy = springbot.massCenter()
147
148
149 x1, y1, x2, y2 = springbot.boundingBox()
150
151
152 eq_ratio += (y2-cy)/(y2-y1)
153 height_av += y2-y1
154
155 springbot.refresh()
156 springbot.colideWall(height, gear.DOWN)
157
158
159 return 0 if height_av/simulation_time < RADIUS else eq_ratio/simulation_time
160
161
162
163
164
165 -def height(springbot, width, height, enable_graphics=False, simulation_time=400):
166 """
167 Rewards maximum average aspect ratio of body's height over width
168 """
169 springbot['adapted'] = 'height'
170
171
172 if len(springbot['bloodline'].split('.')) == 1:
173 springbot.centerGround(height)
174
175
176 aspect_ratio = 0
177
178 if enable_graphics and HAS_PYGAME:
179 ticks = 0
180 screen = pygame.display.get_surface()
181
182
183 for i in xrange(simulation_time):
184 if enable_graphics and HAS_PYGAME:
185 springbot.draw(screen, ticks, extrainfo="evolving for height")
186 pygame.display.flip()
187 ticks += 1
188
189 for event in pygame.event.get():
190 if event.type == pygame.QUIT or \
191 (event.type == pygame.KEYDOWN and event.key == 27):
192 raise KeyboardInterrupt
193
194
195 x1, y1, x2, y2 = springbot.boundingBox()
196
197
198 if x2 - x1 != 0:
199 aspect_ratio += (y2 - y1) / (x2 - x1)
200
201
202 springbot.refresh()
203 springbot.colideWall(height, gear.DOWN)
204
205
206 return aspect_ratio/simulation_time
207
208
209
210
211
212 -def swim(springbot, width, height, enable_graphics=False, simulation_time=1500):
213 """
214 Rewards maximum diference between mass centers positions before and after the
215 simulation in a liquid enviroment without gravity.
216 """
217 springbot['adapted'] = 'swim'
218
219
220 start_x, start_y = springbot.massCenter()
221
222 if enable_graphics and HAS_PYGAME:
223 ticks = 0
224 screen = pygame.display.get_surface()
225
226
227 angle = 0.0
228 for i in xrange(simulation_time):
229 if enable_graphics and HAS_PYGAME:
230 springbot.draw(screen, ticks, track_x=True, track_y=True,
231 backgroundcolor=(0,10,20), extrainfo="evolving for swim")
232 pygame.display.flip()
233 ticks += 1
234
235 for event in pygame.event.get():
236 if event.type == pygame.QUIT or \
237 (event.type == pygame.KEYDOWN and event.key == 27):
238 raise KeyboardInterrupt
239
240 springbot.refresh(grav=(0,0), visc=gear.VISCOSITY)
241
242
243 end_x, end_y = springbot.massCenter()
244
245
246 return sqrt((end_x-start_x)**2 + (end_y-start_y)**2)
247
248
249
250
251
252