예제 #1
0
def to_params(x0,
              sigma0,
              str_algo=b'acmaes',
              fplot=None,
              lbounds=None,
              ubounds=None,
              scaling=False,
              vscaling=None,
              vshift=None,
              **kwargs):
    """return parameter object instance for `lcmaes.pcmaes`.

    Keys in `kwargs` must correspond to `name` in `set_name` attributes
    of `lcmaes.CMAParameters`, e.g. ``ftarget=1e-7``.

    Details: when `fplot is None` (default), the default output filename
    is used.
    """
    has_bounds = not lbounds == None and not ubounds == None
    p = None
    if has_bounds:
        if scaling == False:
            gp = lcmaes.make_genopheno_pwqb(lbounds, ubounds, len(ubounds))
            p = lcmaes.make_parameters_pwqb(x0, sigma0, gp)
        else:
            gp = lcmaes.make_genopheno_pwqb_ls(lbounds, ubounds, len(ubounds))
            p = lcmaes.make_parameters_pwqb_ls(x0, sigma0, gp, -1, 0)
    else:
        if vscaling is None:
            p = lcmaes.make_simple_parameters(x0, sigma0)
        else:
            gp = lcmaes.make_genopheno_ls(vscaling, vshift)
            p = lcmaes.make_parameters_ls(x0, sigma0, gp)
    p.set_str_algo(str_algo)
    if fplot and fplot != True:  # then fplot must be filename
        global fplot_current
        fplot_current = fplot
    if fplot or fplot is None:  # 0 or False or '' or "" prevents writing
        p.set_fplot(fplot_current)
    for key, val in kwargs.items():
        setter = "set_" + key
        if not hasattr(p, setter):
            raise ValueError(setter +
                             " is not known as method of CMAParameters")
        getattr(p, setter)(val)  # call setter with value
    return p
예제 #2
0
def to_params(x0, sigma0, str_algo=b'acmaes', fplot=None, lbounds=None, ubounds=None, scaling=False, vscaling=None, vshift=None, **kwargs):
    """return parameter object instance for `lcmaes.pcmaes`.

    Keys in `kwargs` must correspond to `name` in `set_name` attributes
    of `lcmaes.CMAParameters`, e.g. ``ftarget=1e-7``.

    Details: when `fplot is None` (default), the default output filename
    is used.
    """
    has_bounds = not lbounds==None and not ubounds == None
    p = None
    if has_bounds:
        if scaling==False:
            gp = lcmaes.make_genopheno_pwqb(lbounds,ubounds,len(ubounds))
            p = lcmaes.make_parameters_pwqb(x0,sigma0,gp)
        else:
            gp = lcmaes.make_genopheno_pwqb_ls(lbounds,ubounds,len(ubounds))
            p = lcmaes.make_parameters_pwqb_ls(x0,sigma0,gp,-1,0)
    else:
        if vscaling is None:
            p = lcmaes.make_simple_parameters(x0, sigma0)
        else:
            gp = lcmaes.make_genopheno_ls(vscaling,vshift)
            p = lcmaes.make_parameters_ls(x0,sigma0,gp)
    p.set_str_algo(str_algo)
    if fplot and fplot != True:  # then fplot must be filename
        global fplot_current
        fplot_current = fplot
    if fplot or fplot is None:  # 0 or False or '' or "" prevents writing
        p.set_fplot(fplot_current)
    for key, val in kwargs.items():
        setter = "set_" + key
        if not hasattr(p, setter):
            raise ValueError(setter + " is not known as method of CMAParameters")
        getattr(p, setter)(val)  # call setter with value
    return p
예제 #3
0
import lcmaes, random

# input parameters for a 10-D problem
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
scaling = [int(1000 * random.random()) for i in xrange(10)]
shift = [int(1000 * random.random()) for i in xrange(10)]
gp = lcmaes.make_genopheno_ls(scaling, shift)
p = lcmaes.make_parameters_ls(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_ls(objfunc, p)

# collect and inspect results
bcand = cmasols.best_candidate()
bx = lcmaes.get_candidate_x(bcand)
print("best x=", bx)
예제 #4
0
파일: ptests_ls.py 프로젝트: beniz/libcmaes
import lcmaes, random

# input parameters for a 10-D problem
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
scaling = [int(1000*random.random()) for i in range(10)]
shift = [int(1000*random.random()) for i in range(10)]
gp = lcmaes.make_genopheno_ls(scaling,shift)
p = lcmaes.make_parameters_ls(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_ls(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