Exemplo n.º 1
0
    def simple_best_fit_sum_of_squares_model_1(self,
                                               x0=None,
                                               sigma0=0.1,
                                               cma_random_seed=None):
        """Optimisation keeping Hill=1 fixed and varying pIC50 but enforcing its lower bound,
           but CMA-ES operates in minimium 2-d, so have to trick it"""
        opts = cma.CMAOptions()
        if cma_random_seed is not None:
            opts["seed"] = cma_random_seed
        if x0 is None:
            x0 = [2.5, 1.]
        es = cma.CMAEvolutionStrategy(x0, sigma0, opts)
        while not es.stop():
            X = es.ask()
            es.tell(X, [
                sum_of_square_diffs(self.data[:, 0], self.data[:, 1],
                                    scale_params_for_cmaes_model_1(x[0]))
                for x in X
            ])
            #es.disp()
        res = es.result
        self.best_m1_sigma = compute_best_sigma_analytic(
            res[1], self.num_data_pts)
        self.best_m1_pic50 = scale_params_for_cmaes_model_1(res[0][0])

        model = 1
        self.bic_1 = self.compute_bic(model, res[1])
Exemplo n.º 2
0
    def __init__(self,
                 fct,
                 dim,
                 max_fevals=None,
                 x0=None,
                 seed=5,
                 is_restart=True,
                 is_mirror=False):

        super(CMAES, self).__init__(fct, dim, max_fevals=max_fevals, x0=x0)
        self._opts = cma.CMAOptions()
        self._opts.set('tolfun', 1e-11)
        self._opts['tolx'] = 1e-11
        self._opts['verbose'] = -1
        self._opts['verb_disp'] = 0
        self._opts['verb_log'] = 0
        if max_fevals is not None:
            self._opts['maxfevals'] = max_fevals

        # seed is not currently in use
        # self._opts['seed'] = seed
        self._seed = seed
        self._dim_cma = dim
        if dim == 1:
            self._dim_cma = 2
            self._x0 = np.hstack((self._x0, self._x0))
        self._opts['bounds'] = ([0] * self._dim_cma, [1] * self._dim_cma)

        self._is_restart = is_restart
        self._is_mirror = is_mirror

        if self._is_mirror:
            self._opts['CMA_mirrors'] = 1
        else:
            self._opts['CMA_mirrors'] = 0
Exemplo n.º 3
0
    def __init__(self, hyperparam_dict, log_scale_dict):
        self.hyperparam_dict = hyperparam_dict
        self.log_scale_dict = log_scale_dict
        self.nametoi = {}
        self.itoname = {}
        self.init = []
        self.sigma = SIGMA0
        self.bounds = []
        self.gen = 0
        self.best_fitness = 0.0

        for i, name in enumerate(hyperparam_dict):
            self.itoname[i] = name
            self.nametoi[name] = i
            if log_scale_dict[name]:
                values = math.log(hyperparam_dict[name] + EPSILON)
            else:
                values = hyperparam_dict[name]
            self.bounds.append((np.min(values), np.max(values)))
            self.init.append(
                self.__scale_var(
                    np.median(values),
                    np.min(values),
                    np.max(values)))

        self.opts = cma.CMAOptions()
        for param, value in OPTIMIZER_CONFIG.iteritems():
            if param in self.opts and value is not None:
                self.opts[param] = value
        self.es = cma.CMAEvolutionStrategy(np.array(self.init), self.sigma,
                                           self.opts)
Exemplo n.º 4
0
def run_cmaes(expt):
    expt_trace = traces[expt]
    opts = cma.CMAOptions()
    opts['seed'] = expt + 1
    x0 = np.copy(original_gs)
    sigma0 = 0.0001
    es = cma.CMAEvolutionStrategy(x0, sigma0, opts)
    while not es.stop():
        X = es.ask()
        f_vals = [sum_of_square_diffs(x, expt_trace) for x in X]
        es.tell(X, f_vals)
        es.disp()
    res = es.result()
    best_params = res[0]
    ap.LoadStateVariables()
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(times, expt_trace)
    ax.plot(times, ap.SolveForVoltageTraceWithParams(best_params))
    fig.savefig("gary_decker_expt_{}_best_fit.png".format(expt))
    plt.close()
    temp_params_file = "gary_decker_expt_{}_best_fit_params.txt".format(expt)
    np.savetxt(temp_params_file, best_params)
    print "best sos =", res[1]
    return best_params
