Skip to content

Commit

Permalink
Add decimal individual class.
Browse files Browse the repository at this point in the history
  • Loading branch information
PytLab committed Dec 13, 2017
1 parent 1493b67 commit 4c7f531
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
22 changes: 22 additions & 0 deletions gaft/components/decimal_individual.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
''' Definition of individual class with decimal encoding.
'''

from .individual import IndividualBase


class DecimalIndividual(IndividualBase):
''' Individual with decimal encoding.
'''
def __init__(self, ranges, eps=0.001):
super(self.__class__, self).__init__(ranges, eps)
# Initialize it randomly.
self.init()

def encode(self):
return self.solution

def decode(self):
return self.solution

27 changes: 16 additions & 11 deletions gaft/components/individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,13 @@ class IndividualBase(object):
# Actual decrete precisions used in GA.
precisions = DecretePrecision()

def __init__(self, ranges, eps=0.001):
def __init__(self, ranges, eps):
self.ranges = ranges
self.eps = eps
self.precisions = eps

self.solution, self.chromsome = [], []

def _rand_solution(self):
''' Initialize individual solution randomly.
'''
solution = []
for eps, (a, b) in zip(self.precisions, self.ranges):
n_intervals = (b - a)//eps
n = int(uniform(0, n_intervals + 1))
solution.append(a + n*eps)
return solution

def init(self, chromsome=None, solution=None):
'''
Initialize the individual by providing chromsome or solution.
Expand Down Expand Up @@ -110,12 +100,27 @@ def init(self, chromsome=None, solution=None):
def encode(self):
''' *NEED IMPLIMENTATION*
Convert solution to chromsome sequence.
:return chromsome: The chromsome sequence, float list.
'''
raise NotImplementedError

def decode(self):
''' *NEED IMPLIMENTATION*
Convert chromsome sequence to solution.
:return solution: The solution vector, float list.
'''
raise NotImplementedError

def _rand_solution(self):
''' Initialize individual solution randomly.
'''
solution = []
for eps, (a, b) in zip(self.precisions, self.ranges):
n_intervals = (b - a)//eps
n = int(uniform(0, n_intervals + 1))
solution.append(a + n*eps)
return solution


0 comments on commit 4c7f531

Please sign in to comment.