Пример #1
0
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)
Пример #2
0
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)
Пример #3
0
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
Пример #4
0
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
Пример #5
0
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)
Пример #6
0
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
Пример #7
0
# 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()
Пример #8
0
    # 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)
Пример #9
0
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()