def fit(self, my_lambda, coverage=3, max_iters=50):

        #while(True):
        print('beta = ', self.my_beta)
        print('lambda = ', my_lambda)

        my_args = (self, my_lambda, coverage, False)

        nfeatures = self.train_args['feature'].shape[1]
        w0 = self.randomw0(nfeatures)

        sigma_value = 0.02
        print('sigma0 ', sigma_value)
        es = CMAEvolutionStrategy(w0,
                                  sigma0=sigma_value,
                                  inopts={
                                      'maxiter': max_iters,
                                      'popsize': 40
                                  })

        while not es.stop():
            solutions = es.ask()
            fitnesses = [
                InterestingnessLearner.cost(x, *my_args) for x in solutions
            ]
            es.tell(solutions, fitnesses)
            es.disp()

            self.nmcost(es.result[0], my_lambda, coverage, is_debug=True)

        final_model = self.createClassifier(es.result[0], coverage)

        print(final_model.size(), final_model.meanSupport())
        return final_model
Exemplo n.º 2
0
def runSingleSplit_pycma(fid, dim, iids=[1, 2, 3, 4, 5], num_reps=5):
    """
        Function running single-split CMA-ES.

        :param fid: The function id (from bbob)
        :param dim: The dimension to run the bbob-problem in
        :param rep1: The configuration to run before the splitpoint
        :param rep2: The configuration to run after the splitpoint
        :param split_idx: The splitpoint-index at which to switch between rep1 and rep2
        :param iids: The instances of the bbob-function to run
        :param num_reps: The amount of repetitions to run
        :return: The ERT over all num_reps runs on all instances in iids
    """
    obs = create_observer("Pycma", None, None)
    suite = cocoex.Suite("bbob", "", f"dimensions:{dim}")
    HittingTimes = []
    for i, iid in enumerate(iids):
        for j in range(num_reps):
            fitness_function = suite.get_problem_by_function_dimension_instance(
                fid, dim, iid)
            fitness_function.observe_with(obs)
            f = fitnessFunc(fitness_function)
            cma = CMAEvolutionStrategy([0] * 5, 0.5)
            cma.optimize(f)
            HittingTimes.append(cma.result.evaluations)
            print(cma.result.xbest)
            fitness_function.free()
    return np.mean(HittingTimes)
Exemplo n.º 3
0
def main(filename, directory):
    initialFilename = os.path.join(directory, 'initial.json')
    genDir = os.path.join(directory, 'gens')
    finalFilename = os.path.join(directory, 'final.json')
    baseCondor = 'base.condor'

    with open(baseCondor, 'r') as f:
        condorConfig = f.read()

    optMkdir(directory)
    optMkdir(genDir)
    shutil.copyfile(filename, initialFilename)
    with open(filename, 'r') as f:
        conf = json.load(f)
    paramNames = []
    paramVals = []
    flattenParams(conf, paramNames, paramVals, '')
    sigma0 = SIGMA0
    opts = Options()
    opts['popsize'] = POP_SIZE
    #opts.printme()
    cma = CMAEvolutionStrategy(paramVals, sigma0, opts)
    while (cma.countiter < NUM_GENS) and not (cma.stop()):
        thisGenDir = os.path.join(genDir, str(cma.countiter))
        optMkdir(thisGenDir)
        xs = cma.ask()
        xs, fits = runEvals(paramNames, xs, cma.countiter, thisGenDir,
                            condorConfig)
        cma.tell(xs, fits)
    res = cma.result()
    paramsToJsonFile(finalFilename, paramNames, res[0])
Exemplo n.º 4
0
 def __build__(self, init_mean_parameters: Sequence):
     self._evolution_strategy = CMAEvolutionStrategy(
         init_mean_parameters,
         self._parameters_variance,  # Sigma is shared
         {"popsize": self._num_candidate_policies},
     )  # Population size
     self._resample_shared_parameters()
     self.policy.set_param_values(self._shared_params[0])
