def test_crossover(self): for crossover in ['real_de', 'real_sbx', 'real_exp']: print(crossover) method = GA(pop_size=20, crossover=get_crossover(crossover, prob=0.95)) minimize(get_problem("sphere"), method, ("n_gen", 20)) for crossover in [ 'bin_ux', 'bin_hux', 'bin_one_point', 'bin_two_point' ]: print(crossover) method = NSGA2(pop_size=20, crossover=get_crossover(crossover, prob=0.95)) minimize(get_problem("zdt5"), method, ("n_gen", 20))
def compute_mean_HV(algorithm, problems, iterations): #fixed problems, can pass later hypervolume_scores = np.zeros([len(problems), iterations]) for i in range(iterations): results = [] ind = 0 for j in problems: problem = get_problem(j[0]) res = minimize(problem, algorithm, ('n_gen', 5), seed=i, verbose=False) if res.F is not None: hv = get_performance_indicator("hv", ref_point=j[1]).calc(res.F) else: hv = 0 results.append((j[0], res, res.F, hv)) hypervolume_scores[ind, i] = hv ind = ind + 1 mean_hv = np.mean(hypervolume_scores, axis=1) std = np.std(hypervolume_scores, axis=1) output = [] for i in range(len(problems)): output.append([problems[i][0], mean_hv[i], std[i]]) path = 'results/' + algorithm.__module__ + '.csv' pd.DataFrame(np.array(output)).to_csv(path, header=('function', 'mean HV', 'std'), index=False) print("all runs:", hypervolume_scores) return (hypervolume_scores, print("Output written to " + path + '\n'))
def test_against_scipy(): problem = get_problem("rosenbrock") problem.xl = None problem.xu = None x0 = np.array([0.5, 0.5]) hist_scipy = [] def fun(x): hist_scipy.append(x) return problem.evaluate(x) scipy_minimize(fun, x0, method='Nelder-Mead') hist_scipy = np.array(hist_scipy) hist = [] def callback(x, _): if x.shape == 2: hist.extend(x) else: hist.append(x) problem.callback = callback minimize( problem, NelderMead(x0=x0, max_restarts=0, termination=get_termination("n_eval", len(hist_scipy)))) hist = np.row_stack(hist)[:len(hist_scipy)] np.testing.assert_allclose(hist, hist_scipy, rtol=1e-04)
def load_problem(name): """ Load a pre-defined benchmark problem. Arguments: name (str): Name of the desired problem. Returns: problem (): Benchmark problem. range_in (np.array): Input parameter allowable ranges. dim_in (int): Number of input dimensions. dim_out (int): Number of output dimensions. n_constr (int): Number of constraints. """ # Own defined functions if name in problems.keys(): problem = problems[name]() # Functions from Pymoo else: problem = get_problem(name) # Process it range_in = np.stack((problem.xl,problem.xu),axis=1) dim_in = problem.n_var n_constr = problem.n_constr dim_out = problem.n_obj + problem.n_constr return problem, range_in, dim_in, dim_out, n_constr
def test_problems(name, params): n_obj, n_var, k = params problem = get_problem(name, n_var, n_obj, k) X, F, CV = load(name.upper(), suffix=["WFG", "%sobj" % n_obj]) if F is None: print("Warning: No correctness check for %s" % name) return _F, _G, _CV = problem.evaluate(X, return_values_of=["F", "G", "CV"]) if problem.n_obj == 1: F = F[:, None] np.testing.assert_allclose(_F, F) # this is not tested automatically try: optprobs_F = [] for x in X: from optproblems.base import Individual ind = Individual(phenome=x) from_optproblems(problem).evaluate(ind) optprobs_F.append(ind.objective_values) optprobs_F = np.array(optprobs_F) np.testing.assert_allclose(_F, optprobs_F) except: print("NOT MATCHING")
def test_no_bounds(): problem = get_problem("sphere", n_var=2) problem.xl = None problem.xu = None method = NelderMead(x0=np.array([1, 1]), max_restarts=0) minimize(problem, method, verbose=False) assert True
def test_min_vs_loop_vs_infill(): problem = get_problem("zdt1") n_gen = 30 algorithm = NSGA2(pop_size=100) min_res = minimize(problem, algorithm, ('n_gen', n_gen), seed=1) algorithm = NSGA2(pop_size=100) algorithm.setup(problem, ('n_gen', n_gen), seed=1) while algorithm.has_next(): algorithm.next() algorithm.finalize() loop_res = algorithm.result() np.testing.assert_allclose(min_res.X, loop_res.X) algorithm = NSGA2(pop_size=100) algorithm.setup(problem, ('n_gen', n_gen), seed=1) while algorithm.has_next(): infills = algorithm.infill() Evaluator().eval(problem, infills) algorithm.advance(infills=infills) algorithm.finalize() infill_res = algorithm.result() np.testing.assert_allclose(min_res.X, infill_res.X)
def test_against_scipy(self): problem = get_problem("rosenbrock") problem.xl = None problem.xu = None x0 = np.array([0.5, 0.5]) hist_scipy = [] def fun(x): hist_scipy.append(x) return problem.evaluate(x) scipy_minimize(fun, x0, method='Nelder-Mead') hist_scipy = np.array(hist_scipy) hist = [] def callback(x): if x.shape == 2: hist.extend(x) else: hist.append(x) problem.callback = callback minimize( problem, nelder_mead(x0=x0, max_restarts=0, termination=get_termination("n_eval", len(hist_scipy)))) hist = np.row_stack(hist)[:len(hist_scipy)] self.assertTrue(np.all(hist - hist_scipy < 1e-7))
def dtlz_benchmark(prob_name, n_var = 0, n_obj = 0, xu = 0, xl = 0): problem = get_problem(prob_name, n_var, n_obj) # set optimization problem problem.n_var = n_var problem.n_obj = n_obj return problem.evaluate
def test_mutation_bin(name): mut = get_mutation(name) method = NSGA2(pop_size=20, crossover=get_crossover('bin_ux'), mutation=mut) minimize(get_problem("zdt5"), method, ("n_gen", 20)) assert True
def E_SMS_EGO_mean_HV(problems, iterations): hypervolume_scores = np.zeros([len(problems), iterations]) for i in range(4, iterations): global results results = [] ind = 0 for j in problems: problem = get_problem(j[0]) res = E_SMS_EGO(problem, eval_budget=25, time_budget=2500) if np.array(res[3]) is not None: hv = get_performance_indicator("hv", ref_point=j[1]).calc( np.array(res[1])) else: hv = 0 results.append( (j[0], res[0].tolist(), res[1].tolist(), hv, j[1].tolist())) respath = 'results/E_SMS_EGO_run_' + str(i) + '.csv' pd.DataFrame(results).to_csv(respath, header=('problem', 'x', 'y', 'hv', 'ref'), index=False) hypervolume_scores[ind, i] = hv ind = ind + 1 mean_hv = np.mean(hypervolume_scores, axis=1) std = np.std(hypervolume_scores, axis=1) output = [] for i in range(len(problems)): output.append([problems[i][0], mean_hv[i], std[i]]) path = 'results/E_SMS_EGO_res.csv' pd.DataFrame(np.array(output)).to_csv(path, header=('function', 'mean HV', 'std'), index=False) print("all runs:", hypervolume_scores) return (print("Output written to " + path + '\n'))
def test_mw(name): problem = get_problem(name) X, F, CV = load(name.upper(), suffix=["MW"]) _F, _CV = problem.evaluate(X, return_values_of=["F", "CV"]) np.testing.assert_allclose(_F, F) np.testing.assert_allclose(_CV[:, 0], CV)
def test_kktpm_correctness(str_problem, params): problem = BoundariesAsConstraints( AutomaticDifferentiation(get_problem(str_problem))) # problem = AutomaticDifferentiation(BoundariesAsConstraints(get_problem(str_problem))) def load_file(f): return np.loadtxt( path_to_test_resource("kktpm", "%s_%s.txt" % (str_problem, f))) X = load_file("x") _F, _G, _dF, _dG = problem.evaluate( X, return_values_of=["F", "G", "dF", "dG"]) # the gradient needs to be implemented again! assert _dF is not None and _dG is not None F, G, dF, dG = load_file("f"), load_file("g"), load_file("df"), load_file( "dg") dF = dF.reshape(_dF.shape) np.testing.assert_almost_equal(F, _F, decimal=5) np.testing.assert_almost_equal(dF, _dF, decimal=5) if problem.n_constr > 0: G = G[:, :problem.n_constr] dG = dG[:, :problem.n_constr * problem.n_var].reshape(_dG.shape) np.testing.assert_almost_equal(G, _G, decimal=5) np.testing.assert_almost_equal(dG, _dG, decimal=5) # indices = np.random.permutation(X.shape[0])[:100] indices = np.arange(X.shape[0]) # load the correct results kktpm = load_file("kktpm")[indices] # calculate the KKTPM measure # _kktpm, _ = KKTPM(var_bounds_as_constraints=True).calc(np.array([[4.8, 3.0]]), problem, **params) # _kktpm, _ = KKTPM(var_bounds_as_constraints=True).calc(X[[55]], problem, rho=0, **params) _kktpm = KKTPM().calc(X[indices], problem, **params) error = np.abs(_kktpm[:, 0] - kktpm) for i in range(len(error)): if error[i] > 0.0001: print("Error for ", str_problem) print("index: ", i) print("Error: ", error[i]) print("X", ",".join(np.char.mod('%f', X[i]))) print("Python: ", _kktpm[i]) print("Correct: ", kktpm[i]) # os._exit(1) # make sure the results are almost equal np.testing.assert_almost_equal(kktpm, _kktpm[:, 0], decimal=4) print(str_problem, error.mean())
def test_same_seed_same_result(self): problem = get_problem("zdt3") algorithm = nsga2(pop_size=100, elimate_duplicates=True) res1 = minimize(problem, algorithm, ('n_gen', 20), seed=1) res2 = minimize(problem, algorithm, ('n_gen', 20), seed=1) self.assertEqual(res1.X.shape, res2.X.shape) self.assertTrue(np.all(np.allclose(res1.X, res2.X)))
def test_dascomp(i, j): name, difficulty = f"dascmop{i}", DIFFICULTIES[j] problem = get_problem(name, difficulty) X, F, CV = load(name.upper(), suffix=["DASCMOP", str(j)]) _F, _CV = problem.evaluate(X, return_values_of=["F", "CV"]) np.testing.assert_allclose(_F, F) np.testing.assert_allclose(-_CV[:, 0], CV)
def test_same_seed_same_result(): problem = get_problem("zdt3") algorithm = NSGA2(pop_size=100, eliminate_duplicates=True) res1 = minimize(problem, algorithm, ('n_gen', 20), seed=1) np.random.seed(200) res2 = minimize(problem, algorithm, ('n_gen', 20), seed=1) np.testing.assert_almost_equal(res1.X, res2.X) np.testing.assert_almost_equal(res1.F, res2.F)
def test_single_crossover(self): problem = get_problem('zdt1') sampling = get_sampling('real_random') pop = sampling.do(problem, n_samples=3) crossover = get_crossover('real_sbx', eta=20) crossover.do(problem, pop[0], pop[1]) crossover.do(problem, pop, np.array([[0, 1]])) crossover = get_crossover('real_de') crossover.do(problem, pop[0], pop[1], pop[2]) crossover.do(problem, pop, np.array([[0, 1, 2]]))
def demo_hello_world(): problem = get_problem("zdt1") algorithm = NSGA2(pop_size=10) res = minimize(problem, algorithm, ('n_gen', 10), seed=1, verbose=False) plot = Scatter() plot.add(problem.pareto_front(), plot_type="line", color="black", alpha=0.7) plot.add(res.F, color="red") plot.show()
def test_problems(self): problems = [ get_problem("dc1dtlz1"), get_problem("dc1dtlz3"), get_problem("dc2dtlz1"), get_problem("dc2dtlz3"), get_problem("dc3dtlz1"), get_problem("dc3dtlz3"), ] for problem in problems: name = str(problem.__class__.__name__) print("Testing: " + name) X, F, CV = load(name) _F, _CV, _G = problem.evaluate(X, return_values_of=["F", "CV", "G"]) if _G.shape[1] > 1: # We need to do a special CV calculation to test for correctness since # the original code does not sum the violations but takes the maximum _CV = np.max(_G, axis=1)[:, None] _CV = np.maximum(_CV, 0) np.testing.assert_allclose(_F, F) np.testing.assert_allclose(_CV, CV)
async def evaluate(X): assert type(X) == np.ndarray, 'Input X must be a numpy array' start_time = time.time() await asyncio.sleep(wall_time) # denormalize the parameters X = vrange[0] + (vrange[1] - vrange[0]) * X prob = get_problem(prob_id) Y = prob.evaluate(X) end_time = time.time() return Y
def test_same_result(self): problem = get_problem("zdt1") algorithm = NSGA2(pop_size=100) min_res = minimize(problem, algorithm, ('n_gen', 200), seed=1, verbose=True) algorithm = NSGA2(pop_size=100) algorithm.setup(problem, ('n_gen', 200), seed=1) while algorithm.has_next(): algorithm.next() print(algorithm.n_gen) loop_res = algorithm.result() np.testing.assert_allclose(min_res.X, loop_res.X)
def test_problems(name): problem = get_problem(name) X, F, CV = load(name.upper()) _F, _CV, _G = problem.evaluate(X, return_values_of=["F", "CV", "G"]) if _G.shape[1] > 1: # We need to do a special CV calculation to test for correctness since # the original code does not sum the violations but takes the maximum _CV = np.max(_G, axis=1)[:, None] _CV = np.maximum(_CV, 0) np.testing.assert_allclose(_F, F) np.testing.assert_allclose(_CV[:, 0], CV)
def test_de(selection, crossover): problem = get_problem("ackley", n_var=10) algorithm = DE( pop_size=100, sampling=LHS(), variant=f"DE/{selection}/1/{crossover}") ret = minimize(problem, algorithm, ('n_gen', 20), seed=1, verbose=True) assert len(ret.opt) > 0
def test_elementwise_crossover(): problem = get_problem('zdt1') sampling = FloatRandomSampling() pop = sampling.do(problem, n_samples=3) crossover = ElementwiseCrossover(SBX(20)) off = crossover.do(problem, pop[0], pop[1]) assert len(off) == 2 crossover = ElementwiseCrossover(DEX()) off = crossover.do(problem, pop[0], pop[1], pop[2]) assert len(off) == 1
def get_dascmop(name, difficulty): difficulty = int(difficulty) if name == "dascmop1": problem = get_problem("dascmop1", difficulty) g = problem.g1 elif name == "dascmop2": problem = get_problem("dascmop2", difficulty) g = problem.g1 elif name == "dascmop3": problem = get_problem("dascmop3", difficulty) g = problem.g1 elif name == "dascmop4": problem = get_problem("dascmop4", difficulty) g = problem.g2 elif name == "dascmop5": problem = get_problem("dascmop5", difficulty) g = problem.g2 elif name == "dascmop6": problem = get_problem("dascmop6", difficulty) g = problem.g2 elif name == "dascmop7": problem = get_problem("dascmop7", difficulty) g = problem.g2 elif name == "dascmop8": problem = get_problem("dascmop8", difficulty) g = problem.g2 elif name == "dascmop9": problem = get_problem("dascmop9", difficulty) g = problem.g3 return (problem, g)
def test_problems(self): for n_obj, n_var, k in [(2, 6, 4), (3, 6, 4), (10, 20, 18)]: problems = [ get_problem("wfg1", n_var, n_obj, k), get_problem("wfg2", n_var, n_obj, k), get_problem("wfg3", n_var, n_obj, k), get_problem("wfg4", n_var, n_obj, k), get_problem("wfg5", n_var, n_obj, k), get_problem("wfg6", n_var, n_obj, k), get_problem("wfg7", n_var, n_obj, k), get_problem("wfg8", n_var, n_obj, k), get_problem("wfg9", n_var, n_obj, k) ] for problem in problems: name = str(problem.__class__.__name__) print("Testing: " + name + "-" + str(n_obj)) X, F, CV = load(name, n_obj) # other = from_optproblems(problem) # F = np.row_stack([other.objective_function(x) for x in X]) if F is None: print("Warning: No correctness check for %s" % name) continue _F, _G, _CV = problem.evaluate( X, return_values_of=["F", "G", "CV"]) if problem.n_obj == 1: F = F[:, None] self.assertTrue(anp.all(anp.abs(_F - F) < 0.00001)) if problem.n_constr > 0: self.assertTrue(anp.all(anp.abs(_CV[:, 0] - CV) < 0.0001))
def test_problems(self): PROBLEMS = get_problem_options() PROBLEMS.extend(get_global_optimization_problem_options()) for _tuple in PROBLEMS: name = _tuple[0] print(name, "OK") if name in exclude: continue problem = get_problem(name) X = get_sampling("real_random").do(problem, Population(), 100).get("X") out = problem.evaluate(X)
def test_problems(name, params): X, F, CV = load(name) if F is None: print("Warning: No correctness check for %s" % name) return problem = get_problem(name, *params) _F, _G, _CV, _dF, _dG = problem.evaluate( X, return_values_of=["F", "G", "CV", "dF", "dG"]) if problem.n_obj == 1: F = F[:, None] assert anp.all(anp.abs(_F - F) < 0.00001) if problem.n_constr > 0: assert anp.all(anp.abs(_CV[:, 0] - CV) < 0.0001)
def compare_ZDT1(): N = 100 problem = get_problem("ZDT1") algorithm = NSGA2(pop_size=100, elimate_duplicates=False) start = time.time() res = minimize(problem, algorithm, ('n_gen', 2000), verbose=False) end = time.time() print('耗时:', end - start, '秒') zdt1 = np.loadtxt('ZDT1.txt') plt.scatter(zdt1[:, 0], zdt1[:, 1], marker='o', color='green') plt.scatter(res.F[:, 0], res.F[:, 1], marker="o", color='red') best_solution = zdt1 # coverage (C-metric) 评价 number = 0 for i in range(N): dominated_num = 0 for j in range(len(best_solution)): if dominate(best_solution[j], res.F[i]): dominated_num += 1 if dominated_num != 0: number += 1 C_AB = float(number / N) print("c-metric %2f" % C_AB) # 越小越好 # distance from representatives in the PF (D-metric) min_d = 0 for i in range(len(best_solution)): temp = [] for j in range(N): dd = 0 for k in range(2): dd += float((best_solution[i][k] - res.F[j][k]) ** 2) temp.append(math.sqrt(dd)) min_d += np.min(temp) D_AP = float(min_d / N) print("D-metric: %2f" % D_AP) plt.grid(True) plt.show()
async def evaluate(X, configs={ 'prob_id': 'zdt1', 'vrange': [0, 1], 'wall_time': 1 }): assert type(X) == np.ndarray, 'Input X must be a numpy array' prob_id, vrange, wall_time = itemgetter('prob_id', 'vrange', 'wall_time')(configs) # start_time = time.time() await asyncio.sleep(wall_time) # denormalize the parameters X = vrange[0] + (vrange[1] - vrange[0]) * X prob = get_problem(prob_id) Y = prob.evaluate(X) # end_time = time.time() return Y