Objective Functions
Objective functions measure the quality of a candidate solution. Opytimizer always minimizes the value returned by the function.
Function Classes
Function: Wraps a callable (or a list of callables) that receives an agent's position and returns its fitness value(s).ConstrainedFunction: Wraps a callable and a list of constraints, penalizing positions that violate them.WeightedFunction: Combines several callables into a single objective through a weighted sum.
Single and Multiple Objectives
When a list of callables is passed to Function, it returns an objective vector:
Alternatively, a single callable may return the whole objective vector at once.
Constraints
Each constraint is a callable that returns True when the position is feasible. Infeasible positions receive a penalty proportional to their fitness:
- , for every violated constraint
Where is the penalty factor.
Weighted Aggregation
WeightedFunction reduces objectives to a single scalar value:
Benchmark Problems
The opytimizer.benchmarks package provides ready-made objective functions from the literature (classic single-objective functions, CEC 2022, ZDT, DTLZ, WFG and ZCAT). They are callables, so they are wrapped by Function like any other function:
from opytimizer.benchmarks import Rastrigin
from opytimizer.core import Function
function = Function(Rastrigin(n_variables=10))
See Benchmark Problems for details.
Evaluation Counter
Every Function counts its calls in n_calls, which is used by the MaxEvaluations stopping criterion.
Maximization
To maximize a function, simply return its negative value: