def __init__(self, config, w_num, popsize, reeval_num0):
     # initialize CMA-ES with all mean zeros
     cma_inopts = {}
     if config['seed'] == None:
         cma_inopts['seed'] = int.from_bytes(os.urandom(4), byteorder='big', signed=False) // 2
     else:
         cma_inopts['seed'] = config['seed'] + 1 # cma treats 0 as a random seed
     cma_inopts['popsize'] = popsize
     cma_inopts['bounds'] = [-1, 1]
     init = [0] * w_num
     self.es = cma.CMAEvolutionStrategy(init, sigma0=config['cl_cmaes_sigma0'], inopts=cma_inopts)
     self.nh = MyNoiseHandler(self.es.N, maxevals=[0, reeval_num0, 5.01], parallel=True, aggregate=np.mean)
     self.logger = cma.CMADataLogger().register(self.es)
Exemplo n.º 2
0
def load_best_params_from_log():
    log_path = RUNS_DIR / 'outcmaes'
    logger = cma.CMADataLogger(str(log_path)).load()
    best_function_val_at_each_iteration = logger.xrecent[:, 4]

    arg_min = np.argmin(best_function_val_at_each_iteration)
    min_val = best_function_val_at_each_iteration[arg_min]

    print('best_frangi_cca_obj_val',
          np.min(best_function_val_at_each_iteration))
    print('best_frangi_cca_f1',
          1 - np.min(best_function_val_at_each_iteration))

    # param_names = ['Frangi sigma', 'beta1', 'beta2',
    #                'Frangi threshold', 'small object threshold']
    best_params, after_scaling = cmaes_utils.get_best_params(logger)

    print('best_params', best_params)
    print('after scaling', after_scaling)

    # after_scaling (best_params found)
    # [2.00000000e+00, 9.21819899e-02, 1.05175985e+00, 3.75864362e-05, 1.92000000e+02]
    return after_scaling
Exemplo n.º 3
0
    #we need to save solver object
    #we need to solve current fitness list and
    #we need to save logs
    #name new file in terms of current gen and so on..
    #add list of solvers..?


env = gym.make('CarRacing-v0')
env.reset()  #adding these two to test behaviour of background visualization
env.render()  #could it speed up?

solver = cma.CMAEvolutionStrategy(99 * [0], 0.1, {
    'popsize': POPULATION_SIZE,
})  #876 total parameters (#99 in VAE model)

logger_res = cma.CMADataLogger().register(solver)  #Store and plot outside

best_par_score = [
]  #list with best parameters and scores each round (solver format)
best_par_score2 = []  #(my format)
score_point_gen = []
generation = 0

while True:
    generation += 1
    print('Generation: ', generation)

    solutions = solver.ask()
    #fitness_list = np.zeros(len(solutions)) #I think it is accepted
    fitness_list = []
