Exemple #1
0
def run_hartmann6_benchmarks(D, rep, random_subspace=False):
    if D == 100:
        problem = hartmann6_100
    elif D == 1000 and not random_subspace:
        problem = hartmann6_1000
    elif D == 1000 and random_subspace:
        problem = hartmann6_random_subspace_1000

    experiment, f = benchmark_minimize_callable(
        problem=problem,
        num_trials=200,
        method_name='cmaes',
        replication_index=rep,
    )

    try:
        cma.fmin(
            objective_function=f,
            x0=[0.5] * D,
            sigma0=0.25,
            options={
                'bounds': [[0] * D, [1] * D],
                'maxfevals': 200
            },
        )
    except ValueError:
        pass  # CMA-ES doesn't always terminate at exactly maxfevals

    rs_str = 'random_subspace_' if random_subspace else ''
    with open(f'results/hartmann6_{rs_str}{D}_cmaes_rep_{rep}.json',
              "w") as fout:
        json.dump(object_to_json(experiment), fout)
Exemple #2
0
def launchCMAESForSpecificTargetSize(target_size, rs, save):
    '''
    Run cmaes for a specific target size

    Input:	
            -target_size, size of the target, float
            -setuFile, file of setup, string
            -save: do we use a previous Best.theta file? True = Yes, False = use current controller, None = random controller
    '''
    print("Starting the CMAES Optimization for target " + str(target_size) + " !")
    foldername = rs.OPTIpath + str(target_size) + "/"
    if save:
        thetaname = foldername + rs.thetaFile
        copyRegressiontoCMAES(rs, rs.thetaFile, target_size)
    else:
        thetaname = foldername + "Best"

    #Initializes all the class used to generate trajectory
    exp = Experiments(rs, target_size, False, foldername, thetaname,rs.popsizeCmaes,rs.period)
    theta = exp.tm.controller.getTheta()
    thetaCMA = theta.flatten()

    #run the optimization (cmaes)
    cma.fmin(exp.runTrajectoriesCMAES, thetaCMA, rs.sigmaCmaes, options={'maxiter':rs.maxIterCmaes, 'popsize':rs.popsizeCmaes, 'CMA_diagonal':True, 'verb_log':50, 'verb_disp':1,'termination_callback':term()})
    print("End of optimization for target " + str(target_size) + " !")
Exemple #3
0
 def run(self):
     if self._is_restart:
         x0 = '{lb} + ({delta})*(np.random.random({d}))'.format(
             lb=list(self._opts['bounds'][0]),
             delta=list(
                 np.array(self._opts['bounds'][1]) -
                 self._opts['bounds'][0]),
             d=self._dim_cma)
         res = cma.fmin(lambda x: self._fct(x[:self._dim]),
                        x0,
                        0.25,
                        self._opts,
                        eval_initial_x=True,
                        restarts=5,
                        bipop=True)
     else:
         res = cma.fmin(lambda x: self._fct(x[:self._dim]),
                        self._x0,
                        0.25,
                        self._opts,
                        eval_initial_x=True,
                        restarts=0)
     x_opt = res[0][:self._dim]  # index trick as 1D is not supported
     f_opt = res[1]
     num_fevals = res[3]  # num of fevals used
     return x_opt, f_opt, num_fevals
Exemple #4
0
def launchCMAESMissing(rs, save, gamma, point):
    '''
    Run cmaes for a specific target size

    Input:    -target_size, size of the target, float
            -setuFile, file of setup, string
            -save: do we use a previous Best.theta file? True = Yes, False = use current controller, None = random controller
    '''
    rs.gamma = gamma/10.0
    pos=point[0]
    x=point[1][0]
    y=point[1][1]
    target_size = point[2]
    print("Starting the CMAES Optimization for gamma = "+ str(gamma) + ",target " + str(target_size) + " for point "+ str(pos)+" !")
    foldername = rs.OPTIpathMissing + "gamma" + str(gamma) + "/"+str(target_size)+"/"+str(pos)+"/"

    thetaname = foldername + "Best"
    if save:
        checkIfFolderExists(foldername)
        copyfile(rs.OPTIpathMissing + "gamma" + str(gamma) + "/" + str(target_size)+"/" + "Best.theta",foldername + "Best.theta")
    elif save==None:
        thetaname=None


    #Initializes all the classes used to generate trajectory
    exp = Experiments(rs, target_size, False, foldername, thetaname,rs.popsizeCmaes,rs.period)
    theta = exp.tm.controller.getTheta()
    thetaCMA = theta.flatten()

    #run the optimization (cmaes)
    cma.fmin(partial(exp.runTrajectoriesCMAESOnePointMulti, x, y), thetaCMA, rs.sigmaCmaes, options={'maxiter':rs.maxIterCmaes, 'popsize':rs.popsizeCmaes, 'CMA_diagonal':True, 'verb_log':0, 'verb_disp':0,'termination_callback':term()})
    print("End of optimization for gamma = "+ str(gamma) + ",target " + str(target_size) + " for point "+ str(pos)+" !")
Exemple #5
0
def launchCMAESForSpecificTargetSizeAndSpecificPoint(target_size,
                                                     rs,
                                                     save,
                                                     point,
                                                     noise=None):
    '''
    Run cmaes for a specific target size

    Input:    -target_size, size of the target, float
            -setuFile, file of setup, string
            -save: do we use a previous Best.theta file? True = Yes, False = use current controller, None = random controller
            noise: noise on muscle, if None, defalt noise from muscle setup, float
    '''
    pos = point[0]
    x = point[1][0]
    y = point[1][1]
    print("Starting the CMAES Optimization for target " + str(target_size) +
          " for point " + str(pos) + " !")
    foldername = rs.OPTIpath + str(target_size) + "/" + str(pos) + "/"

    thetaname = foldername + "Best"
    if save:
        checkIfFolderExists(foldername)
        copyfile(rs.OPTIpath + str(target_size) + "/" + "Best.theta",
                 foldername + "Best.theta")
    elif save == None:
        thetaname = None

    #Initializes all the class used to generate trajectory
    exp = Experiments(rs, target_size, False, foldername, thetaname,
                      rs.popsizeCmaes, rs.period)
    if (noise != None):
        exp.setNoise(noise)
    theta = exp.tm.controller.getTheta()
    thetaCMA = theta.flatten()

    #run the optimization (cmaes)
    cma.fmin(partial(exp.runTrajectoriesCMAESOnePoint, x, y),
             thetaCMA,
             rs.sigmaCmaes,
             options={
                 'maxiter': rs.maxIterCmaes,
                 'popsize': rs.popsizeCmaes,
                 'CMA_diagonal': True,
                 'verb_log': 0,
                 'verb_disp': 0,
                 'termination_callback': term()
             })
    print("End of optimization for target " + str(target_size) +
          " for point " + str(pos) + " !")
Exemple #6
0
    def optimize(self) -> List[Component]:
        solutions = []
        for i in range(10):
            x0a = [random() for i in range(8)]

            xra, fx = cma.fmin(self.objective,
                               x0a,
                               0.1,
                               options={
                                   'bounds': [0, 1],
                                   'tolfun': 1e-8,
                                   'verbose': 10,
                                   'ftarget': 1e-8,
                                   'maxiter': 100
                               })[0:2]

            x_sol = xra
            points = [
                vm.Point2D(x_sol[2 * i], x_sol[2 * i + 1])
                for i in range(int(len(x_sol) / 2.))
            ]
            self.component.update(*points)
            length = self.component.length()
            if abs(length - 0.2) < 1e-3:
                solutions.append(self.component.copy())
        return solutions
def infere_variance(subject,x0,lbounds=None,ubounds=None,cma_sigma=1/3,cma_options=cma.CMAOptions(),restarts=0):
	if not isinstance(cma_options,cma.CMAOptions):
		cma_options = cma.CMAOptions(cma_options)
	if lbounds is None:
		lbounds = 5*np.ones(len(x0))
	else:
		lbounds = np.array(lbounds)
	if ubounds is None:
		ubounds = np.inf*np.ones(len(x0))
	else:
		ubounds = np.array(ubounds)
	if any(np.isinf(ubounds)):
		scaling_of_variables = None
	else:
		scaling_of_variables = ubounds-lbounds
	cma_options.set('bounds',[lbounds,ubounds])
	if scaling_of_variables is not None:
		cma_options.set('scaling_of_variables',scaling_of_variables)
	
	dat,t,d = subject.load_data()
	inds = dat[:,1]<1000
	dat = dat[inds]
	t = np.mean(t[inds],axis=2)
	d = np.mean(d[inds],axis=2)
	rt_ind = np.floor(dat[:,1]/40)
	model = pe.KnownVarPerfectInference(prior_mu_t=50,prior_mu_d=50,prior_va_t=15**2,prior_va_d=15**2)
	args = (model,dat[:,2],rt_ind,t,d)
	
	cma_fmin_output = cma.fmin(objective,x0,cma_sigma,options=cma_options,args=args,restarts=restarts)
	stop_cond = {}
	for key in cma_fmin_output[7].keys():
		stop_cond[key] = cma_fmin_output[7][key]
	return {'xopt':cma_fmin_output[0],'fopt':cma_fmin_output[1],'evalsopt':cma_fmin_output[2],\
			'evals':cma_fmin_output[3],'iterations':cma_fmin_output[4],'xmean':cma_fmin_output[5],\
			'stds':cma_fmin_output[6],'stop':stop_cond}
