def _do_optimize_delayed_update(self, fun, max_evals, workers=mp.cpu_count()): self.fun = fun self.max_evals = max_evals evaluator = Evaluator(self.fun) evaluator.start(workers) evals_x = {} self.iterations = 0 self.evals = 0 self.p = 0 self.improves = deque() for _ in range(workers): # fill queue with initial population p, x = self.ask_one() evaluator.pipe[0].send((self.evals, x)) evals_x[self.evals] = p, x # store x self.evals += 1 while True: # read from pipe, tell de and create new x evals, y = evaluator.pipe[0].recv() p, x = evals_x[evals] # retrieve evaluated x del evals_x[evals] self.tell_one(p, y, x) # tell evaluated x if self.stop != 0 or self.evals >= self.max_evals: break # shutdown worker if stop criteria met p, x = self.ask_one() # create new x evaluator.pipe[0].send((self.evals, x)) evals_x[self.evals] = p, x # store x self.evals += 1 evaluator.stop() return self.best_x, self.best_value, self.evals, self.iterations, self.stop
def do_optimize_delayed_update(self, fun, workers=mp.cpu_count()): evaluator = Evaluator(fun) evaluator.start(workers) evals_x = {} self.evals = 0 for _ in range(workers): # fill queue x = self.ask_one() evaluator.pipe[0].send((self.evals, x)) evals_x[self.evals] = x # store x self.evals += 1 while True: # read from pipe, tell es and create new x evals, y = evaluator.pipe[0].recv() x = evals_x[evals] # retrieve evaluated x del evals_x[evals] stop = self.tell_one(y, x) # tell evaluated x if stop != 0 or self.evals >= self.max_evaluations: break # shutdown worker if stop criteria met x = self.ask_one() # create new x evaluator.pipe[0].send((self.evals, x)) evals_x[self.evals] = x # store x self.evals += 1 evaluator.stop() return self.best_x, self.best_value, evals, self.iterations, self.stop
class parallel(object): """Convert an objective function for parallel execution for cmaes.minimize. Parameters ---------- fun : objective function mapping a list of float arguments to a float value. represents a function mapping a list of lists of float arguments to a list of float values by applying the input function using parallel processes. stop needs to be called to avoid a resource leak""" def __init__(self, fun, workers=mp.cpu_count()): self.evaluator = Evaluator(fun) self.evaluator.start(workers) def __call__(self, xs): return eval_parallel(xs, self.evaluator) def stop(self): self.evaluator.stop()