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)
# 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)
cost += fit(model, loss, opt, X_train[:, start:end, :], Y_train[start:end]) # Predicting samples from evaluating set preds = predict(model, X_val) # Calculates accuracy acc = np.mean(preds == Y_val) return 1 - acc # Number of agents and decision variables n_agents = 10 n_variables = 2 # Lower and upper bounds (has to be the same size as `n_variables`) lower_bound = [0, 0] upper_bound = [1, 1] # Creates the space, optimizer and function space = SearchSpace(n_agents, n_variables, lower_bound, upper_bound) optimizer = PSO() function = Function(lstm) # Bundles every piece into Opytimizer class opt = Opytimizer(space, optimizer, function) # Runs the optimization task opt.start(n_iterations=100)
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)])
# 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 opytimizer import Opytimizer from opytimizer.core import Function from opytimizer.optimizers.swarm import PSO from opytimizer.spaces import SearchSpace # 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 # Every call on `start` will the continue the optimization for `n_iterations` # Note that the following lines achieves the same results as a 100-iteration running opt.start(n_iterations=50) opt.start(n_iterations=25) opt.start(n_iterations=25)
momentum=0, decay=0, temperature=1, dropout=dropout, use_gpu=False) # Training an RBM error, _ = model.fit(train, batch_size=128, epochs=5) return error # Number of agents and decision variables n_agents = 5 n_variables = 1 # Lower and upper bounds (has to be the same size as `n_variables`) lower_bound = [0] upper_bound = [1] # Creates the space, optimizer and function space = SearchSpace(n_agents, n_variables, lower_bound, upper_bound) optimizer = PSO() function = Function(dropout_rbm) # Bundles every piece into Opytimizer class opt = Opytimizer(space, optimizer, function) # Runs the optimization task opt.start(n_iterations=5)