Cleaner random functions used.

This commit is contained in:
SimpleArt
2020-11-20 19:58:25 -05:00
parent 73461f7915
commit 470b33aa31
3 changed files with 9 additions and 9 deletions

View File

@ -72,7 +72,7 @@ class Crossover_Methods:
def single_point(ga, parent_1, parent_2):
"""Cross two parents by swapping genes at one random point."""
swap_index = random.randint(0, len(parent_1)-1)
swap_index = random.randrange(len(parent_1))
return parent_1[:swap_index] + parent_2[swap_index:]

View File

@ -37,7 +37,7 @@ class Mutation_Methods:
def random_selection(ga):
"""Selects random chromosomes."""
index = random.randint(0, len(ga.population)-1)
index = random.randrange(len(ga.population))
ga.population[index] = ga.mutation_individual_impl(ga, ga.population[index])
@ -45,7 +45,7 @@ class Mutation_Methods:
def random_selection_then_cross(ga):
"""Selects random chromosomes and self-crosses with parent."""
index = random.randint(0, len(ga.population)-1)
index = random.randrange(len(ga.population))
chromosome = ga.population[index]
ga.population[index] = ga.crossover_individual_impl(ga, chromosome, ga.mutation_individual_impl(ga, chromosome))
@ -56,7 +56,7 @@ class Mutation_Methods:
@loop_mutations
def individual_genes(ga, chromosome):
"""Mutates a random gene in the chromosome."""
index = random.randint(0, len(chromosome)-1)
index = random.randrange(len(chromosome))
# Using the chromosome_impl
if ga.chromosome_impl is not None:
@ -79,7 +79,7 @@ class Mutation_Methods:
def swap_genes(ga, chromosome):
"""Swaps two random genes in the chromosome."""
index_one = random.randint(0, len(chromosome)-1)
index_two = random.randint(0, len(chromosome)-1)
index_one = random.randrange(len(chromosome))
index_two = random.randrange(len(chromosome))
chromosome[index_one], chromosome[index_two] = chromosome[index_two], chromosome[index_one]

View File

@ -68,7 +68,7 @@ class Parent_Selection:
while (len(ga.population.get_mating_pool()) < len(ga.population)*ga.parent_ratio):
# Generate a random tournament group and sort by fitness.
tournament_group = sorted([random.randint(0, len(ga.population)-1) for n in range(tournament_size)])
tournament_group = sorted([random.randrange(len(ga.population)) for _ in range(tournament_size)])
# For each chromosome, add it to the mating pool based on its rank in the tournament.
for index in range(tournament_size):
@ -78,7 +78,7 @@ class Parent_Selection:
# second ranked fitness has probability: selection_probability * (1-selection_probability)
# third ranked fitness has probability: selection_probability * (1-selection_probability)^2
# etc.
if random.uniform(0, 1) < ga.selection_probability * pow(1-ga.selection_probability, index):
if random.random() < ga.selection_probability * pow(1-ga.selection_probability, index):
ga.population.set_parent(tournament_group[index])
# Stop if parent ratio reached
@ -141,7 +141,7 @@ class Parent_Selection:
while (len(ga.population.get_mating_pool()) < len(ga.population)*ga.parent_ratio):
# Selected chromosome
index = random.randint(0, len(ga.population)-1)
index = random.randrange(len(ga.population))
# Probability of becoming a parent is fitness/max_fitness
if random.uniform(ga.selection_probability, 1) < ga.get_chromosome_fitness(index)/max_fitness: