SHARE
TWEET

Untitled

a guest Oct 13th, 2017 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. envs = [gym.make(self.env_name) for _ in range(multiprocessing.cpu_count())]
  2.  
  3. amount_per_thread = int(np.floor(self.population_size / multiprocessing.cpu_count()))
  4. left_over = self.population_size - amount_per_thread * multiprocessing.cpu_count()
  5.  
  6. fitnesses = np.zeros(len(self.population))
  7.  
  8. def get_weights_reward(begin, size, env):
  9.     for i in range(begin, begin + size):
  10.         fitnesses[i] = -self.get_reward(self.population[i],
  11.                                         self.sess,
  12.                                         env,
  13.                                         self.meta)
  14. threads = []
  15. idx = 0
  16. for i in range(multiprocessing.cpu_count()):
  17.     amt = (amount_per_thread + 1) if i < left_over else amount_per_thread
  18.     thread = threading.Thread(target=get_weights_reward,
  19.                               args=[idx, amt, envs[i]])
  20.     threads.append(thread)
  21.     idx += amt
  22.  
  23. assert idx == len(self.population)
  24.  
  25. for t in threads:
  26.     t.start()
  27. for t in threads:
  28.     t.join()
RAW Paste Data
Top