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)
x = [3]*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 lbounds = [-4]*10 ubounds = [4]*10 gp = lcmaes.make_genopheno_pwqb(lbounds,ubounds,10) p = lcmaes.make_parameters_pwqb(x,sigma,olambda,seed,gp) # 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_pwqb(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"
x = [3]*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 lbounds = [-4]*10 ubounds = [4]*10 gp = lcmaes.make_genopheno_pwqb(lbounds,ubounds,10) p = lcmaes.make_parameters_pwqb(x,sigma,gp,lambda_,seed) # 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_pwqb(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")