def evaluate(mth, run_i, seed):
    print(mth, run_i, seed, '===== start =====', flush=True)

    def objective_function(config):
        y = problem.evaluate_config(config)
        return y

    from cma import CMAEvolutionStrategy
    from litebo.utils.util_funcs import get_types
    from litebo.utils.config_space import Configuration

    types, bounds = get_types(cs)
    assert all(types == 0)

    # Check Constant Hyperparameter
    const_idx = list()
    for i, bound in enumerate(bounds):
        if np.isnan(bound[1]):
            const_idx.append(i)

    hp_num = len(bounds) - len(const_idx)
    es = CMAEvolutionStrategy(hp_num * [0], 0.99, inopts={'bounds': [0, 1], 'seed': seed})

    global_start_time = time.time()
    global_trial_counter = 0
    config_list = []
    perf_list = []
    time_list = []
    eval_num = 0
    while eval_num < max_runs:
        X = es.ask(number=es.popsize)
        _X = X.copy()
        for i in range(len(_X)):
            for index in const_idx:
                _X[i] = np.insert(_X[i], index, 0)  # np.insert returns a copy
        # _X = np.asarray(_X)
        values = []
        for xi in _X:
            # convert array to Configuration
            config = Configuration(cs, vector=xi)
            perf = objective_function(config)
            global_time = time.time() - global_start_time
            global_trial_counter += 1
            values.append(perf)
            print('=== CMAES Trial %d: %s perf=%f global_time=%f' % (global_trial_counter, config, perf, global_time))

            config_list.append(config)
            perf_list.append(perf)
            time_list.append(global_time)
        values = np.reshape(values, (-1,))
        es.tell(X, values)
        eval_num += es.popsize

    print('===== Total evaluation times=%d. Truncate to max_runs=%d.' % (eval_num, max_runs))
    config_list = config_list[:max_runs]
    perf_list = perf_list[:max_runs]
    time_list = time_list[:max_runs]
    return config_list, perf_list, time_list
Exemplo n.º 6
0
    def init_optimization_strategy(self, *args, **kwds):
        """
        init_optimization_strategy initializes optimizer object. Its argument depend on the specific initializer used
        IN the case of the CMA optimizer the options are described here: https://pypi.python.org/pypi/cma
        :param args: see https://pypi.python.org/pypi/cma
        :param kwds: see https://pypi.python.org/pypi/cma
        :return: None
        """

        self.optim = CMAEvolutionStrategy(*args, **kwds)
Exemplo n.º 7
0
 def create_optimizer(self):
     from cma import CMAEvolutionStrategy
     opts = {'bounds': self.bounds}
     stddevs = (self.param_space.param_uppers - self.param_space.
                param_lowers) * self.stddev + self.param_space.param_lowers
     optimizer = CMAEvolutionStrategy(self.init_guess,
                                      np.amin(stddevs),
                                      inopts=opts)
     _ = optimizer.optimize(self._priv_evaluator)
     self.is_converged = True
Exemplo n.º 8
0
    def __init__(self, expt_dir):

        raise NotImplementedError('The CMA chooser is not yet implemented!')
        
        self.state_pkl = os.path.join(expt_dir, self.__module__ + ".pkl")

        #TODO: params needs to be an array of starting values
        # - need to figure out how to map Spearmint params into
        # all floats usable by the evolution strategy.
        self.optimizer = CMAEvolutionStrategy(params)
Exemplo n.º 9
0
 def init_algos(self):
     del self.algos[:]
     if self.algo_type == 'CMA':
         for cluster in self.clusters:
             self.algos.append(
                 CMAEvolutionStrategy([0.5] * self.dimension, 0.2, {
                     'popsize': self.n_points,
                     'bounds': [0, 1]
                 }))
         for algo in self.algos:
             new_trans_positions = algo.ask()
Exemplo n.º 10
0
 def setup(self):
     # dummy_env = gym.make(sellf.args.env_name)
     # num_actions = dummy_env.action_space.n
     # dummy_c = ControlModel(encoder=self.model,
     #                        num_actions=num_actions)
     num_params = np.prod(self.model.fc.weight.size()) + np.prod(
         self.model.fc.bias.size())
     param0 = np.random.randn(num_params)
     es = CMAEvolutionStrategy(param0,
                               sigma0=1,
                               inopts={"popsize":
                                       self.args.workers + 1})  #maximize
     return es
Exemplo n.º 11
0
    def initialize(self, **kwargs):
        super().initialize(**kwargs)
        if self.x0 is None:
            self.x0 = self.domain.l + self.domain.range / 2

        # cma operates on normalized scale
        x0 = self.domain.normalize(self.x0)
        self.cma = CMAEvolutionStrategy(x0=x0,
                                        sigma0=self.config.sigma0,
                                        inopts={'bounds': [0, 1]})
        self._X = None
        self._X_i = 0
        self._Y = None
