From a3a9f76a2a09922e19b233b67352356153bc87b9 Mon Sep 17 00:00:00 2001 From: pytlab Date: Tue, 12 Dec 2017 21:05:39 +0800 Subject: [PATCH] Name changing: variants -> solution. --- gaft/components/individual.py | 50 ++++++++++----------- tests/dynamic_linear_scaling_test.py | 2 +- tests/engine_test.py | 2 +- tests/exponential_ranking_selection_test.py | 2 +- tests/flip_bit_big_mutation_test.py | 2 +- tests/flip_bit_mutation_test.py | 2 +- tests/individual_test.py | 18 ++++---- tests/linear_ranking_selection_test.py | 2 +- tests/linear_scaling_test.py | 2 +- tests/population_test.py | 2 +- tests/roulette_wheel_selection_test.py | 2 +- tests/tournament_selection_test.py | 2 +- tests/uniform_crossover_test.py | 4 +- 13 files changed, 46 insertions(+), 46 deletions(-) diff --git a/gaft/components/individual.py b/gaft/components/individual.py index c2d70b3..2e79f87 100644 --- a/gaft/components/individual.py +++ b/gaft/components/individual.py @@ -76,52 +76,52 @@ def __init__(self, ranges, eps=0.001): self.eps = eps self.precisions = eps - self.variants, self.chromsome = [], [] + self.solution, self.chromsome = [], [] - def _rand_variants(self): - ''' Initialize individual variants randomly. + def _rand_solution(self): + ''' Initialize individual solution randomly. ''' - variants = [] + solution = [] 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 + solution.append(a + n*eps) + return solution - def init(self, chromsome=None, variants=None): + def init(self, chromsome=None, solution=None): ''' - Initialize the individual by providing chromsome or variants. + Initialize the individual by providing chromsome or solution. - If both chromsome and variants are provided, only the chromsome would + If both chromsome and solution 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. + :param solution: the variable vector of the target function. + :type solution: list of float. ''' - if not any([chromsome, variants]): - self.variants = self._rand_variants() + if not any([chromsome, solution]): + self.solution = self._rand_solution() self.chromsome = self.encode() elif chromsome: self.chromsome = chromsome - self.variants = self.decode() + self.solution = self.decode() else: - self.variants = variants + self.solution = solution self.chromsome = self.encode() return self def encode(self): ''' *NEED IMPLIMENTATION* - Convert variants to chromsome sequence. + Convert solution to chromsome sequence. ''' raise NotImplementedError def decode(self): ''' *NEED IMPLIMENTATION* - Convert chromsome sequence to variants. + Convert chromsome sequence to solution. ''' raise NotImplementedError @@ -129,7 +129,7 @@ def decode(self): class BinaryIndividual(IndividualBase): def __init__(self, ranges, eps=0.001, verbosity=1): ''' - Class for individual in population. Random variants will be initialized + Class for individual in population. Random solution will be initialized by default. NOTE: The decrete precisions for different components in varants may be @@ -139,7 +139,7 @@ def __init__(self, ranges, eps=0.001, verbosity=1): Please check it before you put it into GA engine. If you don't want to see the warning info, set verbosity to 0 :) - :param ranges: value ranges for all entries in variants. + :param ranges: value ranges for all entries in solution. :type ranges: list of range tuples. e.g. [(0, 1), (-1, 1)] :param eps: decrete precisions for binary encoding, default is 0.001. @@ -165,7 +165,7 @@ def __init__(self, ranges, eps=0.001, verbosity=1): self.lengths.append(length) self.precisions[i] = precision - # The start and end indices for each gene segment for entries in variants. + # The start and end indices for each gene segment for entries in solution. self.gene_indices = self._get_gene_indices() # Initialize individual randomly. @@ -183,10 +183,10 @@ def clone(self): def encode(self): ''' - Encode variant to gene sequence in individual using different encoding. + Encode solution to gene sequence in individual using different encoding. ''' chromsome = [] - for var, (a, _), length, eps in zip(self.variants, self.ranges, + for var, (a, _), length, eps in zip(self.solution, self.ranges, self.lengths, self.precisions): chromsome.extend(self.binarize(var-a, eps, length)) @@ -194,12 +194,12 @@ def encode(self): def decode(self): ''' - Decode gene sequence to variants of target function. + Decode gene sequence to solution of target function. ''' - variants = [self.decimalize(self.chromsome[start: end], eps, lower_bound) + solution = [self.decimalize(self.chromsome[start: end], eps, lower_bound) for (start, end), (lower_bound, _), eps in zip(self.gene_indices, self.ranges, self.precisions)] - return variants + return solution def _get_gene_indices(self): ''' diff --git a/tests/dynamic_linear_scaling_test.py b/tests/dynamic_linear_scaling_test.py index a5cc63c..ed5281c 100644 --- a/tests/dynamic_linear_scaling_test.py +++ b/tests/dynamic_linear_scaling_test.py @@ -39,7 +39,7 @@ def test_run(self): @engine.fitness_register @engine.dynamic_linear_scaling() def fitness(indv): - x, = indv.variants + x, = indv.solution return x + 10*sin(5*x) + 7*cos(4*x) engine.run(50) diff --git a/tests/engine_test.py b/tests/engine_test.py index bebdd6e..98c2274 100644 --- a/tests/engine_test.py +++ b/tests/engine_test.py @@ -39,7 +39,7 @@ def test_run(self): @engine.fitness_register @engine.minimize def fitness(indv): - x, = indv.variants + x, = indv.solution return x + 10*sin(5*x) + 7*cos(4*x) engine.run(50) diff --git a/tests/exponential_ranking_selection_test.py b/tests/exponential_ranking_selection_test.py index 1c3d707..3be6f48 100644 --- a/tests/exponential_ranking_selection_test.py +++ b/tests/exponential_ranking_selection_test.py @@ -15,7 +15,7 @@ class ExponentialRankingSelectionTest(unittest.TestCase): def setUp(self): self.maxDiff def fitness(indv): - x, = indv.variants + x, = indv.solution return x**3 - 60*x**2 + 900*x + 100 self.fitness = fitness diff --git a/tests/flip_bit_big_mutation_test.py b/tests/flip_bit_big_mutation_test.py index f49e48b..d034162 100644 --- a/tests/flip_bit_big_mutation_test.py +++ b/tests/flip_bit_big_mutation_test.py @@ -36,7 +36,7 @@ def test_mutate(self): @engine.fitness_register def fitness(indv): - x, = indv.variants + x, = indv.solution return x + 10*sin(5*x) + 7*cos(4*x) mutation.mutate(indv_template, engine) diff --git a/tests/flip_bit_mutation_test.py b/tests/flip_bit_mutation_test.py index 682853a..290fc92 100644 --- a/tests/flip_bit_mutation_test.py +++ b/tests/flip_bit_mutation_test.py @@ -17,7 +17,7 @@ def setUp(self): def test_mutate(self): ''' Make sure the individual can be mutated correctly. ''' - indv = BinaryIndividual(ranges=[(0, 1)], verbosity=0).init(variants=[0.398]) + indv = BinaryIndividual(ranges=[(0, 1)], verbosity=0).init(solution=[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 73c87b1..6e4519c 100644 --- a/tests/individual_test.py +++ b/tests/individual_test.py @@ -17,7 +17,7 @@ def test_binary_encoding(self): ''' Make sure individual can decode and encode binary gene correctly. ''' indv = BinaryIndividual(ranges=[(0, 1)], eps=0.001, verbosity=0) - indv.init(variants=[0.398]) + indv.init(solution=[0.398]) # Test binary chromsome. ref_chromsome = [0, 1, 1, 0, 0, 1, 0, 1, 1] @@ -27,7 +27,7 @@ def test_binary_encoding(self): self.assertListEqual(indv.decode(), [0.396484375]) indv = BinaryIndividual(ranges=[(0, 1), (-1, 1)], eps=0.001, verbosity=0) - indv.init(variants=[0.398, 0.66]) + indv.init(solution=[0.398, 0.66]) # Test binary chromsome. ref_chromsome = [0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1] @@ -45,12 +45,12 @@ def test_init(self): indv.init(chromsome=[0, 1, 1, 0, 0, 0, 1, 1, 1, 0]) self.assertListEqual([0, 1, 1, 0, 0, 0, 1, 1, 1, 0], indv.chromsome) - self.assertListEqual(indv.variants, [0.388671875]) + self.assertListEqual(indv.solution, [0.388671875]) - # Check variants initialization. - indv.init(variants=[0.398]) + # Check solution initialization. + indv.init(solution=[0.398]) - self.assertListEqual(indv.variants, [0.398]) + self.assertListEqual(indv.solution, [0.398]) self.assertListEqual(indv.chromsome, [0, 1, 1, 0, 0, 1, 0, 1, 1]) def test_clone(self): @@ -58,11 +58,11 @@ def test_clone(self): ''' indv = BinaryIndividual(ranges=[(0, 1)], eps=0.001, - verbosity=0).init(variants=[0.398]) + verbosity=0).init(solution=[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.assertAlmostEqual(indv.solution[0], indv_clone.solution[0], places=2) self.assertEqual(indv.ranges, indv_clone.ranges) self.assertEqual(indv.eps, indv_clone.eps) @@ -71,7 +71,7 @@ def test_multi_precisions(self): ''' indv = BinaryIndividual(ranges=[(0, 1), (0, 10)], eps=[0.01, 1.0], - verbosity=0).init(variants=[0.3, 0.5]) + verbosity=0).init(solution=[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 b4296d6..5f134d8 100644 --- a/tests/linear_ranking_selection_test.py +++ b/tests/linear_ranking_selection_test.py @@ -15,7 +15,7 @@ class LinearRankingSelectionTest(unittest.TestCase): def setUp(self): self.maxDiff def fitness(indv): - x, = indv.variants + x, = indv.solution return x**3 - 60*x**2 + 900*x + 100 self.fitness = fitness diff --git a/tests/linear_scaling_test.py b/tests/linear_scaling_test.py index c43b6de..85c2ed5 100644 --- a/tests/linear_scaling_test.py +++ b/tests/linear_scaling_test.py @@ -39,7 +39,7 @@ def test_run(self): @engine.fitness_register @engine.linear_scaling(target='max', ksi=0.5) def fitness(indv): - x, = indv.variants + x, = indv.solution return x + 10*sin(5*x) + 7*cos(4*x) engine.run(50) diff --git a/tests/population_test.py b/tests/population_test.py index 086a6a8..92167c0 100644 --- a/tests/population_test.py +++ b/tests/population_test.py @@ -15,7 +15,7 @@ def setUp(self): self.maxDiff = True self.indv_template = BinaryIndividual(ranges=[(0, 1)], verbosity=0) def fitness(indv): - x, = indv.variants + x, = indv.solution return x**3 - 60*x**2 + 900*x + 100 self.fitness = fitness diff --git a/tests/roulette_wheel_selection_test.py b/tests/roulette_wheel_selection_test.py index 9d031ca..a3ef658 100644 --- a/tests/roulette_wheel_selection_test.py +++ b/tests/roulette_wheel_selection_test.py @@ -15,7 +15,7 @@ class RouletteWheelSelectionTest(unittest.TestCase): def setUp(self): self.maxDiff def fitness(indv): - x, = indv.variants + x, = indv.solution return x**3 - 60*x**2 + 900*x + 100 self.fitness = fitness diff --git a/tests/tournament_selection_test.py b/tests/tournament_selection_test.py index 0c7a30b..1df8000 100644 --- a/tests/tournament_selection_test.py +++ b/tests/tournament_selection_test.py @@ -15,7 +15,7 @@ class TournamentSelectionTest(unittest.TestCase): def setUp(self): self.maxDiff def fitness(indv): - x, = indv.variants + x, = indv.solution return x**3 - 60*x**2 + 900*x + 100 self.fitness = fitness diff --git a/tests/uniform_crossover_test.py b/tests/uniform_crossover_test.py index d04abf3..6475415 100644 --- a/tests/uniform_crossover_test.py +++ b/tests/uniform_crossover_test.py @@ -17,8 +17,8 @@ def setUp(self): def test_cross(self): ''' Make sure individuals can be crossed correctly. ''' - father = BinaryIndividual(ranges=[(0, 1)], verbosity=0).init(variants=[0.398]) - mother = BinaryIndividual(ranges=[(0, 1)], verbosity=0).init(variants=[0.298]) + father = BinaryIndividual(ranges=[(0, 1)], verbosity=0).init(solution=[0.398]) + mother = BinaryIndividual(ranges=[(0, 1)], verbosity=0).init(solution=[0.298]) crossover = UniformCrossover(pc=1.0, pe=0.5) child1, child2 = crossover.cross(father, mother)