Exemple #1
0
 def code(cls, phenotype: float) -> List[int]:
     assert phenotype <= GenotypePhenotypeConfigSingleton(
     ).max_phenotype_value, "Value of phenotype is greater than the possible maximum value"
     assert phenotype >= GenotypePhenotypeConfigSingleton(
     ).min_phenotype_value, "Value of phenotype is lower than the possible minimal value"
     sign = Coder.__get_value_of_bit_sign(phenotype)
     bits_integer_part = Coder.__get_bits_values_for_integer_parts(
         phenotype)
     bits_floating_part = Coder.__get_bits_value_for_floating_part(
         phenotype)
     return sign + bits_integer_part + bits_floating_part
Exemple #2
0
 def __generate_list_exponents_for_floating_part(cls) -> List[int]:
     list_exponents = []
     for index in range(
             -1, -(GenotypePhenotypeConfigSingleton().
                   number_of_bits_on_floating_part + 1), -1):
         list_exponents.append(index)
     return list_exponents
 def mutation_of_genotype(cls, genotype, number_bits_for_mutation):
     new_genotype = list(genotype)
     indexes_of_bits_for_mutation_for_genotype_x = BitsRandomizer.random_without_returning(
         GenotypePhenotypeConfigSingleton().number_of_bits_for_genotype, number_bits_for_mutation)
     for index_to_invert in indexes_of_bits_for_mutation_for_genotype_x:
         genotype[index_to_invert] = BitInverter.invert(genotype[index_to_invert])
     new_phenotype = Decoder.decode(new_genotype)
     return {'genotype': new_genotype,
             'phenotype': new_phenotype}
Exemple #4
0
 def __init__(self, size_of_population: int, probability_of_crossing: float,
              number_points_to_crossing: int,
              probability_of_mutation: float,
              number_points_to_mutation: int):
     SizePopulationValidator.validation(size_of_population)
     ProbabilityValidator.validation(probability_of_crossing,
                                     'probability_of_crossing')
     ProbabilityValidator.validation(probability_of_mutation,
                                     'probability_of_mutation')
     NumberOfBitsValidator.validation(
         number_points_to_crossing, 'Crossing',
         GenotypePhenotypeConfigSingleton().number_of_bits_for_genotype)
     NumberOfBitsValidator.validation(
         number_points_to_mutation, 'Mutation',
         GenotypePhenotypeConfigSingleton().number_of_bits_for_genotype)
     self.__size_of_population = size_of_population
     self.__probability_of_crossing = probability_of_crossing
     self.number_points_to_crossing = number_points_to_crossing
     self.probability_of_mutation = probability_of_mutation
     self.number_points_to_mutation = number_points_to_mutation
 def __crossing_of_genotypes(self, genotype_one, genotype_two,
                             number_bits_for_crossing) -> Dict:
     new_genotype_one = list(genotype_one)
     new_genotype_two = list(genotype_two)
     indexes_of_bits_for_crossing_for_genotype = BitsRandomizer.random_without_returning(
         GenotypePhenotypeConfigSingleton().number_of_bits_for_genotype,
         number_bits_for_crossing)
     for index_for_crossing in indexes_of_bits_for_crossing_for_genotype:
         tmp_bit_value = new_genotype_one[index_for_crossing]
         new_genotype_one[index_for_crossing] = new_genotype_two[
             index_for_crossing]
         new_genotype_two[index_for_crossing] = tmp_bit_value
     return {
         'genotype_one': new_genotype_one,
         'genotype_two': new_genotype_two
     }
 def generate(cls):
     start = GenotypePhenotypeConfigSingleton().min_phenotype_value
     stop = GenotypePhenotypeConfigSingleton().max_phenotype_value
     phenotype_x = random.uniform(start, stop)
     phenotype_y = random.uniform(start, stop)
     return Individual(phenotype_x, phenotype_y)
import matplotlib.pyplot as plt
import numpy as np

from geneticAlgorithm.GeneticAlgorithm import GeneticAlgorithm
from geneticAlgorithm.fun.ConcreteFunction import ConcreteFunction
from geneticAlgorithm.lib.config.ConfigGeneticAlgorithm import ConfigGeneticAlgorithm
from geneticAlgorithm.lib.config.GenotypePhenotypeConfigSingleton import GenotypePhenotypeConfigSingleton
from geneticAlgorithm.lib.selection.RankedSelection import RankedSelection

# Skonfigurowanie kodowania genotypu
from geneticAlgorithm.lib.selection.RouletteWheelSelection import RouletteWheelSelection
from geneticAlgorithm.lib.stopconditionals.StopByAchieveValueFunction import StopByAchieveValueFunction
from geneticAlgorithm.lib.stopconditionals.StopByPercentageBestIndividualsInPopulation import \
    StopByPercentageBestIndividualsInPopulation

config_genotype_phenotype = GenotypePhenotypeConfigSingleton()
config_genotype_phenotype.min_phenotype_value = -8
config_genotype_phenotype.max_phenotype_value = 8
config_genotype_phenotype.set_and_count_number_of_bits_to_write_integer()
config_genotype_phenotype.number_of_bits_on_floating_part = 28

# Przygotowanie konfiguracji algorytmu genetycznego
config_genetic_algorithm = ConfigGeneticAlgorithm(probability_of_crossing=0.8,
                                                  number_points_to_crossing=4,
                                                  probability_of_mutation=0.05,
                                                  number_points_to_mutation=1,
                                                  size_of_population=1000)

# Określenie warunku stopu
# stop_condition = StopByNumberOfGenerations(number_of_generation_to_stop=100)
# stop_condition = StopByPercentageBestIndividualsInPopulation(percentage_the_best_individuals=0.95)