Coder Social home page Coder Social logo

johnmee / nigel Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 1.0 4.93 MB

An adventure into "Genetic Programming" for Nigel O'Neill. Building a program that randomly 'evolves' code to solve a problem.

Python 100.00%
deap evolutionary-algorithm notebook python

nigel's People

Contributors

johnmee avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

4sp1r3

nigel's Issues

Create a crossover (graft?) routine

Suitable for using the normalised probabilistic selection routine created in #2 create a crossover (mate) routine which, given two candidates (contributing and receiving parents)

  • determines what node types it has in common
  • picks one of those nodes at random in the parent and copies the node and its subtree/branch
  • picks a similarly typed node in the receiving parent at random
  • overwrites that node/subtree/branch with the one taken from the contributor
  • returns the new individual

if there are no common node-types then it raises an error so the calling algorithm can decide what to do.

ADF Capability - Create initial population with automatically defined architectures

Firstly, let's define what we mean by program 'architecture'.
According to Koza (GP2 pp525) the architecture of an individual program in the population is defined by:
a) the number of function-defining branches,
b) the number of arguments possessed by each function-defining branch, and
c) if there is more than one function-defining branch, the nature of the hierarchical references (if any) allowed between the function-defining branches

Secondly, we need to step through the following process each time we create an individual:

  1. Randomly select the number of ADF's it will have (between 0 and Max_ADFs_Allowed). Note than "Max_ADFs_Allowed" is a control parameter we need to be able to set.
  2. Name the ADF's from left to right as ADF0, ADF1, ADF2, ...... etc.
  3. Randomly select the number of arguments that each ADF will have (between 1 and "Max_ARGs_Allowed_in_ADFs"). Note than "Max_ARGs_Allowed_in_ADFs" is a control parameter we need to be able to set.
  4. Name the arguments fro left to right as ARG0, ARG1, ARG2, ...... etc.
  5. Create function sets for each ADF and RPB, comprising the union of the primitive functions of the problem with whatever ADF's may appear to the left of that branch (i.e. if the primitive function set for the problem is {NOR, AND, OR} and their are 2 ADF's then ADF0 would have a function set being {NOR, AND, OR}, ADF1 would have a function set being {ADF0, NOR, AND, OR}, and the Result Producing Branch (RPB) will have a function set {ADF0, ADF1, NOR, AND, OR})
  6. We now have a terminal set and function set for each ADF and RPB so we can then go about using the ourGrow method to create the individual
  7. Repeat steps 1 through 6 to create all individuals of the population

Check gengrow is behaving as desired

When generating populations we're seeing some pretty large trees. We're expecting to only see initial trees of depth 5.

Work out what's going on there...

screen shot 2015-05-11 at 9 30 43 am

Catch error in Crossover routine when there are no common Graft Types

IndexError                                Traceback (most recent call last)
<ipython-input-6-a45f87ec14a5> in <module>()
      7 types2 = set([node.ret for node in contributor[1:]])
      8 common_types = types1.intersection(types2)
----> 9 graft_type = random.choice(list(common_types))
     10 
     11 print("Receiver Types: ", types1)

/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/random.py in choice(self, seq)
    253             i = self._randbelow(len(seq))
    254         except ValueError:
--> 255             raise IndexError('Cannot choose from an empty sequence')
    256         return seq[i]
    257 

IndexError: Cannot choose from an empty sequence

Draw pretty graphs of trees

The documentation talks about and demonstrates drawing of trees with graphing software. Pete even had it going in his work. They look a bit like this:

image

Make that happen so that we can spit out images of selected individuals during a run.

ADF Capability - Point Typing for Structure-Preserving Crossover

Firstly, Koza states that point typing is used when the architecture of the individuals is evolutionarily selected (GP2, pp532).

For the Crossover routine/method to be compatible with various program architectures it will work best if each reproductive operation between two parents results in only 1 offspring being generated. Unlike the double-crossover which is possible when the architectures of each program in a population is homogenous and each crossover operation can produce 2 offspring, it works best if we only produce 1 offspring each time 2 parents mate due to the non-homogenous architecture of the population. This is because there are a number of compatibility checks which need to be done (aside from the type matching which always is required) making a successful swap very rare. This is because for both crossover fragments to be mutually compatible with their respective receivers both syntactically and and semantically is unlikely. This would result in a great deal of failed attempts and wasted computer resources. Hence, when programs contain ADF's with automatically defined architectures, we define parents as either a contributing parent, or a receiving parent, and the crossover is only done in one direction, from contributor to receiver.

Naturally, the contributing parent will contribute a randomly chosen program fragment for insertion into a randomly chosen point in the receiving parent, if (and only if) the below list of checks do not identify any incompatibility.

Before detailing the checks explicitly, they can broadly be described as falling into 3 areas.
• Firstly, every terminal and function contained in the crossover fragment must be in the terminal set or function set of the branch of the receiving parent. (This includes actual variables, dummy variables (e.g. ARG0, ARG1...), random constants, primitive functions, automatically defined functions)
• Secondly, The number of arguments of every function contained in the crossover fragment must equal the number of arguments for that function in the branch of the receiving parent to which it is being inserted.
• Thirdly, all other syntactic rules of construction must be satisfied, (including the type of the crossover fragment and the insertion point being a match)

Koza (GP2, pp533) explicitly states these 7 conditions of point-typing as:

(1) All the actual variables of the problem, if any; actually appearing in the
crossover fragment from the contributing parent must be in the terminal set of the branch of the receiving parent containing the point of insertion.

(2) All the dummy variables (e.g. ARG0, ARG1, etc.) if any actually appearing in the crossover fragment from the contributing parent must be in the terminal set of the branch of the receiving parent containing the point of insertion.

(3) All the automatically defined functions, if any actually appearing in the crossover fragment from the contributing parent must be in the function set of the branch of the receiving parent containing the point of insertion.

(4) All the automatically defined functions, if any actually appearing in the crossover fragment from the contributing parent must have exactly the number of arguments specified for that automatically defined function for the branch of the receiving parent containing the point of insertion.

(5) All functions (other than automatically defined functions) must also satisfy conditions (3) and (4).

(6) All terminals (other than dummy variables and actual variables of the problem already mentioned in conditions (1) and (2), if any actually appearing in the crossover fragment from the contributing parent must be in the terminal set of the branch of the receiving parent containing the point of insertion.

(7) All other syntactic rules of construction of the problem must be satisfied (including the type of the crossover fragment and the insertion point being a match).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.