def pcmaes(fitfunc,p): has_bounds = isinstance(p,lcmaes.CMAParametersPB) or isinstance(p,lcmaes.CMAParametersPBS) has_scaling = isinstance(p,lcmaes.CMAParametersNBS) or isinstance(p,lcmaes.CMAParametersPBS) if not has_bounds: if not has_scaling: return lcmaes.pcmaes(fitfunc,p) else: return lcmaes.pcmaes_ls(fitfunc,p) else: if not has_scaling: return lcmaes.pcmaes_pwqb(fitfunc,p) else: return lcmaes.pcmaes_pwqb_ls(fitfunc,p)
def pcmaes(fitfunc, p): has_bounds = isinstance(p, lcmaes.CMAParametersPB) or isinstance( p, lcmaes.CMAParametersPBS) has_scaling = isinstance(p, lcmaes.CMAParametersNBS) or isinstance( p, lcmaes.CMAParametersPBS) if not has_bounds: if not has_scaling: return lcmaes.pcmaes(fitfunc, p) else: return lcmaes.pcmaes_ls(fitfunc, p) else: if not has_scaling: return lcmaes.pcmaes_pwqb(fitfunc, p) else: return lcmaes.pcmaes_pwqb_ls(fitfunc, p)
# input parameters for a 10-D problem x = [10]*10 olambda = 10 # lambda is a reserved keyword in python, using olambda instead. seed = 0 # 0 for seed auto-generated within the lib. sigma = 0.1 p = lcmaes.make_simple_parameters(x,sigma,olambda,seed) p.set_str_algo("acmaes") # objective function. def nfitfunc(x,n): val = 0.0 for i in range(0,n): val += x[i]*x[i] return val # generate a function object objfunc = lcmaes.fitfunc_pbf.from_callable(nfitfunc); # pass the function and parameter to cmaes, run optimization and collect solution object. cmasols = lcmaes.pcmaes(objfunc,p) # collect and inspect results bcand = cmasols.best_candidate() bx = lcmaes.get_candidate_x(bcand) print "best x=",bx print "distribution mean=",lcmaes.get_solution_xmean(cmasols) cov = lcmaes.get_solution_cov(cmasols) # numpy array print "cov=",cov print "elapsed time=",cmasols.elapsed_time(),"ms"
import cma_multiplt as lcmaplt # input parameters for a 10-D problem x = [10.0] * 10 sigma = 0.1 outfile = 'simple.dat' p = lcmaes.make_simple_parameters(x, sigma) p.set_str_algo("acmaes") p.set_fplot(outfile) # objective function. def nfitfunc(x, n): assert len(x) == n # should not be necessary return sum([xi**2 for xi in x]) # pass the function and parameters to cmaes, run optimization and collect solution object. cmasols = lcmaes.pcmaes(lcmaes.fitfunc_pbf.from_callable(nfitfunc), p) # visualize results lcmaplt.plot(outfile) lcmaplt.pylab.ioff() lcmaplt.pylab.show() if __name__ == "__main__": msg = ' --- press return to continue --- ' try: raw_input(msg) except NameError: input(msg)
# input parameters for a 10-D problem x = [10.0] * 10 sigma = 0.1 outfile = 'simple.dat' p = lcmaes.make_simple_parameters(x, sigma) p.set_str_algo("acmaes") p.set_fplot(outfile) # objective function. def nfitfunc(x, n): assert len(x) == n # should not be necessary return sum([xi**2 for xi in x]) # pass the function and parameters to cmaes, run optimization and collect solution object. cmasols = lcmaes.pcmaes(lcmaes.fitfunc_pbf.from_callable(nfitfunc), p) # visualize results lcmaplt.plot(outfile) lcmaplt.pylab.ioff() lcmaplt.pylab.show() if __name__ == "__main__": msg = ' --- press return to continue --- ' try: raw_input(msg) except NameError: input(msg)
x = [10] * 10 lambda_ = 10 # lambda is a reserved keyword in python, using lambda_ instead. seed = 0 # 0 for seed auto-generated within the lib. sigma = 0.1 p = lcmaes.make_simple_parameters(x, sigma, lambda_, seed) p.set_str_algo("acmaes") # objective function. def nfitfunc(x, n): val = 0.0 for i in range(0, n): val += x[i] * x[i] return val # generate a function object objfunc = lcmaes.fitfunc_pbf.from_callable(nfitfunc) # pass the function and parameter to cmaes, run optimization and collect solution object. cmasols = lcmaes.pcmaes(objfunc, p) # collect and inspect results bcand = cmasols.best_candidate() bx = lcmaes.get_candidate_x(bcand) print("best x=", bx) print("distribution mean=", lcmaes.get_solution_xmean(cmasols)) cov = lcmaes.get_solution_cov(cmasols) # numpy array print("cov=", cov) print("elapsed time=", cmasols.elapsed_time(), "ms")