Exemple #8
0
    def maximize(self):
        """
        Maximizes the given acquisition function.

        Returns
        -------
        np.ndarray(N,D)
            Point with the highest acquisition value.
        """

        verbose_level = -9
        if self.verbose:
            verbose_level = 0

        start_point = init_random_uniform(self.lower, self.upper, 1, self.rng)

        def obj_func(x):
            a = self.objective_func(x[None, :])
            return a[0]

        res = cma.fmin(obj_func, x0=start_point[0], sigma0=0.6,
                       restarts=self.restarts,
                       options={"bounds": [self.lower, self.upper],
                                "verbose": verbose_level,
                                "verb_log": sys.maxsize,
                                "maxfevals": self.n_func_evals})
        if res[0] is None:
            logging.error("CMA-ES did not find anything. \
                Return random configuration instead.")
            return start_point

        return res[0]
Exemple #9
0
def wrapper_CMA(f, bounds):
    '''
    Wrapper the Covariance Matrix Adaptation Evolutionary strategy (CMA-ES) optimization method. It works generating 
    an stochastic seach based on mutivariate Gaussian samples. Only requieres f and the box constrains to work
    :param f: function to optimize, acquisition.
    :param bounds: tuple determining the limits of the optimizer.

    '''
    try:
        import cma
        import numpy as np

        def CMA_f_wrapper(f):
            def g(x):
                return f(np.array([x]))[0][0]

            return g

        lB = np.asarray(bounds)[:, 0]
        uB = np.asarray(bounds)[:, 1]
        x = cma.fmin(CMA_f_wrapper(f), (uB + lB) * 0.5,
                     0.6,
                     options={
                         "bounds": [lB, uB],
                         "verbose": -1
                     })[0]
        print x
        return reshape(x, len(bounds))
    except:
        print("Cannot find cma library, please install it to use this option.")
Exemple #10
0
    def optimize(self, x0, f=None, df=None, f_df=None):
        """
        :param x0: initial point for a local optimizer.
        :param f: function to optimize.
        :param df: gradient of the function to optimize.
        :param f_df: returns both the function to optimize and its gradient.
        """
        try:
            import cma

            def CMA_f_wrapper(f):
                def g(x):
                    val = f(np.array([x]))
                    val = np.reshape(val, (1, ))
                    return val

                return g

            lB = np.asarray(self.bounds)[:, 0]
            uB = np.asarray(self.bounds)[:, 1]
            x = cma.fmin(CMA_f_wrapper(f),
                         x0,
                         0.6,
                         options={
                             "bounds": [lB, uB],
                             "verbose": -1,
                             "maxfevals": 150
                         })[0]
            return np.atleast_2d(x), f(np.atleast_2d(x))
        except ImportError:
            print(
                "Cannot find cma library, please install it to use this option."
            )
def st_cma(objfunc, par_b, par_0, maxiter, scale, offset):
    try:
        import cma
    except ImportError:
        cma = None
    assert cma is not None, 'Library for cma is not installed'

    sigma0 = 0.2  # "step size", should be around 1/4 of search domain
    popsize = 5
    lb = [v[0] for v in par_b]
    ub = [v[1] for v in par_b]
    res = cma.fmin(
        objfunc,
        par_0,
        sigma0,
        restarts=0,
        options={
            'bounds': [lb, ub],
            #'maxfevals': args.maxiter,
            'maxiter': maxiter,
            'popsize': popsize,
        })
    cand = [scale[i] * v + offset[i] for i, v in enumerate(res[0])]

    return res[1], cand
Exemple #12
0
    def main(self, data, path, dict_config_LSTM):

        # try:
        self.array_df = []
        self.epoch = 0
        self._configure_dir(path)
        self.dict_c['path_save'] = path
        self.dict_config_LSTM = dict_config_LSTM
        pickle_save(self.dict_c['path_save'] + '/dict.p', dict_config_LSTM)

        self.df_f_train = data['df_f_train']
        self.df_f_val = data['df_f_val']
        self.df_f_test = data['df_f_test']

        self.df_t_train = data['df_t_train']
        self.df_t_val = data['df_t_val']
        self.df_t_test = data['df_t_test']

        self.dimension = self.df_f_train['error_e'].iloc[0].shape[1]

        self.bias = np.median(np.concatenate(data['df_f_train']['error_e'],
                                             axis=0),
                              axis=0)

        es = cma.fmin(
            self._opt_function, self.dimension * [1], self.sigma, {
                'bounds': self.bounds,
                'maxfevals': self.evals,
                'verb_disp': self.verbose,
                'verb_log': self.verbose_log
            })
Exemple #13
0
    def main_CMA_ES(self):

        self.data, self.dict_c[
            'path_save'], self.dict_config_LSTM = data_manager(
                dict_c).configure_data(dict_c)

        self._configure_dir(self.dict_c['path_save'])

        self.df_f_train = self.data['df_f_train']
        self.df_f_val = self.data['df_f_val']
        self.df_f_test = self.data['df_f_test']

        self.df_t_train = self.data['df_t_train']
        self.df_t_val = self.data['df_t_val']
        self.df_t_test = self.data['df_t_test']

        dimension = self.df_f_train['error_e'].iloc[0].shape[1]

        es = cma.fmin(
            self._opt_function, dimension * [1], self.sigma, {
                'bounds': self.bounds,
                'maxfevals': self.evals,
                'verb_disp': self.verbose,
                'verb_log': self.verbose_log
            })
Exemple #14
0
def launchCMAESForSpecificTargetSize(sizeOfTarget, thetaFile, save):
    '''
    Run cmaes for a specific target size

    Input:	-sizeOfTarget, size of the target, float
    '''
    print("Starting the CMAES Optimization for target " + str(sizeOfTarget) +
          " !")
    rs = ReadSetupFile()
    foldername = rs.CMAESpath + str(sizeOfTarget) + "/"
    thetaname = foldername + thetaFile
    if save:
        copyRBFNtoCMAES(rs, thetaFile, sizeOfTarget)

    #Initializes all the class used to generate trajectory
    exp = Experiments(rs, sizeOfTarget, False, foldername, thetaname,
                      rs.popsizeCmaes, rs.period)
    theta = exp.tm.controller.theta
    thetaCMA = theta.flatten()

    #run the optimization (cmaes)
    resCma = cma.fmin(exp.runTrajectoriesCMAES,
                      thetaCMA,
                      rs.sigmaCmaes,
                      options={
                          'maxiter': rs.maxIterCmaes,
                          'popsize': rs.popsizeCmaes,
                          'CMA_diagonal': True,
                          'verb_log': 50,
                          'verb_disp': 1,
                          'termination_callback': term()
                      })
    print("End of optimization for target " + str(sizeOfTarget) + " !")
Exemple #15
0
def launchCMAESForSpecificTargetSize(sizeOfTarget, thetaFile, save):
    """
    Run cmaes for a specific target size

    Input:	-sizeOfTarget, size of the target, float
    """
    print("Starting the CMAES Optimization for target " + str(sizeOfTarget) + " !")
    rs = ReadSetupFile()
    foldername = rs.CMAESpath + str(sizeOfTarget) + "/"
    thetaname = foldername + thetaFile
    if save:
        copyRBFNtoCMAES(rs, thetaFile, sizeOfTarget)

    # Initializes all the class used to generate trajectory
    exp = Experiments(rs, sizeOfTarget, False, foldername, thetaname, rs.popsizeCmaes, rs.period)
    theta = exp.tm.controller.theta
    thetaCMA = theta.flatten()

    # run the optimization (cmaes)
    resCma = cma.fmin(
        exp.runTrajectoriesCMAES,
        thetaCMA,
        rs.sigmaCmaes,
        options={
            "maxiter": rs.maxIterCmaes,
            "popsize": rs.popsizeCmaes,
            "CMA_diagonal": True,
            "verb_log": 50,
            "verb_disp": 1,
            "termination_callback": term(),
        },
    )
    print("End of optimization for target " + str(sizeOfTarget) + " !")
    def _fit(self, cma_maxiter, X, XXtr_state, Xytr_state, hyperparameters_state):
        warnings.filterwarnings('error')
        assert XXtr_state.shape == (self.basis_dim_state, self.basis_dim_state)
        assert Xytr_state.shape == (self.basis_dim_state, self.state_dim)

        X = X.copy()
        XXtr_state = XXtr_state.copy()
        Xytr_state = Xytr_state.copy()
        hyperparameters_state = hyperparameters_state.copy()

        Llower_state = spla.cholesky((hyperparameters_state[-2]/hyperparameters_state[-1])**2*np.eye(self.basis_dim_state) + XXtr_state, lower=True)

        import cma
        options = {'maxiter': cma_maxiter, 'verb_disp': 1, 'verb_log': 0}
        res = cma.fmin(self._loss, self.thetas, 2., args=(X.copy(),
                                                          Llower_state.copy(),
                                                          XXtr_state.copy(),
                                                          Xytr_state.copy(),
                                                          hyperparameters_state.copy()), options=options)
        self.thetas = res[0].copy()
        if self.dump_model:
            print('Unique identifier:', self.uid)
            directory = './models/'
            if not os.path.exists(directory):
                os.makedirs(directory)
            with open(directory+self.uid+'_epoch:'+str(self.epoch)+'.p', 'wb') as fp:
                pickle.dump(self.thetas, fp)
            self.epoch += 1