Exemplo n.º 5
0
    def simple_best_fit_sum_of_squares_model_2(self,
                                               x0=None,
                                               sigma0=0.1,
                                               cma_random_seed=123):
        """Optimisation varying both pIC50 and Hill, but enforcing the lower bounds"""
        opts = cma.CMAOptions()
        #opts["seed"] = cma_random_seed
        if x0 is None:
            x0 = [2.5, 1.]
        es = cma.CMAEvolutionStrategy(x0, sigma0, opts)
        while not es.stop():
            X = es.ask()
            es.tell(X, [
                sum_of_square_diffs(self.data[:, 0], self.data[:, 1],
                                    *scale_params_for_cmaes_model_2(*x))
                for x in X
            ])
            #es.disp()
        res = es.result
        self.best_m2_sigma = compute_best_sigma_analytic(
            res[1], self.num_data_pts)
        self.best_m2_pic50, self.best_m2_hill = scale_params_for_cmaes_model_2(
            *res[0])

        model = 2
        self.bic_2 = self.compute_bic(model, res[1])
Exemplo n.º 6
0
def run_cmaes(seed):
    expt = 0
    expt_trace = traces[expt]
    opts = cma.CMAOptions()
    #opts['seed'] = seed
    npr.seed(seed)
    x0 = lower_bounds + (upper_bounds - lower_bounds) * npr.rand(num_params)
    print "seed:", seed
    print "x0:", x0
    sigma0 = 0.0001
    es = cma.CMAEvolutionStrategy(x0, sigma0, opts)
    while not es.stop():
        X = es.ask()
        f_vals = [sum_of_square_diffs(x, expt_trace) for x in X]
        es.tell(X, f_vals)
        es.disp()
    res = es.result()
    best_params = res[0]
    ap.LoadStateVariables()
    #fig = plt.figure()
    #ax = fig.add_subplot(111)
    #ax.plot(times, expt_trace)
    #ax.plot(times, ap.SolveForVoltageTraceWithParams(best_params))
    #fig.savefig("gary_decker_expt_{}_best_fit.png".format(expt))
    #plt.close()
    temp_params_file = "gary_decker_expt_0_different_seeds_best_fit_params.txt".format(
        expt)
    np.savetxt(temp_params_file, best_params)
    print "best_params:", best_params
    print "best sos:", res[1], "\n"
    return best_params
