Skip to content

Commit

Permalink
Change version number
Browse files Browse the repository at this point in the history
  • Loading branch information
PytLab committed Oct 25, 2018
1 parent 6fcc7d5 commit 6b3e043
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 75 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ A **G**\ enetic **A**\ lgorithm **F**\ ramework in py\ **T**\ hon
:target: https://www.python.org/downloads/release/python-351/
:alt: platform

.. image:: https://img.shields.io/badge/pypi-v0.6.0-blue.svg
.. image:: https://img.shields.io/badge/pypi-v0.5.5-blue.svg
:target: https://pypi.python.org/pypi/gaft/
:alt: versions

Expand Down
2 changes: 1 addition & 1 deletion gaft/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from .engine import GAEngine

__version__ = '0.6.0'
__version__ = '0.5.5'
__author__ = 'ShaoZhengjiang <[email protected]>'

# Set root logger.
Expand Down
56 changes: 34 additions & 22 deletions gaft/components/binary_individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@


class BinaryIndividual(IndividualBase):
def __init__(self, ranges, eps=0.001):
'''
Class for individual in population. Random solution will be initialized
by default.
'''
Class for individual in population. Random solution will be initialized
by default.
NOTE: The decrete precisions for different components in varants may be
adjusted automatically (possible precision loss) if eps and ranges
are not appropriate.
:param ranges: value ranges for all entries in solution.
:type ranges: tuple list
: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.
:type eps: float or float list (with the same length with ranges)
:param eps: decrete precisions for binary encoding, default is 0.001.
:type eps: float or float list with the same length with ranges.
'''
.. Note:
The decrete precisions for different components in varants may be
adjusted automatically (possible precision loss) if eps and ranges
are not appropriate.
'''
def __init__(self, ranges, eps=0.001):
super(self.__class__, self).__init__(ranges, eps)

# Lengths for all binary sequence in chromsome and adjusted decrete precisions.
Expand All @@ -47,8 +49,7 @@ def __init__(self, ranges, eps=0.001):
self.init()

def encode(self):
'''
Encode solution 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.solution, self.ranges,
Expand All @@ -58,8 +59,7 @@ def encode(self):
return chromsome

def decode(self):
'''
Decode gene sequence to solution of target function.
''' Decode gene sequence to solution of target function.
'''
solution = [self.decimalize(self.chromsome[start: end], eps, lower_bound)
for (start, end), (lower_bound, _), eps in
Expand All @@ -76,21 +76,33 @@ def _get_gene_indices(self):

@staticmethod
def binarize(decimal, eps, length):
'''
Helper function to convert a float to binary sequence.
''' Helper function to convert a float to binary sequence.
:param decimal: the decimal number to be converted
:type decimal: float
:param eps: the decrete precision of binary sequence
:type eps: float
:param decimal: the decimal number to be converted.
:param eps: the decrete precision of binary sequence.
:param length: the length of binary sequence.
:type length: int
'''
n = int(decimal/eps)
bin_str = '{:0>{}b}'.format(n, length)
return [int(i) for i in bin_str]

@staticmethod
def decimalize(binary, eps, lower_bound):
'''
Helper function to convert a binary sequence back to decimal number.
''' Helper function to convert a binary sequence back to decimal number.
:param binary: The binary list to be converted
:type binary: list of int
:param eps: the decrete precision of binary sequence
:type eps: float
:param lower_bound: the lower bound for decimal number
:type lower_bound: float
'''
bin_str = ''.join([str(bit) for bit in binary])
return lower_bound + int(bin_str, 2)*eps
Expand Down
10 changes: 10 additions & 0 deletions gaft/components/decimal_individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@

class DecimalIndividual(IndividualBase):
''' Individual with decimal encoding.
:param ranges: value ranges for all entries in solution.
:type ranges: tuple list
:param eps: decrete precisions for binary encoding, default is 0.001.
:type eps: float or float list (with the same length with ranges)
'''
def __init__(self, ranges, eps=0.001):
super(self.__class__, self).__init__(ranges, eps)
# Initialize it randomly.
self.init()

def encode(self):
''' Encode solution to gene sequence
'''
return self.solution

def decode(self):
''' Decode gene sequence to decimal solution
'''
return self.solution

37 changes: 23 additions & 14 deletions gaft/components/individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,17 @@ def __set__(self, obj, precisions):

class IndividualBase(object):
''' Base class for individuals.
:param ranges: value ranges for all entries in solution.
:type ranges: tuple list
:param eps: decrete precisions for binary encoding, default is 0.001.
:type eps: float or float list (with the same length with ranges)
'''
# Solution ranges.
ranges = SolutionRanges()

# Original decrete precisions (provided by users).
# Orginal decrete precisions (provided by users).
eps = DecretePrecision()

# Actual decrete precisions used in GA.
Expand All @@ -74,17 +80,17 @@ def __init__(self, ranges, eps):
self.solution, self.chromsome = [], []

def init(self, chromsome=None, solution=None):
'''
Initialize the individual by providing chromsome or solution.
If both chromsome and solution are provided, only the chromsome would
be used. If neither is provided, individual would be initialized randomly.
''' Initialize the individual by providing chromsome or solution.
:param chromsome: chromesome sequence for the individual
:type chromsome: list of float/int.
:type chromsome: list of (float / int)
:param solution: the variable vector of the target function.
:type solution: list of float.
:type solution: list of float
.. Note::
If both chromsome and solution are provided, only the chromsome would
be used. If neither is provided, individual would be initialized randomly.
'''
if not any([chromsome, solution]):
self.solution = self._rand_solution()
Expand All @@ -99,27 +105,30 @@ def init(self, chromsome=None, solution=None):
return self

def clone(self):
'''
Clone a new individual from current one.
''' Clone a new individual from current one.
'''
indv = self.__class__(deepcopy(self.ranges), eps=deepcopy(self.eps))
indv.init(chromsome=deepcopy(self.chromsome))
return indv


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

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

Expand Down
Loading

0 comments on commit 6b3e043

Please sign in to comment.