Skip to content

Commit

Permalink
Merge 5c0fc8e into f1a3a5b
Browse files Browse the repository at this point in the history
  • Loading branch information
SuryaThiru committed Jun 21, 2020
2 parents f1a3a5b + 5c0fc8e commit 1efa180
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion pyeasyga/pyeasyga.py
Expand Up @@ -35,6 +35,7 @@ class GeneticAlgorithm(object):

def __init__(self,
seed_data,
initial_population=None,
population_size=50,
generations=100,
crossover_probability=0.8,
Expand All @@ -47,6 +48,10 @@ def __init__(self,
:param seed_data: input data to the Genetic Algorithm
:type seed_data: list of objects
:param list initial_population: list of lists representing initial
initial population. If list is smaller
than population size, the rest will be
generated randomly
:param int population_size: size of population
:param int generations: number of generations to evolve
:param float crossover_probability: probability of crossover operation
Expand Down Expand Up @@ -116,6 +121,17 @@ def tournament_selection(population):
key=attrgetter('fitness'), reverse=self.maximise_fitness)
return members[0]

def check_initial_population(population):
"""Check for errors in the initial population before using it
Returns the population if there are no errors
"""
if not population: # if initial population is None
return []
if len(population) > self.population_size:
raise ValueError('Length of the initial population exceeds '
'the size of population')
return population

self.fitness_function = None
self.tournament_selection = tournament_selection
self.tournament_size = self.population_size // 10
Expand All @@ -124,12 +140,18 @@ def tournament_selection(population):
self.crossover_function = crossover
self.mutate_function = mutate
self.selection_function = self.tournament_selection
self.initial_population = check_initial_population(initial_population)

def create_initial_population(self):
"""Create members of the first population randomly.
"""
initial_population = []
for _ in range(self.population_size):

for genes in self.initial_population:
individual = Chromosome(genes)
initial_population.append(individual)

for _ in range(self.population_size - len(initial_population)):
genes = self.create_individual(self.seed_data)
individual = Chromosome(genes)
initial_population.append(individual)
Expand Down

0 comments on commit 1efa180

Please sign in to comment.