Skip to content

Instantly share code, notes, and snippets.

@fedden
Created October 12, 2017 18:11
Show Gist options
  • Save fedden/c4e919a94707b2f587e416aa99d64c86 to your computer and use it in GitHub Desktop.
Save fedden/c4e919a94707b2f587e416aa99d64c86 to your computer and use it in GitHub Desktop.
multi_dfo
envs = [gym.make(self.env_name) for _ in range(multiprocessing.cpu_count())]
amount_per_thread = int(np.floor(self.population_size / multiprocessing.cpu_count()))
left_over = self.population_size - amount_per_thread * multiprocessing.cpu_count()
fitnesses = np.zeros(len(self.population))
def get_weights_reward(begin, size, env):
for i in range(begin, begin + size):
fitnesses[i] = -self.get_reward(self.population[i],
self.sess,
env,
self.meta)
threads = []
idx = 0
for i in range(multiprocessing.cpu_count()):
amt = (amount_per_thread + 1) if i < left_over else amount_per_thread
thread = threading.Thread(target=get_weights_reward,
args=[idx, amt, envs[i]])
threads.append(thread)
idx += amt
assert idx == len(self.population)
for t in threads:
t.start()
for t in threads:
t.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment