gintool / gin

GI in No Time - a Simple Microframework for Genetic Improvement
MIT License
43 stars 21 forks source link

POC for node mutations #18

Closed sandybrownlee closed 6 years ago

sandybrownlee commented 6 years ago

Addresses #13

Rather than statements, the idea is to modify types of nodes in the source tree, so that we can include statements, expressions, types and so on too. In this proof of concept we have the following:

A new abstract subclass of gin.edit.Edit, gin.edit.ModifyNode. ModifyNode maintains its own list of nodes that are of the relevant type in the original source, as well as the index of the particular node to be modified.

A new abstract class gin.edit.ModifyNodeFactory, whose job it is to make ModifyNode objects, provides a list of applicable node types for ModifyNode, and can filter the nodes in a CompilationUnit to be provided as candidates for modification.

A concrete class gin.edit.modifynode.LogicalOperatorReplacement, based on the LOR operator in MuJava. This applies to any BinaryStatement nodes, and will swap the operator with others of the same category (e.g. arithmetic operators are one category, comparisons are another, boolean operators are another).

A concrete class gin.edit.modifynode.LogicalOperatorReplacementFactory, which makes instances of the above and provides "BinaryStatement" as its only applicable node type.

Patch.java - added necessary code to make new random LogicalOperatorReplacement instances and apply them - this is quite brief as most of the work is in the factory.

I went with the factory method, so the factory can take care of things like working out whether the relevant nodes exist before we've made any operators of a particular type. It will also take care of setting up the node lists as a appropriate to avoid doing it every time a new operator instance is made.

I've provided a Triangle2 test program in examples/, where the delay is wrapped in a if(currentTime > 0) block that should always run - one fix is still to delete the delay, but another is to swap the > for another comparison that will usually evaluate to false.

drdrwhite commented 6 years ago

Fixes #13