Exemplo n.º 12
0
def example_cma():
    def yangs4(x):
        # f = (a-b).c
        a = np.sum(np.square(np.sin(x)))
        b = np.exp(-np.sum(np.square(x)))
        c = np.exp(-np.sum(np.square(np.sin(np.sqrt(np.abs(x))))))
        return (a - b) * c

    n = 5
    x0 = n * [0.1]
    sigma0 = 0.1
    # evolution strategy
    es = CMAEvolutionStrategy(x0, sigma0)
    optSol = es.optimize(yangs4)
Exemplo n.º 13
0
def cma(fun, budget):
    sigma0 = 0.02
    range_ = fun.upper_bounds - fun.lower_bounds
    center = fun.lower_bounds + range_ / 2
    x0 = center
    options = dict(scaling=range_ / range_[0],
                   maxfevals=budget,
                   verb_log=0,
                   verb_disp=1,
                   verbose=1)
    es = CMAEvolutionStrategy(x0, sigma0 * range_[0], options)
    res = es.optimize(fun).result()
    xbest, ybest, nbeval, *rest = res
    return xbest, ybest, nbeval
def evolve_greedy_policies(model_dist: ModelDist,
                           iterations: int = 30,
                           population_size: int = 5):
    """
    Evolves the greedy policy to find the best policies

    :param model_dist: Model distribution
    :param iterations: Number of evolutions
    :param population_size: The population size
    """
    print(f'Evolves the greedy policies for {model_dist.name} model with '
          f'{model_dist.num_tasks} tasks and {model_dist.num_servers} servers')

    eval_tasks, eval_servers = model_dist.generate_oneshot()
    lower_bound = greedy_algorithm(eval_tasks, eval_servers, ValuePriority(),
                                   ProductResources(),
                                   SumSpeed()).social_welfare
    print(f'Lower bound is {lower_bound}')
    reset_model(eval_tasks, eval_servers)

    evolution_strategy = CMAEvolutionStrategy(
        11 * [1], 0.2, {'population size': population_size})
    for iteration in range(iterations):
        suggestions = evolution_strategy.ask()
        tasks, servers = model_dist.generate_oneshot()

        solutions = []
        for i, suggestion in enumerate(suggestions):
            solutions.append(
                greedy_algorithm(
                    tasks, servers,
                    TaskPriorityEvoStrategy(i, *suggestion[:5]),
                    ServerSelectionEvoStrategy(i, *suggestion[5:8]),
                    ResourceAllocationEvoStrategy(
                        i, *suggestion[8:11])).social_welfare)
            reset_model(tasks, servers)

        evolution_strategy.tell(suggestions, solutions)
        evolution_strategy.disp()

        if iteration % 2 == 0:
            evaluation = greedy_algorithm(
                eval_tasks, eval_servers,
                TaskPriorityEvoStrategy(0, *suggestions[0][:5]),
                ServerSelectionEvoStrategy(0, *suggestions[0][5:8]),
                ResourceAllocationEvoStrategy(0, *suggestions[0][8:11]))
            print(f'Iter: {iteration} - {evaluation.social_welfare}')

    pprint.pprint(evolution_strategy.result())
Exemplo n.º 15
0
 def shift_matrix(self, i, best_point):
     center = self.clusters[i].transform_inverse([[0.5] * self.dimension
                                                  ])[0]
     translate = best_point - center
     translate_matrix = np.eye(self.dimension + 1)
     translate_matrix[0:-1, -1] = -translate.T
     self.clusters[i].matrix = np.dot(self.clusters[i].matrix,
                                      translate_matrix)
     #self.update_matrix(i)
     if self.algo_type == 'CMA':
         self.algos[i] = CMAEvolutionStrategy([0.5] * self.dimension, 0.2, {
             'popsize': self.n_points,
             'bounds': [0, 1]
         })
         init_positions = self.algos[i].ask()
Exemplo n.º 16
0
    def init_algo(self):
        init_min_bound = self.boundary.init_min_bounds[0]
        init_max_bound = self.boundary.init_max_bounds[0]
        min_bound = self.boundary.min_bounds[0]
        max_bound = self.boundary.max_bounds[0]

        if self.algo_type == 'CMA':
            init_point = [(init_max_bound + init_min_bound) / 2] * dimension
            sigma = (init_max_bound - init_min_bound) * 0.2
            #print 'init_point:', init_point
            #print 'sigma:', sigma
            self.algo = CMAEvolutionStrategy(init_point, sigma, {
                'popsize': self.n_points,
                'bounds': [min_bound, max_bound]
            })