Exemplo n.º 4
0
def run_cma_experiment_parallel(experiment_config):
    import cma
    import time
    import multiprocessing as mp

    num_of_parallel = experiment_config.NUM_OF_PARALLEL_WEBOTS
    first_sample = np.random.uniform(low=experiment_config.WEIGHTS_MIN,
                                     high=experiment_config.WEIGHTS_MAX,
                                     size=(1, experiment_config.NUM_WEIGHTS))
    first_sample = first_sample.tolist()[0]
    if experiment_config.POP_SIZE is None:
        es = cma.CMAEvolutionStrategy(first_sample,
                                      experiment_config.INITIAL_SIGMA,
                                      {'maxiter': experiment_config.MAX_ITERATIONS})
    else:
        es = cma.CMAEvolutionStrategy(first_sample,
                                      experiment_config.INITIAL_SIGMA,
                                      {'maxiter': experiment_config.MAX_ITERATIONS,
                                       'popsize': experiment_config.POP_SIZE})
    if num_of_parallel > 0:
        pool_size = num_of_parallel
    else:
        pool_size = 1

    SimLogger = cma.CMADataLogger(experiment_config.LOG_FILE_PREFIX).register(es)
    pool = mp.Pool(len(experiment_config.sim_config_arr))
    sim_count = 0
    simulator_instance_arr = []
    while not es.stop():
        if sim_count == 0:
            simulator_instance_arr = []
            for sim_config in experiment_config.sim_config_arr:
                simulator_instance_arr.append(start_webots(sim_config.world_file, True))
                sim_config.simulator_instance_pid = simulator_instance_arr[-1].pid
        X = es.ask()
        time.sleep(0.2 * pool_size)  # Wait 0.2 seconds per webots instance to run
        if pool_size < es.popsize:
            f_values = [0.0] * es.popsize
            cur_index = 0
            last_index = cur_index + pool_size
            while cur_index < es.popsize:
                last_index = min(last_index, es.popsize)
                time.sleep(0.05 * pool_size)  # Wait additional 0.05 seconds per webots instance to revert
                f_values[cur_index:last_index] = pool.map_async(WithExtraArgs(experiment_config.SIMULATION_RUN_FUNC,
                                                                              X[cur_index:last_index],
                                                                              experiment_config.sim_config_arr),
                                                                range(last_index - cur_index)).get()
                cur_index = last_index
                last_index = cur_index + pool_size
        else:
            time.sleep(0.2 * pool_size)  # Wait 0.2 seconds per webots instance to revert
            f_values = pool.map_async(WithExtraArgs(experiment_config.SIMULATION_RUN_FUNC,
                                                    X,
                                                    experiment_config.sim_config_arr),
                                      range(len(X))).get()
        sim_count += es.popsize
        # We regularly kill and run webots because of webots memory leak problem (Webots: 8.5.3)
        if sim_count >= experiment_config.WEBOT_RESTART_THRESHOLD:
            print('Simulation count: {}... Restarting Webots instances.'.format(sim_config))
            for simulator_instance in simulator_instance_arr:
                kill_webots_pid(simulator_instance.pid)
            simulator_instance_arr = []
            sim_count = 0
            time.sleep(0.5)  # Wait for all webots instances to die
        es.tell(X, f_values)
        es.disp()
        SimLogger.add()
        try:
            SimLogger.add(es, modulo=bool(SimLogger.modulo))
        except:
            None
        print('Iteration {}/{} is complete. Best value: {}.'.format(es.countiter,
                                                                    experiment_config.MAX_ITERATIONS,
                                                                    es.best.f))

    pool.close()
    pool.join()
    for simulator_instance in simulator_instance_arr:
        kill_webots_pid(simulator_instance.pid)
    res = es.result()
    experiment_config.cma_results = res
    results_file_name = experiment_config.LOG_FILE_PREFIX + 'results.dat'
    save_results_to_file(experiment_config, results_file_name)
    print('Experiment is complete. Best value: {}.'.format(res[1]))
    print('Results saved to : {}'.format(results_file_name))
Exemplo n.º 5
0

b = Bohachevsky()
# Dimension and bounds of the function
bounds = b.get_meta_information()['bounds']
dimensions = len(bounds)
lower = np.array([i[0] for i in bounds])
upper = np.array([i[1] for i in bounds])

start_point = (upper - lower) / 2

# Evolution Strategy
es = cma.CMAEvolutionStrategy(start_point, 0.6, {
    'bounds': [lower, upper],
    "maxfevals": 200
})
logger = cma.CMADataLogger().register(es)

es.optimize(wrapper, 200)

X = X[:200]
y = y[:200]

fvals = np.array(y)
# Saving into the csv file with a paramater given from the user
with open(
        '/home/numair/Pictures/CMA-RUNS/cmaExperiments/Bohachevsky/' +
        str(sys.argv[1]), 'a') as csvfile:
    writer = csv.writer(csvfile, delimiter='\n')
    writer.writerow(fvals)
