Skip to content

Commit

Permalink
Add API docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dhadka committed Sep 11, 2024
1 parent 0aa19d3 commit d53f67b
Show file tree
Hide file tree
Showing 26 changed files with 306 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ Platypus.egg-info/
Platypus_Opt.egg-info/
build/
dist/
docs/_build/
.ipynb_checkpoints/
.idea
7 changes: 7 additions & 0 deletions docs/api/platypus.algorithms.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
platypus.algorithms module
==========================

.. automodule:: platypus.algorithms
:members:
:undoc-members:

9 changes: 9 additions & 0 deletions docs/api/platypus.config.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
platypus.config module
======================

.. automodule:: platypus.config
:members:
:undoc-members:
:private-members:

.. autoattribute:: platypus.config.PlatypusConfig
6 changes: 6 additions & 0 deletions docs/api/platypus.core.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
platypus.core module
====================

.. automodule:: platypus.core
:members:
:undoc-members:
6 changes: 6 additions & 0 deletions docs/api/platypus.distance.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
platypus.distance module
========================

.. automodule:: platypus.distance
:members:
:undoc-members:
6 changes: 6 additions & 0 deletions docs/api/platypus.errors.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
platypus.errors module
======================

.. automodule:: platypus.errors
:members:
:undoc-members:
6 changes: 6 additions & 0 deletions docs/api/platypus.evaluator.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
platypus.evaluator module
=========================

.. automodule:: platypus.evaluator
:members:
:undoc-members:
6 changes: 6 additions & 0 deletions docs/api/platypus.experimenter.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
platypus.experimenter module
============================

.. automodule:: platypus.experimenter
:members:
:undoc-members:
6 changes: 6 additions & 0 deletions docs/api/platypus.filters.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
platypus.filters module
=======================

.. automodule:: platypus.filters
:members:
:undoc-members:
6 changes: 6 additions & 0 deletions docs/api/platypus.indicators.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
platypus.indicators module
==========================

.. automodule:: platypus.indicators
:members:
:undoc-members:
6 changes: 6 additions & 0 deletions docs/api/platypus.io.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
platypus.io module
==================

.. automodule:: platypus.io
:members:
:undoc-members:
6 changes: 6 additions & 0 deletions docs/api/platypus.mpipool.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
platypus.mpipool module
=======================

.. automodule:: platypus.mpipool
:members:
:undoc-members:
6 changes: 6 additions & 0 deletions docs/api/platypus.operators.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
platypus.operators module
=========================

.. automodule:: platypus.operators
:members:
:undoc-members:
6 changes: 6 additions & 0 deletions docs/api/platypus.problems.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
platypus.problems module
========================

.. automodule:: platypus.problems
:members:
:undoc-members:
21 changes: 21 additions & 0 deletions docs/api/platypus.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Platypus API
============

.. toctree::
:maxdepth: 2

platypus.algorithms
platypus.config
platypus.core
platypus.distance
platypus.errors
platypus.evaluator
platypus.experimenter
platypus.filters
platypus.indicators
platypus.io
platypus.mpipool
platypus.operators
platypus.problems
platypus.types
platypus.weights
6 changes: 6 additions & 0 deletions docs/api/platypus.types.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
platypus.types module
=====================

.. automodule:: platypus.types
:members:
:undoc-members:
6 changes: 6 additions & 0 deletions docs/api/platypus.weights.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
platypus.weights module
=======================

.. automodule:: platypus.weights
:members:
:undoc-members:
9 changes: 8 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'sphinx.ext.autodoc',
'sphinx.ext.doctest',
'sphinx.ext.imgmath',
'sphinx_rtd_theme',
]

templates_path = ['_templates']
Expand All @@ -27,5 +28,11 @@
# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = 'alabaster'
html_theme = 'sphinx_rtd_theme'
html_static_path = ['_static']

# The initial api/ contents were created with:
# sphinx-apidoc --separate --remove-old --no-toc -o docs/api platypus "*test*"
#
# Build these docs locally with:
# sphinx-build -M html . _build
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ optimization.
getting-started
types
experimenter
api/platypus
103 changes: 95 additions & 8 deletions platypus/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,26 @@ def run(self):
self.solution.evaluate()

class Algorithm(metaclass=ABCMeta):
"""Base class for all optimization algorithms.
Parameters
----------
problem : Problem
The problem being optimized.
evaluator : Evaluator
The evalutor used to evaluate solutions. If `None`, the default
evaluator defined in :attr:`PlatypusConfig` is selected.
log_frequency : int
The frequency to log evaluation progress. If `None`, the default
log frequency defined in :attr:`PlatypusConfig` is selected.
Attributes
----------
nfe : int
The current number of function evaluations (NFE)
result: list or Archive
The current result, which is updated after each iteration.
"""

