Package springbots :: Module fitness
[hide private]

Source Code for Module springbots.fitness

  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  # We need vector and gear to calculate some stuff 
 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 # Center springbot horizontaly and touches ground 33 if len(springbot['bloodline'].split('.')) == 1: 34 springbot.centerGround(height) 35 36 # Selects its mass center 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 # Starts the simulation 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() # Show display 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 # Selects its mass center now 59 end_x, end_y = springbot.massCenter() 60 61 # Returns the horizontal distance traveled(fitness) 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 # Center springbot horizontaly ant touches ground 75 if len(springbot['bloodline'].split('.')) == 1: 76 springbot.centerGround(height) 77 78 # Selects its mass center 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 # Starts the simulation 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() # Show display 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 # Calculates its mass center 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 # Returns the maximal height achieved 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 # Center springbot horizontaly ant touches ground 120 if len(springbot['bloodline'].split('.')) == 1: 121 springbot.centerGround(height) 122 123 # Equilibrium ratio sum 124 eq_ratio = 0 125 126 # Height sum 127 height_av = 0 128 129 if enable_graphics and HAS_PYGAME: 130 ticks = 0 131 screen = pygame.display.get_surface() 132 133 # Starts the simulation 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() # Show display 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 # Calculates its mass center 146 cx, cy = springbot.massCenter() 147 148 # Calculates its bounding box 149 x1, y1, x2, y2 = springbot.boundingBox() 150 151 # Calculates the equilibrium ratio and sum 152 eq_ratio += (y2-cy)/(y2-y1) 153 height_av += y2-y1 154 155 springbot.refresh() 156 springbot.colideWall(height, gear.DOWN) 157 158 # Returns the equilibrium ratio average 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 # Center springbot horizontaly ant touches ground 172 if len(springbot['bloodline'].split('.')) == 1: 173 springbot.centerGround(height) 174 175 # Aspect ratio sum 176 aspect_ratio = 0 177 178 if enable_graphics and HAS_PYGAME: 179 ticks = 0 180 screen = pygame.display.get_surface() 181 182 # Starts the simulation 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() # Show display 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 # Calculates its bounding box 195 x1, y1, x2, y2 = springbot.boundingBox() 196 197 # Calculates the aspect ratio and sum 198 if x2 - x1 != 0: 199 aspect_ratio += (y2 - y1) / (x2 - x1) 200 201 # Refresgh springbot 202 springbot.refresh() 203 springbot.colideWall(height, gear.DOWN) 204 205 # Returns the aspect ratio average 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 # Selects its mass center 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 # Starts the simulation 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() # Show display 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 # Selects its mass center now 243 end_x, end_y = springbot.massCenter() 244 245 # Returns the horizontal distance traveled(fitness) 246 return sqrt((end_x-start_x)**2 + (end_y-start_y)**2)
247 248 249 # # 250 ################################################################################ 251 # # 252