Generating Python Code

90 views
Skip to first unread message

simonb...@gmail.com

unread,
Aug 22, 2020, 1:06:53 PM8/22/20
to deap-users
Hi All,

Has anyone used DEAP to generate solutions generated during a GP run directly as python code not as a tree?

E.g. the symbreg example will be generated directly as a python function rather than as a tree 

Thanks
Simon

Derek Tishler

unread,
Aug 23, 2020, 11:22:45 AM8/23/20
to deap-users
An interesting question. After some searching it turned into "Python code to AST(abstract syntax tree) and back". But it seems python's lambda becomes an issue quick(can jump to last link for Deap discussion url)

I tried to look into python to AST libs and noticed some existing discussions(outside deap) regarding generally working with python code to AST(abstract syntax trees) and back again such as:
https://stackoverflow.com/questions/56487216/how-can-i-convert-python-code-into-a-parse-tree-and-back-into-the-original-code
Which in a comment links to:
https://libcst.readthedocs.io/en/latest/index.html
And some info regarding retaining whitespace and format via lossy vs lossless tree types:
https://libcst.readthedocs.io/en/latest/why_libcst.html
This seems to have some interesting tradeoffs depending on which type of Tree is used, such as retaining formatting, or proving a way to compare trees of slightly different composition for equivalency via node comparison vs individual string comparison.

I'm not sure how this can play into a Deap program due to limitations of lambda:
https://stackoverflow.com/questions/20256647/python-deap-individual-to-an-ast

More specific discussion here for Deap & AST in which they describe efforts made to work with AST in the past but it didn't stick:
https://groups.google.com/d/msg/deap-users/p2r2nbMRFIE/HN_ApK0Gp6oJ

Andrew Morgan

unread,
Aug 23, 2020, 5:37:17 PM8/23/20
to deap-...@googlegroups.com
Although I am not able to answer the wider question about arbitrary python code from / to ASTs, I have used an external Deap module called Geppy that implements Gene Expression Programming for doing symbolic regression successfully. 

It compiles an individual’s genes into numeric functions via sympy. Perhaps sympy ASTs are a partial solution. My understanding is that once numerical functions are compiled from genes into sympy ASTs there may be code generation options you might investigate, allowing the function to be further exported as actual written code, with several language options available.


My geppy example is here:


HTH
Andrew



Sent from my iPhone

On 23 Aug 2020, at 16:22, Derek Tishler <lstr...@gmail.com> wrote:


--
You received this message because you are subscribed to the Google Groups "deap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to deap-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/deap-users/9bffc6cc-f63e-4442-9d56-574766b00cf5o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages