Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6,683 changes: 2,729 additions & 3,954 deletions docs/source/algorithms.md

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the algorithm examples, the value passed to params parameter is a list, and the lambda function passed to fun does a matrix multiplication. If someone tries to directly copy and paste the example, it fails. How about changing the lambda function to lambda x: sum(v**2 for v in x) or the list into a NumPy array, and also removing the ellipsis from the algorithm options?


For example:

import optimagic as om

om.minimize(
    fun=lambda x: sum(v**2 for v in x),
    params=[1.0, 2.0, 3.0],
    algorithm=om.algos.scipy_neldermead(stopping_maxiter=1_000),
)

Or

import numpy as np
import optimagic as om

om.minimize(
    fun=lambda x: x @ x,
    params=np.array([1.0, 2.0, 3.0]),
    algorithm=om.algos.scipy_neldermead(stopping_maxiter=1_000),
)

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions docs/source/how_to/how_to_add_optimizers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -421,12 +421,13 @@
" needs_jac=False,\n",
" # does the optimizer need the hessian? -> no, gaco is derivative free\n",
" needs_hess=False,\n",
" # does the optimizer require finite bounds? -> yes\n",
" # does the optimizer need bounds? -> yes, gaco needs finite bounds\n",
" needs_bounds=True,\n",
" # does the optimizer support parallelism? -> yes\n",
" supports_parallelism=True,\n",
" # does the optimizer support bounds? -> yes\n",
" supports_bounds=True,\n",
" # does the optimizer support infinite bounds? -> no, bounds must be finite\n",
" supports_infinite_bounds=False,\n",
" # does the optimizer support linear constraints? -> no\n",
" supports_linear_constraints=False,\n",
Expand All @@ -435,7 +436,7 @@
" # should the history be disabled? -> no\n",
" disable_history=False,\n",
")\n",
"# All algortihms need to be frozen dataclasses.\n",
"# All algorithms need to be frozen dataclasses.\n",
"@dataclass(frozen=True)\n",
"class PygmoGaco(Algorithm):\n",
" # for now only set one parameter to get things running. The rest will come later.\n",
Expand Down Expand Up @@ -606,11 +607,11 @@
"of commonly used convergence and stopping criteria. We also align the default values for \n",
"stopping and convergence criteria as much as possible. \n",
"\n",
"You can find the harmonized names and value [here](algo_options_docs). \n",
"You can find the harmonized names and value {ref}`here <algo_options>`. \n",
"\n",
"To align the names of other tuning parameters as much as possible with what is already \n",
"there, simple have a look at the optimizers we already wrapped. For example, if you are \n",
"wrapping a bfgs or lbfgs algorithm from some libray, try to look at all existing wrappers \n",
"there, simply have a look at the optimizers we already wrapped. For example, if you are \n",
"wrapping a bfgs or lbfgs algorithm from some library, try to look at all existing wrappers \n",
"of bfgs algorithms and use the same names for the same options. \n",
"\n",
"You can see what this means for the gaco algorithm [here](\n",
Expand Down
62 changes: 46 additions & 16 deletions docs/source/how_to/how_to_document_optimizers.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ Here's how we document `ScipyLBFGSB`:
class ScipyLBFGSB(Algorithm):
"""Minimize a scalar differentiable function using the L-BFGS-B algorithm.

The optimizer is taken from scipy, which calls the Fortran code written by the
original authors of the algorithm. The Fortran code includes the corrections
and improvements that were introduced in a follow up paper.
The optimizer is taken from scipy, which uses the code written by the original
authors of the algorithm (L-BFGS-B v3.0). This code includes the corrections and
improvements that were introduced in a follow up paper. Since SciPy 1.15, SciPy
uses a C translation of the original Fortran code; older versions call the
Fortran code directly.

lbfgsb is a limited memory version of the original bfgs algorithm, that deals with
lower and upper bounds via an active set approach.
Expand All @@ -110,7 +112,7 @@ class ScipyLBFGSB(Algorithm):
**What makes this docstring effective:**

- **Clear first line**: States exactly what the algorithm does
- **Implementation details**: Explains it uses scipy's Fortran implementation
- **Implementation details**: Explains which implementation scipy uses
- **Algorithm classification**: Identifies it as a quasi-Newton method
- **Problem suitability**: Explains what problems it's good for
- **How it works**: Brief explanation of the algorithm's approach
Expand All @@ -130,7 +132,9 @@ stopping_maxiter: PositiveInt = STOPPING_MAXITER
# Parameter with mathematical formulation
convergence_ftol_rel: NonNegativeFloat = CONVERGENCE_FTOL_REL
r"""Converge if the relative change in the objective function is less than this
value. More formally, this is expressed as.
value.

