def search(self, data: Data, models: Collection[Model], tid: int, **kwargs) -> np.ndarray: kwargs = kwargs['kwargs'] # print ("SEARCH!") prob = SurrogateProblem(self.problem, self.computer, data, models, self.options, tid, self.models_transfer) prob_pymoo = MyProblemPyMoo(self.problem.DP, self.problem.DO, prob) if (kwargs['verbose']): print("prob: ", prob) bestX = [] if (self.problem.DO == 1): # single objective optimizer if ('ga' == kwargs['search_algo']): from pymoo.algorithms.soo.nonconvex.ga import GA from pymoo.optimize import minimize algo = GA(pop_size=kwargs["search_pop_size"]) elif ('pso' == kwargs['search_algo']): from pymoo.algorithms.soo.nonconvex.pso import PSO from pymoo.optimize import minimize algo = PSO(pop_size=kwargs["search_pop_size"]) else: raise Exception( f'Unknown optimization algorithm "{kwargs["search_algo"]}"' ) bestX = [] res = minimize(prob_pymoo, algo, verbose=kwargs['verbose'], seed=1) bestX.append(np.array(res.X).reshape(1, self.problem.DP)) else: # multi objective if ('nsga2' == kwargs['search_algo']): from pymoo.algorithms.moo.nsga2 import NSGA2 from pymoo.optimize import minimize algo = NSGA2(pop_size=kwargs["search_pop_size"]) elif ('moead' == kwargs['search_algo']): from pymoo.algorithms.moo.moead import MOEAD from pymoo.optimize import minimize from pymoo.factory import get_reference_directions ref_dirs = get_reference_directions("das-dennis", self.problem.DO, n_partitions=12) algo = MOEAD(ref_dirs, n_neighbors=15, prob_neighbor_mating=0.7) else: raise Exception( f'Unknown optimization algorithm "{kwargs["search_algo"]}"' ) bestX = [] res = minimize(prob_pymoo, algo, ("n_gen", kwargs["search_gen"]), verbose=kwargs['verbose'], seed=1) firstn = min(int(kwargs['search_more_samples']), np.shape(res.X)[0]) xss = res.X[0:firstn] bestX.append(xss) if (kwargs['verbose']): print(tid, 'OK' if cond else 'KO') sys.stdout.flush() print("bestX", bestX) return (tid, bestX)
mutation_func, mutation_func_args, ) ) elif alg_name == "MOEAD": alg_specific_args = MOO_CONFIG["alg_specific_args"]["MOEAD"] n_neighbors = alg_specific_args["n_neighbors"] prob_neighbor_mating = alg_specific_args["prob_neighbor_mating"] ################# # set algorithm # ################# algorithm = MOEAD( ref_dirs=get_reference_directions(ref_dir_func, **ref_dir_func_args), n_neighbors=n_neighbors, prob_neighbor_mating=prob_neighbor_mating, crossover=get_crossover(crossover_func, **crossover_func_args), mutation=get_mutation(mutation_func, **mutation_func_args), ) ##################### # algorithm logging # ##################### MOO_log( msg="algorithm = {}(\n" "ref_dirs = get_reference_directions({},{}),\n" "n_neighbors = {}\n" "prob_neighbor_mating = {}\n" "crossover=get_crossover({},{}),\n" "mutation=get_mutation({},{}),\n" ")".format( alg_name,
ES() ] @pytest.mark.parametrize('problem', SINGLE_OBJECTIVE_PROBLEMS) @pytest.mark.parametrize('algorithm', SINGLE_OBJECTIVE_ALGORITHMS) def test_singe_obj(problem, algorithm): res = minimize(problem, algorithm, seed=1, verbose=True) fmin = problem.pareto_front().flatten()[0] np.testing.assert_almost_equal(fmin, res.F[0], decimal=3) MULTI_OBJECTIVE_PROBLEMS = [ZDT1()] ref_dirs = get_reference_directions("das-dennis", 2, n_partitions=99) MULTI_OBJECTIVE_ALGORITHMS = [ NSGA2(), RVEA(ref_dirs), MOEAD(ref_dirs), ParallelMOEAD(ref_dirs), AGEMOEA() ] @pytest.mark.parametrize('problem', MULTI_OBJECTIVE_PROBLEMS) @pytest.mark.parametrize('algorithm', MULTI_OBJECTIVE_ALGORITHMS) def test_multi_obj(problem, algorithm): res = minimize(problem, algorithm, ('n_gen', 300), seed=1, verbose=True) pf = problem.pareto_front() assert IGD(pf).do(res.F) < 0.05