def test_rpso_run(): def square(x): return np.sum(x**2) def hook(optimizer, space, function): return new_function = function.Function(pointer=square) new_rpso = pso.RPSO() search_space = search.SearchSpace(n_agents=5, n_iterations=20, n_variables=2, lower_bound=[0, 0], upper_bound=[10, 10]) history = new_rpso.run(search_space, new_function, pre_evaluation=hook) assert len(history.agents) > 0 assert len(history.best_agent) > 0 assert len(history.local) > 0 best_fitness = history.best_agent[-1][1] assert best_fitness <= constants.TEST_EPSILON, 'The algorithm rpso failed to converge.'
def test_ssd_run(): def square(x): return np.sum(x**2) def hook(optimizer, space, function): return new_function = function.Function(pointer=square) hyperparams = {'c': 2.0, 'decay': 0.3} new_ssd = ssd.SSD(hyperparams=hyperparams) search_space = search.SearchSpace(n_agents=50, n_iterations=350, n_variables=2, lower_bound=[-100, -100], upper_bound=[100, 100]) history = new_ssd.run(search_space, new_function, pre_evaluation=hook) assert len(history.agents) > 0 assert len(history.best_agent) > 0 best_fitness = history.best_agent[-1][1] assert best_fitness <= constants.TEST_EPSILON, 'The algorithm ssd failed to converge.'
def test_gsa_run(): def square(x): return np.sum(x**2) def hook(optimizer, space, function): return new_function = function.Function(pointer=square) hyperparams = {'G': 100} new_gsa = gsa.GSA(hyperparams=hyperparams) search_space = search.SearchSpace(n_agents=10, n_iterations=100, n_variables=2, lower_bound=[0, 0], upper_bound=[10, 10]) history = new_gsa.run(search_space, new_function, pre_evaluation=hook) assert len(history.agents) > 0 assert len(history.best_agent) > 0 best_fitness = history.best_agent[-1][1] assert best_fitness <= constants.TEST_EPSILON, 'The algorithm gsa failed to converge.'
def test_discrete_search_callback_on_task_begin(): space = search.SearchSpace(1, 2, [0, 0], [1, 1]) optimizer = swarm.PSO() func = function.Function(Sphere()) opt_model = Opytimizer(space, optimizer, func) try: allowed_values = [[1]] new_discrete_search_callback = callback.DiscreteSearchCallback( allowed_values=allowed_values) new_discrete_search_callback.on_task_begin(opt_model) except: allowed_values = [[1], [1]] new_discrete_search_callback = callback.DiscreteSearchCallback( allowed_values=allowed_values) new_discrete_search_callback.on_task_begin(opt_model) try: allowed_values = [[10], [10]] new_discrete_search_callback = callback.DiscreteSearchCallback( allowed_values=allowed_values) new_discrete_search_callback.on_task_begin(opt_model) except: allowed_values = [[1], [1]] new_discrete_search_callback = callback.DiscreteSearchCallback( allowed_values=allowed_values) new_discrete_search_callback.on_task_begin(opt_model)
def test_wdo_run(): def square(x): return np.sum(x**2) def hook(optimizer, space, function): return new_function = function.Function(pointer=square) hyperparams = {'v_max': 0.3, 'alpha': 0.8, 'g': 0.6, 'c': 1.0, 'RT': 1.5} new_wdo = wdo.WDO(hyperparams=hyperparams) search_space = search.SearchSpace(n_agents=10, n_iterations=100, n_variables=2, lower_bound=[0, 0], upper_bound=[10, 10]) history = new_wdo.run(search_space, new_function, pre_evaluation=hook) assert len(history.agents) > 0 assert len(history.best_agent) > 0 best_fitness = history.best_agent[-1][1] assert best_fitness <= constants.TEST_EPSILON, 'The algorithm wdo failed to converge.'
def test_opytimizer_update_args(): space = search.SearchSpace(1, 1, 0, 1) func = function.Function(callable) optimizer = pso.PSO() new_opytimizer = opytimizer.Opytimizer(space, optimizer, func) assert len(new_opytimizer.update_args) == 1
def test_opytimizer_start(): space = search.SearchSpace(1, 1, 0, 1) func = function.Function(callable) optimizer = pso.PSO() new_opytimizer = opytimizer.Opytimizer(space, optimizer, func) new_opytimizer.start(n_iterations=1)
def test_opytimizer_save(): space = search.SearchSpace(1, 1, 0, 1) func = function.Function(callable) optimizer = pso.PSO() new_opytimizer = opytimizer.Opytimizer(space, optimizer, func) new_opytimizer.save('out.pkl')
def test_opytimizer_history(): space = search.SearchSpace(1, 1, 0, 1) func = function.Function(callable) optimizer = pso.PSO() new_opytimizer = opytimizer.Opytimizer(space, optimizer, func) assert type(new_opytimizer.history).__name__ == 'History'
def test_opytimizer_total_iterations(): space = search.SearchSpace(1, 1, 0, 1) func = function.Function(callable) optimizer = pso.PSO() new_opytimizer = opytimizer.Opytimizer(space, optimizer, func) assert new_opytimizer.total_iterations == 0
def test_function_pointer_setter(): def square(x): return x**2 assert square(2) == 4 new_function = function.Function(pointer=square) assert new_function.pointer == square
def test_bmrfo_update(): new_function = function.Function(pointer=Knapsack( values=(55, 10, 47, 5, 4), weights=(95, 4, 60, 32, 23), max_capacity=100)) new_bmrfo = bmrfo.BMRFO() boolean_space = boolean.BooleanSpace(n_agents=100, n_variables=5) new_bmrfo.update(boolean_space, new_function, 1, 20)
def test_function_pointer_setter(): new_function = function.Function() try: new_function.pointer = 'a' except: new_function.pointer = callable assert new_function.pointer.__class__.__name__ == 'builtin_function_or_method' or 'builtin_function'
def test_function_name_setter(): new_function = function.Function(pointer) try: new_function.name = 1 except: new_function.name = "pointer" assert new_function.name == "pointer"
def test_function_name_setter(): new_function = function.Function() try: new_function.name = 1 except: new_function.name = 'callable' assert new_function.name == 'callable'
def test_opytimizer_update(): space = search.SearchSpace(1, 1, 0, 1) func = function.Function(callable) optimizer = pso.PSO() callbacks = callback.CallbackVessel([]) new_opytimizer = opytimizer.Opytimizer(space, optimizer, func) new_opytimizer.update(callbacks)
def test_function_pointer_setter(): new_function = function.Function(pointer) try: new_function.pointer = "a" except: new_function.pointer = callable assert (new_function.pointer.__class__.__name__ == "builtin_function_or_method" or "builtin_function")
def test_function_function_setter(): def square(x): return x**2 assert square(2) == 4 try: new_function = function.Function(pointer=0) except: new_function = function.Function(pointer=square) def square2(x, y): return x**2 + y**2 assert square2(2, 2) == 8 try: new_function = function.Function(pointer=square2) except: new_function = function.Function(pointer=square)
def test_function_call(): def square(x): return np.sum(x**2) assert square(2) == 4 def square2(x, y): return x**2 + y**2 assert square2(2, 2) == 8 new_function = function.Function(square) assert new_function(np.zeros(2)) == 0 try: new_function = function.Function(square2) except: new_function = function.Function(square) assert new_function.name == "square"
def test_function(): class Square(): def __call__(self, x): return np.sum(x**2) s = Square() assert s(2) == 4 new_function = function.Function(pointer=s) assert new_function.name == 'Square'
def test_optimizer_evaluate(): new_function = function.Function(pointer=square) search_space = search.SearchSpace(n_agents=20, n_iterations=10, n_variables=2, lower_bound=[0, 0], upper_bound=[10, 10]) new_optimizer = optimizer.Optimizer() new_optimizer._evaluate(search_space, new_function) assert search_space.best_agent.fit < sys.float_info.max
def test_optimizer_evaluate(): def square(x): return np.sum(x**2) new_function = function.Function(square) new_search_space = search.SearchSpace(n_agents=1, n_variables=2, lower_bound=[0, 0], upper_bound=[10, 10]) new_optimizer = optimizer.Optimizer() new_optimizer.evaluate(new_search_space, new_function) assert new_search_space.best_agent.fit < sys.float_info.max
def test_cem_update(): def square(x): return np.sum(x**2) new_function = function.Function(pointer=square) search_space = search.SearchSpace(n_agents=10, n_variables=2, lower_bound=[0, 0], upper_bound=[10, 10]) new_cem = cem.CEM() new_cem.compile(search_space) new_cem.update(search_space, new_function)
def test_bpso_evaluate(): def square(x): return np.sum(x**2) new_function = function.Function(pointer=square) boolean_space = boolean.BooleanSpace(n_agents=2, n_variables=5) new_bpso = bpso.BPSO() new_bpso.compile(boolean_space) new_bpso.evaluate(boolean_space, new_function) assert boolean_space.best_agent.fit < sys.float_info.max
def test_opytimizer_history_setter(): space = search.SearchSpace(1, 1, 0, 1) func = function.Function(callable) optimizer = pso.PSO() hist = history.History() new_opytimizer = opytimizer.Opytimizer(space, optimizer, func) try: new_opytimizer.history = 1 except: new_opytimizer.history = hist assert type(new_opytimizer.history).__name__ == 'History'
def test_function_penalty_setter(): new_function = function.Function() try: new_function.penalty = 'a' except: new_function.penalty = 1 try: new_function.penalty = -1 except: new_function.penalty = 1 assert new_function.penalty == 1
def test_opytimizer_space_setter(): space = search.SearchSpace(1, 1, 0, 1) func = function.Function(callable) optimizer = pso.PSO() new_opytimizer = opytimizer.Opytimizer(space, optimizer, func) try: space.built = False new_opytimizer.space = space except: space.built = True new_opytimizer.space = space assert type(new_opytimizer.space).__name__ == 'SearchSpace'
def test_opytimizer_function_setter(): space = search.SearchSpace(1, 1, 0, 1) func = function.Function(callable) optimizer = pso.PSO() new_opytimizer = opytimizer.Opytimizer(space, optimizer, func) try: func.built = False new_opytimizer.function = func except: func.built = True new_opytimizer.function = func assert type(new_opytimizer.function).__name__ == 'Function'
def test_cs_update(): def square(x): return np.sum(x**2) new_function = function.Function(pointer=square) new_cs = cs.CS() search_space = search.SearchSpace(n_agents=20, n_iterations=100, n_variables=2, lower_bound=[-10, -10], upper_bound=[10, 10]) new_cs._update(search_space.agents, search_space.best_agent, new_function) assert search_space.agents[0].position[0] != 0
def test_gco_update(): def square(x): return np.sum(x**2) new_function = function.Function(pointer=square) new_gco = gco.GCO() search_space = search.SearchSpace(n_agents=4, n_iterations=10, n_variables=2, lower_bound=[1, 1], upper_bound=[10, 10]) new_gco._update(search_space.agents, new_function, np.array([70, 70, 70, 70]), np.array([1, 1, 1, 1])) assert search_space.agents[0].position[0] != 0