Exemplo n.º 6
0
 def __init__(self,
              ff,
              sigma=0.2,
              initials=None,
              maxiter=2000,
              cmaopts={},
              restart=None,
              runtimelimit=None):
     optimizer.__init__(self, ff)
     try:
         import cma
     except:
         raise ImportError(
             "CMA not available --> Install via 'pip install cma'")
     assert string.split(cma.__version__,
                         '.')[0] == '2', 'Outdated cma version installed'
     if initials is not None:
         assert len(initials) == len(
             ff.initials
         ), 'Number of intial values array does not match number of variables.'
     else:
         initials = ff.initials
     self.dim = len(initials)
     options = {'bounds': ff.get_bounds(), 'maxiter': maxiter}
     options.update(cmaopts)
     self.popsize = 0
     if self.is_master:
         self.ff.pprint(41 * '-' + 'Optimization loop' + 42 * '-')
         if restart is not None:
             self.es = pickle.load(open(os.path.join('..', restart), 'rb'))
             # check if appropriate check if range is used
             assert len(initials) == len(self.es.ask()[0])
             self.es.opts.set({'maxiter': maxiter})
         else:
             self.es = cma.CMAEvolutionStrategy(initials, sigma, options)
         self.popsize = len(self.es.ask())
         self.logger = cma.CMADataLogger().register(self.es)
     self.popsize = self.mpi_comm.bcast(self.popsize, root=0)
     # if pobj > 1 a new communicator has to be created only involving the
     # master nodes of every ffgen instance, using the Group feature of MPI
     self.idx = []
     if ff.pobj > 1:
         assert self.mpi_size % ff.pobj == 0
         mpi_group = self.mpi_comm.Get_group()
         opt_group = mpi_group.Incl(range(self.mpi_size)[::ff.pobj])
         self.opt_comm = self.mpi_comm.Create(opt_group)
     else:
         self.opt_comm = self.mpi_comm
     if self.in_optgroup:
         self.opt_size = self.opt_comm.Get_size()
         self.opt_rank = self.opt_comm.Get_rank()
         assert self.opt_size <= self.popsize
         assert self.popsize % self.opt_size == 0
         #        self.popsize = self.mpi_comm.bcast(self.popsize, root = 0)
         self.nindperrank = self.popsize / self.opt_size
         self.idx = np.array(
             range(self.opt_rank * self.nindperrank,
                   (self.opt_rank + 1) * self.nindperrank))
         # self.idx has to be communicated to ALL threads vial ff.local_comm
     self.idx = self.ff.local_comm.bcast(self.idx, root=0)
     # handle runtimelimit
     if runtimelimit != None:
         self.runtimelimit = np.sum(
             np.array(map(int, string.split(runtimelimit, ":"))) *
             np.array([3600, 60, 1]))
     else:
         self.runtimelimit = np.inf
     return
Exemplo n.º 7
0
    """
    file_to_store = (solver, logger, (solutions, fitness_list, list_points,
                                      generation))
    with open(
            'evo_vae_{0}_pop_size_{1}_length_{2}_avg_rollout.pkl'.format(
                args.pop_size, args.max_steps, args.num_rolls), 'wb') as f:
        pickle.dump(file_to_store, f)


parameters = controllers[0].parameters()

solver = cma.CMAEvolutionStrategy(
    torch.cat([p.detach().view(-1) for p in parameters], dim=0).cpu().numpy(),
    0.1, {'popsize': args.pop_size})  #876 total parameters (#99 in VAE model)

logger_res = cma.CMADataLogger().register(solver)
score_point_gen = []

# list with best parameters and scores each round (solver format)
best_par_score = []
#(my format)
best_par_score2 = []
generation = 0


def rollout(params, controller):

    envir = gym.make('CarRacing-v0')
    obs = envir.reset()

    step_counter = 0
Exemplo n.º 8
0
        return errors

    return res


## Optimalisation
es = cma.CMAEvolutionStrategy(
    numrunways * [
        airport.centroid.coords[0][0], airport.centroid.coords[0][1],
        minrunwaylength, minrunwaywidth * 100, 0
    ] + [
        airport.centroid.coords[0][0], airport.centroid.coords[0][1],
        1000 * 10, 1000 * 10, 0
    ], 1000)

logger = cma.CMADataLogger().register(es)

## Draw in in difrent filles
if printpreviousresult:
    result = printpreviousresult
else:
    es.optimize(fitness, logger=logger)
    es.result_pretty()
    result = es.result()[0]

print('Result: [' + ', '.join(map(str, result)) + ']')

##uit result haal je plaats voor gate.
coords = result[numrunways * 5:]

x = coords[0]
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 24 16:11:53 2018

@author: ivan
"""
import cma

logger2 = cma.CMADataLogger('outcmaes').load()
logger2.plot()
logger2.disp()