From c41112f170ee1f76631ef389913539f525b1f313 Mon Sep 17 00:00:00 2001 From: pytlab Date: Tue, 12 Dec 2017 17:43:25 +0800 Subject: [PATCH] Add Individual base class. --- gaft/components/__init__.py | 2 +- gaft/components/individual.py | 163 ++++++++++---------- gaft/components/population.py | 17 +- gaft/engine.py | 6 +- gaft/plugin_interfaces/metaclasses.py | 14 +- tests/dynamic_linear_scaling_test.py | 4 +- tests/engine_test.py | 4 +- tests/exponential_ranking_selection_test.py | 8 +- tests/flip_bit_big_mutation_test.py | 4 +- tests/flip_bit_mutation_test.py | 4 +- tests/individual_test.py | 34 ++-- tests/linear_ranking_selection_test.py | 8 +- tests/linear_scaling_test.py | 4 +- tests/population_test.py | 6 +- tests/roulette_wheel_selection_test.py | 8 +- tests/tournament_selection_test.py | 8 +- tests/uniform_crossover_test.py | 6 +- 17 files changed, 148 insertions(+), 152 deletions(-) diff --git a/gaft/components/__init__.py b/gaft/components/__init__.py index 091d46b..ecebf8e 100644 --- a/gaft/components/__init__.py +++ b/gaft/components/__init__.py @@ -1,3 +1,3 @@ -from .individual import GAIndividual +from .individual import * from .population import GAPopulation diff --git a/gaft/components/individual.py b/gaft/components/individual.py index a8a4f6a..da72b65 100644 --- a/gaft/components/individual.py +++ b/gaft/components/individual.py @@ -9,8 +9,85 @@ from ..mpiutil import mpi -class GAIndividual(object): - def __init__(self, ranges, encoding='binary', eps=0.001, verbosity=1): +class IndividualBase(object): + ''' Base class for individuals. + ''' + def __init__(self, ranges, eps=0.001): + self.ranges = ranges + self.eps = eps + self.precisions = eps + + self._check_parameters() + + self.variants, self.chromsome = [], [] + + def _check_parameters(self): + ''' + Private helper function to check individual parameters. + ''' + # Check decrete precision. + if type(self.eps) is float: + self.eps = [self.eps]*len(self.ranges) + self.precisions = self.eps + else: + # Check eps length. + if len(self.eps) != len(self.ranges): + raise ValueError('Lengths of eps and ranges should be the same') + for eps, (a, b) in zip(self.eps, self.ranges): + if eps > (b - a): + msg = 'Invalid eps {} in range ({}, {})'.format(eps, a, b) + raise ValueError(msg) + + def _rand_variants(self): + ''' Initialize individual variants randomly. + ''' + variants = [] + for eps, (a, b) in zip(self.precisions, self.ranges): + n_intervals = (b - a)//eps + n = int(uniform(0, n_intervals + 1)) + variants.append(a + n*eps) + return variants + + def init(self, chromsome=None, variants=None): + ''' + Initialize the individual by providing chromsome or variants. + + If both chromsome and variants are provided, only the chromsome would + be used. If neither is provided, individual would be initialized randomly. + + :param chromsome: chromesome sequence for the individual + :type chromsome: list of float/int. + + :param variants: the variable vector of the target function. + :type variants: list of float. + ''' + if not any([chromsome, variants]): + self.variants = self._rand_variants() + self.chromsome = self.encode() + elif chromsome: + self.chromsome = chromsome + self.variants = self.decode() + else: + self.variants = variants + self.chromsome = self.encode() + + return self + + def encode(self): + ''' *NEED IMPLIMENTATION* + Convert variants to chromsome sequence. + ''' + raise NotImplementedError + + def decode(self): + ''' *NEED IMPLIMENTATION* + Convert chromsome sequence to variants. + ''' + raise NotImplementedError + + +class BinaryIndividual(IndividualBase): + def __init__(self, ranges, eps=0.001, verbosity=1): ''' Class for individual in population. Random variants will be initialized by default. @@ -25,28 +102,20 @@ def __init__(self, ranges, encoding='binary', eps=0.001, verbosity=1): :param ranges: value ranges for all entries in variants. :type ranges: list of range tuples. e.g. [(0, 1), (-1, 1)] - :param encoding: gene encoding, 'decimal' or 'binary', default is binary. - :type encoding: str - :param eps: decrete precisions for binary encoding, default is 0.001. :type eps: float or float list with the same length with ranges. :param verbosity: The verbosity level of info output. :param verbosity: int, 0 or 1(default) ''' + super(self.__class__, self).__init__(ranges, eps) - self.ranges = ranges - self.eps = eps - self.encoding = encoding self.verbosity = verbosity - # Check parameters. - self._check_parameters() - # Lengths for all binary sequence in chromsome and adjusted decrete precisions. - self.lengths, self.precisions = [], [] + self.lengths = [] - for (a, b), eps in zip(self.ranges, self.eps): + for i, ((a, b), eps) in enumerate(zip(self.ranges, self.eps)): length = int(log2((b - a)/eps)) precision = (b - a)/(2**length) @@ -54,87 +123,28 @@ def __init__(self, ranges, encoding='binary', eps=0.001, verbosity=1): print('Precision loss {} -> {}'.format(eps, precision)) self.lengths.append(length) - self.precisions.append(precision) + self.precisions[i] = precision # The start and end indices for each gene segment for entries in variants. self.gene_indices = self._get_gene_indices() - # Generate randomly. - self.variants = self._init_variants() - - # Gene encoding. - self.chromsome = self.encode() - - def init(self, chromsome=None, variants=None): - ''' - Initialize the individual by providing chromsome or variants. - If both chromsome and variants are provided, only the chromsome would - be used. - - :param chromsome: chromesome sequence for the individual - :type chromsome: list of float/int. - - :param variants: the variable vector of the target function. - :type variants: list of float. - ''' - if not any([chromsome, variants]): - msg = 'Chromsome or variants must be supplied for individual initialization' - raise ValueError(msg) - - if chromsome: - self.chromsome = chromsome - self.variants = self.decode() - else: - self.variants = variants - self.chromsome = self.encode() - - return self + # Initialize individual randomly. + self.init() def clone(self): ''' Clone a new individual from current one. ''' indv = self.__class__(self.ranges, - encoding=self.encoding, eps=self.eps, verbosity=self.verbosity) indv.init(chromsome=self.chromsome) return indv - def _check_parameters(self): - ''' - Private helper function to check individual parameters. - ''' - # Check decrete precision. - if type(self.eps) is float: - self.eps = [self.eps]*len(self.ranges) - else: - # Check eps length. - if len(self.eps) != len(self.ranges): - raise ValueError('Lengths of eps and ranges should be the same') - for eps, (a, b) in zip(self.eps, self.ranges): - if eps > (b - a): - msg = 'Invalid eps {} in range ({}, {})'.format(eps, a, b) - raise ValueError(msg) - - def _init_variants(self): - ''' - Initialize individual variants randomly. - ''' - variants = [] - for eps, (a, b) in zip(self.precisions, self.ranges): - n_intervals = (b - a)//eps - n = int(uniform(0, n_intervals + 1)) - variants.append(a + n*eps) - return variants - def encode(self): ''' Encode variant to gene sequence in individual using different encoding. ''' - if self.encoding == 'decimal': - return self.variants - chromsome = [] for var, (a, _), length, eps in zip(self.variants, self.ranges, self.lengths, self.precisions): @@ -146,9 +156,6 @@ def decode(self): ''' Decode gene sequence to variants of target function. ''' - if self.encoding == 'decimal': - return self.variants - variants = [self.decimalize(self.chromsome[start: end], eps, lower_bound) for (start, end), (lower_bound, _), eps in zip(self.gene_indices, self.ranges, self.precisions)] diff --git a/gaft/components/population.py b/gaft/components/population.py index 5ebcd93..dd94bda 100644 --- a/gaft/components/population.py +++ b/gaft/components/population.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from .individual import GAIndividual +from .individual import IndividualBase, BinaryIndividual class Memoized(object): @@ -123,22 +123,23 @@ def init(self, indvs=None): :param indvs: Initial individuals in population, randomly initialized individuals are created if not provided. - :type indvs: list of GAIndividual + :type indvs: list of Individual object ''' + IndvType = self.indv_template.__class__ + if indvs is None: for _ in range(self.size): - indv = GAIndividual(ranges=self.indv_template.ranges, - encoding=self.indv_template.encoding, - eps=self.indv_template.eps, - verbosity=self.indv_template.verbosity) + indv = IndvType(ranges=self.indv_template.ranges, + eps=self.indv_template.eps, + verbosity=self.indv_template.verbosity) self.individuals.append(indv) else: # Check individuals. if len(indvs) != self.size: raise ValueError('Invalid individuals number') for indv in indvs: - if not isinstance(indv, GAIndividual): - raise ValueError('individual must be GAIndividual object') + if not isinstance(indv, IndividualBase): + raise ValueError('individual class must be subclass of IndividualBase') self.individuals = indvs self._updated = True diff --git a/gaft/engine.py b/gaft/engine.py index 76c021d..89595bc 100644 --- a/gaft/engine.py +++ b/gaft/engine.py @@ -12,7 +12,7 @@ import pstats import os -from .components import GAIndividual, GAPopulation +from .components import IndividualBase, GAPopulation from .plugin_interfaces.operators import GASelection, GACrossover, GAMutation from .plugin_interfaces.analysis import OnTheFlyAnalysis from .mpiutil import mpi @@ -257,8 +257,8 @@ def _fn_with_fitness_check(indv): A wrapper function for fitness function with fitness value check. ''' # Check indv type. - if not isinstance(indv, GAIndividual): - raise TypeError('indv must be a GAIndividual object') + if not isinstance(indv, IndividualBase): + raise TypeError('indv\'s class must be subclass of IndividualBase') # Check fitness. fitness = fn(indv) diff --git a/gaft/plugin_interfaces/metaclasses.py b/gaft/plugin_interfaces/metaclasses.py index 866648f..645f878 100644 --- a/gaft/plugin_interfaces/metaclasses.py +++ b/gaft/plugin_interfaces/metaclasses.py @@ -5,7 +5,7 @@ import inspect from functools import wraps -from ..components.individual import GAIndividual +from ..components.individual import IndividualBase from ..components.population import GAPopulation from ..mpiutil import master_only @@ -76,9 +76,9 @@ def _wrapped_cross(self, father, mother): ''' Wrapper to add parameters type checking. ''' # Check parameter types. - if not (isinstance(father, GAIndividual) and - isinstance(mother, GAIndividual)): - raise TypeError('father and mother must be GAIndividual object') + if not (isinstance(father, IndividualBase) and + isinstance(mother, IndividualBase)): + raise TypeError('father and mother\'s type must be subclass of IndividualBase') return cross(self, father, mother) @@ -115,8 +115,8 @@ def _wrapped_mutate(self, individual, engine): ''' Wrapper to add parameters type checking. ''' # Check parameter types. - if not isinstance(individual, GAIndividual): - raise TypeError('individual must be a GAIndividual object') + if not isinstance(individual, IndividualBase): + raise TypeError('individual\' type must be subclass of IndividualBase') return mutate(self, individual, engine) @@ -154,7 +154,7 @@ def _wrapped_select(self, population, fitness): ''' # Check parameter types. if not isinstance(population, GAPopulation): - raise TypeError('father and mother must be GAIndividual object') + raise TypeError('population must be GAPopulation object') if not callable(fitness): raise TypeError('fitness must be a callable object') diff --git a/tests/dynamic_linear_scaling_test.py b/tests/dynamic_linear_scaling_test.py index a16da3d..a5cc63c 100644 --- a/tests/dynamic_linear_scaling_test.py +++ b/tests/dynamic_linear_scaling_test.py @@ -8,7 +8,7 @@ from math import sin, cos from gaft import GAEngine -from gaft.components import GAIndividual +from gaft.components import BinaryIndividual from gaft.components import GAPopulation from gaft.operators import RouletteWheelSelection from gaft.operators import UniformCrossover @@ -24,7 +24,7 @@ def test_run(self): ''' Make sure GA engine can run correctly. ''' - indv_template = GAIndividual(ranges=[(0, 10)], encoding='binary', eps=0.001, verbosity=0) + indv_template = BinaryIndividual(ranges=[(0, 10)], eps=0.001, verbosity=0) population = GAPopulation(indv_template=indv_template, size=50).init() # Create genetic operators. diff --git a/tests/engine_test.py b/tests/engine_test.py index 58ec187..bebdd6e 100644 --- a/tests/engine_test.py +++ b/tests/engine_test.py @@ -8,7 +8,7 @@ from math import sin, cos from gaft import GAEngine -from gaft.components import GAIndividual +from gaft.components import BinaryIndividual from gaft.components import GAPopulation from gaft.operators import RouletteWheelSelection from gaft.operators import UniformCrossover @@ -24,7 +24,7 @@ def test_run(self): ''' Make sure GA engine can run correctly. ''' - indv_template = GAIndividual(ranges=[(0, 10)], encoding='binary', eps=0.001, verbosity=0) + indv_template = BinaryIndividual(ranges=[(0, 10)], eps=0.001, verbosity=0) population = GAPopulation(indv_template=indv_template, size=50).init() # Create genetic operators. diff --git a/tests/exponential_ranking_selection_test.py b/tests/exponential_ranking_selection_test.py index 9c17112..1c3d707 100644 --- a/tests/exponential_ranking_selection_test.py +++ b/tests/exponential_ranking_selection_test.py @@ -7,7 +7,7 @@ import unittest from gaft.components.population import GAPopulation -from gaft.components.individual import GAIndividual +from gaft.components.individual import BinaryIndividual from gaft.operators import ExponentialRankingSelection class ExponentialRankingSelectionTest(unittest.TestCase): @@ -20,15 +20,15 @@ def fitness(indv): self.fitness = fitness def test_selection(self): - indv = GAIndividual(ranges=[(0, 30)], verbosity=0) + indv = BinaryIndividual(ranges=[(0, 30)], verbosity=0) p = GAPopulation(indv) p.init() selection = ExponentialRankingSelection() father, mother = selection.select(p, fitness=self.fitness) - self.assertTrue(isinstance(father, GAIndividual)) - self.assertTrue(isinstance(mother, GAIndividual)) + self.assertTrue(isinstance(father, BinaryIndividual)) + self.assertTrue(isinstance(mother, BinaryIndividual)) self.assertNotEqual(father.chromsome, mother.chromsome) if '__main__' == __name__: diff --git a/tests/flip_bit_big_mutation_test.py b/tests/flip_bit_big_mutation_test.py index 7b901f5..f49e48b 100644 --- a/tests/flip_bit_big_mutation_test.py +++ b/tests/flip_bit_big_mutation_test.py @@ -8,7 +8,7 @@ from math import sin, cos from gaft import GAEngine -from gaft.components import GAIndividual +from gaft.components import BinaryIndividual from gaft.components import GAPopulation from gaft.operators import RouletteWheelSelection from gaft.operators import UniformCrossover @@ -22,7 +22,7 @@ def setUp(self): def test_mutate(self): ''' Make sure the individual can be mutated correctly. ''' - indv_template = GAIndividual(ranges=[(0, 10)], encoding='binary', eps=0.001, verbosity=0) + indv_template = BinaryIndividual(ranges=[(0, 10)], eps=0.001, verbosity=0) population = GAPopulation(indv_template=indv_template, size=50).init() # Create genetic operators. diff --git a/tests/flip_bit_mutation_test.py b/tests/flip_bit_mutation_test.py index 8b89f04..682853a 100644 --- a/tests/flip_bit_mutation_test.py +++ b/tests/flip_bit_mutation_test.py @@ -6,7 +6,7 @@ import unittest -from gaft.components.individual import GAIndividual +from gaft.components.individual import BinaryIndividual from gaft.operators.mutation.flip_bit_mutation import FlipBitMutation class FlipBitMutationTest(unittest.TestCase): @@ -17,7 +17,7 @@ def setUp(self): def test_mutate(self): ''' Make sure the individual can be mutated correctly. ''' - indv = GAIndividual(ranges=[(0, 1)], verbosity=0).init(variants=[0.398]) + indv = BinaryIndividual(ranges=[(0, 1)], verbosity=0).init(variants=[0.398]) mutation = FlipBitMutation(pm=1.0) chromsome_before = [0, 1, 1, 0, 0, 1, 0, 1, 1] chromsome_after = [1, 0, 0, 1, 1, 0, 1, 0, 0] diff --git a/tests/individual_test.py b/tests/individual_test.py index 85ae082..73c87b1 100644 --- a/tests/individual_test.py +++ b/tests/individual_test.py @@ -1,12 +1,12 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -''' Test case for GAIndividual +''' Test case for Individual. ''' import unittest -from gaft.components.individual import GAIndividual +from gaft.components.individual import BinaryIndividual class IndividualTest(unittest.TestCase): @@ -16,7 +16,7 @@ def setUp(self): def test_binary_encoding(self): ''' Make sure individual can decode and encode binary gene correctly. ''' - indv = GAIndividual(ranges=[(0, 1)], encoding='binary', eps=0.001, verbosity=0) + indv = BinaryIndividual(ranges=[(0, 1)], eps=0.001, verbosity=0) indv.init(variants=[0.398]) # Test binary chromsome. @@ -26,7 +26,7 @@ def test_binary_encoding(self): # Test decode. self.assertListEqual(indv.decode(), [0.396484375]) - indv = GAIndividual(ranges=[(0, 1), (-1, 1)], encoding='binary', eps=0.001, verbosity=0) + indv = BinaryIndividual(ranges=[(0, 1), (-1, 1)], eps=0.001, verbosity=0) indv.init(variants=[0.398, 0.66]) # Test binary chromsome. @@ -36,19 +36,10 @@ def test_binary_encoding(self): # Test decode. self.assertListEqual(indv.decode(), [0.396484375, 0.658203125]) - def test_decimal_construction(self): - ''' Make sure individual can decode and encode decimal gene correctly. - ''' - indv = GAIndividual(ranges=[(0, 1)], encoding='decimal', eps=0.001, verbosity=0) - - indv.init(variants=[0.398]) - self.assertListEqual(indv.encode(), [0.398]) - self.assertListEqual(indv.decode(), [0.398]) - def test_init(self): ''' Make sure the individual can be initialized correctly. ''' - indv = GAIndividual(ranges=[(0, 1)], encoding='binary', eps=0.001, verbosity=0) + indv = BinaryIndividual(ranges=[(0, 1)], eps=0.001, verbosity=0) # Check chromsome initialization. indv.init(chromsome=[0, 1, 1, 0, 0, 0, 1, 1, 1, 0]) @@ -65,25 +56,22 @@ def test_init(self): def test_clone(self): ''' Make sure individual can be cloned correctly. ''' - indv = GAIndividual(ranges=[(0, 1)], - encoding='binary', - eps=0.001, - verbosity=0).init(variants=[0.398]) + indv = BinaryIndividual(ranges=[(0, 1)], + eps=0.001, + verbosity=0).init(variants=[0.398]) indv_clone = indv.clone() self.assertListEqual(indv.chromsome, indv_clone.chromsome) self.assertAlmostEqual(indv.variants[0], indv_clone.variants[0], places=2) self.assertEqual(indv.ranges, indv_clone.ranges) self.assertEqual(indv.eps, indv_clone.eps) - self.assertEqual(indv.encoding, indv_clone.encoding) def test_multi_precisions(self): ''' Make sure we can construct individual using different decrete precisions. ''' - indv = GAIndividual(ranges=[(0, 1), (0, 10)], - encoding='binary', - eps=[0.01, 1.0], - verbosity=0).init(variants=[0.3, 0.5]) + indv = BinaryIndividual(ranges=[(0, 1), (0, 10)], + eps=[0.01, 1.0], + verbosity=0).init(variants=[0.3, 0.5]) self.assertNotEqual(indv.precisions[0], indv.precisions[1]) if '__main__' == __name__: diff --git a/tests/linear_ranking_selection_test.py b/tests/linear_ranking_selection_test.py index 65834fb..b4296d6 100644 --- a/tests/linear_ranking_selection_test.py +++ b/tests/linear_ranking_selection_test.py @@ -7,7 +7,7 @@ import unittest from gaft.components.population import GAPopulation -from gaft.components.individual import GAIndividual +from gaft.components.individual import BinaryIndividual from gaft.operators import LinearRankingSelection class LinearRankingSelectionTest(unittest.TestCase): @@ -20,15 +20,15 @@ def fitness(indv): self.fitness = fitness def test_selection(self): - indv = GAIndividual(ranges=[(0, 30)], verbosity=0) + indv = BinaryIndividual(ranges=[(0, 30)], verbosity=0) p = GAPopulation(indv) p.init() selection = LinearRankingSelection() father, mother = selection.select(p, fitness=self.fitness) - self.assertTrue(isinstance(father, GAIndividual)) - self.assertTrue(isinstance(mother, GAIndividual)) + self.assertTrue(isinstance(father, BinaryIndividual)) + self.assertTrue(isinstance(mother, BinaryIndividual)) self.assertNotEqual(father.chromsome, mother.chromsome) if '__main__' == __name__: diff --git a/tests/linear_scaling_test.py b/tests/linear_scaling_test.py index 08a3b6f..c43b6de 100644 --- a/tests/linear_scaling_test.py +++ b/tests/linear_scaling_test.py @@ -8,7 +8,7 @@ from math import sin, cos from gaft import GAEngine -from gaft.components import GAIndividual +from gaft.components import BinaryIndividual from gaft.components import GAPopulation from gaft.operators import RouletteWheelSelection from gaft.operators import UniformCrossover @@ -24,7 +24,7 @@ def test_run(self): ''' Make sure GA engine can run correctly. ''' - indv_template = GAIndividual(ranges=[(0, 10)], encoding='binary', eps=0.001, verbosity=0) + indv_template = BinaryIndividual(ranges=[(0, 10)], eps=0.001, verbosity=0) population = GAPopulation(indv_template=indv_template, size=50).init() # Create genetic operators. diff --git a/tests/population_test.py b/tests/population_test.py index a1cc80d..086a6a8 100644 --- a/tests/population_test.py +++ b/tests/population_test.py @@ -7,13 +7,13 @@ import unittest from gaft.components.population import GAPopulation -from gaft.components.individual import GAIndividual +from gaft.components.individual import BinaryIndividual class PopulationTest(unittest.TestCase): def setUp(self): self.maxDiff = True - self.indv_template = GAIndividual(ranges=[(0, 1)], verbosity=0) + self.indv_template = BinaryIndividual(ranges=[(0, 1)], verbosity=0) def fitness(indv): x, = indv.variants return x**3 - 60*x**2 + 900*x + 100 @@ -29,7 +29,7 @@ def test_initialization(self): self.assertEqual(len(population.individuals), 10) # Check individual. - self.assertTrue(isinstance(population[0], GAIndividual)) + self.assertTrue(isinstance(population[0], BinaryIndividual)) def test_new_population(self): ''' Make sure population can clone a new population. ''' diff --git a/tests/roulette_wheel_selection_test.py b/tests/roulette_wheel_selection_test.py index da53634..9d031ca 100644 --- a/tests/roulette_wheel_selection_test.py +++ b/tests/roulette_wheel_selection_test.py @@ -7,7 +7,7 @@ import unittest from gaft.components.population import GAPopulation -from gaft.components.individual import GAIndividual +from gaft.components.individual import BinaryIndividual from gaft.operators.selection.roulette_wheel_selection import RouletteWheelSelection class RouletteWheelSelectionTest(unittest.TestCase): @@ -20,15 +20,15 @@ def fitness(indv): self.fitness = fitness def test_selection(self): - indv = GAIndividual(ranges=[(0, 30)], verbosity=0) + indv = BinaryIndividual(ranges=[(0, 30)], verbosity=0) p = GAPopulation(indv) p.init() selection = RouletteWheelSelection() father, mother = selection.select(p, fitness=self.fitness) - self.assertTrue(isinstance(father, GAIndividual)) - self.assertTrue(isinstance(mother, GAIndividual)) + self.assertTrue(isinstance(father, BinaryIndividual)) + self.assertTrue(isinstance(mother, BinaryIndividual)) self.assertNotEqual(father.chromsome, mother.chromsome) if '__main__' == __name__: diff --git a/tests/tournament_selection_test.py b/tests/tournament_selection_test.py index 2744375..0c7a30b 100644 --- a/tests/tournament_selection_test.py +++ b/tests/tournament_selection_test.py @@ -7,7 +7,7 @@ import unittest from gaft.components.population import GAPopulation -from gaft.components.individual import GAIndividual +from gaft.components.individual import BinaryIndividual from gaft.operators import TournamentSelection class TournamentSelectionTest(unittest.TestCase): @@ -20,15 +20,15 @@ def fitness(indv): self.fitness = fitness def test_selection(self): - indv = GAIndividual(ranges=[(0, 30)], verbosity=0) + indv = BinaryIndividual(ranges=[(0, 30)], verbosity=0) p = GAPopulation(indv) p.init() selection = TournamentSelection() father, mother = selection.select(p, fitness=self.fitness) - self.assertTrue(isinstance(father, GAIndividual)) - self.assertTrue(isinstance(mother, GAIndividual)) + self.assertTrue(isinstance(father, BinaryIndividual)) + self.assertTrue(isinstance(mother, BinaryIndividual)) if '__main__' == __name__: suite = unittest.TestLoader().loadTestsFromTestCase(TournamentSelectionTest) diff --git a/tests/uniform_crossover_test.py b/tests/uniform_crossover_test.py index bad0b8d..d04abf3 100644 --- a/tests/uniform_crossover_test.py +++ b/tests/uniform_crossover_test.py @@ -6,7 +6,7 @@ import unittest -from gaft.components.individual import GAIndividual +from gaft.components.individual import BinaryIndividual from gaft.operators.crossover.uniform_crossover import UniformCrossover class UniformCrossoverTest(unittest.TestCase): @@ -17,8 +17,8 @@ def setUp(self): def test_cross(self): ''' Make sure individuals can be crossed correctly. ''' - father = GAIndividual(ranges=[(0, 1)], verbosity=0).init(variants=[0.398]) - mother = GAIndividual(ranges=[(0, 1)], verbosity=0).init(variants=[0.298]) + father = BinaryIndividual(ranges=[(0, 1)], verbosity=0).init(variants=[0.398]) + mother = BinaryIndividual(ranges=[(0, 1)], verbosity=0).init(variants=[0.298]) crossover = UniformCrossover(pc=1.0, pe=0.5) child1, child2 = crossover.cross(father, mother)