Exemple #17
0
    def maximize(self):
        """
        Maximizes the given acquisition function.

        Returns
        -------
        np.ndarray(N,D)
            Point with the highest acquisition value.
        """

        verbose_level = -9
        if self.verbose:
            verbose_level = 0

        start_point = init_random_uniform(self.lower, self.upper, 1, self.rng)

        def obj_func(x):
            a = self.objective_func(x[None, :])
            return a[0]

        res = cma.fmin(obj_func, x0=start_point[0], sigma0=0.6,
                       restarts=self.restarts,
                       options={"bounds": [self.lower, self.upper],
                                "verbose": verbose_level,
                                "verb_log": sys.maxsize,
                                "maxfevals": self.n_func_evals})
        if res[0] is None:
            logging.error("CMA-ES did not find anything. \
                Return random configuration instead.")
            return start_point

        return res[0]
    def _fit(self, cma_maxiter, X, XXtr, Xytr, hyperparameters, sess):
        warnings.filterwarnings('error')
        assert len(XXtr) == self.state_dim + self.learn_reward
        assert len(Xytr) == self.state_dim + self.learn_reward
        assert len(hyperparameters) == self.state_dim + self.learn_reward

        X = np.copy(X)
        XXtr = [np.copy(ele) for ele in XXtr]
        Xytr = [np.copy(ele) for ele in Xytr]
        hyperparameters = [np.copy(ele) for ele in hyperparameters]

        Llowers = [
            scipy.linalg.cholesky((hp[-2] / hp[-1])**2 * np.eye(len(XX)) + XX,
                                  lower=True)
            for XX, hp in zip(XXtr, hyperparameters)
        ]

        import cma
        options = {'maxiter': cma_maxiter, 'verb_disp': 1, 'verb_log': 0}
        res = cma.fmin(self._loss,
                       self.thetas,
                       2.,
                       args=(np.copy(X), [np.copy(ele) for ele in Llowers
                                          ], [np.copy(ele) for ele in Xytr],
                             [np.copy(ele) for ele in hyperparameters], sess),
                       options=options)
        self.thetas = np.copy(res[0])
def weighted_ensemble(predictions,
                      true_labels,
                      task_type,
                      metric,
                      weights,
                      tolfun=1e-3):
    seed = np.random.randint(0, 1000)
    logging.debug("CMA-ES uses seed: " + str(seed))
    n_models = predictions.shape[0]
    if n_models > 1:
        res = cma.fmin(
            weighted_ensemble_error,
            weights,
            sigma0=0.25,
            restarts=1,
            args=(predictions, true_labels, metric, task_type),
            options={
                'bounds': [0, 1],
                'seed': seed,
                'verb_log': 0,  # No output files
                'tolfun': tolfun  # Default was 1e-11
            })
        weights = np.array(res[0])
    else:
        # Python CMA-ES does not work in a 1-D space
        weights = np.ones([1])
    weights = weights / weights.sum()

    return weights
Exemple #20
0
                def __call__(self, fun, x0, callback, minimizer_kwargs):
                    class InnerCMACallback:
                        def __init__(self, realcb):
                            self.realcb = realcb

                        def __call__(self, cma):
                            self.realcb(cma.best.x)

                    cb = minimizer_kwargs['callback']
                    innercb = InnerCMACallback(cb) if cb is not None else None

                    try:
                        return cma.fmin(fun,
                                        x0,
                                        10. / 4.,
                                        options={
                                            'ftarget': self.ftarget,
                                            'maxfevals': self.maxfevals,
                                            'termination_callback': innercb,
                                            'verb_disp': 0,
                                            'verb_filenameprefix':
                                            '/tmp/outcmaes'
                                        })
                    except cma._Error, e:
                        print "CMA error: " + str(e)
                        return None
Exemple #21
0
def fit_cma(log_price):
    init_limits = [
        (4, 6),
        (0, 5),
        (1, 3),
    ]
    non_lin_vals = [random.uniform(a[0], a[1]) for a in init_limits]
    tc_0 = non_lin_vals[0]
    beta_0 = non_lin_vals[1]
    omega_0 = non_lin_vals[2]
    seed = np.array([tc_0, beta_0, omega_0])

    opti_sol = cma.fmin(lambda x: func(x, log_price),
                        sigma0=2,
                        x0=seed,
                        options={'popsize': 100})
    tc, beta, omega = wrapper_scaling(opti_sol[0], log_price)
    fit_res = fit_ABC(beta, omega, tc, log_price)
    normed_residual = func([tc, beta, omega], log_price) / len(log_price)
    sol = {
        'tc': tc,
        'beta': beta,
        'omega': omega,
        'A': fit_res['A'],
        'B': fit_res['B'],
        'C1': fit_res['C1'],
        'C2': fit_res['C2'],
        'Res': normed_residual
    }
    return sol
Exemple #22
0
        def cma_wrapper(fun, x0, callback, minimizer_kwargs):
            class InnerCMACallback:
                def __init__(self, realcb):
                    self.realcb = realcb

                def __call__(self, cma):
                    self.realcb(cma.best.x)

            cb = minimizer_kwargs.pop('callback')
            minimizer_kwargs['options'][
                'termination_callback'] = InnerCMACallback(
                    cb) if cb is not None else None

            if 'restarts' in minimizer_kwargs:
                # We ignore passed x0 in case a restart strategy is employed
                # as it is important to start at a different point in each restart
                # (esp. in the smallpop stages of BIPOP, obviously).
                x0 = '10. * np.random.rand(%d) - 5' % minimizer_kwargs.pop(
                    'dim')

            try:
                return cma.fmin(fun, x0, 10. / 4., **minimizer_kwargs)
            except cma._Error, e:
                print "CMA error: " + str(e)
                return None
    def goalBabble(self, x):
        print("Doing goal babbling now\n")
        p = self.net.params
        p[:] = x[3]

        i = 0
        self.sMemory = np.array([1] * (INPUTSIZE + PREDICTSIZE))
        self.mMemory = np.array([0] * OUTPUTSIZE)
        self.rest()
        sensedAngles = self.get_sensor_values()
        pv = sensedAngles[x[2]]
        #Choose a goal sensor value in the range of the predicted angle
        #self.randomGoal = self.rand.uniform(self.Body[x[1]][0],self.Body[x[1]][1])
        self.randomGoal = self.rand.uniform(-0.5, 0.5)
        print(self.randomGoal)
        #CMA-ES can be used to discover joint angles that get
        #closer to the goal state!!!!!!!!
        self.x1 = x[1]
        self.x2 = x[2]
        res = cma.fmin(self.calcGoalScore,
                       0.1 * (np.random.random_sample(2, ) - 0.5),
                       0.5,
                       maxfevals=500,
                       verb_disp=1,
                       bounds=[self.Body[x[1]][0], self.Body[x[1]][1]])
        cma.plot()
        cma.show()
