GithubHelp home page GithubHelp logo

cuplgb / stochopy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from keurfonluu/stochopy

0.0 1.0 0.0 659 KB

StochOPy (STOCHastic OPtimization for PYthon) provides user-friendly routines to sample or optimize objective functions with the most popular algorithms.

License: MIT License

Python 100.00%

stochopy's Introduction

StochOPy

examples/stochopy_viewer.png

StochOPy (STOCHastic OPtimization for PYthon) provides user-friendly routines to sample or optimize objective functions with the most popular algorithms.

Version:1.7.3
Author:Keurfon Luu
Web site:https://github.com/keurfonluu/stochopy
Copyright:This document has been placed in the public domain.
License:StochOPy is released under the MIT License.

NOTE: StochOPy has been implemented in the frame of my Ph. D. thesis. If you find any error or bug, or if you have any suggestion, please don't hesitate to contact me.

Features

StochOPy provides routines for sampling of a model parameter space:

  • Pure Monte-Carlo
  • Metropolis-Hastings algorithm
  • Hamiltonian (Hybrid) Monte-Carlo [1] [2]

or optimization of an objective function:

  • Differential Evolution [3]
  • Particle Swarm Optimization [4] [5]
  • Competitive Particle Swarm Optimization [6]
  • Covariance Matrix Adaptation - Evolution Strategy [7]
  • VD-CMA [8]

Installation

The recommended way to install StochOPy is through pip (internet required):

pip install stochopy

Otherwise, download and extract the package, then run:

python setup.py install

Usage

New in 1.4.0: added support for MPI for evolutionary algorithms (you may need to install the package MPI4PY beforehand). Run the example script inside the folder examples:

mpiexec -n 4 python example_mpi.py

Note that StochOPy still work even though MPI4PY is not installed.

New in 1.3.0: run StochOPy Viewer to see how popular stochastic algorithms work, and play with the tuning parameters on several benchmark functions.

from stochopy.gui import main

main()

First, import StochOPy and define an objective function (here Rosenbrock):

import numpy as np
from stochopy import MonteCarlo, Evolutionary

f = lambda x: 100*np.sum((x[1:]-x[:-1]**2)**2)+np.sum((1-x[:-1])**2)

You can define the search space boundaries if necessary:

n_dim = 2
lower = np.full(n_dim, -5.12)
upper = np.full(n_dim, 5.12)

Initialize the Monte-Carlo sampler:

max_iter = 1000
mc = MonteCarlo(f, lower = lower, upper = upper, max_iter = max_iter)

Now, you can start sampling with the simple method 'sample':

mc.sample(sampler = "hamiltonian", stepsize = 0.005, n_leap = 20, xstart = [ 2., 2. ])

Note that sampler can be set to "pure" or "hastings" too. The models sampled and their corresponding energies are stored in:

print(mc.models)
print(mc.energy)

Optimization is just as easy:

n_dim = 10
lower = np.full(n_dim, -5.12)
upper = np.full(n_dim, 5.12)
popsize = 4 + np.floor(3.*np.log(n_dim))
ea = Evolutionary(f, lower = lower, upper = upper, popsize = popsize, max_iter = max_iter)
xopt, gfit = ea.optimize(solver = "cmaes")
print(xopt)
print(gfit)

Related works

  • StochOPy WebApp: StochOPy WebApp allows the users to see how popular stochastic algorithms perform on different benchmark functions.
  • StochANNPy: StochANNPy (STOCHAstic Artificial Neural Network for PYthon) provides user-friendly routines compatible with Scikit-Learn for stochastic learning.
  • StochOptim: StochOptim provides user friendly functions written in modern Fortran to solve optimization problems using stochastic algorithms in a parallel environment (MPI).

References

[1]S. Duane, A. D. Kennedy, B. J. Pendleton and D. Roweth, Hybrid Monte Carlo, Physics Letters B., 1987, 195(2): 216-222
[2]N. Radford, MCMC Using Hamiltonian Dynamics, Handbook of Markov Chain Monte Carlo, Chapman and Hall/CRC, 2011
[3]R. Storn and K. Price, Differential Evolution - A Simple and Efficient Heuristic for global Optimization over Continuous Spaces, Journal of Global Optimization, 1997, 11(4): 341-359
[4]J. Kennedy and R. Eberhart, Particle swarm optimization, Proceedings of ICNN'95 - International Conference on Neural Networks, 1995, 4: 1942-1948
[5]F. Van Den Bergh, An analysis of particle swarm optimizers, University of Pretoria, 2001
[6]K. Luu, M. Noble, A. Gesret, N. Balayouni and P.-F. Roux, A parallel competitive Particle Swarm Optimization for non-linear first arrival traveltime tomography and uncertainty quantification, Computers & Geosciences, 2018, 113: 81-93
[7]N. Hansen, The CMA evolution strategy: A tutorial, Inria, Université Paris-Saclay, LRI, 2011, 102: 1-34
[8]Y. Akimoto, A. Auger and N. Hansen, Comparison-Based Natural Gradient Optimization in High Dimension, Proceedings of the 2014 conference on Genetic and evolutionary computation, 2014, 373-380

stochopy's People

Contributors

keurfonluu avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.