def optimize_umda(target, n_agents, n_variables, n_iterations, hyperparams): """Abstracts Opytimizer's Univariate Marginal Distribution Algorithm into a single method. Args: target (callable): The method to be optimized. n_agents (int): Number of agents. n_variables (int): Number of variables. n_iterations (int): Number of iterations. hyperparams (dict): Dictionary of hyperparameters. Returns: A History object containing all optimization's information. """ # Creating the BooleanSpace space = BooleanSpace(n_agents=n_agents, n_iterations=n_iterations, n_variables=n_variables) # Creating the Function function = Function(pointer=target) # Creating UMDA's optimizer optimizer = UMDA(hyperparams=hyperparams) # Creating the optimization task task = Opytimizer(space=space, optimizer=optimizer, function=function) return task.start(store_best_only=True)
def optimize(opt, target, n_agents, n_iterations, hyperparams): """Abstracts all Opytimizer's mechanisms into a single method. Args: opt (Optimizer): An Optimizer-child class. target (callable): The method to be optimized. n_agents (int): Number of agents. n_iterations (int): Number of iterations. hyperparams (dict): Dictionary of hyperparameters. Returns: A History object containing all optimization's information. """ # Creating the SearchSpace space = SearchSpace(n_agents=n_agents, n_variables=1, n_iterations=n_iterations, lower_bound=[0.0001], upper_bound=[1]) # Creating the Optimizer optimizer = opt(hyperparams=hyperparams) # Creating the Function function = Function(pointer=target) # Creating the optimization task task = Opytimizer(space=space, optimizer=optimizer, function=function) return task.start(store_best_only=True)
def optimize_gp(target, n_trees, n_terminals, n_variables, n_iterations, min_depth, max_depth, functions, lb, ub, hyperparams): """Abstracts Opytimizer's Genetic Programming into a single method. Args: target (callable): The method to be optimized. n_trees (int): Number of agents. n_terminals (int): Number of terminals n_variables (int): Number of variables. n_iterations (int): Number of iterations. min_depth (int): Minimum depth of trees. max_depth (int): Maximum depth of trees. functions (list): Functions' nodes. lb (list): List of lower bounds. ub (list): List of upper bounds. hyperparams (dict): Dictionary of hyperparameters. Returns: A History object containing all optimization's information. """ # Creating the TreeSpace space = TreeSpace(n_trees=n_trees, n_terminals=n_terminals, n_variables=n_variables, n_iterations=n_iterations, min_depth=min_depth, max_depth=max_depth, functions=functions, lower_bound=lb, upper_bound=ub) # Creating the Function function = Function(pointer=target) # Creating GP's optimizer optimizer = GP(hyperparams=hyperparams) # Creating the optimization task task = Opytimizer(space=space, optimizer=optimizer, function=function) return task.start(store_best_only=True)
def test_store_best_agent_only(): pso = PSO() n_iters = 10 target_fn = Function(pointer=square) space = SearchSpace(lower_bound=[-10], upper_bound=[10], n_iterations=n_iters) history = Opytimizer(space, pso, target_fn).start(store_best_only=True) assert not hasattr(history, 'agents') assert hasattr(history, 'best_agent') assert len(history.best_agent) == n_iters
def test_store_all_agents(): pso = PSO() n_iters = 10 n_agents = 2 target_fn = Function(pointer=square) space = SearchSpace(lower_bound=[-10], upper_bound=[10], n_iterations=n_iters, n_agents=n_agents) history = Opytimizer(space, pso, target_fn).start() assert hasattr(history, 'agents') # Ensuring that the amount of entries is the same as the amount of iterations and # that for each iteration all agents are kept assert len(history.agents) == n_iters assert all(len(iter_agents) == n_agents for iter_agents in history.agents) assert hasattr(history, 'best_agent') assert len(history.best_agent) == n_iters
def test_hook(): pso = PSO() n_iters = 10 counter = 0 target_fn = Function(pointer=square) space = SearchSpace(lower_bound=[-10], upper_bound=[10], n_iterations=n_iters, n_agents=15) def eval_hook(arg_opt, arg_space, arg_target_fn): assert arg_opt is pso assert arg_space is space assert arg_target_fn is target_fn nonlocal counter counter += 1 Opytimizer(space, pso, target_fn).start(pre_evaluation=eval_hook) # The hook is evaluated for each iteration plus initialization assert counter == n_iters + 1
# Number of decision variables n_variables = 1 # Number of running iterations n_iterations = 100 # Lower and upper bounds (has to be the same size as n_variables) lower_bound = [0.00001] upper_bound = [10] # Creating the SearchSpace class s = SearchSpace(n_agents=n_agents, n_iterations=n_iterations, n_variables=n_variables, lower_bound=lower_bound, upper_bound=upper_bound) # Hyperparameters for the optimizer hyperparams = { 'w': 0.7, 'c1': 1.7, 'c2': 1.7 } # Creating PSO's optimizer p = PSO(hyperparams=hyperparams) # Finally, we can create an Opytimizer class o = Opytimizer(space=s, optimizer=p, function=f) # Running the optimization task history = o.start()
# Predicts new data preds = opf.predict(X_val_selected) # Calculates accuracy acc = g.opf_accuracy(Y_val, preds) return 1 - acc # Number of agents and decision variables n_agents = 5 n_variables = 64 # Parameters for the optimizer params = { 'c1': r.generate_binary_random_number(size=(n_variables, 1)), 'c2': r.generate_binary_random_number(size=(n_variables, 1)) } # Creates the space, optimizer and function space = BooleanSpace(n_agents, n_variables) optimizer = BPSO() function = Function(supervised_opf_feature_selection) # Bundles every piece into Opytimizer class opt = Opytimizer(space, optimizer, function) # Runs the optimization task opt.start(n_iterations=3)
from opytimark.markers.n_dimensional import Sphere from opytimizer import Opytimizer from opytimizer.core import Function from opytimizer.optimizers.misc import GS from opytimizer.spaces import GridSpace # Number of decision variables and step size of the grid n_variables = 2 step = [0.1, 1] # Lower and upper bounds (has to be the same size as `n_variables`) lower_bound = [-10, -10] upper_bound = [10, 10] # Creates the space, optimizer and function space = GridSpace(n_variables, step, lower_bound, upper_bound) optimizer = GS() function = Function(Sphere()) # Bundles every piece into Opytimizer class opt = Opytimizer(space, optimizer, function, save_agents=False) # Runs the optimization task opt.start()
n_iterations = 1000 # Lower and upper bounds (has to be the same size as n_variables) lower_bound = [-10, -10] upper_bound = [10, 10] # Creating the SearchSpace class s = SearchSpace(n_agents=n_agents, n_iterations=n_iterations, n_variables=n_variables, lower_bound=lower_bound, upper_bound=upper_bound) # Hyperparameters for the optimizer hyperparams = { 'alpha': 0.5, 'beta': 0.2, 'gamma': 1.0 } # Creating FA's optimizer p = FA(hyperparams=hyperparams) # Defining task's main function z = Multi(functions=[b.sphere, b.exponential], weights=[0.5, 0.5], method='weight_sum') # Finally, we can create an Opytimizer class o = Opytimizer(space=s, optimizer=p, function=z) # Running the optimization task o.start(history=True)
from opytimizer.spaces import SearchSpace from opytimizer.utils.callback import DiscreteSearchCallback # Random seed for experimental consistency np.random.seed(0) # Number of agents and decision variables n_agents = 20 n_variables = 2 # Lower and upper bounds (has to be the same size as `n_variables`) lower_bound = [-10, -10] upper_bound = [10, 10] # Defines the allowed values for performing the discrete search allowed_values = [ list(range(lb, ub, 2)) for lb, ub in zip(lower_bound, upper_bound) ] # Creates the space, optimizer and function space = SearchSpace(n_agents, n_variables, lower_bound, upper_bound) optimizer = PSO() function = Function(Sphere()) # Bundles every piece into Opytimizer class opt = Opytimizer(space, optimizer, function, save_agents=False) # Runs the optimization task opt.start(n_iterations=5, callbacks=[DiscreteSearchCallback(allowed_values=allowed_values)])
n_variables = 2 # Minimum and maximum depths of the trees min_depth = 2 max_depth = 5 # Functions nodes, lower and upper bounds functions = ["SUM", "MUL", "DIV"] lower_bound = [-10, -10] upper_bound = [10, 10] # Creates the space, optimizer and function space = TreeSpace( n_agents, n_variables, lower_bound, upper_bound, n_terminals, min_depth, max_depth, functions, ) optimizer = GP() function = Function(Sphere()) # Bundles every piece into Opytimizer class opt = Opytimizer(space, optimizer, function, save_agents=False) # Runs the optimization task opt.start(n_iterations=1000)
# Random seed for experimental consistency np.random.seed(0) # Number of agents and decision variables n_agents = 20 n_variables = 2 # Lower and upper bounds (has to be the same size as `n_variables`) lower_bound = [-10, -10] upper_bound = [10, 10] # Creates the space, optimizer and function space = SearchSpace(n_agents, n_variables, lower_bound, upper_bound) optimizer = PSO() function = Function(Sphere()) # Bundles every piece into Opytimizer class opt = Opytimizer(space, optimizer, function, save_agents=False) # Runs the optimization task opt.start(n_iterations=10, callbacks=[CheckpointCallback(frequency=10)]) # Deletes the optimization objecs del opt # Loads the task from file and resumes it # Note that the following lines achieves the same results as a 35-iteration running opt = Opytimizer.load('iter_10_checkpoint.pkl') opt.start(n_iterations=25)
from opytimark.markers.n_dimensional import Sphere from opytimizer import Opytimizer from opytimizer.core import Function from opytimizer.optimizers.swarm import PSO from opytimizer.spaces import SearchSpace from opytimizer.utils.callback import CheckpointCallback # Random seed for experimental consistency np.random.seed(0) # Number of agents and decision variables n_agents = 20 n_variables = 2 # Lower and upper bounds (has to be the same size as `n_variables`) lower_bound = [-10, -10] upper_bound = [10, 10] # Creates the space, optimizer and function space = SearchSpace(n_agents, n_variables, lower_bound, upper_bound) optimizer = PSO() function = Function(Sphere()) # Bundles every piece into Opytimizer class opt = Opytimizer(space, optimizer, function, save_agents=False) # Runs the optimization task # CheckpointCallback will snapshot the optimization every `frequency` iterations opt.start(n_iterations=10, callbacks=[CheckpointCallback(frequency=1)])
# Number of agents and decision variables n_agents = 3 n_variables = 2 # Lower and upper bounds (has to be the same size as `n_variables`) lower_bound = [-10, -10] upper_bound = [10, 10] # Creates the space, optimizer and function space = SearchSpace(n_agents, n_variables, lower_bound, upper_bound) optimizer = PSO() function = Function(Sphere()) # Bundles every piece into Opytimizer class opt = Opytimizer(space, optimizer, function, save_agents=True) # Runs the optimization task opt.start(n_iterations=10) # Saves the optimization task opt.save("opt_task.pkl") # One can load the optimization task from disk or work directly with the attribute that is saved # History keys are saved as lists, where the last dimension stands for their iteration # opt = Opytimizer.load('opt_task.pkl') # Prints the last iteration best agent and checks that it matches the best agent in space # Also prints a random iteration best agent best_agent_pos, best_agent_fit = opt.history.get_convergence("best_agent") print(