Exemple #24
0
def pp_cmaes_fitting(plan, real_perf_values):
    # x0 = np.array([4.0, 2.0, 15])  # initial guess of delays
    x0 = np.array([0.5, 4.0, 2.0, 15])  # initial guess including perfpot
    load_scale_factor = calc_pp_load_scale_factor(plan)
    perf_scale_factor = calc_pp_perf_scale_factor(real_perf_values)
    scaled_plan = list(map(lambda l: load_scale_factor * l, plan))
    scaled_perfs = list(map(lambda p: perf_scale_factor * p, real_perf_values))
    args = (scaled_plan,
            scaled_perfs,
            calc_rmse,
            unpack_pp_parms_list,
            pp_performance_over_time2)
    opts = cma.CMAOptions()
    # only optimize delays
    '''bounds = [[0.001, 0.001, 0.001],
              [30.0, 30.0, 30.0]]'''
    # optimize delays and init_p aka perfpot
    bounds = [[0.0, 0.001, 0.001, 0.001],
              [1.0, 30.0, 30.0, 30.0]]
    opts.set('bounds', bounds)
    # opts.set('verb_disp', False)
    # opts.set('verbose', -9)
    opts.set('maxiter', 800)
    res = cma.fmin(objective_f, x0, 0.5, args=args, options=opts)
    print('res[0] = {}'.format(res[0]))
    print('res[1] = {}'.format(res[1]))
    return res, load_scale_factor, perf_scale_factor
    def goalBabble(self, x):
        print("Doing goal babbling now\n")
        p = self.net.params
        p[:] = x[3]

        i = 0
        self.sMemory = np.array([1] * (INPUTSIZE + PREDICTSIZE))
        self.mMemory = np.array([0] * OUTPUTSIZE)
        self.rest()
        sensedAngles = self.get_sensor_values()
        pv = sensedAngles[x[2]]
        #Choose a goal sensor value in the range of the predicted angle
        #self.randomGoal = self.rand.uniform(self.Body[x[1]][0],self.Body[x[1]][1])
        for i in range(2):
            self.randomGoal = self.rand.uniform(-0.5, 0.5)
            print(self.randomGoal)
            #CMA-ES can be used to discover joint angles that get
            #closer to the goal state!!!!!!!!
            self.x1 = x[1]
            self.x2 = x[2]
            print("Range motor = " + str(self.Body[x[1]][0]) + " " +
                  str(self.Body[x[1]][1]))
            initG = [
                self.rand.uniform(self.Body[x[1]][0], self.Body[x[1]][1]),
                self.rand.uniform(self.Body[x[1]][0], self.Body[x[1]][1])
            ]
            print("Initial motor  = " + str(initG))
            #             res = cma.fmin(self.calcGoalScore, initG, (self.Body[x[1]][1]- self.Body[x[1]][0])/10.0,maxfevals=200, verb_disp=1, bounds=[self.Body[x[1]][0]-1, self.Body[x[1]][1]+1] )
            #              res = cma.fmin(self.calcGoalScore, initG, 0.1,maxfevals=200, verb_disp=1, bounds=[-10,10])
            res = cma.fmin(self.calcGoalScore,
                           initG,
                           0.1,
                           maxfevals=50,
                           verb_disp=1)
        def optimize_local(output=output):
            #
            if self.allocationMethod == 'variablePrecision':
                # importing CMAES libarary
                import cma

                # setting up parameters and running the optimization
                x0 = 50 + 15 * np.random.randn(12 * (self.depth - 1))
                res = cma.fmin(obj_func, x0, 30, options={'bounds':[1,100],\
                             'tolfun':1, 'maxfevals': int(1e4)})
                sigma_opt = res[0]

            elif self.allocationMethod == 'equalPrecision':
                # importing Bayesian optimization libraries
                import GPy, GPyOpt
                from GPyOpt.methods import BayesianOptimization

                # setting up parameters and running the optimization
                kernel = GPy.kern.Matern52(input_dim=1)
                domain = [{
                    'name': 'var_1',
                    'type': 'continuous',
                    'domain': (0, 100)
                }]
                optimizer = BayesianOptimization(obj_func,
                                                 domain=domain,
                                                 kernel=kernel)
                optimizer.run_optimization(max_iter=50)
                sigma_opt = optimizer.X[optimizer.Y.argmin()]

            # appending the result (scalar sigma) to output queue
            output.put(sigma_opt)
    def reproduction_for_sin(self,
                             o=None,
                             shape=True,
                             avoidance=False,
                             verbose=0):

        #if verbose <= 1:
        #print("Trajectory with x0 = %s, g = %s, self.step=%.2f, step=%.3f" % (self.x0, self.goal, self.step, self.step))

        #puts evething that was from X to x; from array to matrix
        x = copy.copy(self.y_asis_of_sin)
        temp_matrix_of_x1 = copy.copy(x)
        temp_matrix_of_x2 = copy.copy(x)

        original_matrix_1 = [copy.copy(temp_matrix_of_x1)]
        original_matrix_2 = [copy.copy(temp_matrix_of_x2)]

        #reproducing the x-asis
        t = 0.1 * self.step
        ti = 0

        S = self.converges_for_sin()
        while t < self.step:
            t += self.step
            ti += 1
            self.s = S[ti]

            x += self.step * temp_matrix_of_x1
            temp_matrix_of_x1 += self.step * temp_matrix_of_x2

            sd = self.spring_damping_for_sin()
            # the weighted shape base on the movement
            f = self.shape_path_for_sin() if shape else 0.
            C = self.step.obstacle_for_sin(
                o, x, temp_matrix_of_x1) if avoidance else 0.0

            #print(temp_matrix_of_x2)

            #Everything that you implemented in the  matrix that was temperary will initialize will be put into the none temperary matrix
            if ti % self.step > 0:
                temp_matrix_of_x1 = np.append(copy.copy(x),
                                              copy.copy(self.y_asis_of_sin))
                original_matrix_1 = np.append(copy.copy(self.y_asis_of_sin),
                                              copy.copy(temp_matrix_of_x1))
                original_matrix_2 = np.append(copy.copy(self.y_asis_of_sin),
                                              copy.copy(temp_matrix_of_x2))

            self.BlackBox = cma.fmin(cma.ff.linear, self.y_asis_of_sin, 1)

            print(self.BlackBox[0])
            #for i  in range(len(self.BlackBox[0])):

            #original_matrix_1[i] *= self.BlackBox[0][i]

            temp = np.array(self.y_asis_of_sin)

            #return the matrix as array when returning
            original_matrix_1 = temp * (self.BlackBox[2] / 150)
            return np.array(
                self.y_asis_of_sin), np.array(x), np.array(original_matrix_1)
     def goalBabble(self, x):
         print("Doing goal babbling now\n")
         p = self.net.params
         p[:] = x[3]
         
         i = 0
         self.sMemory = np.array([1]*(INPUTSIZE + PREDICTSIZE))
         self.mMemory = np.array([0]*OUTPUTSIZE)
         self.rest()
         sensedAngles = self.get_sensor_values()
         pv = sensedAngles[x[2]]
         #Choose a goal sensor value in the range of the predicted angle
         #self.randomGoal = self.rand.uniform(self.Body[x[1]][0],self.Body[x[1]][1])
         for i in range(2):
              self.randomGoal = self.rand.uniform(-0.5,0.5)
              print(self.randomGoal)
              #CMA-ES can be used to discover joint angles that get
              #closer to the goal state!!!!!!!!
              self.x1 = x[1]
              self.x2 = x[2]
              print("Range motor = " + str(self.Body[x[1]][0]) +  " " + str(self.Body[x[1]][1]))
              initG = [self.rand.uniform(self.Body[x[1]][0],self.Body[x[1]][1]),self.rand.uniform(self.Body[x[1]][0],self.Body[x[1]][1])]
              print("Initial motor  = " + str(initG))
 #             res = cma.fmin(self.calcGoalScore, initG, (self.Body[x[1]][1]- self.Body[x[1]][0])/10.0,maxfevals=200, verb_disp=1, bounds=[self.Body[x[1]][0]-1, self.Body[x[1]][1]+1] )
#              res = cma.fmin(self.calcGoalScore, initG, 0.1,maxfevals=200, verb_disp=1, bounds=[-10,10])
              res = cma.fmin(self.calcGoalScore, initG, 0.1,maxfevals=50, verb_disp=1)
Exemple #29
0
 def learn(self, _):
     ''' Learn until convergence. '''
     returns = []
     function = lambda parameters: self.objective_function(returns, parameters)
     res = cma.fmin(function, self.get_parameters(), self.sigma)
     self.set_parameters(res[5])
     return returns
        def improve(nbevals):
            stimParName = optSet.stimParName
            stimMax = optSet.stimMax
            synParName = optSet.synParName
            synMax = optSet.synMax
            res = fmin(f,
                       optSet.x0,
                       optSet.cmaes_sigma,
                       options={
                           'bounds': [optSet.lower, optSet.upper],
                           'verb_log': 3,
                           'verb_disp': True,
                           'maxfevals': nbevals,
                           'seed': 0
                       })
            x = res[0]

            # Save the best asim file in simFiles directory
            simSet = SimulationSet.SimulationSet()
            for st in range(len(stimParName)):
                simSet.set_by_range({stimParName[st]: [x[st] * stimMax[st]]})
            for sy in range(len(synParName)):
                simSet.set_by_range(
                    {synParName[sy]: [x[st + 1 + sy] * synMax[sy]]})
            print simSet.samplePts
            projMan.make_asims(simSet)
            # Copy sim file from "SimFiles" to "CMAeBestSimFiles" directory
            destdir = folders.animatlab_rootFolder + "CMAeBestSimFiles/"
            sourcedir = folders.animatlab_simFiles_dir
            simFileName = findChartName(folders.animatlab_commonFiles_dir)[0]
            filesource = simFileName + "-1.asim"
            filedest = simFileName + ".asim"
            comment = ""
            copyRenameFile(sourcedir, filesource, destdir, filedest, comment)
            return [res, simSet]
