A evolutionary computation module
Project description
DarwinPy
Introduction
DarwinPy is a Python-based evolutionary computation module that makes it easy to implement evolutionary methods such as genetic algorithms and evolutionary strategies with only a few lines of code.
Installing DarwinPy
DarwinPy can be installed with the command pip install DarwinPy.
Classes in DarwinPy
Genetics Classes (DarwinPy.Genetics.Genetics)
Methods:
setSearchSpace(search_space): Sets thesearch_spaceattribute of the DarwinPy object.setChromosomeLength(chromosome_length): Sets thechromosome_lengthattribute of the DarwinPy object.setPopulationSize(population_size): Sets thepopulation_sizeattribute of the DarwinPy object.setChromosomeMatrix(search_space): Sets thechromosome_matrixattribute of the DarwinPy object.getChromosomeMatrix(): Returns thechromosome_matrixattribute, which is a NumPy array.getSearchSpace(): Returns thesearch_spaceattribute, which is a tuple.populate(): Initializes thechromosome_matrixattribute with initial values or gene population.select(fitness_vector): Selects mating pairs based on the fitness.crossover(crossover_rate): Mates selected pairs and updates thechromosome_matrixattribute.mutate(mutation_rate): Mutates thechromosome_matrixattribute of the DarwinPy object.evolve(fitness_vector, mutation_rate, crossover_rate): Performs a complete genetic algorithm cycle, which includes selection, crossover, and mutation.
| Identifier | Type |
|---|---|
setSearchSpace |
None |
setChromosomeLength |
None |
setPopulationSize |
None |
setChromosomeMatrix |
None |
getChromosomeMatrix |
numpy.array |
getSearchSpace |
tuple |
populate |
None |
select |
None |
crossover |
None |
mutate |
None |
evolve |
None |
Attributes:
chromosome_length: The length of each chromosome in the DarwinPy object.population_size: The size of the population in the DarwinPy object.search_space: The search space for the genetic algorithm, which is a tuple with an upper and lower bound.chromosome_matrix: The array of chromosomes in the DarwinPy object.pair_list: The list of mating pairs.data_type: The data type in which the genetic algorithm is bounded.
| Identifier | Type |
|---|---|
chromosome_length |
int |
population_size |
int |
search_space |
tuple |
chromosome_matrix |
numpy.array |
pair_list |
list |
data_type |
type |
A Sample DarwinPy implementation
import DarwinPy
import numpy as np
def hammingDist(goal, matrix):
result = []
for i in range(len(matrix)):
temp = 0.
for j in range(len(goal)):
if goal[j] == matrix[i][j]:
temp += 1.
result.append(temp)
return result
if __name__ == "__main__":
mouse_population = 5
mutation_rate = 0.5
search_space = (0,1)
goal = np.array([1,0,1,1,0,1,1, 0,1,1,0],int)
chromosome_length = len(goal)
mouse_species = DarwinPy.Genetics.Genetics(chromosome_length,
mouse_population, (0,1), int)
print(f"mouse species instantiated:\n {mouse_species}")
print("the goal:\n {}".format(goal))
mouse_species.populate()
print(f"get mouse matrix(GA):\n {mouse_species.getChromosomeMatrix()}")
fitness_vector = np.array(
hammingDist(goal,mouse_species.getChromosomeMatrix()),
float)
print(f"get fitness vector: {fitness_vector}")
is_goal = False
gen = 1
while is_goal == False:
print(f"Generation #{gen}\n")
gen += 1
mouse_species.evolve(fitness_vector,
mutation_rate, 0.5)
print(f"get mouse matrix(GA):\n {mouse_species.getChromosomeMatrix()}")
fitness_vector = np.array(
hammingDist(goal,mouse_species.getChromosomeMatrix()),
float)
print(f"get fitness vector: {fitness_vector}")
if chromosome_length in fitness_vector:
is_goal = True
Change Log
0.0.1 (10/10/2021)
- First Release
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
DarwinPy-0.0.2.tar.gz
(6.6 kB
view details)
File details
Details for the file DarwinPy-0.0.2.tar.gz.
File metadata
- Download URL: DarwinPy-0.0.2.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8700b53cc12d778aa927227bbf22981f2963d0f86b93a2564043180405fc5e41
|
|
| MD5 |
ee64c998fdb2e535a05decbe6442a54d
|
|
| BLAKE2b-256 |
cffe9efe02fbb23a701897d7d0fb39fb1295bec9642ebeca42160530f702d821
|