Skip to content

Commit

Permalink
Change the generate default type in gp to pset.ret.
Browse files Browse the repository at this point in the history
This fixes a problem when a STGP user would forget
to set the type_ argument, the generate function
would request primitive of the type 'object'. The
type_ argument is only to accomodate GP mutations.
It is from now on set to None by default, and
replace by pset.ret if the no other type has been
specified.

--HG--
branch : dev
  • Loading branch information
cmd-ntrf authored and felix.antoine.fortin committed Apr 30, 2014
1 parent aeb6bd4 commit b14aca0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions deap/gp.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def compileADF(expr, psets):
######################################
# GP Program generation functions #
######################################
def genFull(pset, min_, max_, type_=__type__):
def genFull(pset, min_, max_, type_=None):
"""Generate an expression where each leaf has a the same depth
between *min* and *max*.
Expand All @@ -503,7 +503,7 @@ def condition(height, depth):
return depth == height
return generate(pset, min_, max_, condition, type_)

def genGrow(pset, min_, max_, type_=__type__):
def genGrow(pset, min_, max_, type_=None):
"""Generate an expression where each leaf might have a different depth
between *min* and *max*.
Expand All @@ -522,7 +522,7 @@ def condition(height, depth):
(depth >= min_ and random.random() < pset.terminalRatio)
return generate(pset, min_, max_, condition, type_)

def genHalfAndHalf(pset, min_, max_, type_=__type__):
def genHalfAndHalf(pset, min_, max_, type_=None):
"""Generate an expression with a PrimitiveSet *pset*.
Half the time, the expression is generated with :func:`~deap.gp.genGrow`,
the other half, the expression is generated with :func:`~deap.gp.genFull`.
Expand All @@ -537,7 +537,7 @@ def genHalfAndHalf(pset, min_, max_, type_=__type__):
method = random.choice((genGrow, genFull))
return method(pset, min_, max_, type_)

def genRamped(pset, min_, max_, type_=__type__):
def genRamped(pset, min_, max_, type_=None):
"""
.. deprecated:: 1.0
The function has been renamed. Use :func:`~deap.gp.genHalfAndHalf` instead.
Expand All @@ -546,7 +546,7 @@ def genRamped(pset, min_, max_, type_=__type__):
FutureWarning)
return genHalfAndHalf(pset, min_, max_, type_)

def generate(pset, min_, max_, condition, type_=__type__):
def generate(pset, min_, max_, condition, type_=None):
"""Generate a Tree as a list of list. The tree is build
from the root to the leaves, and it stop growing when the
condition is fulfilled.
Expand All @@ -562,6 +562,8 @@ def generate(pset, min_, max_, condition, type_=__type__):
:returns: A grown tree with leaves at possibly different depths
dependending on the condition function.
"""
if type_ is None:
type_ = pset.ret
expr = []
height = random.randint(min_, max_)
stack = [(0, type_)]
Expand Down
2 changes: 1 addition & 1 deletion examples/gp/spambase.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def if_then_else(input, output1, output2):
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMax)

toolbox = base.Toolbox()
toolbox.register("expr", gp.genHalfAndHalf, pset=pset, type_=pset.ret, min_=1, max_=2)
toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=1, max_=2)
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("compile", gp.compile, pset=pset)
Expand Down

0 comments on commit b14aca0

Please sign in to comment.