Giter Site home page Giter Site logo

lanl / pyharmonysearch Goto Github PK

View Code? Open in Web Editor NEW
79.0 9.0 23.0 121 KB

pyHarmonySearch is a pure Python implementation of the harmony search (HS) global optimization algorithm.

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%
harmony-search python optimization-algorithms applied-mathematics heuristic-search-algorithms

pyharmonysearch's Introduction

pyHarmonySearch

LICENSE

This software is licensed under the BSD 3-Clause License. Please refer to the separate LICENSE file for the exact text of the license. You are obligated to give attribution if you use this code.

ABOUT

pyHarmonySearch is a pure Python implementation of the harmony search (HS) global optimization algorithm. HS is a metaheuristic search algorithm that, similar to simulated annealing, tabu, and evolutionary searches, is based on real world phenomena. Specifically, HS mimics a jazz band improvising together. Courtesy Wikipedia:

In the HS algorithm, each musician (= decision variable) plays (= generates) a note (= a value) for finding a best harmony (= global optimum) all together.

pyHarmonySearch supports both continuous and discrete variables and can take advantage of parallel processing using Python's multiprocessing module.

REQUIREMENTS

This code does not rely on any 3rd party software. It only requires Python 3.7 or higher.

INSTALL

pyHarmonySearch is available on PyPI at https://pypi.org/project/pyHarmonySearch/.

Install using pip:

pip install pyHarmonySearch

Install from source:

python setup.py install

USING THIS CODE

There are five examples included. The first three examples (2-D_continuous_seed.py, 2-D_discrete_x.py, and 2-D_continuous_fixed_x.py) are variations on each other that find the global maximum of a simple 2-D function. The fourth example (5-D_linear_system.py) stochastically solves a 5-D linear system of equations. The fifth example (2-D_continuous_specified_initial_harmonies.py). Read the documentation in each example for more information.

> ./2-D_continuous_fixed_x.py
Elapsed time: 0:00:23.396807
Best harmony: [0.5, -1.0001700408343779]
Best fitness: 3.7499999710861145
> ./2-D_continuous_seed.py
Elapsed time: 0:00:03.119576
Best harmony: [0.027035817109283933, -0.9950109785422445]
Best fitness: 3.9992441742581275
> ./2-D_discrete_x.py
Elapsed time: 0:00:30.877592
Best harmony: [0, -0.9987024210158837]
Best fitness: 3.9999983162887798
> ./5-D_linear_system.py
Elapsed time: 0:02:29.715337
Best harmony: [8.118886532259536, 2.0254098515892665, 0.6678692319283357, 2.906307072622585, 9.814436850217918]
Best fitness: 0.8690865628414204
> ./2-D_continuous_specified_initial_harmonies.py
Elapsed time: 0:00:02.788820
Best harmony: [-0.0017887282724807774, -0.9977360240692968]
Best fitness: 3.99999167486

HarmonySearchResults, a namedtuple, is returned. Currently, five fields are attached: elapsed_time, best_harmony, best_fitness, harmony_memories, and harmony_histories.

Note that like many similar optimization algorithms, HS is stochastic, so you will get a slightly different result every time you run it. Because of the stochasticity, I have added the ability to run multiple iterations of HS simultaneously using Python's multiprocessing module; you simply specify the number of processes on which to run the specified number of iterations. The resulting solution is the best solution found from all iterations. Also, the user can specify the initial harmonies. An optional random seed can be used to generate reproducible results.

In general, you will make use of this code in three steps:

  1. Implement your own objective function that inherits from ObjectiveFunctionInterface.
  2. Tune the various input parameters (e.g., hms, hmcr). These are problem-specific, and the numbers used in the example implementations might not be appropriate for your problem.
  3. Run HS.

It will look something like this:

from pyharmonysearch import ObjectiveFunctionInterface, harmony_search
class ObjectiveFunction(ObjectiveFunctionInterface):
    #IMPLEMENT ME
