Added distance attribute
This commit is contained in:
@ -126,7 +126,7 @@ class GA(Attributes):
|
|||||||
|
|
||||||
# Difference between best and i-th chromosomes
|
# Difference between best and i-th chromosomes
|
||||||
best_chromosome = self.population[0]
|
best_chromosome = self.population[0]
|
||||||
tol = lambda i: sqrt(abs(best_chromosome.fitness - self.population[i].fitness))
|
tol = lambda i: self.dist(best_chromosome, self.population[i])
|
||||||
|
|
||||||
# Change rates with:
|
# Change rates with:
|
||||||
multiplier = 1 + self.adapt_probability_rate
|
multiplier = 1 + self.adapt_probability_rate
|
||||||
@ -185,7 +185,7 @@ class GA(Attributes):
|
|||||||
|
|
||||||
# Difference between best and i-th chromosomes
|
# Difference between best and i-th chromosomes
|
||||||
best_chromosome = self.population[0]
|
best_chromosome = self.population[0]
|
||||||
tol = lambda i: sqrt(abs(best_chromosome.fitness - self.population[i].fitness))
|
tol = lambda i: self.dist(best_chromosome, self.population[i])
|
||||||
|
|
||||||
# First non-zero tolerance after amount_converged/4
|
# First non-zero tolerance after amount_converged/4
|
||||||
for i in range(amount_converged//4, len(self.population)):
|
for i in range(amount_converged//4, len(self.population)):
|
||||||
|
|||||||
@ -1,3 +1,6 @@
|
|||||||
|
# Import square root function for ga.adapt()
|
||||||
|
from math import sqrt
|
||||||
|
|
||||||
import random
|
import random
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
@ -75,6 +78,7 @@ class Attributes:
|
|||||||
min_selection_probability = 0.01,
|
min_selection_probability = 0.01,
|
||||||
min_chromosome_mutation_rate = 0.01,
|
min_chromosome_mutation_rate = 0.01,
|
||||||
min_gene_mutation_rate = None,
|
min_gene_mutation_rate = None,
|
||||||
|
dist = None,
|
||||||
initialization_impl = Initialization_Methods.random_initialization,
|
initialization_impl = Initialization_Methods.random_initialization,
|
||||||
fitness_function_impl = Fitness_Examples.is_it_5,
|
fitness_function_impl = Fitness_Examples.is_it_5,
|
||||||
make_population = create_population,
|
make_population = create_population,
|
||||||
@ -132,6 +136,13 @@ class Attributes:
|
|||||||
self.min_chromosome_mutation_rate = min_chromosome_mutation_rate
|
self.min_chromosome_mutation_rate = min_chromosome_mutation_rate
|
||||||
self.min_gene_mutation_rate = gene_mutation_rate if (min_gene_mutation_rate is None) else min_gene_mutation_rate
|
self.min_gene_mutation_rate = gene_mutation_rate if (min_gene_mutation_rate is None) else min_gene_mutation_rate
|
||||||
|
|
||||||
|
# Distance between two chromosomes
|
||||||
|
if dist is None:
|
||||||
|
self.dist = lambda chromosome_1, chromosome_2:\
|
||||||
|
sqrt(abs(chromosome_1.fitness - chromosome_2.fitness))
|
||||||
|
else:
|
||||||
|
self.dist = dist
|
||||||
|
|
||||||
# Mutation variables
|
# Mutation variables
|
||||||
self.chromosome_mutation_rate = deepcopy(chromosome_mutation_rate)
|
self.chromosome_mutation_rate = deepcopy(chromosome_mutation_rate)
|
||||||
self.gene_mutation_rate = deepcopy(gene_mutation_rate)
|
self.gene_mutation_rate = deepcopy(gene_mutation_rate)
|
||||||
|
|||||||
Reference in New Issue
Block a user