def gcmaes(x_init, fun=None, fun_grad=None, grad_lookup=None, options={}): """ EXPERIMENTAL CMA-Es where every point in the cloud is optimized with LBFG-S and the resulting cloud and results are used for the CMA update. """ options_cma = options["cmaes"] if "init_point" in options_cma: init_point = bool(options_cma.pop("init_point")) else: init_point = False if "spread" in options_cma: spread = float(options_cma.pop("spread")) else: spread = 0.1 shrinked_check = False if "stop_at_convergence" in options_cma: sigma_conv = int(options_cma.pop("stop_at_convergence")) sigmas = [] shrinked_check = True sigma_check = False if "stop_at_sigma" in options_cma: stop_sigma = int(options_cma.pop("stop_at_sigma")) sigma_check = True settings = options_cma es = cma.CMAEvolutionStrategy(x_init, spread, settings) iter = 0 while not es.stop(): if shrinked_check: sigmas.append(es.sigma) if iter > sigma_conv: if all(sigmas[-(i + 1)] < sigmas[-(i + 2)] for i in range(sigma_conv - 1)): print(f"C3:STATUS:Shrinked cloud for {sigma_conv} steps. " "Switching to gradients.") break if sigma_check: if es.sigma < stop_sigma: print("C3:STATUS:Goal sigma reached. Stopping CMA.") break samples = es.ask() if init_point and iter == 0: samples.insert(0, x_init) print("C3:STATUS:Adding initial point to CMA sample.") solutions = [] points = [] for sample in samples: r = lbfgs( sample, fun_grad=fun_grad, grad_lookup=grad_lookup, options=options["lbfgs"], ) solutions.append(r.fun) points.append(r.x) es.tell(points, solutions) es.disp() iter += 1 return es.result.xbest
def cmaes(x_init, fun=None, fun_grad=None, grad_lookup=None, options={}): """ Wrapper for the pycma implementation of CMA-Es. See also: http://cma.gforge.inria.fr/apidocs-pycma/ Parameters ---------- x_init : float Initial point. fun : callable Goal function. fun_grad : callable Function that computes the gradient of the goal function. grad_lookup : callable Lookup a previously computed gradient. options : dict Options of pycma and the following custom options. noise : float Artificial noise added to a function evaluation. init_point : boolean Force the use of the initial point in the first generation. spread : float Adjust the parameter spread of the first generation cloud. stop_at_convergence : int Custom stopping condition. Stop if the cloud shrunk for this number of generations. stop_at_sigma : float Custom stopping condition. Stop if the cloud shrunk to this standard deviation. Returns ------- np.array Parameters of the best point. """ if "noise" in options: noise = float(options.pop("noise")) else: noise = 0 if "init_point" in options: init_point = bool(options.pop("init_point")) else: init_point = False if "spread" in options: spread = float(options.pop("spread")) else: spread = 0.1 shrunk_check = False if "stop_at_convergence" in options: sigma_conv = int(options.pop("stop_at_convergence")) sigmas = [] shrunk_check = True sigma_check = False if "stop_at_sigma" in options: stop_sigma = int(options.pop("stop_at_sigma")) sigma_check = True settings = options es = cma.CMAEvolutionStrategy(x_init, spread, settings) iter = 0 while not es.stop(): if shrunk_check: sigmas.append(es.sigma) if iter > sigma_conv: if all(sigmas[-(i + 1)] < sigmas[-(i + 2)] for i in range(sigma_conv - 1)): print(f"C3:STATUS:Shrunk cloud for {sigma_conv} steps. " "Switching to gradients.") break if sigma_check: if es.sigma < stop_sigma: print("C3:STATUS:Goal sigma reached. Stopping CMA.") break samples = es.ask() if init_point and iter == 0: samples.insert(0, x_init) print("C3:STATUS:Adding initial point to CMA sample.") solutions = [] for sample in samples: goal = fun(sample) if noise: goal = goal + (np.random.randn() * noise) solutions.append(goal) es.tell(samples, solutions) es.disp() iter += 1 es.result_pretty() return es.result.xbest