예제 #1
0
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)
예제 #2
0
def test_umda_run():
    def hook(optimizer, space, function):
        return

    new_function = function.Function(pointer=Knapsack(
        values=(55, 10, 47, 5, 4), weights=(95, 4, 60, 32, 23), max_capacity=100))

    new_umda = umda.UMDA()

    boolean_space = boolean.BooleanSpace(
        n_agents=2, n_iterations=10, n_variables=5)

    history = new_umda.run(boolean_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 umda failed to converge.'
예제 #3
0
# Number of agents
n_agents = 5

# Number of decision variables
n_variables = 5

# Number of running iterations
n_iterations = 10

# Creating the BooleanSpace class
s = BooleanSpace(n_agents=n_agents, n_iterations=n_iterations, n_variables=n_variables)

# Hyperparameters for the optimizer
hyperparams = {
    'c1': r.generate_binary_random_number(size=(n_variables, 1)),
    'c2': r.generate_binary_random_number(size=(n_variables, 1))
}

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

# Creating Function's object
f = Function(pointer=Knapsack(values=[55, 10, 47, 5, 4], weights=[95, 4, 60, 32, 23], max_capacity=100))

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

# Running the optimization task
history = o.start()
예제 #4
0
from opytimizer.core import Function
from opytimizer.optimizers.boolean import BPSO
from opytimizer.spaces import BooleanSpace

# Random seed for experimental consistency
np.random.seed(0)

# Number of agents and decision variables
n_agents = 5
n_variables = 5

# 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(params)
function = Function(
    Knapsack(values=(55, 10, 47, 5, 4),
             weights=(95, 4, 60, 32, 23),
             max_capacity=100))

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

# Runs the optimization task
opt.start(n_iterations=1000)