pyswarms.utils.decorators package¶
The pyswarms.decorators
module implements a decorator that
can be used to simplify the task of writing the cost function for
an optimization run. The decorator can be directly called by using
@pyswarms.cost
.
-
pyswarms.utils.decorators.
cost
(cost_func)[source]¶ A decorator for the cost function
This decorator allows the creation of much simpler cost functions. Instead of writing a cost function that returns a shape of
(n_particles, 0)
it enables the usage of shorter and simpler cost functions that directly return the cost. A simple example might be:The decorator expects your cost function to use a d-dimensional array (where d is the number of dimensions for the optimization) as and argument.
Note
Some
numpy
functions return anp.ndarray
with single values in it. Be aware of the fact that without unpacking the value the optimizer will raise an exception.Parameters: cost_func (callable) – A callable object that can be used as cost function in the optimization (must return a float
or anint
).Returns: The vectorized output for all particles as defined by cost_func
Return type: callable