if __name__ == '__main__':
    obj_fun = ObjectiveFunction()
    num_processes = cpu_count()  # use number of logical CPUs
    num_iterations = num_processes * 5  # each process does 5 iterations
    results = harmony_search(obj_fun, num_processes, num_iterations)
    print('Elapsed time: %s\nBest harmony: %s\nBest fitness: %s' % (results.elapsed_time, results.best_harmony, results.best_fitness))

More documentation is provided in harmony_search.py and objective_function_interface.py and in the examples.

REFERENCES

http://harry.me/blog/2011/07/05/neat-algorithms-harmony-search/ provides a simple introduction on how HS works. Also, see the HS Wikipedia entry.

HS was first introduced by Geem et al. in 2001:

Z. W. Geem, J. H. Kim, and G. V. Loganathan, "A New Heuristic Optimization Algorithm: Harmony Search," Simulation, vol. 76, no. 2, pp. 60โ€“68, Feb. 2001.

Some modifications and improvements have been suggested to the original algorithm. None of these are implemented here. This package only implements the original algorithm.

  • Z. W. Geem, "Improved Harmony Search from Ensemble of Music Players", in Knowledge-Based Intelligent Information and Engineering Systems, B. Gabrys, R. Howlett, and L. Jain, Eds. Springer Berlin / Heidelberg, 2006, pp. 86-93.
  • M. Mahdavi, M. Fesanghary, and E. Damangir, "An improved harmony search algorithm for solving optimization problems", Applied Mathematics and Computation, vol. 188, no. 2, pp. 1567-1579, May 2007.
  • M. G. H. Omran and M. Mahdavi, "Global-best harmony search", Applied Mathematics and Computation, vol. 198, no. 2, pp. 643-656, May 2008.
  • Z. W. Geem and K.-B. Sim, "Parameter-setting-free harmony search algorithm," Applied Mathematics and Computation, vol. 217, no. 8, pp. 3881โ€“3889, Dec. 2010.

DIFFERENCES

Python optimization packages that implement HS:

pyHarmonySearch differs from other optimization packages in that it relies on no 3rd party software. It is simple and easy-to-use standalone Python code. The primary reason I developed this was because I got tired of having to compile and maintain my own 3rd party packages on a machine running several-year-old software for which I have only user-level access. SciPy is particularly nasty to build, and many scientific packages rely on it. I skirted this issue by implementing HS myself from scratch.

pyharmonysearch's People

Contributors

chrishhx avatar gfairchild avatar kkew3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pyharmonysearch's Issues

implement Mahdavi's improved harmony search

Mahdavi et al. suggested an improved harmony search that dynamically changes PAR and MPAP. This is a pretty easy modification. The suggestion comes from the following paper:

M. Mahdavi, M. Fesanghary, and E. Damangir, "An improved harmony search algorithm for solving optimization problems", Applied Mathematics and Computation, vol. 188, no. 2, pp. 1567-1579, May 2007.

Return history

Hi @gfairchild , I have extended HarmonySearch to return harmony history as well. At the moment, it saves the harmonies after N evaluations, where N is the number of solutions. Do you think that this is useful? If so, I will do a PR. Thanks.

track changes

A changelog needs to be maintained. CHANGES.md sounds good.

Provide initial position

Hi @gfairchild,

I would like to provide HS an initial position. The present implementation does not provide such an option. I am thinking to modify the run() function to take an initial position. I am thinking to provide it a list of lists (the initial positions, one for each individual), and modify self.initialize(). Any suggestions would be much appreciated.

Thank you very much.

put in PyPI

Once issue #2 is fixed, this can be put in PyPI. This will allow the following:

pip install pyHarmonySearch

Setup from source

Hi,
I'm using python 3.5 and I try to setup pyHarmonySearch from source by running python setup.py install, But I get the following error:

TypeError: chown() missing 1 required positional argument: 'numeric_owner'

How can I resolve the error? Is it maybe because I use python 3.5 and it's not compatible with the setup process from source?

Add unit tests

This project does not currently have unit tests, but it really should. Travis CI should be used to automate testing.

make more Pythonic

The gist should be something like this:

from pyharmonysearch import HarmonySearch

hs = HarmonySearch(parameters)
solution_vector, solution = hs.run()

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.