Author : Yingkai Liu
updated : Aug.2017
NOTE
This project has fulfilled my expectation, so I will let it rest for a while
This is document for populationclass.py
in /old
, new documentation will be coming out
status: still incomplete, even this documentation is incomplete
This code is written in python3 compatible with python2. Used packages only includes easygui
radom
itertools
to utilize maximum compatibility.
It has a GUI and you can also disable it with minimum modification.
This code is designed to perform evolutionary algorithm on a population whose gene are in the form of lists. The fitness function
is an arbitrary user defined function. It can be as simple as sum
or any other complicated function such as the how similar of a graph A compared to B
.
This code features a relatively wide range of evolutionary operators. mutate
hybrid
select
are three basics ones. To prevent pre-mature, there are two other functions migrate
and niche
.
The whole package is included in a single class called population. you can add new functions to this class as well, such as adapt
(which I am planning to add later) .
Also you might find that sometimes you get one more or one less choromosomes than you expected, that is because I used int()
everything to make sure interger. I assume it won't be a big problem for a large population. This will be fixed in later releases.
def __init__(self,gene_set,gene_range,population_size):
self.gene_set = gene_set
self.gene_size = len(gene_set)
self.gene_range = gene_range
self.population_size = population_size
self.chromosomes = self.initialize_chromosomes(self.population_size)
self.generation_number = 0
This is user defined function
Use attributes and assign_fitness
to generate a chromosome in the form of
This function use initialize_chromosome
to generate a list of chromosomes in the form of
This function will use fitness as a bias to choose a chromosome
mutate mutate_strength
sites in the chromosome
same as initialize chromosomes
generate a child using genes randomly chosen from father and mother. If the site of gene are odd, then mother have one more contribution than father. Else choose half of gene_site
randomly from father and mother
This function takes in chromosomes and extract thier gene and then return the distance of genes as similarity, the distance is simply absulute difference.
you can make it use euclidean distance
The similairty is averaged normalized in range (0,1)
the code need to use gene_range[site]
to normalize and gene_size
to average
similarity = difference / gene_range
the similarity is then averaged over all sites
This function use assign_similarity
and
like hybrid using genes from 3 parents. This can be done but is of little use since generating a purmutation list of three parents are slow, and multiple parents is basically the same if you tune up the crossover_ratio
and set roulettel
= True
later
special thanks to Kai Wu for initial discussions, Yifei Liu for brilliant ideas, Qiang Zhu and Xiangfeng Zhou for instruction. USPEX inspired this script