More formally, this is expressed as

.. math::

Expand Down Expand Up @@ -173,6 +177,18 @@ more memory but may converge faster for some problems.
Without this, type hints such as ``PositiveInt`` may appear decomposed in the
documentation (e.g., as ``Annotated[int, Gt(gt=0)]``).

.. warning::
Option values are validated against the field annotations with pydantic, which
resolves the annotations at runtime. Every name used in a field annotation must
therefore be importable at runtime -- a name that is only imported inside an
``if TYPE_CHECKING:`` block will raise an error when the algorithm is
instantiated. If an optional dependency provides a type you want to use in an
annotation, add a runtime fallback next to the ``TYPE_CHECKING`` import, as is
done for ``HessianApproximation`` in ``optimagic/optimizers/fides.py``.

Since annotations are enforced, they must also be honest: document and annotate
the values the optimizer actually accepts, rather than a narrower set.

```

### Step 4: Integrate into `algorithms.md`
Expand All @@ -183,42 +199,56 @@ This creates a dropdown section that shows users how to use the algorithm.
Add the following to `docs/source/algorithms.md` in an `eval-rst` block:

```text
.. dropdown:: scipy_lbfgsb
.. dropdown:: scipy_lbfgsb

**How to use this algorithm:**

.. code-block:: python

import numpy as np
import optimagic as om

om.minimize(
fun=lambda x: x @ x,
params=[1.0, 2.0, 3.0],
algorithm=om.algos.scipy_lbfgsb(stopping_maxiter=1_000, ...),
fun=lambda x: x @ x,
params=np.array([1.0, 2.0, 3.0]),
algorithm=om.algos.scipy_lbfgsb(stopping_maxiter=1_000),
)

or using the string interface:

.. code-block:: python

import numpy as np
import optimagic as om

om.minimize(
fun=lambda x: x @ x,
params=[1.0, 2.0, 3.0],
algorithm="scipy_lbfgsb",
algo_options={"stopping_maxiter": 1_000, ...},
fun=lambda x: x @ x,
params=np.array([1.0, 2.0, 3.0]),
algorithm="scipy_lbfgsb",
algo_options={"stopping_maxiter": 1_000},
)

**Description and available options:**