Exemplo n.º 17
0
def main():
    # param length
    # 10 fights
    # rating_mu, rating_sig, wins, losses, odds
    input_params_len = 10 * 2 * 4
    es = CMAEvolutionStrategy([0] * input_params_len, 0.5)

    while not es.stop():
        solutions = es.ask()
        func_vals = [get_betting_result(x) for x in solutions]
        es.tell(solutions, func_vals)
        es.logger.add()  # write data to disc to be plotted
        es.disp()

    es.result_pretty()
Exemplo n.º 18
0
    def maximize(self, runhistory: HistoryContainer, num_points: int,
                 **kwargs) -> Iterable[Tuple[float, Configuration]]:
        try:
            from cma import CMAEvolutionStrategy
        except ImportError:
            raise ImportError("Package cma is not installed!")

        types, bounds = get_types(self.config_space)
        assert all(types == 0)

        # Check Constant Hyperparameter
        const_idx = list()
        for i, bound in enumerate(bounds):
            if np.isnan(bound[1]):
                const_idx.append(i)

        hp_num = len(bounds) - len(const_idx)
        es = CMAEvolutionStrategy(hp_num * [0],
                                  0.99,
                                  inopts={'bounds': [0, 1]})

        eval_num = 0
        next_configs_by_acq_value = list()
        while eval_num < num_points:
            X = es.ask(number=es.popsize)
            _X = X.copy()
            for i in range(len(_X)):
                for index in const_idx:
                    _X[i] = np.insert(_X[i], index, 0)
            _X = np.asarray(_X)
            values = self.acquisition_function._compute(_X)
            values = np.reshape(values, (-1, ))
            es.tell(X, values)
            next_configs_by_acq_value.extend([(values[i], _X[i])
                                              for i in range(es.popsize)])
            eval_num += es.popsize

        next_configs_by_acq_value.sort(reverse=True, key=lambda x: x[0])
        next_configs_by_acq_value = [_[1] for _ in next_configs_by_acq_value]
        next_configs_by_acq_value = [
            Configuration(self.config_space, vector=array)
            for array in next_configs_by_acq_value
        ]

        challengers = ChallengerList(next_configs_by_acq_value,
                                     self.config_space, self.random_chooser)
        self.random_chooser.next_smbo_iteration()
        return challengers
Exemplo n.º 19
0
    def otimizar(self):
        lw = [0.0, 0.0, 0.0]
        up = [1.0, 1.0, 1.0]
        x0 = 3 * [0.1]
        sigma = 0.25

        # Otimizando
        c = CMAEvolutionStrategy(x0, sigma, {'bounds': [lw, up]})
        self.iniciar_tempo()
        c.optimize(self.func_objetivo, iterations=self.num_chamadas)
        self.finalizar_tempo()

        # Extraindo os melhores hiperparametros
        C_ = 2 ** (-5 + c.best.x[0] * 20)
        gamma_ = 2 ** (-15 + c.best.x[1] * 18)
        epsilon_ = abs(c.best.x[2])

        # Treinando SVM final com os parâmetros encontrados
        self.svm = SVM(gamma_, C_, epsilon_)
        self.svm.treinar(self.X_treinamento, self.Y_treinamento)
        self.svm.testar(self.X_teste, self.Y_teste)
Exemplo n.º 20
0
def main():

    bias0 = [0.0, -3.0] * total_biases
    weight0 = [0.0, -3.0] * total_weights
    x0 = bias0
    x0.extend(weight0)

    # assert(len(x0) == 108)
    sigma0 = 0.1

    # evolution strategy
    es = CMAEvolutionStrategy(x0, sigma0)
    optSol = es.optimize(regression_problem)
    print(optSol)

    sol = [-3.18979598e-02,  1.21945777e+00,  3.80408518e-02,
            7.99747577e-01,  9.51714572e-02,  8.10528673e-01, -5.97132372e-02,
            1.04204663e+00,  1.74305244e-02,  9.93532532e-01, -8.67464165e-02,
            9.51615254e-01, -3.77311791e-02,  9.97444596e-01, -5.84591048e-03,
            1.08793924e+00, -5.59697903e-02,  9.51347690e-01,  3.33323542e-02,
            1.00693954e+00]
