-
Notifications
You must be signed in to change notification settings - Fork 335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add cost function decorator #226
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
pyswarms.utils.decorators package | ||
================================= | ||
|
||
.. automodule:: pyswarms.utils.decorators | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
""" | ||
The :mod:`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 | ||
:code:`@pyswarms.cost`. | ||
""" | ||
from .decorators import cost | ||
|
||
__all__ = ["cost"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import numpy as np | ||
|
||
|
||
def cost(cost_func): | ||
"""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 :code:`(n_particles, 0)` it enables | ||
the usage of shorter and simpler cost functions that directly return the cost. | ||
A simple example might be: | ||
|
||
.. code-block:: python | ||
import pyswarms | ||
import numpy as np | ||
|
||
@pyswarms.cost | ||
def cost_func(x): | ||
cost = np.abs(np.sum(x)) | ||
return cost | ||
|
||
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 :code:`numpy` functions return a :code:`np.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 :code:`float` or an :code:`int`). | ||
|
||
Returns | ||
------- | ||
|
||
cost_dec : callable | ||
The vectorized output for all particles as defined by :code:`cost_func` | ||
""" | ||
def cost_dec(particles, **kwargs): | ||
n_particles = particles.shape[0] | ||
vector = np.array([cost_func(particles[i], **kwargs) for i in range(n_particles)]) | ||
assert vector.shape == (n_particles, ), "The cost function should return a single value." | ||
return vector | ||
return cost_dec |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import pytest | ||
import numpy as np | ||
|
||
|
||
@pytest.fixture() | ||
def particles(): | ||
shape = (np.random.randint(10, 20), np.random.randint(2, 6)) | ||
particles_ = np.random.uniform(0, 10, shape) | ||
print(particles_) | ||
return particles_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Import modules | ||
import pytest | ||
import numpy as np | ||
|
||
# Import from package | ||
from pyswarms.utils.decorators import cost | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"objective_func", | ||
[np.sum, np.prod] | ||
) | ||
def test_cost_decorator(objective_func, particles): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests are good already 💯 |
||
n_particles = particles.shape[0] | ||
|
||
def cost_func_without_decorator(x): | ||
n_particles_in_func = x.shape[0] | ||
cost = np.array([objective_func(x[i]) for i in range(n_particles_in_func)]) | ||
return cost | ||
|
||
@cost | ||
def cost_func_with_decorator(x): | ||
cost = objective_func(x) | ||
return cost | ||
|
||
undecorated = cost_func_without_decorator(particles) | ||
decorated = cost_func_with_decorator(particles) | ||
|
||
assert np.array_equal(decorated, undecorated) | ||
assert decorated.shape == (n_particles, ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯 This is good already