stochopy provides functions for sampling or optimizing objective functions with or without constraints. Its API is directly inspired by scipy’s own optimization submodule which should make the switch from one module to another straightforward.
Optimization of 2D multimodal function Styblinski-Tang using PSO.
Features
Sampling algorithms:
Hamiltonian (Hybrid) Monte-Carlo (HMC),
Markov-Chain Monte-Carlo (McMC).
Stochastic optimizers:
Competitive Particle Swarm Optimization (CPSO),
Covariance Matrix Adaptation - Evolution Strategy (CMA-ES),
Differential Evolution (DE),
Neighborhood Algorithm (NA),
Particle Swarm Optimization (PSO),
VD-CMA.
Parallel backends:
Installation
The recommended way to install stochopy and all its dependencies is through the Python Package Index:
pip install stochopy --user
Otherwise, clone and extract the package, then run from the package location:
pip install . --user
To test the integrity of the installed package, check out this repository and run:
pytest
Documentation
Refer to the online documentation for detailed description of the API and examples.
Alternatively, the documentation can be built using Sphinx:
pip install -r doc/requirements.txt
sphinx-build -b html doc/source doc/build
Usage
Given an optimization problem defined by an objective function and a feasible space:
import numpy as np
def rosenbrock(x):
x = np.asarray(x)
sum1 = ((x[1:] - x[:-1] ** 2) ** 2).sum()
sum2 = np.square(1.0 - x[:-1]).sum()
return 100.0 * sum1 + sum2
bounds = [[-5.12, 5.12], [-5.12, 5.12]] # The number of variables to optimize is len(bounds)
The optimal solution can be found following:
from stochopy.optimize import minimize
x = minimize(rosenbrock, bounds, method="cmaes", options={"maxiter": 100, "popsize": 10, "seed": 0})
minimize returns a dictionary that contains the results of the optimization:
fun: 3.862267657514075e-09
message: 'best solution value is lower than ftol'
nfev: 490
nit: 49
status: 1
success: True
x: array([0.99997096, 0.99993643])
Contributing
Please refer to the Contributing Guidelines to see how you can help. This project is released with a Code of Conduct which you agree to abide by when contributing.
Release files for stochopy 2.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| stochopy-2.3.0.tar.gz | 31.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| stochopy-2.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 76.7 kB
Release files / stochopy-2.3.0.tar.gz
| Download URL | stochopy-2.3.0.tar.gz |
|---|---|
| Size | 31.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
556dda0e2eb9b32189a9067a6dafc5c8056d52d2685b3caf8ee9ebbb518e5a7f
|
|
BLAKE2b-256 checksum How to use checksums |
21e8ae6ed7239ed373fb3d3b92a2f7775884ec5d7756171f752ffadc90af021c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.15
|
Release files / stochopy-2.3.0-py3-none-any.whl
| Download URL | stochopy-2.3.0-py3-none-any.whl |
|---|---|
| Size | 44.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
90a5c25b851c41c6f24ee32c3b3db35a04c21ed01380bb20b46f2e40064949c6
|
|
BLAKE2b-256 checksum How to use checksums |
3b601fa7d20ee4c9b24b0e6f55374ac0e620fc9e369b32726a3f78715acc1e67
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.15
|