Nested Power functions

44 views
Skip to first unread message

Leonidas Gkimisis

unread,
Mar 20, 2021, 6:46:57 AM3/20/21
to deap-users
Dear all, 
I am a master's student in engineering and I am interested in using DEAP for genetic programming purposes. 
I want to introduce a power function, that takes as an exponent only an ephemeral constant value and not some variable, so it should be x^k, where k is a constant (e.g. x^-3.4). 
This is due to natural "constraints" of my problem, but also to avoid having nested exponentials that have no physical meaning (e.g. x^(y^x)...).
My level in python programming is rather novice and have not yet found a good way to add this constraint to the tree structure.
Thanks for your help in advance!
Leonidas Gkimisis

Derek Tishler

unread,
Mar 20, 2021, 12:02:48 PM3/20/21
to deap-users
Perhaps one method could be working with strongly typed GP and only allow PowerFloat(a custom class but it's essentially a float, the custom class then prevent the GP from mixing types, you can essentially add units this way but the pset can get complication as you need to have mixing function, like power in this case, or like if-then-else like in the case of mixing bools with other types).
https://deap.readthedocs.io/en/master/examples/gp_spambase.html

Assuming you are using INT and FLOAT types in your pset already, lets say you want to form a power function that work like:

power(in_float, in_power_only_float)

Where in_power_only_float is a new type to put the ephemeral floats in. Then we can try something like:

#Perhaps like this
class 
PowerFloat():
    pass
#Modify pset accordingly
pset.addPrimitive(power, [float, 
PowerFloat], float, 'power')
pset.addEphemeralConstant("rand100", lambda: random.random() * 100,  PowerFloat)

Here is an example with integers for a case of trading actions, note these are for constants but I imagine the addEphemeralConstant can be played with too like above or perhaps in the class.

Leonidas Gkimisis

unread,
Mar 22, 2021, 3:08:18 PM3/22/21
to deap-users
Derek, thanks so much for the extensive answer! 

Indeed I made this work. 
One more thing (for anyone else interested in this) is that one should add a terminal and a primitive (possibly an identity function) that take the class PowerFloat as an input, so that the tree can be built, due to the fact that ephemeral constants are just leaf nodes.

Thanks again!

Reply all
Reply to author
Forward
0 new messages