Strongly Type GP - how to create my own type?

118 views
Skip to first unread message

ML

unread,
Oct 26, 2018, 8:06:33 AM10/26/18
to deap-users
Some primitives and the output of the GP tree is a tuple of 3 floats, for example (1.2, 1.4, 4.2). How can I create the "tuple" type?      

Marc-André Gardner

unread,
Oct 26, 2018, 6:08:29 PM10/26/18
to deap-users
You can pass any valid Python class (including native types) to the addPrimitive function, so I would guess something like :
pset.addPrimitive(operator.add, [tuple,tuple], tuple)

See the spambase example if you want more context.

Mae Leong

unread,
Oct 27, 2018, 4:56:15 PM10/27/18
to deap-users
It works!  Thanks. 
Message has been deleted

Ami Hauptman

unread,
Nov 29, 2018, 3:35:26 AM11/29/18
to deap-users
In the same context - what if "my own type" is something that doesn't exist in python?

For example, I want to divide my features (all of them are floats) to 2 groups, let's call them "floats-a" and "floats-b". There is no such type in Python

What would be the best way to do that?



בתאריך יום שישי, 26 באוקטובר 2018 בשעה 15:06:33 UTC+3, מאת ML:

Derek Tishler

unread,
Nov 29, 2018, 10:43:21 AM11/29/18
to deap-users
You can try a custom class, something like:

# in a global scope for all to see
class Action():
    Short = 0
    Cover = 1
    Long  = 2
action_class = Action()

# Then i pset creation
pset.addPrimitive(if_then_else, [bool, Action, Action], Action, 'ite_a')

pset.addTerminal(action_class.Cover, Action, 'Cover')
pset.addTerminal(action_class.Short, Action, 'Short')
pset.addTerminal(action_class.Long, Action, 'Long')

Ami Hauptman

unread,
Dec 3, 2018, 6:50:15 AM12/3/18
to deap-users
Thank you, Derek - I will try that

Ami

בתאריך יום חמישי, 29 בנובמבר 2018 בשעה 17:43:21 UTC+2, מאת Derek Tishler:

Ami Hauptman

unread,
Dec 17, 2018, 8:50:42 AM12/17/18
to deap-users
I tried what you suggested, but it seems that I need something different. Let me state my question more precisely:

I am trying to write a classifier, very similar to the Spambase Problem in the examples

However

I need to divide the float variables in my table to 3 groups, and use specific operators (e.g. add) for each group, 
to construct if clauses which are from different groups such as:

if (feature_3_from_group_1 + feature_5_from_group_1) > (feature_6_from_group1-feature_7_from_group_1)
   then  (feature_2_from_group_2 - feature_7_from_group_2)
   else  (feature_9_from_group_2 * feature_10_from_group_2)

so - while basically how can I do this, where all features are floats, some operators are only for specific groups, while others (for example if) can take operators from several groups.

Also - how can I define terminals for such groups? do they have to be functions? Do I need to add them one by one, or can i use the  gp.PrimitiveSetTyped with itertools.repeat?

Thanks you in advance!
Ami


בתאריך יום חמישי, 29 בנובמבר 2018 בשעה 17:43:21 UTC+2, מאת Derek Tishler:
You can try a custom class, something like:
Reply all
Reply to author
Forward
0 new messages