예제 #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

# 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"
예제 #4
0
파일: simple.py 프로젝트: IloneM/libcmaes
import lcmaes
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)
예제 #5
0
import lcmaes
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)
예제 #6
0
import lcmaes

# input parameters for a 10-D problem
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