Skip to content

Commit

Permalink
Name changing: variants -> solution.
Browse files Browse the repository at this point in the history
  • Loading branch information
PytLab committed Dec 12, 2017
1 parent b13edde commit a3a9f76
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 46 deletions.
50 changes: 25 additions & 25 deletions gaft/components/individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,60 +76,60 @@ 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


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
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -183,23 +183,23 @@ 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))

return chromsome

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):
'''
Expand Down
2 changes: 1 addition & 1 deletion tests/dynamic_linear_scaling_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/engine_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/exponential_ranking_selection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/flip_bit_big_mutation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/flip_bit_mutation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
18 changes: 9 additions & 9 deletions tests/individual_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -45,24 +45,24 @@ 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):
''' Make sure individual can be cloned correctly.
'''
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)

Expand All @@ -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__:
Expand Down
2 changes: 1 addition & 1 deletion tests/linear_ranking_selection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/linear_scaling_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/population_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/roulette_wheel_selection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/tournament_selection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions tests/uniform_crossover_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit a3a9f76

Please sign in to comment.