100 / Solid
- понедельник, 26 июня 2017 г. в 03:11:45
Python
🎯 A comprehensive gradient-free optimization framework written in Python
pip install solidpy
.run()
method, which always returns the best solution and its objective function valuefrom random import choice, randint, random
from string import lowercase
from Solid.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)
best_solution, best_objective_value = algorithm.run()
To run tests, look in the tests
folder.
Use pytest; it should automatically find the test files.
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.