Exemple #31
0
    def optimize(self, x0=None, f=None, df=None, f_df=None):
        """
        :param x0: initial point for a local optimizer.
        :param f: function to optimize.
        :param df: gradient of the function to optimize.
        :param f_df: returns both the function to optimize and its gradient.
        """
        try:
            import cma
            import numpy as np

            def CMA_f_wrapper(f):
                def g(x):
                    return f(np.array([x]))[0][0]

                return g

            lB = np.asarray(self.space.get_bounds())[:, 0]
            uB = np.asarray(self.space.get_bounds())[:, 1]
            x = cma.fmin(CMA_f_wrapper(f), (uB + lB) * 0.5,
                         0.6,
                         options={
                             "bounds": [lB, uB],
                             "verbose": -1
                         })[0]
            return np.atleast_2d(x), f(np.atleast_2d(x))
        except:
            print("CMA does not work in problems of dimension 1.")
Exemple #32
0
    def __init__(self, space, maxiter=1000):
        super(Opt_CMA, self).__init__(space)
        self.maxiter = maxiter
        assert self.space.has_types['continuous']
        try:
            import cma
            import numpy as np

            def CMA_f_wrapper(f):
                def g(x):
                    return f(np.array([x]))[0][0]

                return g

            lB = np.asarray(self.space.get_bounds())[:, 0]
            uB = np.asarray(self.space.get_bounds())[:, 1]
            x = cma.fmin(CMA_f_wrapper(f), (uB + lB) * 0.5,
                         0.6,
                         options={
                             "bounds": [lB, uB],
                             "verbose": -1
                         })[0]
            return np.atleast_2d(x), f(np.atleast_2d(x))
        except:
            print(
                "Cannot find cma library, please install it to use this option."
            )
Exemple #33
0
    def optimize(self, x0, f=None, df=None, f_df=None):
        """
        :param x0: initial point for a local optimizer.
        :param f: function to optimize.
        :param df: gradient of the function to optimize.
        :param f_df: returns both the function to optimize and its gradient.
        """
        try:
            import cma

            def CMA_f_wrapper(f):
                def g(x):
                    return f(np.array([x]))[0][0]

                return g

            lB = np.asarray(self.bounds)[:, 0]
            uB = np.asarray(self.bounds)[:, 1]
            x = cma.fmin(CMA_f_wrapper(f),
                         x0,
                         0.6,
                         options={
                             "bounds": [lB, uB],
                             "verbose": -1
                         })[0]
            return np.atleast_2d(x), f(np.atleast_2d(x))
        except ImportError:
            print(
                "Cannot find cma library, please install it to use this option."
            )
        except:
            print("CMA does not work in problems of dimension 1.")
Exemple #34
0
def launchCMAESMissing(rs, save, gamma, point):
    '''
    Run cmaes for a specific target size

    Input:    -target_size, size of the target, float
            -setuFile, file of setup, string
            -save: do we use a previous Best.theta file? True = Yes, False = use current controller, None = random controller
    '''
    rs.gamma = gamma / 10.0
    pos = point[0]
    x = point[1][0]
    y = point[1][1]
    target_size = point[2]
    print("Starting the CMAES Optimization for gamma = " + str(gamma) +
          ",target " + str(target_size) + " for point " + str(pos) + " !")
    foldername = rs.OPTIpathMissing + "gamma" + str(gamma) + "/" + str(
        target_size) + "/" + str(pos) + "/"

    thetaname = foldername + "Best"
    if save:
        checkIfFolderExists(foldername)
        copyfile(
            rs.OPTIpathMissing + "gamma" + str(gamma) + "/" +
            str(target_size) + "/" + "Best.theta", foldername + "Best.theta")
    elif save == None:
        thetaname = None

    #Initializes all the classes used to generate trajectory
    exp = Experiments(rs, target_size, False, foldername, thetaname,
                      rs.popsizeCmaes, rs.period)
    theta = exp.tm.controller.getTheta()
    thetaCMA = theta.flatten()

    #run the optimization (cmaes)
    cma.fmin(partial(exp.runTrajectoriesCMAESOnePointMulti, x, y),
             thetaCMA,
             rs.sigmaCmaes,
             options={
                 'maxiter': rs.maxIterCmaes,
                 'popsize': rs.popsizeCmaes,
                 'CMA_diagonal': True,
                 'verb_log': 0,
                 'verb_disp': 0,
                 'termination_callback': term()
             })
    print("End of optimization for gamma = " + str(gamma) + ",target " +
          str(target_size) + " for point " + str(pos) + " !")
def posterior_mean_optimization(model, lower, upper, n_restarts=10, method="scipy", with_gradients=False):
    """
    Estimates the incumbent by minimize the posterior
    mean of the objective function.

    Parameters
    ----------
    model : Model object
        Posterior belief of the objective function.
    lower : (D) numpy array
        Specified the lower bound of the input space. Each entry
        corresponds to one dimension.
    upper : (D) numpy array
        Specified the upper bound of the input space. Each entry
        corresponds to one dimension.
    n_restarts: int
        Number of independent restarts of the optimization procedure from random starting points
    method : {'scipy', 'cma'}
        Specifies which optimization method is used to minimize
        the posterior mean.
    with_gradients : bool
        Specifies if gradient information are used. Only valid
        if method == 'scipy'.

    Returns
    -------
    np.ndarray(D,)
        best point that was found
    """

    def f(x):
        return model.predict(x[np.newaxis, :])[0][0]

    def df(x):
        dmu = model.predictive_gradients(x[np.newaxis, :])[0]
        return dmu

    startpoints = init_random_uniform(lower, upper, n_restarts)

    x_opt = np.zeros([len(startpoints), lower.shape[0]])
    fval = np.zeros([len(startpoints)])
    for i, startpoint in enumerate(startpoints):
        if method == "scipy":
            if with_gradients:
                res = optimize.fmin_l_bfgs_b(f, startpoint, df, bounds=list(zip(lower, upper)))
                x_opt[i] = res[0]
                fval[i] = res[1]
            else:
                res = optimize.minimize(f, startpoint, bounds=list(zip(lower, upper)), method="L-BFGS-B")
                x_opt[i] = res["x"]
                fval[i] = res["fun"]
        elif method == 'cma':
            res = cma.fmin(f, startpoint, 0.6, options={"bounds": [lower, upper]})
            x_opt[i] = res[0]
            fval[i] = res[1]

    # Return the point with the lowest function value
    best = np.argmin(fval)
    return x_opt[best]
