项目作者: tcompa

项目描述 :
General-purpose simulated-annealing optimization.
高级语言: Python
项目地址: git://github.com/tcompa/anneal.git
创建时间: 2016-04-19T08:32:50Z
项目社区:https://github.com/tcompa/anneal

开源协议:MIT License

下载


anneal

Build Status
DOI

Anneal implements a general-purpose simulated-annealing optimization algorithm (originally written for a sudoku solver).
The optimization procedure is decoupled from the optimization problem, so that it can be used in a large set of different cases.

You can follow these steps to install anneal, or just give it a try without installing it.

Install anneal

To install anneal, follow these instructions:

Using pip

If you use pip as a package manager, anneal can be installed via

  1. pip install https://github.com/tcompa/anneal/archive/v1.0.zip

(for version 1.0).

Manual install

Clone this repository, then use the setup.py script:

  1. git clone git@github.com:tcompa/anneal.git .
  2. cd anneal
  3. python setup.py install --record installed_files.txt

(the --record option is helpful to later uninstall this package).
Warning: this procedure will install the current development version.

Give it a try (without installing)

If you prefer not to install this package, just copy the file
anneal.py in your working directory, and proceed as in the How to use anneal section.

Versions and requirements

Anneal is tested on python 2.7 and 3.4, without additional dependencies.
On python 2.7, the future package is required for the tests and for some examples.
Some of the examples additionally require numpy (version >=1.10) and matplotlib (version >=1.5).

How to use anneal

First, you need to define a class which describes your optimization problem.
This class needs to include (at least) the following attributes and methods:

  • Attributes:
    • beta: the inverse temperature (could be the ‘real’ inverse temperature, or an artificial parameter).
    • energy: the quantity which will be minimised.
  • Methods:
    • set_beta(beta): change the value of beta.
    • MC_move(): perform a Monte Carlo move, and return 1 or 0 if this is accepted/rejected.
    • update_MC_parameters(acc_ratio): update the Monte Carlo parameters, trying to keep the acceptance ratio in a reasonable interval.

Then you can import the annealing function via

  1. from anneal import simulated_annealing

and use it on an instance of your class (see examples below).

Examples

A simple example of how to use anneal is the following.
First, we define the Potential_1d class, as

  1. class Potential_1d(object):
  2. '''Naive class, to test the simulated-annealing function.
  3. '''
  4. def __init__(self):
  5. self.x = 10.0
  6. self.beta = 1e8
  7. self.energy = self.compute_energy(self.x)
  8. self.dx = 0.2
  9. def compute_energy(self, _x):
  10. return 0.5 * _x ** 2 * math.cos(_x) ** 2
  11. def set_beta(self, beta):
  12. self.beta = beta
  13. def update_MC_parameters(self, acc_ratio):
  14. if acc_ratio < 0.2 and self.dx > 0.01:
  15. self.dx *= 0.90909090909090909090
  16. elif acc_ratio > 0.8 and self.dx < 1.0:
  17. self.dx *= 1.1
  18. def MC_move(self):
  19. xnew = random.uniform(self.x - self.dx, self.x + self.dx)
  20. E_old = self.energy
  21. E_new = self.compute_energy(xnew)
  22. dE = E_new - E_old
  23. if dE < 0.0 or random.random() < math.exp(- self.beta * dE):
  24. self.x = xnew
  25. self.energy = E_new
  26. return 1
  27. else:
  28. return 0

Then we call the simulated_annealing function via

  1. from anneal import simulated_annealing
  2. P = Potential_1d()
  3. ID = 'Vx_1d'
  4. P, E, t = simulated_annealing(P, ID, beta_min=1e-2, beta_max=1e2,
  5. cooling_rate=1e-2, n_steps_per_T=1000,
  6. quench_to_T0=True, n_steps_T0=5000)

The output E is a list of the values of the energy during the optimization.

More examples are available in the examples directory.

Note

The simulated-annealing library itself is not optimized in any way, assuming
that the time-consuming part of the code is somewhere in the class defining the
optimization problem (e.g. in the Monte Carlo moves).