def solve(seed): print(f"Starting seed {seed}") folder = os.path.join( os.path.dirname( os.path.dirname(os.path.dirname(os.path.realpath(__file__)))), "results") start = time.time() problem = PenaltyProblem(0.1) method = CMAES(integer_variables=list(range(problem.n_var)), display=MyDisplay(), callback=MyCallback(), parallelize=True) res = minimize(problem, method, termination=('n_eval', 200000), seed=seed, verbose=True) end = time.time() elapsed = end - start np.savetxt(os.path.join(folder, f"cmaes_{seed}.x"), res.pop.get("X").astype(np.int)) np.savetxt(os.path.join(folder, f"cmaes_{seed}.f"), res.pop.get("_F")) np.savetxt(os.path.join(folder, f"cmaes_{seed}.g"), res.pop.get("_G")) print(f"Finished seed {seed} - runtime: {elapsed}")
def optimization(problem, x, weights, queue): ''' Parallel worker for single-objective CMA-ES optimization ''' evaluator = ScalarizedEvaluator(decomposition=Tchebicheff(), weights=weights) res = minimize(problem, CMAES(x), evaluator=evaluator) queue.put([res.X[0], res.F[0]])
def runCMAES(aPopulation, aGeneration, anInitialGuess): # def runCMAES(anInitialGuess): """CMA-ES algorithm""" initial_guess = strArrayToFloatArray(anInitialGuess, 'PARAMS', 'comma') # problems to be solved, problem = objectiveFunction() if (aPopulation != "NONE" and aGeneration != "NONE"): pop_size = int(aPopulation) gen_size = int(aGeneration) elif aPopulation != "NONE": pop_size = int(aPopulation) else: # number of individuals = 4+int(3*np.log(N)), N is number of input parameters pop_size = 4 + int(3 * np.log(total_number_of_params)) # use CMA-ES with no fixed number of individuals and generations algorithm = CMAES(popsize=pop_size, sigma=0.05, restarts=2, maxfevals=np.inf, tolfun=1e-6, restart_from_best=True, incpopsize=2, bipop=True) if img_scale != 0: pop_size = int(pop_size / 2**img_scale) gen_size = int(gen_size / 2**img_scale) if img_scale == 4: # use CMA-ES algorithm = CMAES(popsize=pop_size, CMA_stds=0.1) else: if initial_guess.size != 0: initial_guess[1] /= 1000 for i in range(22): initial_guess[i + 2] /= 100 # initial guess algorithm = CMAES(x0=initial_guess, popsize=pop_size, CMA_stds=0.1) else: # no initial guess algorithm = CMAES(popsize=pop_size, sigma=0.05, restarts=2, maxfevals=np.inf, tolfun=1e-6, restart_from_best=True, incpopsize=2, bipop=True) # record time start = time.time() if aGeneration != "NONE": # set up the optimiser res = minimize(problem, algorithm, ('n_iters', gen_size), verbose=False) else: # ('n_evals', 80000), seed = 10 + total_number_of_runs res = minimize(problem, algorithm, seed=seed, verbose=False) end = time.time() total_time = end - start target_image = target resX = np.array(res.X) resX[1] *= 1000 for j in range(22): resX[j + 2] *= 100 if args.add_masks: target_image = (target_image - target_image.mean()) / target_image.std() # save results saveImageAndCSV(full_path, target_image, resX, res.F, single_metric, total_time, img_scale) # collect all variables global nb_individuals, nb_generations, runtime, metric_value, parameters if aPopulation != "NONE": nb_individuals = len(res.pop) else: nb_individuals = pop_size nb_generations = call_count runtime = total_time metric_value = computeAllMetricsValue(target, resX, target_full_reso, img_scale) for x in range(len(resX)): parameters.append(resX[x])
# ----------------------------------------------------------------------------- # Opimization loop and file saving for itr in range(repeat): problem = FunctionalProblem(N + s - 2 + 1, objs, constr_ieq=constr_ieq, xl=np.concatenate( (-np.ones(N), 0.125 * np.ones(s - 2), 1 * np.ones(1))), xu=np.concatenate( (np.ones(N), 12.5 * np.ones(s - 2), 30 * np.ones(1)))) # sigma = 0.001 # increase population size for each itr algorithm = CMAES( #x0 = x0, sigma=10 * 10**(-3), restarts=2) r = minimize(problem, algorithm, termination, save_history=True, verbose=True) n_evals = [] # corresponding number of function evaluations y = [] # the objective space values in each generation cv = [] # constraint violation in each generation X = [] filename = '{0}s_{1}d_job_{2}_{3}T_invG_kappa_0.01.csv'.format( s, d, job, inv_G / T) # iterate over the deepcopies of algorithms for algorithm in r.history:
from pymoo.algorithms.so_cmaes import CMAES from pymoo.factory import get_problem from pymoo.optimize import minimize problem = get_problem("sphere") algorithm = CMAES() res = minimize(problem, algorithm, seed=1, verbose=False) print("Best solution found: \nX = %s\nF = %s" % (res.X, res.F))
def cmaes(problem, x): solver = CMAES(x0=x, tolfun=1e-11, tolx=1e-3, restarts=0) solver.initialize(problem) solver.next() return solver