def __init__(self,
problem,
Expand Down Expand Up @@ -371,6 +391,17 @@ def step(self):
pass

def evaluate_all(self, solutions):
"""Evaluates all of the given solutions.
Subclasses should prefer using this method to evaluate solutions,
ideally providing an entire population to leverage parallelization,
as it tracks NFE.
Parameters
----------
solutions : list of Solution
The solutions to evaluate.
"""
unevaluated = [s for s in solutions if not s.evaluated]

jobs = [_EvaluateJob(s) for s in unevaluated]
Expand All @@ -389,6 +420,17 @@ def evaluate_all(self, solutions):
self.nfe += len(solutions)

def run(self, condition, callback=None):
"""Runs this algorithm until the termination condition is reached.
Parameters
----------
condition : int or TerminationCondition
The termination condition. Providing an integer value is converted
into the :class:`MaxEvaluations` condition.
callback : Callable, optional
Callback function that is invoked after every iteration. The
callback is passed this algorithm instance.
"""
if isinstance(condition, int):
condition = MaxEvaluations(condition)

Expand Down Expand Up @@ -438,6 +480,22 @@ def _constraint_gt(x, y, delta=0.0001):
return 0 if x > y else abs(x - y) + delta

class Constraint:
"""Defines an constraint on an optimization problem.
A constraint can be defined in several ways. First, with a given operator
and value::
Constraint("<=", 10)
Second, by providing a string with the operator and value together::
Constraint("<= 10")
Third, by providing a function to compute the constraint value, where any
non-zero value is considered a constraint violation::
Constraint(lambda x : 0 if x <= 10 else math.abs(10 - x))
"""

OPERATORS = {
"==": _constraint_eq,
Expand Down Expand Up @@ -549,10 +607,10 @@ def __call__(self, solution1, solution2):

@abstractmethod
def compare(self, solution1, solution2):
"""Compare two solutions.
"""Compare two solutions for dominance.
Returns -1 if the first solution dominates the second, 1 if the
second solution dominates the first, or 0 if the two solutions are
Returns `-1` if the first solution dominates the second, `1` if the
second solution dominates the first, or `0` if the two solutions are
mutually non-dominated.
Parameters
Expand Down Expand Up @@ -624,7 +682,7 @@ class EpsilonDominance(Dominance):
Similar to Pareto dominance except if the two solutions are contained
within the same epsilon-box, the solution closer to the optimal corner
or the box is preferred.
of the box is preferred.
"""

def __init__(self, epsilons):
Expand Down Expand Up @@ -997,12 +1055,41 @@ def add(self, solution):
return True

def nondominated(solutions):
"""Returns the non-dominated solutions."""
"""Filters the solutions to only include non-dominated.
Parameters
----------
solutions : iterable of Solution
The solutions to filter.
Returns
-------
list of Solution
The non-dominated solutions.
"""
archive = Archive()
archive += solutions
return archive._contents

def nondominated_sort_cmp(x, y):
"""Compares two solutions using nondominated sorting results.
After processing a population with :func:`nondominated_sort`, this
comparison function can be used to order solutions by their rank and
crowding distance.
Parameters
----------
x : Solution
The first solution.
y : Solution
The second solution.
Returns
-------
:code:`-1`, :code:`0`, or :code:`1` to indicate if :code:`x` is better,
equal, or worse than :code:`y` based on rank and crowding distance.
"""
if x.rank == y.rank:
if -x.crowding_distance < -y.crowding_distance:
return -1
Expand Down Expand Up @@ -1032,7 +1119,7 @@ def nondominated_sort(solutions):
Parameters
----------
solutions : iterable
solutions : iterable of Solution
The collection of solutions
"""
rank = 0
Expand All @@ -1058,7 +1145,7 @@ def crowding_distance(solutions):
Parameters
----------
solutions : iterable
solutions : iterable of Solution
The collection of solutions
"""
for solution in solutions:
Expand Down Expand Up @@ -1099,7 +1186,7 @@ def nondominated_split(solutions, size):
Parameters
----------
solutions : iterable
solutions : iterable of Solution
The collection of solutions that have been non-dominated sorted
size : int
The size of the truncated result
Expand Down
Loading

0 comments on commit d53f67b

Please sign in to comment.