def test_surrogate_optimize_with_all_selection_and_infill_methods( infill, surrogate_select_method): # Define original problem try: problem = benchmarks.zdt3() # Sample randomSample = sampling.rand(problem, 15) # Define surrogate ensemble # Define Optimizer optimizer = NSGA2(pop_size=20, n_offsprings=10, sampling=get_sampling("real_random"), crossover=get_crossover("real_sbx", prob=0.9, eta=15), mutation=get_mutation("real_pm", eta=20), eliminate_duplicates=True) # Define termination criteria termination = get_termination("n_gen", 10) # Define infill criteria infill_method = infill # Define surrogate selection surrogate_selection_function = surrogate_select_method # Optimize samples = copy.deepcopy(randomSample) surrogate_ensemble = [ LinearRegression(), KNeighborsRegressor(), DecisionTreeRegressor(), ] res = surrogate_optimization.optimize(problem, optimizer, termination, surrogate_ensemble, samples, infill_method, surrogate_selection_function, n_infill=2, max_samples=20) except Exception as e: raise pytest.fail( "infill: {0} /n and surrogate_selection: {1}/n raise a error: {2}". format(infill, surrogate_select_method, e))
def test_rand_samples_should_respect_upper_bound(test_problem): upper_bound = test_problem.xu samples = sampling.rand(test_problem, 1000) X_samples = samples['X'] if (type(X_samples[0]) is np.ndarray): sample_bigger_than_upper_bound = False for sample in X_samples: sample_bigger_than_upper_bound = any(sample > upper_bound) if (sample_bigger_than_upper_bound): break assert (not sample_bigger_than_upper_bound) else: max_sample = max(X_samples) assert (max_sample < upper_bound)
def test_rand_samples_should_respect_lower_bound(test_problem): lower_bound = test_problem.xl samples = sampling.rand(test_problem, 1000) X_samples = samples['X'] if (type(X_samples[0]) is np.ndarray): sample_smaller_than_lower_bound = False for sample in X_samples: sample_smaller_than_lower_bound = any(sample < lower_bound) if (sample_smaller_than_lower_bound): break assert (not sample_smaller_than_lower_bound) else: min_sample = min(X_samples) assert (min_sample > lower_bound)
def test_rand_should_return_50_samples(test_problem): assert (len(sampling.rand(test_problem, 50)['X']) == 50)
def test_rand_sampling_should_return_dict_with_X_F_G(test_problem, variable_name): assert (variable_name in sampling.rand(test_problem))
def test_rand_sampling_should_return_a_dict(test_problem): assert (type(sampling.rand(test_problem)) is dict)
from pymoo.factory import get_sampling, get_crossover, get_mutation from pymoo.factory import get_termination from pymoo.util.plotting import plot import time import copy if __name__ == "__main__": # Define original problem # problem = benchmarks.mw1() # Define original problem problem = benchmarks.zdt3() # Sample randomSample = sampling.rand(problem, 70) # Define surrogate ensemble # Define Optimizer optimizer = NSGA2(pop_size=250, n_offsprings=100, sampling=get_sampling("real_random"), crossover=get_crossover("real_sbx", prob=0.9, eta=15), mutation=get_mutation("real_pm", eta=20), eliminate_duplicates=True) # Define termination criteria termination = get_termination("n_gen", 100)