Skip to main content

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:

  • F(x)=[f1(x),f2(x),…,fM(x)]TF(x) = [f_1(x), f_2(x), \dots, f_M(x)]^T

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:

  • fp(x)=f(x)+λ⋅f(x)f_p(x) = f(x) + \lambda \cdot f(x), for every violated constraint

Where λ\lambda is the penalty factor.

Weighted Aggregation​

WeightedFunction reduces MM objectives to a single scalar value:

  • f(x)=∑i=1Mwifi(x)f(x) = \sum_{i=1}^{M} w_i f_i(x)

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:

  • max⁡f(x)=−min⁡(−f(x))\max f(x) = -\min (-f(x))