Exemplo n.º 21
0
    def update_borders(self):
        for i in range(len(self.clusters)):
            cluster = self.clusters[i]
            len_bounds = np.linalg.norm(cluster.border[1] - cluster.border[1])
            es = CMAEvolutionStrategy(
                cluster.border.ravel().tolist(), len_bounds * 0.1, {
                    'bounds':
                    [self.boundary.min_bounds[0], self.boundary.max_bounds[0]]
                })
            while not es.stop():
                solutions = es.ask()
                #TODO
                es.tell(
                    solutions,
                    [self.evaluate_border(border, i) for border in solutions])
                #es.tell( solutions, [cluster.evaluate_border(border) for border in solutions] )

                x_best = es.result()[0]
                #if x_best is not None and cluster.in_global_border( x_best ):
                if x_best is not None:
                    cluster.border = x_best.reshape(cluster.border.shape)
            '''
Exemplo n.º 22
0
def run():
    train = 0
    
    names = [
        # 'bet_pred_a', 'bet_pred_b', 'bet_odds_a', 'bet_odds_b', 'bet_wnl_a', 'bet_wnl_b',
        'bet_ts_a', 'bet_ts_b', 'bet_tmi_a', 'bet_tmi_b', 'bet_tma_a', 'bet_tma_b',
    ]
    params = [
        0, 0, 0, 0, 0, 0
    ]
    bounds = [[-np.inf],
              [np.inf]]
    assert len(params) == len(names)
    # assert len(params) == len(bounds[0])

    if train:
        sigma = 1
        opts = CMAOptions()
        # opts['tolx'] = 1E-2
        opts['bounds'] = bounds
        es = CMAEvolutionStrategy(params, sigma, inopts=opts)
        while not es.stop():
            solutions = es.ask()
            fitness = [main(x, train=1) for x in solutions]
            es.tell(solutions, fitness)
            es.disp()
            print(list(es.result[0]))
            print(list(es.result[5]))
        es.result_pretty()
        print('')
        print('best')
        print(list(es.result[0]))
        print('')
        print('xfavorite: distribution mean in "phenotype" space, to be considered as current best estimate of the optimum')
        print(list(es.result[5]))

    else:
        main(params)
Exemplo n.º 23
0
def cma_minimize(acq_function,
                 bounds,
                 return_best_only=True,
                 **kwargs) -> Tuple[torch.Tensor, ...]:
    x0 = 0.5 * np.ones(bounds.shape[-1])
    opts = {
        'bounds': [0, 1],
        "popsize": kwargs.get('popsize', 100),
        "seed": 10,
        "verbose": -1
    }
    if "maxiter" in kwargs:
        opts.update(maxiter=kwargs["maxiter"])
    es = CMAEvolutionStrategy(x0=x0,
                              sigma0=kwargs.get('sigma0', 0.5),
                              inopts=opts)

    xs_list, y_list = [], []
    with torch.no_grad():
        while not es.stop():
            xs = es.ask()
            X = torch.tensor(xs, dtype=torch.float64)
            Y = acq_function(X.unsqueeze(-2))
            y = Y.view(-1).double().numpy()
            es.tell(xs, y)
            xs_list.append(xs)
            y_list.append(y)

        if return_best_only:
            cand = torch.tensor([es.best.x])
            cand_val = torch.tensor([es.best.f])
        else:
            cand = torch.tensor(np.concatenate(xs_list, axis=0))
            cand_val = torch.tensor(np.concatenate(y_list, axis=0))

    return cand, cand_val
Exemplo n.º 24
0
 def __init__(self, mu, sigma, popsize, weight_decay=0.01):
     self.es = CMAEvolutionStrategy(mu.tolist(), sigma,
                                    {'popsize': popsize})
     self.weight_decay = weight_decay
     self.solutions = None
Exemplo n.º 25
0
    def train(self, cost, model, x_data, y_data=None, tolfun=1e-11, popsize=None, maxiter=None, use_grad=False):
        """Trains the ``model`` using the custom `cost` function.

        Args:
            cost (theta.costfunctions): the cost function.
            model (theta.model.Model or theta.rtbm.RTBM): the model to be trained.
            x_data (numpy.array): the support data with shape (Nv, Ndata).
            y_data (numpy.array): the target prediction.
            tolfun (float): the maximum tolerance of the cost function fluctuation to stop the minimization.
            popsize (int): the population size.
            maxiter (int): the maximum number of iterations.
            use_grad (bool): if True the gradients for the cost and model are used in the minimization.

        Returns:
            numpy.array: the optimal parameters

        Note:
            The parameters of the model are changed by this algorithm.
        """

        initsol = np.real(model.get_parameters())
        args = {'bounds': model.get_bounds(),
                'tolfun': tolfun,
                'verb_log': 0}
        sigma = np.max(model.get_bounds()[1])*0.1

        if popsize is not None:
            args['popsize'] = popsize

        if maxiter is not None:
            args['maxiter'] = maxiter

        grad = None
        if use_grad:
            grad = worker_gradient

        es = CMAEvolutionStrategy(initsol, sigma, args)
        if self.num_cores > 1:
            with closing(mp.Pool(self.num_cores, initializer=worker_initialize,
                                 initargs=(cost, model, x_data, y_data))) as pool:
                while not es.stop():
                    f_values, solutions = [], []
                    while len(solutions) < es.popsize:
                        x = es.ask(es.popsize-len(solutions), gradf=grad)
                        curr_fit = pool.map_async(worker_compute, x).get()
                        for value, solution in zip(curr_fit,x):
                            if not np.isnan(value):
                                solutions.append(solution)
                                f_values.append(value)
                    es.tell(solutions, f_values)
                    es.disp()
                pool.terminate()
        else:
            worker_initialize(cost, model, x_data, y_data)
            while not es.stop():
                f_values, solutions = [], []
                while len(solutions) < es.popsize:
                    curr_fit = x = np.NaN
                    while np.isnan(curr_fit):
                        x = es.ask(1, gradf=grad)[0]
                        curr_fit = worker_compute(x)
                    solutions.append(x)
                    f_values.append(curr_fit)
                es.tell(solutions, f_values)
                es.disp()
        print(es.result)

        model.set_parameters(es.result[0])
        return es.result[0]
Exemplo n.º 26
0
def run():
    train = 0

    names = [
        # 'pred_a', 'pred_b',             # 0.0
        # 'odds_a', 'odds_b',             # 2.1
        # 'bet_wnl_a', 'bet_wnl_b',       # 2.1
        # 'bet_ts_a', 'bet_ts_b',         # 1.5
        # 'bet_tmi_a', 'bet_tmi_b',       # 1.1
        # 'bet_tma_a', 'bet_tma_b',       # 0.9
        # 'bet_drs_a', 'bet_drs_b',       # 0.5
        'bet_sfc_a',
        'bet_sfc_b',  # -0.1

        # 'bet_spd_a', 'bet_spd_b',     # 0.8
        # 'bet_set_a', 'bet_set_b',       # 1.0
        # 'bet_gms_a', 'bet_gms_b',       # -0.2
        # 'bet_tie_a', 'bet_tie_b',       # 4.1
        # 'bet_ups_a', 'bet_ups_b',       # -0.2
        # 'bet_age_a', 'bet_age_b',       # -2.5
    ]
    params = [0, 0]
    bounds = [[-np.inf], [np.inf]]
    assert len(params) == len(names)
    assert len(params) == len(bounds)

    if train:
        sigma = 1
        opts = CMAOptions()
        # opts['tolx'] = 1E-2
        opts['bounds'] = bounds
        es = CMAEvolutionStrategy(params, sigma, inopts=opts)
        while not es.stop():
            solutions = es.ask()
            try:
                fitness = [main(x, train) for x in solutions]
            except ValueError as exc:
                print(str(exc))
                continue
            es.tell(solutions, fitness)
            es.disp()
            print(list(es.result[0]))
            print(list(es.result[5]))
        es.result_pretty()
        print(
            f'finished after {es.result[3]} evaluations and {es.result[4]} iterations'
        )
        print('')
        print('best')
        print(list(es.result[0]))
        print('')
        print(
            'xfavorite: distribution mean in "phenotype" space, to be considered as current best estimate of the optimum'
        )
        print(list(es.result[5]))

        # res = minimize(main, params, (train,), bounds=bounds)
        # print('')
        # print(f'{res.nit} iterations')
        # print(f'Success: {res.success} {res.message}')
        # print(f'Solution: {res.x}')
        # return

    else:
        main(params)
Exemplo n.º 27
0
    def run_optimization(self):
        """
        Runs optimization job
        :return:
        """
        simulation_name = self.parse_args.input
        population_size = self.parse_args.population_size

        self.optim_param_mgr = OptimizationParameterManager()
        optim_param_mgr = self.optim_param_mgr

        # optim_param_mgr.parse(args.params_file)
        optim_param_mgr.parse(self.parse_args.params_file)

        starting_params = optim_param_mgr.get_starting_points()
        print 'starting_params (mapped to [0,1])=', starting_params
        print 'remapped (true) starting params=', optim_param_mgr.params_from_0_1(
            starting_params)
        print 'dictionary of remapped parameters labeled by parameter name=', optim_param_mgr.param_from_0_1_dict(
            starting_params)

        print 'simulation_name=', simulation_name
        self.workload_dict = self.prepare_optimization_run(
            simulation_name=simulation_name)
        workload_dict = self.workload_dict

        print workload_dict

        std_dev = optim_param_mgr.std_dev
        default_bounds = optim_param_mgr.default_bounds

        optim = CMAEvolutionStrategy(starting_params, std_dev,
                                     {'bounds': list(default_bounds)})

        while not optim.stop():  # iterate
            # get candidate solutions
            # param_set_list = optim.ask(number=self.num_workers)
            # param_set_list = optim.ask(number=1)
            param_set_list = optim.ask(number=population_size)

            # set param_set_list for run_task to iterate over
            self.set_param_set_list(param_set_list=param_set_list)

            # #debug
            # return_result_vec = [self.fcn(optim_param_mgr.params_from_0_1(X)) for X in param_set_list]

            # evaluate  targert function values at the candidate solutions
            return_result_vec = np.array([], dtype=float)
            for param_set in self.param_generator(self.num_workers):
                print 'CURRENT PARAM SET=', param_set
                # distribution param_set to workers - run tasks spawns appropriate number of workers
                # given self.num_workers and the size of the param_set
                partial_return_result_vec = self.run_task(
                    workload_dict, param_set)

                return_result_vec = np.append(return_result_vec,
                                              partial_return_result_vec)

                print 'FINISHED PARAM_SET=', param_set

            optim.tell(param_set_list,
                       return_result_vec)  # do all the real "update" work
            optim.disp(20)  # display info every 20th iteration
            optim.logger.add()  # log another "data line"

        optimal_parameters = optim.result()[0]

        print('termination by', optim.stop())
        print('best f-value =', optim.result()[1])
        optimal_parameters_remapped = optim_param_mgr.params_from_0_1(
            optim.result()[0])
        print('best solution =', optimal_parameters_remapped)

        # print('best solution =', optim_param_mgr.params_from_0_1(optim.result()[0]))

        print optim_param_mgr.params_names

        self.save_optimal_parameters(optimal_parameters)
        self.save_optimal_simulation(optimal_parameters)
Exemplo n.º 28
0
This code majoyly compare our implementation of Cholesky CMAES with the cma from official package
Seems our implementation is better. Most probably we don't fiddle with Cov matrix frequently.
"""
#%%
cmasteps = 100
for unit_id in range(30):
    unit = ("alexnet", "fc6", unit_id)
    scorer.select_unit(unit)
    savedir = r"E:\OneDrive - Washington University in St. Louis\BigGAN_Optim_Tune\%s_%s_%d"%unit
    os.makedirs(savedir, exist_ok=True)
    for triali in range(5):
        fixnoise = 0.7 * truncated_noise_sample(1, 128)
        RND = np.random.randint(1E5)
        #%%
        methodlab = "CMA_prod"
        optim_noise = CMAEvolutionStrategy(fixnoise, 0.4)#0.4)  # 0.2
        optim_class = CMAEvolutionStrategy(128 * [0.0], 0.2)
        scores_all = []
        generations = []
        for i in tqdm.trange(cmasteps, desc="CMA steps"):
            class_codes = optim_class.ask()
            noise_codes = optim_noise.ask()
            codes_tsr = torch.from_numpy(np.array(class_codes)).float()
            noise_tsr = torch.from_numpy(np.array(noise_codes)).float()
            latent_code = torch.cat((noise_tsr, codes_tsr), dim=1).cuda()  # this initialize inner loop
            with torch.no_grad():
                imgs = G.visualize(latent_code).cpu()
            scores = scorer.score_tsr(imgs)
            print("step %d dsim %.3f (%.3f) (norm %.2f noise norm %.2f)" % (
                i, scores.mean(), scores.std(), codes_tsr.norm(dim=1).mean(), noise_tsr.norm(dim=1).mean()))
            optim_class.tell(class_codes, -scores)
Exemplo n.º 29
0
def run():
    train = 0

    names = [
        'bet_multi_param',
        'bet_tma_a',
        'bet_tma_b',
        'bet_lati_a',
        'bet_lati_b',
        'bet_tiew_a',
        'bet_tiew_b',

        # 'bet_upsr_a', 'bet_upsr_b',
        # 'bet_sfcw_a', 'bet_sfcw_b',
        # 'bet_wnll_a', 'bet_wnll_b',

        # 'bet_tier_a', 'bet_tier_b',
        # 'bet_upsl_a', 'bet_upsl_b',
        # 'bet_ts_a', 'bet_ts_b',

        # 'bet_wnlw_a', 'bet_wnlw_b',
        # 'bet_setw_a', 'bet_setw_b',
        # 'bet_setl_a', 'bet_setl_b',

        # 'bet_gms_a', 'bet_gms_b',
        # 'bet_drsl_a', 'bet_drsl_b',
        # 'bet_tmi_a', 'bet_tmi_b',

        # 'bet_wnlr_a', 'bet_wnlr_b',
        # 'bet_upsw_a', 'bet_upsw_b',
        # 'bet_drs_a', 'bet_drs_b',

        # 'bet_setr_a', 'bet_setr_b',
        # 'bet_drsw_a', 'bet_drsw_b',
        # 'bet_tiel_a', 'bet_tiel_b',

        # 'bet_age_a', 'bet_age_b',
        # 'bet_spd_a', 'bet_spd_b',

        # 'bet_sfcr_a', 'bet_sfcr_b',
    ]
    tolx = 10000  # more higher then longer time
    params = [-14, 0, 0, 0, 0, 0, 0]
    bounds = [[-np.inf], [np.inf]]
    assert len(params) == len(names)
    # assert len(params) == len(bounds)

    if train:
        time_start = time()
        mins = 60 * 4
        sigma = 1
        opts = CMAOptions()
        opts['bounds'] = bounds
        es = CMAEvolutionStrategy(params, sigma, inopts=opts)
        while not es.stop():
            solutions = es.ask()
            try:
                fitness = [main(x, train) for x in solutions]
            except ValueError as exc:
                print(str(exc))
                continue
            es.tell(solutions, fitness)
            es.disp()
            # print(list(es.result[0]))
            print(f'tolx={es.opts["tolx"]:.3f}  sol={list(es.result[5])}')
            es.opts['tolx'] = es.result[3] / tolx
            if time() - time_start > 60 * mins:
                print(f'{mins}min limit reached')
                break
        es.result_pretty()
        print(
            f'finished after {es.result[3]} evaluations and {es.result[4]} iterations'
        )
        # print('')
        # print('best')
        # print(list(es.result[0]))
        print('')
        print(
            'xfavorite: distribution mean in "phenotype" space, to be considered as current best estimate of the optimum'
        )
        print(list(es.result[5]))

        # pmin = -20
        # pmax = 20
        # step = (pmax - pmin) / 10
        # rranges = [
        #     slice(pmin, pmax, step),
        #     slice(pmin, pmax, step),
        # ]
        # res = optimize.brute(main, rranges, (train,), finish=None)
        # print(res)
        # return

        # res = minimize(main, params, (train,), bounds=bounds)
        # print('')
        # print(f'{res.nit} iterations')
        # print(f'Success: {res.success} {res.message}')
        # print(f'Solution: {res.x}')
        # return

    else:
        main(params)
Exemplo n.º 30
0
        rand_network.eval()

        #the model will not be trained, so we only need to save it once for reproduceability
        torch.save(rand_network.state_dict(), logdir + "/model")

        #create virtual display
        if opt.vdisplay:
            from xvfbwrapper import Xvfb
            xvfb = Xvfb()
            xvfb.start()

        if opt.multiproc:
            ray.init()

        #TODO:revisit sigma
        es = CMAEvolutionStrategy(torch.zeros(n_features), sigma0=5.0)

        i = 0
        n_generation = 10000
        while not es.stop() and i < n_generation:
            start_time = time.time()
            solutions = es.ask()

            #calculate fitness
            objectives = []
            bcs = []

            if opt.multiproc:
                futures = [
                    multi_fit_func.remote(torch.from_numpy(solution).float(),
                                          opt.device,