.. autoclass:: optimagic.optimizers.scipy_optimizers.ScipyLBFGSB
:members:
:inherited-members: Algorithm, object
```

**What this section provides:**

- **The dropdown button and title**: Makes it easy to find the algorithm
- **Concrete usage examples** showing both the object and string interfaces
- **Algorithm-specific parameter** in the usage example
- **Copy-pasteable code**: Each code block is self-contained and runs as is. Pass
`params` as a NumPy array so that `x @ x` works, don't use `...` placeholders, add
finite `bounds` if the algorithm needs them, and mark least-squares or likelihood
functions with `om.mark.least_squares` or `om.mark.likelihood`
- **Auto-generated documentation** via the `autoclass` directive that pulls in your
docstrings
docstrings. The `:inherited-members: Algorithm, object` option is required:
`mark.minimizer` re-creates the algorithm class as a pydantic dataclass that inherits
from your class, so without it autodoc treats your documented fields as inherited and
does not show them

## Working with Existing Documentation

Expand Down
199 changes: 194 additions & 5 deletions docs/source/refs.bib
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ @Article{Gablonsky2001
}

@Article{DaSilva2010,
author = {Da Silva, C and Santos, M and Goncalves, H and Hernandez-Figueroa},
author = {{da Silva Santos}, C. H. and Gon{\c{c}}alves, M. S. and Hern{\'a}ndez-Figueroa, H. E.},
journal = {IEEE Photonics Technology Letters},
title = {Designing Novel Photonic Devices by Bio-Inspired Computing},
year = {2010},
Expand All @@ -621,8 +621,8 @@ @Article{DaSilva2010
}

@Misc{DaSilva2010a,
author = {Da Silva, C and Santos},
title = {Parallel and Bio-Inspired Computing Applied to Analyze Microwave and Photonic Metamaterial Strucutures},
author = {{da Silva Santos}, C. H.},
title = {Parallel and Bio-Inspired Computing Applied to Analyze Microwave and Photonic Metamaterial Structures},
year = {2010},
}

Expand Down Expand Up @@ -658,7 +658,7 @@ @Article{PhilipRunarsson2005
}

@Article{Thomas2000,
author = {Thomas, P and Runarsson, Xin and Yao},
author = {Runarsson, Thomas P. and Yao, Xin},
journal = {IEEE Trans. Evolutionary Computation},
title = {Stochastic ranking for constrained evolutionary optimization},
year = {2000},
Expand Down Expand Up @@ -712,7 +712,7 @@ @InProceedings{Powell2004
@Article{Dembo1983,
author = {Dembo, R and Steihaug, T},
journal = {Math. Programming},
title = {Truncated Newton algorithms for large-scale optimization},
title = {Truncated-{N}ewton algorithms for large-scale unconstrained optimization},
year = {1983},
pages = {190--212},
volume = {26},
Expand Down Expand Up @@ -1114,6 +1114,195 @@ @article{Ni2013
year = {2013}
}

@Article{Froehlich2022,
author = {Fabian Fr{\"o}hlich and Peter K. Sorger},
journal = {PLOS Computational Biology},
title = {Fides: Reliable trust-region optimization for parameter estimation of ordinary differential equation models},
year = {2022},
month = {jul},
number = {7},
pages = {e1010322},
volume = {18},
doi = {10.1371/journal.pcbi.1010322},
publisher = {Public Library of Science ({PLoS})},
}

@Misc{Johnson2007,
author = {Johnson, Steven G.},
title = {The NLopt nonlinear-optimization package},
year = {2007},
url = {https://github.com/stevengj/nlopt},
}

@Article{Svanberg1987,
author = {Svanberg, Krister},
journal = {International Journal for Numerical Methods in Engineering},
title = {The method of moving asymptotes---a new method for structural optimization},
year = {1987},
number = {2},
pages = {359--373},
volume = {24},
doi = {10.1002/nme.1620240207},
}

@Article{LeeWiswall2007,
author = {Lee, Donghoon and Wiswall, Matthew},
journal = {Computational Economics},
title = {A Parallel Implementation of the Simplex Function Minimization Routine},
year = {2007},
number = {2},
pages = {171--187},
volume = {30},
doi = {10.1007/s10614-007-9094-2},
}

@Article{Wessing2019,
author = {Wessing, Simon},
journal = {Optimization Letters},
title = {Proper initialization is crucial for the Nelder--Mead simplex search},
year = {2019},
pages = {847--856},
volume = {13},
doi = {10.1007/s11590-018-1284-4},
}

@Book{Nash1990,
author = {Nash, John C.},
publisher = {Adam Hilger},
title = {Compact Numerical Methods for Computers: Linear Algebra and Function Minimisation},
year = {1990},
address = {Bristol},
edition = {2nd},
}

@Misc{Varadhan2016,
author = {Varadhan, Ravi and Borchers, Hans W.},
title = {dfoptim: Derivative-Free Optimization},
year = {2016},
note = {R package version 2016.7-1},
url = {https://CRAN.R-project.org/package=dfoptim},
}

@Article{Powell1964,
author = {Powell, M. J. D.},
journal = {The Computer Journal},
title = {An efficient method for finding the minimum of a function of several variables without calculating derivatives},
year = {1964},
number = {2},
pages = {155--162},
volume = {7},
doi = {10.1093/comjnl/7.2.155},
}

@Article{Branch1999,
author = {Branch, Mary Ann and Coleman, Thomas F. and Li, Yuying},
journal = {SIAM Journal on Scientific Computing},
title = {A Subspace, Interior, and Conjugate Gradient Method for Large-Scale Bound-Constrained Minimization Problems},
year = {1999},
number = {1},
pages = {1--23},
volume = {21},
doi = {10.1137/S1064827595289108},
}

@InProceedings{Voglis2004,
author = {Voglis, C. and Lagaris, I. E.},
booktitle = {WSEAS International Conference on Applied Mathematics},
title = {A Rectangular Trust Region Dogleg Approach for Unconstrained and Bound Constrained Nonlinear Optimization},
year = {2004},
address = {Corfu, Greece},
url = {https://www.cs.uoi.gr/~lagaris/papers/PREPRINTS/dogbox.pdf},
}

@InCollection{More1978,
author = {Mor{\'e}, Jorge J.},
booktitle = {Numerical Analysis},
editor = {Watson, G. A.},
pages = {105--116},
publisher = {Springer},
series = {Lecture Notes in Mathematics},
title = {The Levenberg-Marquardt algorithm: Implementation and theory},
volume = {630},
year = {1978},
doi = {10.1007/BFb0067700},
}

@Article{Nash1984,
author = {Nash, Stephen G.},
journal = {SIAM Journal on Numerical Analysis},
title = {Newton-Type Minimization via the Lanczos Method},
year = {1984},
number = {4},
pages = {770--788},
volume = {21},
doi = {10.1137/0721052},
}

@Article{Endres2018,
author = {Endres, Stefan C. and Sandrock, Carl and Focke, Walter W.},
journal = {Journal of Global Optimization},
title = {A simplicial homology algorithm for {Lipschitz} optimisation},
year = {2018},
number = {2},
pages = {181--217},
volume = {72},
doi = {10.1007/s10898-018-0645-y},
}

@Article{Tsallis1988,
author = {Tsallis, Constantino},
journal = {Journal of Statistical Physics},
title = {Possible generalization of {Boltzmann-Gibbs} statistics},
year = {1988},
number = {1-2},
pages = {479--487},
volume = {52},
doi = {10.1007/BF01016429},
}

@Article{TsallisStariolo1996,
author = {Tsallis, Constantino and Stariolo, Daniel A.},
journal = {Physica A: Statistical Mechanics and its Applications},
title = {Generalized simulated annealing},
year = {1996},
number = {1-2},
pages = {395--406},
volume = {233},
doi = {10.1016/S0378-4371(96)00271-3},
}

@Article{Xiang1997,
author = {Xiang, Y. and Sun, D. Y. and Fan, W. and Gong, X. G.},
journal = {Physics Letters A},
title = {Generalized simulated annealing algorithm and its application to the {Thomson} model},
year = {1997},
number = {3},
pages = {216--220},
volume = {233},
doi = {10.1016/S0375-9601(97)00474-X},
}

@article{ChiangZavala2016,
author = {Chiang, Nai-Yuan and Zavala, Victor M.},
title = {An Inertia-Free Filter Line-Search Algorithm for Large-Scale Nonlinear Programming},
journal = {Computational Optimization and Applications},
year = {2016},
volume = {64},
number = {2},
pages = {327--354},
doi = {10.1007/s10589-015-9820-y}
}

@techreport{Gabler2024,
author = {Gabler, Jano{\'s} and Gsell, Sebastian and Mensinger, Tim and Petrosyan, Mariam},
title = {Tranquilo: An Optimizer for the Method of Simulated Moments},
institution = {CRC TR 224, University of Bonn and University of Mannheim},
type = {Discussion Paper},
number = {522},
year = {2024},
url = {https://www.crctr224.de/research/discussion-papers/archive/dp522}
}

@article{LeDigabel2024,
author = {Le Digabel, S{\'e}bastien and Wild, Stefan M.},
title = {A Taxonomy of Constraints in Black-Box Simulation-Based Optimization},
Expand Down
Loading
Loading