Exemplo n.º 7
0
def run(train_set, test_set, ranker, num_interation, click_model, num_rankers):
    ndcg_scores = []
    cndcg_scores = []
    query_set = train_set.get_all_querys()
    index = np.random.randint(query_set.shape[0], size=num_interation)
    es = cma.CMAEvolutionStrategy(np.zeros((FEATURE_SIZE, )), 3)
    opt = cma.CMAOptions()
    opt.set('CSA_dampfac', 0.3)
    #es.sp.popsize = 500
    record = []
    batch_size = 10
    for j in range(0, num_interation // batch_size):
        canditate_rankers = es.ask()
        fitness = np.zeros((len(canditate_rankers, )))
        for i in index[j * batch_size:j * batch_size + batch_size]:
            qid = query_set[i]

            result_list = ranker.get_query_result_list(train_set, qid)

            clicked_doc, click_label, _ = click_model.simulate(
                qid, result_list, train_set)

            # if no clicks, skip.
            if len(clicked_doc) == 0:
                all_result = ranker.get_all_query_result_list(test_set)
                ndcg = evl_tool.average_ndcg_at_k(test_set, all_result, 10)
                cndcg = evl_tool.query_ndcg_at_k(train_set, result_list, qid,
                                                 10)

                ndcg_scores.append(ndcg)
                cndcg_scores.append(cndcg)
                continue

            # flip click label. exp: [1,0,1,0,0] -> [0,1,0,0,0]
            last_click = np.where(click_label == 1)[0][-1]
            click_label[:last_click + 1] = 1 - click_label[:last_click + 1]

            # bandit record
            record.append(
                (qid, result_list, click_label, ranker.get_current_weights()))
            fitness += ranker.fitness(canditate_rankers, record, train_set)[1:]

        es.tell(canditate_rankers, fitness / 10)

        best = np.argmax(fitness[1:])
        ranker.assign_weights(canditate_rankers[best])

        all_result = ranker.get_all_query_result_list(test_set)
        ndcg = evl_tool.average_ndcg_at_k(test_set, all_result, 10)
        cndcg = evl_tool.query_ndcg_at_k(train_set, result_list, qid, 10)

        ndcg_scores.append(ndcg)
        cndcg_scores.append(cndcg)
        final_weight = ranker.get_current_weights()
        print(ndcg, cndcg)

    return ndcg_scores, cndcg_scores, final_weight
Exemplo n.º 8
0
    def fit(self, X, Y):

        opts = cma.CMAOptions()
        opts.set("bounds", [[-2000] * 8, [0] * 8])

        self.Xs = X
        self.Ys = Y

        return cma.fmin(self.objective_func, self.val, 20, opts)
Exemplo n.º 9
0
def cmaOptFrcVals(env, trainDict, polDict, expDict):
    import cma
    #set to newPolDict if using retrained baseline
    polDictToUse = polDict
    #polDictToUse = newPolDict  #for trained baseline
    #use func==fitnessRwrd for policy, ==fitnessBL for baseline
    func = fitnessRwrd
    #func = fitnessBL

    #initial state and state dot for CMA optimization
    #idxsToUse is a list of indexes in precalced state/statedot to use - set to none to use all
    idxsToUse = None
    #idxsToUse = [0]
    initQList, initQdotList = tFuncs.loadInitStates(env,
                                                    expDict,
                                                    idxsToUse=idxsToUse)

    #set CMA options
    opts = cma.CMAOptions()
    opts['bounds'] = [0, .5]  # Limit our values to be between 0 and .5
    opts['maxfevals'] = 450  #max # of evals of fitness func
    opts[
        'tolfun'] = 1.0e-02  #termination criterion: tolerance in function value, quite useful
    #for each proposal set initial state/statedot
    #env.wrapped_env.env.unwrapped.setDebugMode(False)
    #initialize CMA optimizer
    initVals = 2 * [0.25]
    initSTD = 0.25
    es = cma.CMAEvolutionStrategy(initVals, initSTD, opts)
    itr = 1

    #whether to use force value or force multiplier value - ignored for rwrd function calculation, only used to match training for baseline calc
    useFrc = expDict[
        'bl_useForce']  #should be true, not training policy using only multiplier

    #while not converged
    while not es.stop():
        solns = es.ask()
        #for each solution proposal, set init state/state-dot and sol proposal, record return
        resList, resDictList, _ = execFunc(func, env, trainDict, polDictToUse,
                                           initQList, initQdotList, solns,
                                           useFrc)
        #inform CMA of results
        es.tell(solns, resList)
        es.logger.add()
        es.disp()
        print('')
        print('iter : {} done'.format(itr))
        print('')
        itr = itr + 1
    es.result_pretty()
    cma.plot()  # shortcut for es.logger.plot()
    #result value : es.mean
    #actual force values, instead of multiplier
    frcX = env.wrapped_env.env.unwrapped.getForceFromMult(es.mean[0])
    frcY = env.wrapped_env.env.unwrapped.getForceFromMult(es.mean[1])
    print('final optimal force result : {},{}'.format(frcX, frcY))
Exemplo n.º 10
0
    def _initialise(self):
        """
        Initialises the optimiser for the first iteration.
        """
        assert(not self._running)

        # Import cma (may fail!)
        # Only the first time this is called in a running program incurs
        # much overhead.
        import cma

        # Set up simulation
        options = cma.CMAOptions()

        # Set boundaries, or use manual boundary checking
        self._manual_boundaries = False
        if isinstance(self._boundaries, pints.RectangularBoundaries):
            options.set(
                'bounds',
                [list(self._boundaries._lower), list(self._boundaries._upper)]
            )
        elif self._boundaries is not None:
            self._manual_boundaries = True

        # Set stopping criteria
        #options.set('maxiter', max_iter)
        #options.set('tolfun', min_significant_change)
        # options.set('ftarget', target)

        # Tell CMA not to worry about growing step sizes too much
        #options.set('tolfacupx', 10000)

        # CMA-ES wants a single standard deviation as input, use the smallest
        # in the vector (if the user passed in a scalar, this will be the
        # value used).
        self._sigma0 = np.min(self._sigma0)

        # Tell cma-es to be quiet
        options.set('verbose', -9)

        # Set population size
        options.set('popsize', self._population_size)

        # CMAES always seeds np.random, whether you ask it too or not, so to
        # get consistent debugging output, we should always pass in a seed.
        # Instead of using a fixed number (which would be bad), we can use a
        # randomly generated number: This will ensure pseudo-randomness, but
        # produce consistent results if np.random has been seeded before
        # calling.
        options.set('seed', np.random.randint(2**31))

        # Search
        self._es = cma.CMAEvolutionStrategy(self._x0, self._sigma0, options)

        # Update optimiser state
        self._running = True
Exemplo n.º 11
0
    def __init__(self,
                 fct,
                 dim,
                 max_fevals=None,
                 x0=None,
                 seed=None,
                 is_restart=True,
                 is_mirror=False,
                 bounds=None):
        """

        :param fct: objective function
        :param dim: problem dimensionality
        :param max_fevals: maximum number of function evaluations (this is a rough budget as CMAES does not
                            conform to it strictly
        :param x0: initial guess
        :param seed: seed for random runs
        :param is_restart:
        :param is_mirror:
        :param bounds:
        """

        super(CMAES, self).__init__(fct,
                                    dim,
                                    max_fevals=max_fevals,
                                    x0=x0,
                                    seed=seed)
        self._opts = cma.CMAOptions()
        self._opts.set('tolfun', 1e-11)
        self._opts['tolx'] = 1e-11
        self._opts['verbose'] = -1
        self._opts['verb_disp'] = 0
        self._opts['verb_log'] = 0
        if max_fevals is not None:
            self._opts['maxfevals'] = max_fevals
        if seed is not None:
            self._opts['seed'] = seed

        self._dim_cma = dim
        if dim == 1:
            self._dim_cma = 2
            self._x0 = np.hstack((self._x0, self._x0))

        if bounds is None:
            self._opts['bounds'] = ([0] * self._dim_cma, [1] * self._dim_cma)
        else:
            self._opts['bounds'] = bounds

        self._is_restart = is_restart
        self._is_mirror = is_mirror

        if self._is_mirror:
            self._opts['CMA_mirrors'] = 1
        else:
            self._opts['CMA_mirrors'] = 0
Exemplo n.º 12
0
def test_cma(urdf_directory, dim):

    print('loading files...', end='')
    centroids, tasks = load(urdf_directory, 2)
    print('data loaded')
    
    opts = cma.CMAOptions()
    #for i in opts:
    #    print(i, ' => ', opts[i])
    max_evals = 1e6
    opts.set('tolfun', 1e-20)
    opts['tolx'] = 1e-20
    opts['verb_disp'] = 1e10
    opts['maxfevals'] = max_evals / centroids.shape[0]
    opts['BoundaryHandler'] = cma.BoundPenalty
    opts['bounds'] = [0, 1]
    
    es_vector = []
    for c in range(0, centroids.shape[0]):
        es_vector += [cma.CMAEvolutionStrategy(dim * [0.5], 0.5, opts)]

    num_cores = multiprocessing.cpu_count()
    pool = multiprocessing.Pool(num_cores)

    total_evals = 0
    log = open('cover_max_mean.dat', 'w')
    while total_evals < max_evals:
        #result_file = open('archive_'+ str(total_evals) + '.dat', 'w')
        archive = []
        for c in range(0, centroids.shape[0]):
            centroid = centroids[c, :]
            task = tasks[c]
            def func(angles):
                return hexapod(angles, task)[0]
            solutions = es_vector[c].ask()
#            print(len(solutions))# pop =14
            s_list = pool.map(evaluate, [(x, task, hexapod) for x in solutions])
            es_vector[c].tell(solutions, s_list)
            total_evals += len(solutions)
            # save to file
            xopt = es_vector[c].result[0]
            xval = es_vector[c].result[1]
            # save
            archive += [-xval]
            # write
            #result_file.write(str(-xval) + ' ')
            #write_array(centroid, result_file)
            #write_array(xopt, result_file)
            #result_file.write('\n')
        mean = np.mean(archive)
        max_v = max(archive)
        coverage = len(archive)
        log.write(str(total_evals) + ' ' + str(coverage) + ' ' + str(max_v) + ' ' + str(mean) + '\n')
        log.flush()
        print(total_evals)
Exemplo n.º 13
0
    def fit(self, max_iteration=2500, factor_sigma=0.1, pop_size=1):
        """
        Runs the optimazation loop

        Args:
            max_iteration (int, optional): maximum number of iterations. Defaults to 2500.
            factor_sigma (float, optiona): factor to multiplying the range of the bounded values
            pop_size (int, optional): population size for CMA ES
            cores (int, optional): number of parallel runs
        Returns:
            [List]: all optimized and calculated values for tc, m, w, a, b, c, c1, c2
        """
        # best guess of the starting values
        m = 0.5
        w = 9.
        # INFO: so far as I've understand the tc time this cannot be smaller als the max time of the time series
        tc = np.max(self.observations[0, :])

        # define options for CMAES
        opts = cm.CMAOptions()
        # here we define the initial search steps for CMAES usually I use to calculate the range of the
        # max and min bounds of the value and then apply a factor for sigma
        opts.set('CMA_stds', [
            factor_sigma * tc, factor_sigma * (0.9 - 0.1), factor_sigma *
            (13. - 6.)
        ])
        opts.set('bounds', [(tc, 0.1, 6.), (np.inf, 0.9, 13.)])
        opts.set('popsize', 10 * 2**pop_size)

        es = cm.CMAEvolutionStrategy(x0=[tc, m, w], sigma0=1., inopts=opts)

        # here we go
        while not es.stop() and es.countiter <= max_iteration:
            solutions = es.ask()
            solution = [self.fun_restricted(s) for s in solutions]
            es.tell(solutions, solution)
            es.logger.add()  # write data to disc to be plotted
            es.disp()

        # after while loop print infos and plot the final
        # es.result_pretty()
        # cm.plot()
        # plt.savefig('cmaes.png', dpi=300)

        # get best results
        tc, m, w = es.result.xbest
        a, b, c1, c2 = super().matrix_equation(self.observations, tc, m, w)
        c = (c1**2 + c2**2)**0.5

        # Use sklearn format for storing fit params -> original code from lppls package
        for coef in ['tc', 'm', 'w', 'a', 'b', 'c', 'c1', 'c2']:
            self.coef_[coef] = eval(coef)

        return tc, m, w, a, b, c, c1, c2
Exemplo n.º 14
0
    def __init__(self, sigma0=.1):
        """
        Initialize CMA Evolution Strategy object.

        Parameters
        ----------
        sigma0 : float
            Initial standard deviation in each coordinate.
        """
        self.sigma0 = sigma0
        self.options = cma.CMAOptions()
Exemplo n.º 15
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
Exemplo n.º 16
0
def cmaes_optimization(obj_func, initial_theta, bounds):
    import cma
    initial_sigma = 0.3
    # assuming bounds are same for all dimensions
    cmaes_bounds = [[b[0] for b in bounds], [b[1] for b in bounds]]
    opts = cma.CMAOptions()
    opts.set("bounds", cmaes_bounds)
    opts.set("verbose", -1)
    opts.set("verb_log", 0)
    opts.set("verb_disp", 0)
    opts.set('tolfun', 1e-2)
    es = cma.CMAEvolutionStrategy(initial_theta, initial_sigma, opts)
    es.optimize(obj_func)
    return es.result.xbest, es.result.fbest
Exemplo n.º 17
0
 def __init__(self, prob):
     Solver.__init__(self, prob)
     opts = cma.CMAOptions()
     # for k, v in opts.iteritems():
     #     print k, v
     # exit(0)
     self.p_dir = 'optim_data/cma/'
     opts.set('verb_disp', 1)
     opts.set('popsize', 8)
     opts.set('verb_filenameprefix', self.p_dir)
     opts.set('maxiter', 2000)
     self.options = opts
     self.cen = None
     self.rng = None
Exemplo n.º 18
0
def test_cma(centroids_fname, dim):
    centroids = np.loadtxt(centroids_fname)

    opts = cma.CMAOptions()
    #for i in opts:
    #    print(i, ' => ', opts[i])
    max_evals = 1e6
    opts.set('tolfun', 1e-20)
    opts['tolx'] = 1e-20
    opts['verb_disp'] = 1e10
    opts['maxfevals'] = max_evals / centroids.shape[0]
    opts['BoundaryHandler'] = cma.BoundPenalty
    opts['bounds'] = [0, 1]

    es_vector = []
    for c in range(0, centroids.shape[0]):
        es_vector += [cma.CMAEvolutionStrategy(dim * [0.5], 0.5, opts)]

    total_evals = 0
    log = open('cover_max_mean.dat', 'w')
    while total_evals < max_evals:
        result_file = open('archive_' + str(total_evals) + '.dat', 'w')
        archive = []
        for c in range(0, centroids.shape[0]):
            centroid = centroids[c, :]

            def func(angles):
                return arm(angles, centroid)[0]

            solutions = es_vector[c].ask()
            es_vector[c].tell(solutions, [func(x) for x in solutions])
            total_evals += len(solutions)
            # save to file
            xopt = es_vector[c].result[0]
            xval = es_vector[c].result[1]
            # save
            archive += [-xval]
            # write
            result_file.write(str(-xval) + ' ')
            write_array(centroid, result_file)
            write_array(xopt, result_file)
            result_file.write('\n')
        mean = np.mean(archive)
        max_v = max(archive)
        coverage = len(archive)
        log.write(
            str(total_evals) + ' ' + str(coverage) + ' ' + str(max_v) + ' ' +
            str(mean) + '\n')
        log.flush()
        print(total_evals)
Exemplo n.º 19
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
Exemplo n.º 20
0
def simple_best_fit_sum_of_squares(concs, responses, x0=None, sigma0=0.1, cma_random_seed=123):
    opts = cma.CMAOptions()
    opts["seed"] = cma_random_seed
    if x0 is None:
        x0 = [2.5, 1.]
    es = cma.CMAEvolutionStrategy(x0, sigma0, opts)
    while not es.stop():
        X = es.ask()
        es.tell(X, [sum_of_square_diffs(concs, responses, *scale_params_for_cmaes(x)) for x in X])
        #es.disp()
    res = es.result
    best_sigma = compute_best_sigma_analytic(res[1], len(responses))
    #best_fit_params = np.concatenate((scale_params_for_cmaes(res[0]), [best_sigma]))
    return scale_params_for_cmaes(res[0])
Exemplo n.º 21
0
def fit_cmaes(initial_params,
              additional_arguments,
              bounds_min,
              bounds_max,
              sigma0=1e-17,
              tolx=1e-20,
              tolfun=1e-1,
              log=True):


    #opt_data = cma.CMADataLogger()

    options = cma.CMAOptions()
    options.set('tolfun', tolfun)
    options['tolx'] = tolx
    #options['verb_plot'] = 10
    #options['bounds'] = (bounds_min, bounds_max)

    param_vec = np.array(initial_params)
    
    #options['CMA_stds']=np.sqrt(param_vec)*1e5
    #options['CMA_stds']=param_vec
   
    if log:
      initial_params=trafo(param_vec,1)
      options['bounds'] = (trafo(bounds_min,1), trafo(bounds_max,1))
    else:   
      options['bounds'] = (bounds_min, bounds_max)
      p_min = max(param_vec.min(), 1e-20)
      options['scaling_of_variables'] = param_vec / p_min

    options['tolx'] = tolx  
    print('\n IP: {0}'.format(initial_params))
    
    result = cma.fmin(compute_objective_function, 
                      x0=initial_params,
                      sigma0=sigma0,
                      options=options,
                      args=additional_arguments)
    
    if log:
      results = trafo(np.array(result[0]),-1)
    else:
      results = result[0]

    #print(results)  
    
    return results, result #result[0]
Exemplo n.º 22
0
    def optimize(self, q_data, f_init, tr_target):

        # re-convert input arrays
        q_data = np.reshape(q_data, (q_data.size, ))
        f_init = np.reshape(f_init, (f_init.size, ))
        if tr_target is not None:
            self.tr_target_i = np.rint(tr_target[:, 0:3]).astype(int)
            self.tr_target_q = tr_target[:, 3]

        # settings
        opts = cma.CMAOptions()
        opts['verb_log'] = 0

        # actual run with
        es = cma.CMAEvolutionStrategy(f_init, 1, opts)
        es.optimize(self.objective, args=(q_data, ))  # , 3, 3

        # finalize
        res = es.result()
        es.stop()

        # Calculate initial
        q_hat = self.evaluate(f_init)
        f_current = self.initial(q_data)

        likelihood = np.linalg.norm(q_hat - q_data)
        prior = np.linalg.norm(f_current)
        conditional = get_conditional(q_data, self.size[0], self.size[1],
                                      self.tr_target_i, self.tr_target_q, 10.0)
        costs0 = (self.wlh * likelihood, self.wp * prior,
                  self.wc * conditional)

        # ... and final costs:
        f_hat = res[0]
        q_hat = self.evaluate(f_hat)

        likelihood = np.linalg.norm(q_hat - q_data)
        prior = np.linalg.norm(f_hat)
        conditional = get_conditional(q_hat, self.size[0], self.size[1],
                                      self.tr_target_i, self.tr_target_q, 10.0)

        costs1 = (self.wlh * likelihood, self.wp * prior,
                  self.wc * conditional)

        np.testing.assert_almost_equal(sum(costs1), res[1], verbose=True)
        return res[0], costs0, costs1
    def __init__(self, options={}):
        """
        Initializes the algorithm.  The options dictionary is passed to the
        CMAOptions object
        """
        try:
            cma
        except NameError:
            raise ImportError(
                "The `cma` package is not installed. See the wiki on our "
                "GitHub project for installation instructions.")

        self.store_parameters = []
        self.opts = cma.CMAOptions()
        self.opts.init(options)
        self.es = None
        self.reset()
Exemplo n.º 24
0
def fit_cmaes(paras_0,
              data,
              sigma0=4e-16,
              tolx=1e-20):  #bounds_min, bounds_max,

    options = cma.CMAOptions()
    #options.set('tolfun', 1e-14)
    #options['tolx'] = tolx
    #options['bounds'] = (bounds_min, bounds_max)
    #param_vec = np.array(initial_params)
    #p_min = max(param_vec.min(), 1e-20)
    #options['scaling_of_variables'] = param_vec / p_min
    result = cma.fmin(objective_function,
                      x0=paras_0,
                      sigma0=sigma0,
                      options=options,
                      args=data)
    return result
Exemplo n.º 25
0
    def __init__(self,
                 session,
                 variables=None,
                 scale=1.0,
                 CMA_mu=None,
                 CMA_cmean=1,
                 CMA_rankmu=1,
                 CMA_rankone=1):
        """
        Create a training session.

        Args:
          session: a TF session.
          variables: if specified, only these variables
            are optimized by the algorithm. Otherwise, all
            trainable variables are optimized.
          scale: scale for the parameter stddev.
        """
        self.session = session
        self.variables = (variables or tf.trainable_variables())
        self._placeholders = [
            tf.placeholder(v.dtype, shape=[int(np.prod(v.get_shape()))])
            for v in self.variables
        ]
        self._assigns = tf.group(*[
            tf.assign(v, tf.reshape(ph, tf.shape(v)))
            for v, ph in zip(self.variables, self._placeholders)
        ])
        param_stddevs = []
        for var in self.variables:
            stddev = sqrt(1 / float(var.get_shape()[0].value))
            param_stddevs.extend([stddev] * int(np.prod(var.get_shape())))
        self._param_stddevs = param_stddevs
        opt = cma.CMAOptions()
        #opt.set('CMA_mu', CMA_mu)

        #opt.set('popsize', popsize)
        opt.set('CMA_cmean', CMA_cmean)
        opt.set('CMA_rankmu', CMA_rankmu)
        opt.set('CMA_rankone', CMA_rankone)
        #opt.set('CMA_cmean', 10)
        self.cma = cma.CMAEvolutionStrategy([0] * len(param_stddevs), scale,
                                            opt)
Exemplo n.º 26
0
def fit_k(fitted_parameters, paras_0=[1., 0.], sigma0=3):

    fitted_parameters['k_nutrient'] = fitted_parameters['k_nutrient'] * 1.e+14
    fitted_parameters['k_deg_0'] = fitted_parameters['k_deg_0'] * 1.e+14

    options = cma.CMAOptions()
    options['tolx'] = 1e-20

    f_paras = cma.fmin(
        objective_function,
        paras_0,
        sigma0,
        args=(fitted_parameters['k_nutrient'],
              fitted_parameters['k_deg_0']))  #, options=options)
    #fit_cmaes(paras_0, data)#, [0.,-1],[5,1])
    print('m={0}, n={1}'.format(f_paras[0][0], f_paras[0][1]))
    msd = calculate_msd(fitted_parameters['k_deg_0'],
                        linear_f(fitted_parameters['k_nutrient'], f_paras[0]))
    print('MSD:{0}'.format(msd))
    return f_paras
Exemplo n.º 27
0
def CMA():
	opt = cma.CMAOptions()
	opt['tolfun'] = 1e-11
	opt['popsize'] = n_samples
	opt['maxiter'] = n_iters
	opt['bounds'] = [0, 10]
	mu = np.ones((dim)) * 5
	es = cma.CMAEvolutionStrategy(mu, 2., opt)

	stats = {
		'loss': [],
		'theta': [],
		'mintheta': [],
	}

	best_solution = None
	best_loss = np.inf
	t0 = time.time()

	for i in range(n_iters):
		solutions = es.ask()
		loss = evaluate(solutions)
		loss = np.array(loss)
		idx = np.argmin(loss)
		es.tell(solutions, loss)
		curr_best = np.array(solutions).mean(0)
		curr_min = np.array(solutions)[idx]
		stats['theta'].append(curr_best)
		stats['loss'].append(loss.mean())
		stats['mintheta'].append(curr_min)
		print("[INFO] iter %2d | time %10.4f | avg loss %10.4f | min loss %10.4f" % (
			i,
			time.time() - t0,
			loss.mean(), loss.min()))
		V = transform(curr_best)
		print(V)
		M = transform(curr_min)
		print(M)
		if (i+1) % 5 == 0:
			with open("./stats/{}.npy".format(ENV), 'w') as f:
				np.save(f, stats)
Exemplo n.º 28
0
def cmaUser(func, dim):
    pool = Pool()
    #SM = []
    opts = cma.CMAOptions()
    opts['tolfun'] = 1e-6
    opts['tolfunhist'] = 1e-6
    opts['tolx'] = 1e-6
    opts['popsize'] = 15
    opts['maxiter'] = 10000
    opts['bounds'] = [[-100] * dim, [100] * dim]
    es = cma.CMAEvolutionStrategy(dim * [5], 3, opts)
    while not es.stop():
        #it += 1
        solutions = es.ask()
        es.tell(solutions, list(map(func, solutions)))
        #SM.append(es.result()[0])
    # es.logger.add()
    # es.disp()
    # es.result_pretty()
    print(es.result()[1])
    print(es.result()[0]) 
    print(es.countiter)
Exemplo n.º 29
0
    def __init__(self, **kwargs):
        """
        Initialize the CMAESDriver driver.

        Parameters
        ----------
        **kwargs : dict of keyword arguments
            Keyword arguments that will be mapped into the Driver options.
        """
        super().__init__(**kwargs)

        # What we support
        self.supports['inequality_constraints'] = True
        self.supports['equality_constraints'] = True
        self.supports['multiple_objectives'] = True

        # What we don't support yet
        self.supports['integer_design_vars'] = False
        self.supports['two_sided_constraints'] = False
        self.supports['linear_constraints'] = False
        self.supports['simultaneous_derivatives'] = False
        self.supports['active_set'] = False
        self.supports['distributed_design_vars'] = False
        self.supports._read_only = True

        # expose CMAOptions
        self.CMAOptions = cma.CMAOptions()

        self._desvar_idx = {}

        # random state can be set for predictability during testing
        if 'CMAESDriver_seed' in os.environ:
            self._randomstate = int(os.environ['CMAESDriver_seed'])
        else:
            self._randomstate = None

        # Support for Parallel models.
        self._concurrent_pop_size = 0
        self._concurrent_color = 0
Exemplo n.º 30
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