Skip to main content

Python library for stochastic numerical optimization

Project description

License Stars Pyversions Version Downloads Code style: black Codacy Badge Codecov Build Travis

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.

sample-pso

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),

  • 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

def rosenbrock(x):
   x = numpy.asarray(x)
   sum1 = ((x[1:] - x[:-1] ** 2) ** 2).sum()
   sum2 = numpy.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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stochopy-2.0.1.tar.gz (27.8 kB view details)

Uploaded Source

Built Distribution

stochopy-2.0.1-py3-none-any.whl (38.5 kB view details)

Uploaded Python 3

File details

Details for the file stochopy-2.0.1.tar.gz.

File metadata

  • Download URL: stochopy-2.0.1.tar.gz
  • Upload date:
  • Size: 27.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0.post20201006 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.7

File hashes

Hashes for stochopy-2.0.1.tar.gz
Algorithm Hash digest
SHA256 ef9e4ded67be1a09f694b8ad869013c2ee8b1cc44ac3b4d8a6ae864185ff4c0b
MD5 b99a030eff8f0f58c919a65ee5b55156
BLAKE2b-256 9115f1977097f549fd01981d2a777be72c5732099bf05d12b7efb7a563bbfb94

See more details on using hashes here.

File details

Details for the file stochopy-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: stochopy-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 38.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0.post20201006 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.7

File hashes

Hashes for stochopy-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a35d1021df906f38e076aee4092926a39f59abc156054716b8d919d2034ce9bd
MD5 72abbfde824530c1a187629542c0492e
BLAKE2b-256 c23355a7429418e1061f89506113fe880a50711fc9dcbf6899cac589b77ddfe7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page