Skip to content

Commit

Permalink
Merge.
Browse files Browse the repository at this point in the history
--HG--
branch : dev
  • Loading branch information
fmder committed Nov 1, 2012
2 parents 4cc14bd + 934815b commit fe454fe
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
63 changes: 33 additions & 30 deletions deap/gp.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,20 @@ class Primitive(object):
>>> import operator
>>> pr = Primitive(operator.mul, (int, int), int)
>>> pr.toString("1", "2")
>>> pr.format(1, 2)
'mul(1, 2)'
"""
def __init__(self, primitive, args, ret = __type__):
__slots__ = ('name', 'arity', 'args', 'ret', 'seq')
def __init__(self, primitive, args, ret):
self.name = primitive.__name__
self.arity = len(args)
self.args = args
self.ret = ret
args = ", ".join(("%s",) * self.arity)
self.seq = "%s(%s)" % (self.name, args)
args = ", ".join(map("{{{0}}}".format, range(self.arity)))
self.seq = "{name}({args})".format(name=self.name, args=args)

def toString(self, *args):
return self.seq % args
def format(self, *args):
return self.seq.format(*args)

def __repr__(self):
return self.name
Expand All @@ -142,35 +142,36 @@ class Operator(Primitive):
>>> import operator
>>> op = Operator(operator.mul, '*', (int, int), int)
>>> op.toString("1", "2")
>>> op.format(1, 2)
'(1 * 2)'
>>> op2 = Operator(operator.neg, '-', (int,), int)
>>> op2.toString(1)
>>> op2.format(1)
'-(1)'
"""
def __init__(self, operator, symbol, args, ret = __type__):
def __init__(self, operator, symbol, args, ret):
Primitive.__init__(self, operator, args, ret)
if len(args) == 1:
self.seq = "%s(%s)" % (symbol, "%s")
elif len(args) == 2:
self.seq = "(%s %s %s)" % ("%s", symbol, "%s")
if self.arity == 1:
self.seq = "{symbol}({{0}})".format(symbol=symbol)
elif self.arity == 2:
self.seq = "({{0}} {symbol} {{1}})".format(symbol=symbol)
else:
raise ValueError("Operator arity can be either 1 or 2.")

class Terminal(object):
"""Class that encapsulates terminal primitive in expression. Terminals can
be values or 0-arity functions.
"""
def __init__(self, terminal, ret = __type__, symbolic=False):
__slots__ = ('value', 'ret', 'conv_fct')
def __init__(self, terminal, symbolic, ret):
self.ret = ret
self.arity = 0
self.value = terminal
if symbolic:
self.conv_fct = str
else:
self.conv_fct = repr
self.conv_fct = str if symbolic else repr

def toString(self):
@property
def arity(self):
return 0

def format(self):
return self.conv_fct(self.value)

def __repr__(self):
Expand All @@ -181,13 +182,14 @@ class Ephemeral(Terminal):
The value of the `Ephemeral` can be regenerated by creating a new
Ephemeral object with the same parameters (func and ret).
"""
def __init__(self, func, ret = __type__):
def __init__(self, func, ret):
self.func = func
Terminal.__init__(self, self.func(), ret)
Terminal.__init__(self, self.func(), False, ret)

class EphemeralGenerator(object):
"""Class that generates `Ephemeral` to be added to an expression."""
def __init__(self, ephemeral, ret = __type__):
__slots__ = ('name', 'ret', 'func')
def __init__(self, ephemeral, ret):
self.ret = ret
self.name = ephemeral.__name__
self.func = ephemeral
Expand Down Expand Up @@ -215,8 +217,9 @@ def __init__(self, name, in_types, ret_type, prefix = "ARG"):
self.ret = ret_type
self.ins = in_types
for i, type_ in enumerate(in_types):
self.arguments.append("%s%s" % (prefix,i))
self.terminals[type_].append(Terminal(self.arguments[-1], type_, True))
arg_str = "{prefix}{index}".format(prefix=prefix,index=i)
self.arguments.append(arg_str)
self.terminals[type_].append(Terminal(arg_str, True, type_))
self.terms_count += 1

def renameArguments(self, **kargs):
Expand Down Expand Up @@ -266,7 +269,7 @@ def addTerminal(self, terminal, ret_type, name=None):
terminal = name
symbolic = True

prim = Terminal(terminal, ret_type, symbolic)
prim = Terminal(terminal, symbolic, ret_type)

self.terminals[ret_type].append(prim)
self.terms_count += 1
Expand Down Expand Up @@ -336,7 +339,7 @@ def stringify(expr):
stack.append((node, []))
while len(stack[-1][1]) == stack[-1][0].arity:
prim, args = stack.pop()
string = prim.toString(*args)
string = prim.format(*args)
if len(stack) == 0:
break # If stack is empty, all nodes should have been seen
stack[-1][1].append(string)
Expand Down Expand Up @@ -364,9 +367,9 @@ def lambdify(expr, pset):
This function is a stripped version of the lambdify
function of sympy0.6.6.
"""
string = stringify(expr)
code = stringify(expr)
args = ",".join(arg for arg in pset.arguments)
lstr = "lambda %s: %s" % (args, string)
lstr = "lambda {args}: {code}".format(args=args, code=code)
try:
return eval(lstr, dict(pset.context))
except MemoryError:
Expand Down
2 changes: 1 addition & 1 deletion doc/tutorials/distribution.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ as the main module.
$ python -m scoop your_program.py
That it, your program has been run in parallel on all available processors.
That is it, your program has been run in parallel on all available processors.


Distributed Task Manager
Expand Down

0 comments on commit fe454fe

Please sign in to comment.