Skip to content

Commit

Permalink
GeneDc.kexpression and its simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
ShuhuaGao committed Jul 26, 2018
1 parent eae8d24 commit 90ec47f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
32 changes: 31 additions & 1 deletion geppy/core/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"""
import copy
from ..tools.generator import *
from ..core.symbol import Function, TerminalRNC
from ..core.symbol import Function, TerminalRNC, Terminal


_DEBUG = False
Expand Down Expand Up @@ -411,6 +411,36 @@ def __str__(self):
return str(self.rnc_array[self.dc[0]])
return expr[0].format() # only contains a normal terminal

@property
def kexpression(self):
"""
Get the K-expression of type :class:`KExpression` represented by this gene. The involved RNC terminal will be
replaced by a constant terminal with its value retrived from the :meth:`rnc_array` according to the GEP-RNC
algorithm.
"""
n_rnc = 0

def convert_RNC(p):
nonlocal n_rnc
if isinstance(p, TerminalRNC):
index = self.dc[n_rnc]
value = self.rnc_array[index]
n_rnc += 1
t = Terminal(str(value), value)
return t
return p

# level-order
expr = KExpression([convert_RNC(self[0])])
i = 0
j = 1
while i < len(expr):
for _ in range(expr[i].arity):
expr.append(convert_RNC(self[j]))
j += 1
i += 1
return expr

def __repr__(self):
return super().__repr__() + ', rnc_array=[' + ', '.join(str(num) for num in self.rnc_array) + ']'

Expand Down
15 changes: 11 additions & 4 deletions geppy/support/simplification.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
This module :mod:`simplification` provides utility functions for symbolic simplification of GEP individuals, which may
be used in postprocessing.
"""

import math
import operator
from geppy.core.entity import KExpression, Chromosome, Gene
from geppy.core.symbol import Function, Terminal

import sympy as sp

#: the default symbolic function map for the :func:`simplify` function, which includes common arithmetic operations,
#: Boolean operations.
#: The default symbolic function map for the :func:`simplify` function, which includes common arithmetic operations and
#: Boolean operations. Note that generally normal Python functions cannot work with symbolic variables directly. Thus,
#: such a map is required.
DEFAULT_SYMBOLIC_FUNCTION_MAP = {
operator.and_.__name__: sp.And,
operator.or_.__name__: sp.Or,
Expand All @@ -24,7 +25,8 @@
operator.mul.__name__: operator.mul,
operator.neg.__name__: operator.neg,
operator.floordiv.__name__: operator.floordiv,
operator.truediv.__name__: operator.truediv
operator.truediv.__name__: operator.truediv,
math.log.__name__: sp.log
}


Expand Down Expand Up @@ -98,6 +100,11 @@ def simplify(genome, symbolic_function_map=None):
elif isinstance(genome, Chromosome):
if len(genome) == 1:
return _simplify_kexpression(genome[0].kexpression, symbolic_function_map)
else: # multigenic chromosome
simplified_exprs = [_simplify_kexpression(g.kexpression, symbolic_function_map) for g in genome]
# combine these sub-expressions into a single one with the linking function
linker = symbolic_function_map[genome.linker.__name__]
return sp.simplify(linker(*simplified_exprs))
else:
raise TypeError('Only an argument of type KExpression, Gene, and Chromosome is acceptable. The provided '
'genome type is {}.'.format(type(genome)))

0 comments on commit 90ec47f

Please sign in to comment.