Skip to content

Error raise when use eval() to calculate expression string contained 0 arity function terminal #644

@liningbo

Description

@liningbo

deap 1.3.1
python 3.8

def if_then_else(input, output1, output2):
    return output1 if input else output2

def no_input_func_1():
    return 2.0

def no_input_func_2():
    return True

pset = gp.PrimitiveSetTyped('main', [bool, float], float)

pset.addPrimitive(operator.xor, [bool, bool], bool)
pset.addPrimitive(operator.mul, [bool, bool], bool)
pset.addPrimitive(if_then_else, [bool, float, float], float)
pset.addTerminal(3.0, float)
pset.addTerminal(True, bool)

pset.renameArguments(ARG0 = 'x')
pset.renameArguments(ARG1 = 'y')

pset.addTerminal(no_input_func_1,float)
pset.addTerminal(no_input_func_2,bool)

the expression string of a PrimitiveTree will be like if_then_else(x, 3.0, no_input_func_1) which will raise error when use eval() to calculate it, because of no_input_func_1 can not be executed but no_input_func_1() can.

so I think 0 arity function terminal should be formated as no_input_func_1() not no_input_func_1.

I have created a merge request which I have used well.
#643

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions