0

I'm trying to write a genetic program in Python using DEAP and have several zero argument functions as terminals. I want to be able to combine them using, for example, an if_then_else primitive but this primitive keeps trying to call my other int type terminals. I want to strongly type the pset to avoid this but DEAP won't allow me to use None type arguments when adding strongly typed primitives, what can I do?

This is the code defining the pset I have so far

def progn(*args):
    for arg in args:
        arg()

def prog2(out1, out2): 
    return partial(progn,out1,out2)

def prog3(out1, out2, out3):     
    return partial(progn,out1,out2,out3)

def if_then_else(condition, out1, out2):
    out1() if condition() else out2()   

pset = gp.PrimitiveSetTyped("main", [None, int, bool], None)

pset.addPrimitive(if_wall_ahead, [None, None], None)
pset.addPrimitive(prog2, [None, None], None)
pset.addPrimitive(prog3, [None, None, None], None)
pset.addPrimitive(compare, [int,int], bool)
pset.addPrimitive(operator.and_, [bool, bool], bool)
pset.addPrimitive(operator.or_, [bool, bool], bool)
pset.addPrimitive(operator.not_, [bool], bool)

pset.addTerminal(player.changeDirectionRight, None)
pset.addTerminal(player.changeDirectionUp, None)
pset.addTerminal(player.changeDirectionLeft, None)
pset.addTerminal(player.changeDirectionDown, None)
pset.addTerminal(player.h_target_distance, int)
pset.addTerminal(player.v_target_distance, int)
pset.addEphemeralConstant("rand", random.randint(0,20), int)
markalex
  • 8,623
  • 2
  • 7
  • 32
Davidson
  • 1
  • 1

1 Answers1

1

You can create a dummy class to represent your boolean types, because bool and int are the "same types".

One of the DEAP authors suggest a solution like that:

class Bool(object): pass
pset.addPrimitive(xxxx, [yyy, yyy], Bool)

You can read a discussion about that on github, maybe this can be helpuful.

ItsMeMario
  • 113
  • 8