Exemple #36
0
    def run_cma(self):

        popsize = self.population
        maxiter = self.iterations
        all_parameters = [
            'STRETCH', 'SHEAR', 'BEND', 'FRICTION', 'SELFCOLDIST',
            'SELFCOLFRICTION', 'STIFFPOWER', 'ITERATION', 'WIND',
            'ARM_HEIGHT_PERT_GOOD', 'ARM_HEIGHT_PERT_MISS',
            'ARM_HEIGHT_PERT_CAUG', 'ARM_HORIZONTAL_PERT_GOOD',
            'ARM_HORIZONTAL_PERT_MISS', 'ARM_HORIZONTAL_PERT_CAUG',
            'FIST_RADIUS', 'WRIST_RADIUS', 'ELBOW_RADIUS', 'SHOULDER_RADIUS',
            'FOREARM_LENGTH'
        ]
        use_parameters = np.array(
            [1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
        all_parameters_min = np.array([
            0.0, 0.0, 0.0, 0.3, 0.0, 0.0, 5.0, 0.0, 0.0, -0.04, -0.04, -0.06,
            -0.03, -0.03, -0.03, 0.04, 0.03, 0.04, 0.065, 0.28
        ])
        all_parameters_max = np.array([
            1.0, 1.0, 1.0, 0.8, 0.015, 1.0, 10.0, 20.0, 1.0, 0.04, 0.04, 0.0,
            0.03, 0.03, 0.03, 0.06, 0.043, 0.06, 0.085, 0.35
        ])
        all_parameters_initialization = (all_parameters_max +
                                         all_parameters_min) / 2.
        all_parameters_scaling = (all_parameters_max - all_parameters_min) / 4.

        parameters_min = []
        parameters_max = []
        parameters_init = []
        parameters_scaling = []
        self.parameters = []
        self.iteration_count = 0
        for i in xrange(len(use_parameters)):
            if use_parameters[i]:
                parameters_min.append(all_parameters_min[i])
                parameters_max.append(all_parameters_max[i])
                parameters_init.append(all_parameters_initialization[i])
                parameters_scaling.append(all_parameters_scaling[i])
                self.parameters.append(all_parameters[i])
                # print 'Using parameter: ', all_parameters[i]

        opts = {
            'seed': 1234,
            'ftarget': 0.,
            'popsize': popsize,
            'maxiter': maxiter,
            'maxfevals': 1e8,
            'CMA_cmean': 0.5,
            'scaling_of_variables': parameters_scaling,
            'bounds': [parameters_min, parameters_max]
        }
        self.iteration_start_time = time.time()
        optimization_results = cma.fmin(self.objective_function,
                                        parameters_init,
                                        1.,
                                        options=opts)
        print 'Final Optimization Results: ', optimization_results
        print 'Final total time taken: ', time.time() - self.node_start_time
Exemple #37
0
 def learn(self, _):
     ''' Learn until convergence. '''
     returns = []
     function = lambda parameters: self.objective_function(
         returns, parameters)
     res = cma.fmin(function, self.get_parameters(), self.sigma)
     self.set_parameters(res[5])
     return returns
Exemple #38
0
def Testarea(X,Y,func,nbParam,ff):
    #T=symbols('T')
    #print "a intrat in cma"
    funcu=func.replace("^","**")
    funca=funcu.replace("P","prez")
    rez=[0]
    if nbParam >1:
        if ff==1 :
            rez=cma.fmin(FitnessFunctionsForCma.ME, nbParam*[0], 0.5,args=(X,Y,funcu))
        else :
            rez=cma.fmin(FitnessFunctionsForCma.ME, nbParam*[0], 0.5,args=(X,Y,funcu))
        prez=rez[0].tolist()
        rezult=prez
    #print "a iesit din cma"
        return (prez,rez[1],funca)
    else :
        return (rez,1002,funca)
Exemple #39
0
    def _optimize(self, fun_negative_acquisition, str_initial_method,
                  int_samples):
        list_next_point = []
        if self.str_optimizer_method_bo == 'L-BFGS-B':
            list_bounds = self._get_bounds()
            arr_initials = self.get_initial(
                str_initial_method,
                fun_objective=fun_negative_acquisition,
                int_samples=int_samples)
            for arr_initial in arr_initials:
                next_point = minimize(fun_negative_acquisition,
                                      x0=arr_initial,
                                      bounds=list_bounds,
                                      method=self.str_optimizer_method_bo,
                                      options={'disp': False})
                next_point_x = next_point.x
                list_next_point.append(next_point_x)
                if self.debug:
                    print(
                        '[DEBUG] _optimize in bo.py: optimized point for acq',
                        next_point_x)
        elif self.str_optimizer_method_bo == 'DIRECT':  # pragma: no cover
            list_bounds = self._get_bounds()
            next_point = directminimize(
                fun_negative_acquisition,
                bounds=list_bounds,
            )
            next_point_x = next_point.x
            list_next_point.append(next_point_x)
        elif self.str_optimizer_method_bo == 'CMA-ES':
            list_bounds = self._get_bounds()
            list_bounds = np.array(list_bounds)

            def fun_wrapper(f):
                def g(bx):
                    return f(bx)[0]

                return g

            arr_initials = self.get_initial(
                str_initial_method,
                fun_objective=fun_negative_acquisition,
                int_samples=1)
            next_point_x = cma.fmin(fun_wrapper(fun_negative_acquisition),
                                    arr_initials[0],
                                    0.5,
                                    options={
                                        'bounds':
                                        [list_bounds[:, 0], list_bounds[:, 1]],
                                        'verbose':
                                        -1
                                    })[0]
            list_next_point.append(next_point_x)

        next_points = np.array(list_next_point)
        next_point = get_best_acquisition(next_points,
                                          fun_negative_acquisition)
        return next_point.flatten(), next_points
 def acq_max_(self,ac, gp, y_max, bounds, random_state):
     x_seeds = random_state.uniform(bounds[:, 0], bounds[:, 1],
                                    size=(bounds.shape[0]))
     options = {'ftarget': 1e-3, 'bounds': bounds.T.tolist(), 'maxiter': 1000,
                'verb_log': 0,'verb_time':False,'verbose':-9}
     res = cma.fmin(lambda x: 1 - ac(x.reshape(1, -1), gp=gp, y_max=y_max), x_seeds, 0.25, options=options,
                    restarts=0, incpopsize=0, restart_from_best=False, bipop=False)
     x_max = res[0]
     return np.clip(x_max, bounds[:, 0], bounds[:, 1])
Exemple #41
0
 def get_drive_median(self):
     if len(self.locations) == 0:
         self._drive_median = None
     elif len(self.locations) == 1:
         self._drive_median = self.locations[0]
     elif len(self.locations) == 2:
         self._drive_median = self.path_midpoint(*self.locations)
     else:
         self._drive_median = cma.fmin(self.solution_fitness, list(self.geom_median), self.sigma0(), args=(True,), options={'popsize':2, 'tolfun':1000, 'tolfunhist': 1000})
Exemple #42
0
    def estimate_incumbent(self, startpoints):
        """
        Starts form each startpoint an optimization run in the configuration
        subspace and returns the best found point of all runs as incumbent

        Parameters
        ----------
        startpoints : (N, D) numpy array
            Startpoints where the optimization starts from.

        Returns
        -------
        np.ndarray(1, D)
            Incumbent
        np.ndarray(1,1)
            Incumbent value
        """
        x_opt = np.zeros([len(startpoints), self.X_lower.shape[0]])
        fval = np.zeros([len(startpoints)])
        for i, startpoint in enumerate(startpoints):
            if self.method == "scipy":
                if self.with_gradients:
                    res = optimize.fmin_l_bfgs_b(self.f,
                                startpoint[self.is_env == 0],
                                self.df,
                                bounds=list(zip(self.sub_X_lower, self.sub_X_upper)))
                    # The result has the dimensionality of the projected
                    # configuration space so we have to add the
                    # dimensions of the environmental subspace
                    x_ = np.zeros([self.is_env.shape[0]])
                    x_[self.is_env == 1] = self.env_values
                    x_[self.is_env == 0] = res[0]
                    x_opt[i] = x_
                    fval[i] = res[1]
                else:
                    res = optimize.minimize(self.f,
                        startpoint[self.is_env == 0],
                        bounds=list(zip(self.sub_X_lower, self.sub_X_upper)),
                        method="L-BFGS-B",
                        options={"disp": True})
                    x_ = np.zeros([self.is_env.shape[0]])
                    x_[self.is_env == 1] = self.env_values
                    x_[self.is_env == 0] = res["x"]
                    x_opt[i] = x_
                    fval[i] = res["fun"]
            elif self.method == 'cmaes':
                res = cma.fmin(self.f, startpoint[self.is_env == 0], 0.6,
                    options={"bounds": [self.sub_X_lower, self.sub_X_upper]})
                x_ = np.zeros([self.is_env.shape[0]])
                x_[self.is_env == 1] = self.env_values
                x_[self.is_env == 0] = res[0]
                x_opt[i] = x_
                fval[i] = res[1]
        # Return the point with the lowest function value
        best = np.argmin(fval)
        return x_opt[best, np.newaxis, :], np.array([[fval[best]]])
Exemple #43
0
    def maximize(self, verbose=False):
        if not verbose:
            # Turn off stdout, a bit hacky but that's the only way how we get cma to be quiet
            stdout = sys.stdout
            sys.stdout = io.StringIO()

            # Start from random point
            start_point = np.random.uniform(self.X_lower, self.X_upper, self.X_lower.shape[0])

            res = cma.fmin(self._cma_fkt_wrapper(self.objective_func),
                           start_point, 0.6,
                           options={"bounds": [self.X_lower, self.X_upper], "verbose": 0, "verb_log": sys.maxsize})

            # Turn on stdout again
            sys.stdout = stdout
        else:
            res = cma.fmin(self._cma_fkt_wrapper(self.objective_func), (self.X_upper + self.X_lower) * 0.5,
                           0.6, options={"bounds": [self.X_lower, self.X_upper], "verbose": 0, "verb_log": sys.maxsize})
        return np.array([res[0]])
def genetic_search():
    side = 10
    train_input, train_output, train_splits, test_input, test_output, test_splits, network = test_data.instrumentalness(side=side)

    optimiser = esn.GeneticOptimiser(network, train_input, train_output, 0)
    params = np.array(optimiser.initial_params())
    res = cma.fmin(optimiser.evaluate, params, 0.1, maxiter=5)

    filename = '/parent/best_esn_%s_%s.pkl' % (side, res[1])
    with open(filename, 'w') as f:
        cPickle.dump(network.serialize(), f)
Exemple #45
0
    def solve(self):
        [o.notify_init(self, self.model) for o in self.observers]
        res = {'result': 'NG'}

        # 1. The initial step notification
        [o.notify_step(self, self.model) for o in self.observers]

        print('Solving...')
        pts = []

        solved = []
        values = []
        # for task in self.tasks:
        for task in np.linspace(0.0, 1.0, 51):
            # if not (2.0 / 3.0 <= task and task <= 3.0 / 3.0):
            #     continue
            task = 1.0
            print('')
            print('------- CMA-ES : task = %.6f -------- ' % task)

            self.current_task = task
            opts = cma.CMAOptions()
            opts = cma.CMAOptions()
            opts.set('verb_disp', 1)
            opts.set('ftarget', 0.001)
            opts.set('popsize', 16)
            opts.set('maxiter', 100)

            # opt = {'verb_time': 0, 'popsize': 16, 'tolfun': 1e-5}
            x0 = np.random.rand(self.prob.dim) - 0.5
            res = cma.fmin(self.evaluate, x0, 2.0, opts)
            print('------------ task = %.6f ----------- ' % task)
            print('self.eval_counter = %d' % self.num_evals())
            print('the answer: %s' % res[0])
            print('value: %.6f' % res[1])
            pts += [res[0]]
            solved += [task]
            values += [res[1]]
            print('solved tasks: %s' % solved)
            print('solved values: %s' % values)
            print('')

            # [o.notify_step(self, self.model) for o in self.observers]
        self.mean().fit(pts)

        # 2. The final step notification
        [o.notify_step(self, self.model) for o in self.observers]
        for i in range(self.n):
            print("%d (%.4f) : %s" % (i, self.tasks[i], pts[i]))
        print('sum values: %.6f' % np.mean(self.values()))
        print self.model

        [o.notify_solve(self, self.model) for o in self.observers]
        return res
Exemple #46
0
 def learn(self, steps):
     ''' Learn for a given number of steps. '''
     returns = []
     function = lambda parameters: self.objective_function(returns, parameters)
     for step in range(steps):
         agent = FixedSarsaAgent()
         agent.action_weights = self.action_weights
         agent.parameter_weights = self.parameter_weights
         rets = agent.learn(self.qsteps)
         returns.extend(rets)
         res = cma.fmin(function, self.get_parameters(), self.sigma)
         self.set_parameters(res[5])
     return returns
Exemple #47
0
    def optimize(self):
        opt = {'verb_time':0,
               # 'boundary_handling': 'BoundPenalty', 'bounds': [self.lo, self.hi],
               'popsize':64,
               'tolfun' : 0.001}
        print "==== abstract.model.TWOTIP optimize...."

        self.res = cma.fmin(self.evaluate, 0.5 * (self.lo + self.hi), 1.0, opt)
        print "==== result\n ", self.res
        x_opt = self.res[0]
        print 'optimal value = ', self.evaluate(x_opt)
        print 'optimal solution = ', x_opt
        print "==== abstract.model.TWOTIP optimize.... OK"
Exemple #48
0
def cmaes(f, n, ftol, xtol, bounds, x, **kwargs):
    """Find global maxima of an objective function using CMA-ES algorithm.
    
    Args:
        f (scalar function): Objective function
        n (int): Number of parameters
        ftol (double): Tolerance for the change of f function
        xtol (double): Tolerance for the change of paramters
        bound (dict): Absolute value of lower and upper bounds
        x (array_like): Initial parameter value
        
    Returns:
        f_opt (double): Function value at the optimum
        x_opt (array_like): Parameter value at the optimum
        res (str): Termination criterion 
    
    """
    
    #set options of the cma-es optimizer
    opts = cma.CMAOptions()
    opts['popsize'] = n*10
    opts['CMA_active'] = False
    opts['tolfun'] = ftol
    opts['tolx'] = xtol    
    opts['verb_disp'] = 0
    opts['verb_log'] = 0
    opts['verbose'] = 0

    if type(bounds['lb']) == int or type(bounds['lb']) == float:
        opts['bounds'] = [[bounds['lb']]*n, [bounds['ub']]*n]
    elif len(bounds['lb']) == n and len(bounds['ub']) == n:
        opts['bounds'] = [bounds['lb'], bounds['ub']]
    else:
        print('inconsistent definition of parameter bounds')
    
    opts['CMA_stds'] = ones(n)*(bounds['ub'] - bounds['lb'])/4
    cma_sigma = 1;
    
    for s in kwargs.keys():
        opts[s] = kwargs[s]
    
    #start optimization
    negf = lambda x: -f(x)
    res = cma.fmin(negf, x, cma_sigma, opts)

    #collect results
    x_opt = res[0]
    f_opt = -res[1]
    res = res[-3]    
    
    return f_opt, x_opt, res
Exemple #49
0
def launchCMAESForSpecificTargetSizeAndSpecificPoint(target_size, rs, save, point, noise=None):
    '''
    Run cmaes for a specific target size

    Input:    -target_size, size of the target, float
            -setuFile, file of setup, string
            -save: do we use a previous Best.theta file? True = Yes, False = use current controller, None = random controller
            noise: noise on muscle, if None, defalt noise from muscle setup, float
    '''
    pos=point[0]
    x=point[1][0]
    y=point[1][1]
    print("Starting the CMAES Optimization for target " + str(target_size) + " for point "+ str(pos)+" !")
    foldername = rs.OPTIpath + str(target_size)+"/"+str(pos)+"/"
    

    thetaname = foldername + "Best"
    if save:
        checkIfFolderExists(foldername)
        copyfile(rs.OPTIpath + str(target_size)+"/" + "Best.theta",foldername + "Best.theta")
    elif save==None:
        thetaname=None




    #Initializes all the class used to generate trajectory
    exp = Experiments(rs, target_size, False, foldername, thetaname,rs.popsizeCmaes,rs.period)
    if(noise!=None):
        exp.setNoise(noise)
    theta = exp.tm.controller.getTheta()
    thetaCMA = theta.flatten()

    #run the optimization (cmaes)
    cma.fmin(partial(exp.runTrajectoriesCMAESOnePoint, x, y), thetaCMA, rs.sigmaCmaes, options={'maxiter':rs.maxIterCmaes, 'popsize':rs.popsizeCmaes, 'CMA_diagonal':True, 'verb_log':0, 'verb_disp':0,'termination_callback':term()})
    print("End of optimization for target " + str(target_size) +  " for point "+ str(pos)+" !")
Exemple #50
0
    def optimize_with_fullbody_motion(self):
        # x = np.random.rand(self.dim)
        x = np.array([-0.52563663, -1.57, -1.57, 0.91604437, -0.67324443,
                      -0.43524684, -0.2820, 0.67682579, -1.199266, -0.864372,
                      -1.57, 0.79398899])  # 0.476501049002
        return self.evaluate_fullbody(x)

        lo = np.array([-1.57] * self.dim)
        hi = np.array([1.57] * self.dim)
        opt = {'verb_time': 0, 'boundary_handling': 'BoundPenalty',
               'bounds': [lo, hi], 'tolfun': 0.001}
        print "==== abstract.model.TIP optimize...."
        self.res = cma.fmin(self.evaluate_fullbody, 0.5 * (lo + hi), 1.0, opt)

        self.control = self.res[0]
Exemple #51
0
def ff_cmaes_fitting(plan, real_perf_values):
    x0 = np.array([real_perf_values[0], 1.0, 30.0, 1.0, 15.0])  # initial guess
    args = (plan,
            real_perf_values,
            calc_rmse,
            unpack_ff_parms_list,
            ff_performance_over_time2)
    opts = cma.CMAOptions()
    bounds = [[0.0, 0.01, 1.0, 0.01, 1.0],
              [real_perf_values[0] * 2, 5.0, 70.0, 5.0, 70.0]]
    opts.set('bounds', bounds)
    opts.set('tolfun', 1e-5)
    # opts.set('verb_disp', False)
    # opts.set('verbose', -9)
    # opts.set('maxiter', 800)
    res = cma.fmin(objective_f, x0, 0.5, args=args, options=opts)
    return res
Exemple #52
0
                def __call__(self, fun, x0, callback, minimizer_kwargs):
                    class InnerCMACallback:
                        def __init__(self, realcb):
                            self.realcb = realcb
                        def __call__(self, cma):
                            self.realcb(cma.best.x)
                    cb = minimizer_kwargs['callback']
                    innercb = InnerCMACallback(cb) if cb is not None else None

                    try:
                        return cma.fmin(fun, x0, 10./4.,
                                        options={'ftarget': self.ftarget, 'maxfevals': self.maxfevals,
                                                 'termination_callback': innercb, 'verb_disp': 0,
                                                 'verb_filenameprefix': '/tmp/outcmaes'})
                    except cma._Error, e:
                        print "CMA error: " + str(e)
                        return None
Exemple #53
0
 def __init__(self, space, maxiter=1000):
     super(Opt_CMA, self).__init__(space)
     self.maxiter = maxiter
     assert self.space.has_types['continuous']
     try:
         import cma 
         import numpy as np
         def CMA_f_wrapper(f):
             def g(x):
                 return f(np.array([x]))[0][0]
             return g
         lB = np.asarray(self.space.get_bounds())[:,0]
         uB = np.asarray(self.space.get_bounds())[:,1]
         x = cma.fmin(CMA_f_wrapper(f), (uB + lB) * 0.5, 0.6, options={"bounds":[lB, uB], "verbose":-1})[0]
         return np.atleast_2d(x), f(np.atleast_2d(x))
     except:
         print("Cannot find cma library, please install it to use this option.")
Exemple #54
0
    def solve(self):
        [o.notify_init(self, self.model) for o in self.observers]
        sample_values = []
        for task in self.tasks:
            pt = self.mean().point(task)
            s = Sample(pt, self.prob)
            v = s.evaluate(task)
            sample_values += [v]
        self.iter_values += [sample_values]
        self.iter_params += [self.mean().params()]
        [o.notify_step(self, self.model) for o in self.observers]
        res = {'result': 'NG'}
        # opt = {'verb_time': 0, 'popsize': 16, 'tolfun': 1.0}
        cma.CMAOptions('tol')

        opts = cma.CMAOptions()
        opts.set('verb_disp', 1)
        # opts.set('tolfun', 0.001)
        # opts.set('tolx', 0.0000001)
        # opts.set('tolx', 1.0)
        opts.set('ftarget', 0.001)
        num_offsprings = 16
        opts.set('popsize', num_offsprings)
        max_iter = int(20000 / self.n / num_offsprings)
        print('maxiter: %d' % max_iter)
        opts.set('maxiter', max_iter)
        for key, value in opts.iteritems():
            print '[', key, ']\n', value

        x0 = np.random.rand(self.mean().paramdim) - 0.5

        # print cma.CMAOptions()
        # exit(0)

        print()
        print('------- CMA-ES --------')
        res = cma.fmin(self.evaluate, x0, 1.0, opts)
        print('-----------------------')
        print()
        # np.set_printoptions(precision=6, suppress=True)
        print('the answer: %s' % res[0])

        [o.notify_solve(self, self.model) for o in self.observers]
        return res
def weighted_ensemble(predictions, true_labels, task_type, metric, weights, tolfun=1e-3):
    seed = np.random.randint(0, 1000)
    logging.debug("CMA-ES uses seed: " + str(seed))
    n_models = predictions.shape[0]
    if n_models > 1:
        res = cma.fmin(weighted_ensemble_error, weights, sigma0=0.25, restarts=1,
                       args=(predictions, true_labels, metric,
                             task_type), options={'bounds': [0, 1],
                                                  'seed': seed,
                                                  'verb_log': 0, # No output files
                                                  'tolfun': tolfun # Default was 1e-11
                                                  })
        weights = np.array(res[0])
    else:
        # Python CMA-ES does not work in a 1-D space
        weights = np.ones([1])
    weights = weights / weights.sum()

    return weights
 def fitCapacitanceAndFrequency(self, CdlStart = 1e-4, Cdl1Start = 0.0,
   Cdl2Start = 0.0, Cdl3Start = 0.0):
     """Obtains values of the capacitance parameters and frequency using a
     CMA-ES algorithm.
     """
     start = [CdlStart, Cdl1Start, Cdl2Start, Cdl3Start, self.freq]
     start = self._rescaleCapFreqParams(start)
     verbose = 1 if "loud-cma" in self.debugParams else -9
     optDict = {'tolfun' : 1e-19, 'tolfunhist' : 1e-19, "verbose" : verbose,
                'verb_log' : np.inf}
     res = cma.fmin(self._capacitanceAndFreqObjectiveFunction, start, 1,
                    optDict)
     res = self._unscaleCapFreqParams(res[0])
     print res
     self.Cdl = res[0]
     self.Cdl1 = res[1]
     self.Cdl2 = res[2]
     self.Cdl3 = res[3]
     self.freq = res[4]
Exemple #57
0
    def estimate_incumbent(self, startpoints):
        """
        Starts form each startpoint an optimization run and returns
        the best found point of all runs as incumbent

        Parameters
        ----------
        startpoints : (N, D) numpy array
            Startpoints where the optimization starts from.

        Returns
        -------
        np.ndarray(1, D)
            Incumbent
        np.ndarray(1,1)
            Incumbent value
        """
        x_opt = np.zeros([len(startpoints), self.X_lower.shape[0]])
        fval = np.zeros([len(startpoints)])
        for i, startpoint in enumerate(startpoints):
            if self.method == "scipy":
                if self.with_gradients:
                    res = optimize.fmin_l_bfgs_b(
                        self.f, startpoint, self.df, bounds=list(zip(self.X_lower, self.X_upper))
                    )
                    x_opt[i] = res[0]
                    fval[i] = res[1]
                else:
                    res = optimize.minimize(
                        self.f, startpoint, bounds=list(zip(self.X_lower, self.X_upper)), method="L-BFGS-B"
                    )
                    x_opt[i] = res["x"]
                    fval[i] = res["fun"]
            elif self.method == "cmaes":
                res = cma.fmin(self.f, startpoint, 0.6, options={"bounds": [self.X_lower, self.X_upper]})
                x_opt[i] = res[0]
                fval[i] = res[1]

        # Return the point with the lowest function value
        best = np.argmin(fval)
        return x_opt[best, np.newaxis, :], fval[best, np.newaxis, np.newaxis]
Exemple #58
0
def main_cma_es(args):
    import cma
    print "args", args
    # options = {'CMA_diagonal':100, 'seed':1234, 'verb_time':0, "maxfevals": 2000}
    # options = {'CMA_diagonal': 0, 'seed':32984, 'verb_time':0, "maxfevals": 2000}
    options = {'CMA_diagonal': 0, 'seed':4534985, 'verb_time':0, "maxfevals": 4000}

    hparams = {
        "numsteps": args.numsteps,
        "measure": ComplexityMeasure(),
        "continuous": True,
    }
    pobjective = partial(objective, hparams=hparams)

    # func = objective # args["func"]
    # arguments: function, initial params, initial var, options
    # res = cma.fmin(cma.fcts.griewank, [0.1] * 10, 0.5, options)
    res = cma.fmin(pobjective, [0.5] * 4, 0.3, options)

    print "result cma_es", res[0], res[1]
    return res[0]
Exemple #59
0
def wrapper_CMA(f,bounds):
    '''
    Wrapper the Covariance Matrix Adaptation Evolutionary strategy (CMA-ES) optimization method. It works generating 
    an stochastic seach based on mutivariate Gaussian samples. Only requieres f and the box constrains to work
    :param f: function to optimize, acquisition.
    :param bounds: tuple determining the limits of the optimizer.

    '''
    try:
        import cma 
        import numpy as np
        def CMA_f_wrapper(f):
            def g(x):
                return f(np.array([x]))[0][0]
            return g
        lB = np.asarray(bounds)[:,0]
        uB = np.asarray(bounds)[:,1]
        x = cma.fmin(CMA_f_wrapper(f), (uB + lB) * 0.5, 0.6, options={"bounds":[lB, uB], "verbose":-1})[0]
        print x
        return reshape(x,len(bounds))
    except:
        print("Cannot find cma library, please install it to use this option.")
Exemple #60
0
    def maximize(self, rng = None):
        """
        Maximizes the given acquisition function.

        Parameters
        ----------


        Returns
        -------
        np.ndarray(N,D)
            Point with highest acquisition value.
        """

        # All stdout and stderr is pointed to devnull during
        # the optimization. (That is the only way to keep cmaes quiet)
        if rng is None:
            rng = np.random.RandomState(42)
        if not self.verbose:
            sys.stdout = os.devnull
            sys.stderr = os.devnull
        res = cma.fmin(self._cma_fkt_wrapper(self.objective_func),
                x0=(self.X_upper + self.X_lower) * 0.5, sigma0=0.6,
                restarts=self.restarts,
                options={"bounds": [self.X_lower, self.X_upper],
                         "verbose": 0,
                         "verb_log": sys.maxsize,
                         "maxfevals": self.n_func_evals})
        if res[0] is None:
            logging.error("CMA-ES did not find anything. \
                Return random configuration instead.")
            return np.array([rng.uniform(self.X_lower,
                                               self.X_upper,
                                               self.X_lower.shape[0])])
        if not self.verbose:
            sys.stdout = sys.__stdout__
            sys.stderr = sys.__stderr__

        return np.array([res[0]])