Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
def test_discrete_search_callback_on_evaluate_before():
    allowed_values = [[1], [1]]
    new_discrete_search_callback = callback.DiscreteSearchCallback(
        allowed_values=allowed_values)

    space = search.SearchSpace(1, 2, [0, 0], [1, 1])
    optimizer = swarm.PSO()
    func = function.Function(Sphere())

    opt_model = Opytimizer(space, optimizer, func)

    try:
        new_discrete_search_callback.on_evaluate_before(None)
    except:
        new_discrete_search_callback.on_evaluate_before(opt_model.space)
Ejemplo n.º 3
0
from opytimizer.optimizers.misc.gs import GS
from opytimizer.spaces.grid import GridSpace

# Number of decision variables
n_variables = 2

# And also the size of the step in the grid
step = 0.1

# Lower and upper bounds (has to be the same size as n_variables)
lower_bound = [-10, -10]
upper_bound = [10, 10]

# Creating the GridSpace class
s = GridSpace(n_variables=n_variables,
              step=step,
              lower_bound=lower_bound,
              upper_bound=upper_bound)

# Creating GS optimizer
p = GS()

# Creating Function's object
f = Function(pointer=Sphere())

# Finally, we can create an Opytimizer class
o = Opytimizer(space=s, optimizer=p, function=f)

# Running the optimization task
history = o.start()
Ejemplo n.º 4
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()
Ejemplo n.º 5
0
n_variables = 2

# Number of running iterations
n_iterations = 10

# 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 = {'w': 0.7, 'c1': 1.7, 'c2': 1.7}

# Creating PSO's optimizer
p = PSO(hyperparams=hyperparams)

# Creating Function's object
f = Function(pointer=Sphere(), constraints=[c_1])

# Finally, we can create an Opytimizer class
o = Opytimizer(space=s, optimizer=p, function=f)

# Running the optimization task
history = o.start()
Ejemplo n.º 6
0
# Number of running iterations
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 = WeightedFunction(functions=[Exponential(), Sphere()], weights=[0.5, 0.5])

# Finally, we can create an Opytimizer class
o = Opytimizer(space=s, optimizer=p, function=z)

# Running the optimization task
history = o.start()
def wrapper(x):
    z = Sphere()
    return z(x)
Ejemplo n.º 8
0
import numpy as np

from opytimark.markers.n_dimensional import Sphere

# Declaring a function from the `n_dimensional` package
f = Sphere()

# Declaring an input variable for feeding the function
x = np.zeros(50)

# Printing out the function's output
print(f(x))
Ejemplo n.º 9
0
from opytimizer.spaces import SearchSpace


# Defines a constraint function that returns a boolean
# whether the constraint is valid or not
def c_1(x):
    return x[0] + x[1] < 0


# 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 = ConstrainedFunction(Sphere(), [c_1], penalty=100.0)

# Bundles every piece into Opytimizer class
opt = Opytimizer(space, optimizer, function, save_agents=False)

# Runs the optimization task
opt.start(n_iterations=1000)
Ejemplo n.º 10
0
def wrapper(x):
    s = Sphere()
    return s(x)
n_variables = 2

# Number of running iterations
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 = {'w': 0.7, 'c1': 1.7, 'c2': 1.7}

# Creating PSO's optimizer
p = PSO(hyperparams=hyperparams)

# Creating Function's object
f = Function(pointer=Sphere(), constraints=[c_1], penalty=100.0)

# Finally, we can create an Opytimizer class
o = Opytimizer(space=s, optimizer=p, function=f)

# Running the optimization task
history = o.start()