def main(): logging.basicConfig(level=logging.INFO, format="%(asctime)s (%(levelname)s) %(message)s") tf.random.set_seed(123) max_epochs = 500 log_dir = 'logs/griewank_function' summary_writer = tf.summary.create_file_writer(log_dir) def logging_function(cma, logger): fitness = cma.best_fitness() # Write best fitness to the tensorboard summary log with summary_writer.as_default(): tf.summary.scalar('fitness', fitness, step=cma.generation) # Periodically log progress if cma.generation % 10 == 0: logger.info(f'Generation {cma.generation} - fitness {fitness}') if cma.termination_criterion_met or cma.generation == max_epochs: sol = cma.best_solution() logger.info(f'Final solution at gen {cma.generation}: {sol} (fitness: {fitness})') cma = CMA( initial_solution=[100.] * 10, initial_step_size=600., fitness_function=fitness_fn, enforce_bounds=[[-600, 600]] * 10, callback_function=logging_function, ) cma.search(max_epochs)
def cma_validate(planner_fn): found_error = False failing_test = None def f(env_parameters, render=None): nonlocal found_error nonlocal failing_test if found_error: return 0 # Skip doing these extra evals; we already found a bug error, success, num_expansions, planning_time = planner_fn( env_parameters, render) if error: found_error = True failing_test = env_parameters return -num_expansions max_chains = 3 for i in range(max_chains): initial_mean = random_params() initial_sigma = 2.0 initial_cov = np.eye(len(initial_mean)) opzer = CMA(f, initial_mean, initial_sigma, initial_cov) max_iters = 50 min_val = 0 convergence_patience = 5 remaining_patience = convergence_patience i = 0 while True: print("Iteration", i, "Patience", remaining_patience) val = f(opzer.mean[:, 0], render=True) if found_error: return False, failing_test if (val < min_val): remaining_patience = convergence_patience min_val = val else: remaining_patience = remaining_patience - 1 if remaining_patience == 0: break if i > max_iters: break i += 1 opzer.iter() return True, None
def train_cma(X_source, Y_source, X_aux_list, Y_aux_list, X_target, Y_target): X_train = np.vstack([X_source] + X_aux_list) Y_train = np.hstack([Y_source] + Y_aux_list) print('shapes') print(X_train.shape) print(Y_train.shape) print(X_target.shape) print(Y_target.shape) cma = CMA(dims=12, block=50) Y_pred = cma.cma_fit_predict(X_train, X_target, Y_train, Y_target) evaluate(Y_pred, Y_target) df = pd.read_csv('../rossmann/train_4.csv') df['y_pred'] = pd.DataFrame(Y_pred, index=df.index) df.to_csv('../rossmann/train_p.csv', index=False)
def train_cma(X_source, Y_source, X_aux_list, Y_aux_list, X_target, Y_target): X_train = np.vstack([X_source] + X_aux_list) Y_train = np.hstack([Y_source] + Y_aux_list) print('shapes') print(X_train.shape) print(Y_train.shape) print(X_target.shape) print(Y_target.shape) cma = CMA(dims=7, block=200) Y_pred = cma.cma_fit_predict(X_train, X_target, Y_train, Y_target) #print(classification_report(Y_target, Yp)) print((abs(Y_pred - Y_target)).mean()) print((abs(Y_pred - Y_target)).max()) print((abs(Y_pred - Y_target)).min()) print((abs(Y_pred - Y_target)).std())
def run_benchmark(num_tests, population_size, epochs, D): print("Starting Benchmark...") functions = [ Ackley(D), Rastrigin(D), Rosenbrock(D), Griewank(D), Schwefel(D) ] results = {f: {a: [] for a in algorithms_names} for f in function_names} for fitness in functions: for i in range(num_tests): func_name = fitness.__class__.__name__ results[func_name]['PSO'].append( fitness(PSO(epochs, population_size, fitness))) results[func_name]['BBPSO'].append( fitness(BBPSO(epochs, population_size, fitness))) results[func_name]['GA'].append( fitness(GA(epochs, population_size, fitness))) results[func_name]['CMA-ES'].append( fitness(CMA(epochs, population_size, fitness))) return results
]) env = gym.make('SpaceInvaders-v4') env = gym_wrappers.StepReward(env, step_reward=1) policy_nets = [] cma_file = config.basepath( ) / 'SpaceInvaders-v4' / 'policy_runs' / '603' / 'cma_8' if cma_file.exists(): with Path(cma_file).open('rb') as f: cma = pickle.load(f) else: cma = CMA() episode_steps = [] sample_size = 1000 epochs = 200 rollouts = 1 z_size = 32 viewers = False if viewers: visuals.register_forward_hook(view_decode) visuals.register_forward_hook(view_image) for net in range(sample_size):
population_size = 40 path = "Submissions/original.csv" turb_coords = evaluator.getTurbLoc(path) def logging_function(cma, logger): fitness = cma.best_fitness() solution = cma.best_solution() print("Generation:", cma.generation + 1) print("Generation Best -> Mean_AEP: {}\t Penalty: {}".format( fitness, get_constraints_penalty(solution.reshape((50, 2)) * 4000, 10, 10))) cma = CMA(initial_solution=coords2param(turb_coords), initial_step_size=0.02, fitness_function=evaluate_params, population_size=population_size, callback_function=logging_function) cma.search(num_gen) # es = cma.CMAEvolutionStrategy(coords2param(turb_coords), 0.02) # for gen in range(num_gen): # solutions = es.ask(population_size) # pool = mp.Pool(processes=4) # returns = pool.starmap(evaluate_coords, [[param2coords(p),] for p in solutions]) # pool.close() # returns = np.array(returns) # function_values = -returns[:, 0] # cma-es minimizes the objective