Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.16 KB | None | 0 0
  1. def update(self, dt):
  2.  
  3.         global brain
  4.         global last_reward
  5.         global scores
  6.         global last_distance
  7.         global goal_x
  8.         global goal_y
  9.         global longueur
  10.         global largeur
  11.         global lastTime
  12.  
  13.         longueur = self.width
  14.         largeur = self.height
  15.         if first_update:
  16.             init()
  17.  
  18.         # code you want to evaluate
  19.         start_time = timeit.default_timer()
  20.        
  21.         xx = goal_x - self.car.x
  22.         yy = goal_y - self.car.y
  23.         orientation = Vector(*self.car.velocity).angle((xx,yy))/180.
  24.         last_signal = [self.car.signal1, self.car.signal2, self.car.signal3, orientation, -orientation]
  25.         action = brain.update(last_reward, last_signal)
  26.         scores.append(brain.score())
  27.         rotation = action2rotation[action]
  28.         self.car.move(rotation)
  29.         distance = np.sqrt((self.car.x - goal_x)**2 + (self.car.y - goal_y)**2)
  30.         self.ball1.pos = self.car.sensor1
  31.         self.ball2.pos = self.car.sensor2
  32.         self.ball3.pos = self.car.sensor3
  33.         elapsed = timeit.default_timer() - start_time
  34.         lastTime += elapsed
  35.  
  36.         if sand[int(self.car.x),int(self.car.y)] > 0:
  37.             self.car.velocity = Vector(1, 0).rotate(self.car.angle)
  38.             last_reward = -1
  39.         else: # otherwise
  40.             self.car.velocity = Vector(6, 0).rotate(self.car.angle)
  41.             last_reward = -0.05 * lastTime
  42.             if distance < last_distance:
  43.                 last_reward = 0.1
  44.  
  45.         if self.car.x < 10:
  46.             self.car.x = 10
  47.             last_reward = -1
  48.         if self.car.x > self.width - 10:
  49.             self.car.x = self.width - 10
  50.             last_reward = -1
  51.         if self.car.y < 10:
  52.             self.car.y = 10
  53.             last_reward = -1
  54.         if self.car.y > self.height - 10:
  55.             self.car.y = self.height - 10
  56.             last_reward = -1
  57.        
  58.         if distance < 100:
  59.             last_reward = -0.4 * lastTime + 1
  60.             print lastTime
  61.             lastTime = 0
  62.             goal_x = self.width-goal_x
  63.             goal_y = self.height-goal_y
  64.            
  65.         last_distance = distance
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement