Standard Single-Objective Optimization
This guide demonstrates how to set up and execute a standard single-objective optimization task using Particle Swarm Optimization (PSO) and the Sphere benchmark function.
Example Code
import numpy as np
from opytimark.markers.n_dimensional import Sphere
from opytimizer import Opytimizer
from opytimizer.core import Function
from opytimizer.core.stopping import MaxIterations
from opytimizer.optimizers.single_objective.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
n_objectives = 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 = SearchSpace(n_agents, n_variables, n_objectives, 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(MaxIterations(1000))
# Prints out information about the best agent that has been found
print(
f"Best Agent: {opt.space.best_agent.mapped_position} | Fitness: {opt.space.best_agent.fit}"
)