def prepare_optimization(self): # Wait until (presumably) all workers are awake and ready # to avoid unfair distribution of tasks. time.sleep(1) printf("Waiting for initial connection") self.__fetch_constraints() printf("Received initial data from workers")
def __network_mode(self, workers): printf("Starting optimization with network workers") if not workers: raise RuntimeError("You have to specify a list of remote" " worker addresses") # Connect to workers with ZeroMQ. self.cc = NetworkClientComm(addresses=parse_worker_addresses(workers))
def run(self): u"Fire the Genetic Algorithm Engine." printf("Starting GA: %d generations of %d individuals" % ( self.generations, self.size)) self.timer.start() self.ga.evolve() self.__post_run() # Return the best parameters found. return list(self.ga.bestIndividual().getInternalList())
def run(self): printf("Starting LM: %d iterations" % self.max_iter) objective = LevmarObjective(self.comm) self.timer.start() output = levmar(objective, p0=self.p0, y=self.y, bounds=self.bounds, maxit=self.max_iter, cdif=self.central, eps1=1e-15, eps2=1e-15, eps3=1e-20, full_output=True, breakf=self.__breakf) self.__post_run(output) return list(output.p)
def __breakf(self, i, maxit, p, error): pstr = "Iteration %d of %d, ||error|| %g" % (i, maxit, error) self.timer.lap() if i > 0: iter_left = maxit - i mean_time = self.timer.time_so_far / i pstr += "\t(est. time to end=%d s.)" % (iter_left * mean_time) printf(pstr) if check_stop_flag(self.stop_flag): return 1
def stats_step_callback(ga): u"Function run every generation printing erorr and time stats" pstr = "Step %d out of %d: " % (ga.currentGeneration, ga.nGenerations) pstr += "err [%%] = %g" % (ga.bestIndividual().score * 100.) timer = ga.getParam("timer") timer.lap() if ga.currentGeneration > 0: gens_left = ga.nGenerations - ga.currentGeneration mean_time = timer.time_so_far / ga.currentGeneration pstr += "\t(est. time to end=%d s.)" % (gens_left * mean_time) printf(pstr)
def __local_mode(self, workers, xargs, evaluator_path): if evaluator_path is None: raise ValueError("You have to specify a path to the evaluator" " while running in local mode.") printf("Starting optimization with local workers (%d)" % workers) # Prepare the ZeroMQ communication layer. self.cc = LocalClientComm() # Arguments to be passed to evaluator processes. evaluator_args = xargs + ["-local-mode", "-local-pull-address", self.cc.push_addr, "-local-publish-address", self.cc.sub_addr] # Launch worker processes. spawn_workers(workers, evaluator_path, evaluator_args)
def __local_mode(self, workers, xargs, evaluator_path): if evaluator_path is None: raise ValueError("You have to specify a path to the evaluator" " while running in local mode.") printf("Starting optimization with local workers (%d)" % workers) # Prepare the ZeroMQ communication layer. self.cc = LocalClientComm() # Arguments to be passed to evaluator processes. evaluator_args = xargs + [ "-local-mode", "-local-pull-address", self.cc.push_addr, "-local-publish-address", self.cc.sub_addr ] # Launch worker processes. spawn_workers(workers, evaluator_path, evaluator_args)
def __post_run(self, lm_output): run_time = self.timer.stop() info = lm_output.info printf("Iteration %d of %d, ||error|| %g" % (info[2], self.max_iter, info[1][0])) print "LM finished in %g s, with reason: %s" % (run_time, info[3])
def display_info(self, parameters): u"Asks evaluator for parameters description and displays it." self.cc.get_stats(parameters, "2%d" % id(parameters)) response = self.cc.resp_stats("2%d" % id(parameters), wait=True) printf("Statistics for given parameters:") printf(response["stats"])