From da64af75f00fd4416fbd00618499c13f07024176 Mon Sep 17 00:00:00 2001 From: d-soni Date: Sun, 18 Jun 2017 22:47:03 -0700 Subject: [PATCH] adding to readme --- README.md | 74 ++++++++++++++++++++++++++++++++++++++++++- library/TabuSearch.py | 2 +- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0042219..496cf9c 100644 --- a/README.md +++ b/README.md @@ -1 +1,73 @@ -# Metahueristics-library-placeholder \ No newline at end of file +# Metaheuristics-library-placeholder + +## Current Features: +* Genetic Algorithm +* Evolutionary Algorithm +* Simulated Annealing +* Particle Swarm Optimization +* Tabu Search +* Harmony Search +* Stochastic Hill Climb + +
+ +## Usage: +* ```pip install``` +* Import the relevant algorithm +* Create a class that inherits from that algorithm, and that implements the necessary abstract methods +* Call its ```.run()``` method + +
+ +## Example: + +```python +from random import choice, randint, random +from string import lowercase +from library.EvolutionaryAlgorithm import EvolutionaryAlgorithm + + +class Algorithm(EvolutionaryAlgorithm): + """ + Tries to get a randomly-generated string to match string "clout" + """ + def _initial_population(self): + return list(''.join([choice(lowercase) for _ in range(5)]) for _ in range(50)) + + def _fitness(self, member): + return float(sum(member[i] == "clout"[i] for i in range(5))) + + def _crossover(self, parent1, parent2): + partition = randint(0, len(self.population[0]) - 1) + return parent1[0:partition] + parent2[partition:] + + def _mutate(self, member): + if self.mutation_rate >= random(): + member = list(member) + member[randint(0,4)] = choice(lowercase) + member = ''.join(member) + return member + + +def test_algorithm(): + algorithm = Algorithm(.5, .7, 500, max_fitness=None) + algorithm.run() + +``` + +
+ +## Building and Testing + +To run tests, look in the ```tests``` folder. + +I use [pytest](https://docs.pytest.org/en/latest/); it should automatically find the test files. + +
+ +## Contributing + +Feel free to send a pull request if you want to add any features or if you find a bug. + +Check the issues tab for some potential things to do. + diff --git a/library/TabuSearch.py b/library/TabuSearch.py index 0e621da..d302e1b 100644 --- a/library/TabuSearch.py +++ b/library/TabuSearch.py @@ -25,7 +25,7 @@ class TabuSearch: def __init__(self, initial_state, tabu_size, max_steps, max_score=None): """ - :param initial_state: initial state + :param initial_state: initial state, should implement __eq__ or __cmp__ :param tabu_size: number of states to keep in tabu list :param max_steps: maximum number of steps to run algorithm for :param max_score: score to stop algorithm once reached