DEAP / deap

Distributed Evolutionary Algorithms in Python
http://deap.readthedocs.org/
GNU Lesser General Public License v3.0
5.75k stars 1.12k forks source link

Question about creating Arithmetic Expression #429

Open dancincn121 opened 4 years ago

dancincn121 commented 4 years ago

I follow the example about arithmetic expression:

---------------------code------------------- import operator

from deap import base from deap import creator from deap import gp from deap import tools

pset = gp.PrimitiveSet("MAIN", arity=1) pset.addPrimitive(operator.add, 2) pset.addPrimitive(operator.sub, 2) pset.addPrimitive(operator.mul, 2)

creator.create("FitnessMin", base.Fitness, weights=(-1.0,)) creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMin, pset=pset)

toolbox = base.Toolbox() toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min=1, max=2) toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr) ------------------------code end-------------------------

then the toolbox.individual returns mul(ARG0,ARG0) How can I use other variables to instead ARG0 (for example: to return the result of 3*5)?

fmder commented 4 years ago

I think what you are looking for is the arity.

dancincn121 commented 4 years ago

I think what you are looking for is the arity.

i use pset = gp.PrimitiveSet("MAIN", arity=2) pset.renameArguments(ARG0=3) pset.renameArguments(ARG1=5) then a = toolbox.individual() returns mul(3*5)

what should i do if i want the result to become 15?