Executing a Genetic Programming tree - genetic-programming

I am running GP algorithm using ECJ 26 during that I use describe() function to test the tree, however, currently I need to store and execute the tree outside ECJ framework.
Please, could you let me know if it is possible?
Thanks in advance.

The simplest approach to executing a tree (like an abstract syntax tree) would be traversing the tree using the visitor pattern and taking actions based on the current node and the current state of the executor (your "virtual machine"). This is how the so called tree walk interpreters work (a typical example is the Ruby interpreter prior to v1.9).
This is for sure a very generic answer, but hopefully it'll give you some ideas.

Related

Modify Parse Tree

I have an ANTLR ParseTree for an sql grammar.
Example :
My goal is to edit this tree so that i can delete all the middle (booleanExpression, predicated, valueExpression, primaryExpression ) nodes inbetween.
I have explored visitor and listenors but they don’t generate the tree for me. And i'd like to not touch the grammar since its the official source one.
So how can i do it?
Thanks
There is no such feature in ANTLR's API (removing/mutating the ParseTree). You'll have to walk the tree yourself and create a copy of the ParseTree and ignore certain sub-trees you do not want/need.
I’ve usually found the ParseTree to be too cumbersome (for exactly the reason you’ve shown). I don’t know of any automated way to alter the tree in memory. ( Could be an interesting project to attempt.)
I a couple of implementations I’ve written some generalized approach to make the process easier. The “best” one was where I wrote a small DSL to define the structure I wanted. It generated the classes as well as ANTLR style visitors and listeners for those. I then used a listener to transform the ANTLR ParseTree to my ideal tree, and wrote the rest of my code using that structure with that, much simpler, tree. You can have a listener/visitor generate a tree of your own design as an artifact of processing the ParseTree, but that’s as close as I’ve come.
It was actually a relatively minor effort to set that up early in the process and accounted for a quite small percentage of the overall effort of implementing the language (so, well worth the effort).

Guide in solving spoj GiveAway

I was unable to solve the problem Spoj Give away after thinking for a while and searching for help I came to know that it involves AVL Tree + Segement Tree . Since I haven't used this data Structure yet a detail explanation of Working code and the approach is much encouraged.
Link : http://www.spoj.com/problems/GIVEAWAY/
In SPOJ GIVEAWAY, the problem requires you to address a lot of queries and whenever we have to handle more than one queries, SQRT decomposition technique or Segment tree comes forward. I would suggest you to solve the problem using SQRT decomposition method. If you're new to both the methods, you can watch tutorials or some documentation on the internet. I'm providing some links I hope it'll help you ;)
SQRT Decomposition
Segment Tree

implementing jump-statement in ast-walking interpreter

In an ast-walking interpreter, the code is executed node by node. How can I implement features like goto, break or continue? I stop the current execution and jump to another node? Are there any best practices?
Best practice is "don't interpret ASTs for languages with gotos".
Fundamentally any kind of discontinuity in the tree walk causes serious slowdown if the language is processing mostly scalars. (If your language processes complex values mostly, like the array language APL, it won't matter).
The best you can hope for is to pre-walk the tree and determine where the gotos actually go in the AST, and record that in an associative cache off to the side. Then when you encounter a goto, simply consult the cache rather than searching the tree.
But this is the first step down the road toward compiling, e.g., precomputing what you can before you execute.

How to do Static analysis with ANTLR

I m planning to build a static analyzer tool for a proprietary language. I m planning to use ANTLR to build the AST. I would like to know how does one go about checking for rules and guidelines , set by the project using the proprietary language using the AST.
for e.g. if I build the AST for a C source code with and say i want to check for null pointers . How would i do this check using the AST or CST.
Will i have to code in the test with ANTLR generated lexer/parser?.
Thanks
It depends on the specific analysis you want to perform. Taking your specific example: to determine statically whether a variable might be (or will be) a null pointer, you need to construct a data flow graph. I recommend to study the dragon book.
If you want to check for null pointer values, you will need full control and data flow analysis for your proprietary language. ANTLR won't get you there without beyond-superhuman effort on your part.
Check out the flow analysis capabilities of our DMS. We have used this to do deep flow analysis of very large scale C programs.
Even using this machinery, you are going to have to do a lot of work to explain your langauge to it. This is just a lot easier than any other approach you might take.

Hierarchical state machine implementation in Erlang

I am planning a turn based game (a sort of board game), and the backend will probably be done in Erlang. The game logic part seems to be a fit for a hierarchical state machine, but I'm not sure about how to implement that in Erlang.
Maybe I can spawn a separate process with each child fsm, not sure if this would work.
Another option would be to embed a scripting language or create a DSL for this purpose.
What do you think?
Thanks.
I am the original question author (in fact I would like to claim the question as mine, but I don't know how).
I already know about all the things OTP offers, gen_fsm included.
The idea is to use a HIERARCHICAL state machine, and gen_fsm is a plain state machine. To keep track of the game turns, phases, etc, I don't think a plain state machine as gen_fsm would be enough.
Anyway, I have been investigating further, and I think that I will use erl-lua so I can use Lua for all the game logic. Once it is working I can search for bottlenecks and move them to a C implementation or whatever.
#codecaster you can use gen_fsm to track the state and just build in an additional level of state inside the fsm state... however, erl-lua would work as well. I built a monitoring tool with it called erlmon - we had some trouble with erl-lua crashing so keep in mind it's not bug free. also with the new support for Nifs, i'm waiting for someone to write a new driver for lua that is nif-based - you might want to look around for that - i haven't seen anyone build one yet.
As a starting point, I would suggest you to have a look to the OTP design principles. You'll probably want to have a more detailed look at supervisors and generic FSMs.