devnology workshop genpro 2 feb 2011
TRANSCRIPT
GenPro Genetic Programming made simple
Rob van der Veer
Bluevoid
Genpro preamble
GenPro – 2 feb 2011
Automated help: “Grrr”
GenPro – 2 feb 2011
Settings vs re-programmable logic
GenPro – 2 feb 2011
AI to the rescue!
Genetic Programming
Genetic Algorithm
Neural networks
Fuzzy logic
Self learning machines
Emergent behavior
Reinforcement learningAutomated logic
Simulated annealing
Bayesian networks
“computer programs are the best representation of computer programs” - John R. Koza -
GenPro – 2 feb 2011
Genetic algorithm / programming
1. Generate random solutions
2. Evaluate & score (cumulative error => lower is better)
3. Cross and mutate to generate offspring
4. Repeat step 2 and 3 till score is sufficient
41 32 92 71 80 35 23 78 15 42 99 12
37 29 76 57 45 34 21 12 45 8 98 46
generation x
generation x+1
generate
evaluate
evaluate
cross and mutate
cross and mutate
GenPro – 2 feb 2011
Program representations in GP
• bits• numerical weight vectors• machine / byte code• constructs• concepts?
Which granularity is better?
GenPro – 2 feb 2011
Genpro birth
GenPro – 2 feb 2011
One year later: Spreadsheet Inspiration
• Every cell has a value and a type
3 = A2 + B1 2
1 41
• Every cell might have a formula
• Changing the references (mutation!) changes the outcome
GenPro – 2 feb 2011
One (call)cell, one method call
A GenPro cell holds:• Type• Value• Target-object (or class)• Target method• Parameters
Integer cc1 = Math.max(a, b)
GenPro – 2 feb 2011
One (call)cell, one method call
Integer cc1 = Math.max(a, b)
cc1• Integer • Value: 56
Math
max (a, b)
int 56
int32
param a
param b
GenPro – 2 feb 2011
A grid of cells
i1
37
17.4
Input values
1: 113
2: 212
3: 392
4: 752
ExpectedOutput values
1: 45
2: 100
3: 200
4: 400
O1
x
+
GenPro – 2 feb 2011
Working with GenPro
GenPro – 2 feb 2011
Using GenPro: extending Trainer
public abstract class Trainer {
public abstract Setup createSetup();
public abstract TestSetSolutionEvaluator createEvaluator();
}
TrainerVisual extends Trainer
-> autostart of Swing UI
GenPro – 2 feb 2011
Setup
Defines: how to create programs and run generations of programs
Parts of Setup:• Input and output cells• Type and maximum nr of cells• Elements (constants, libraries)• Generation size• When to stop• When to store solutions
GenPro – 2 feb 2011
GridSolutionEvaluator extends TestSetSolutionEvaluator
Defines how to evaluate and select programs.
public TestSet createTestSet();
public double scoreOutput(ReferenceCell outputCell, Object calculated, Object expected);
public double scoreGrid(Grid g);
public double scoreGridException(Throwable t);
GenPro – 2 feb 2011
TestSet
Defines the inputs and outputs values to evaluate a program.
Is actually a list of cellnames with sets of values.
public TestSet(Setup setup, String... cellNames)
public void addCellValues(final Object... objects)
GenPro – 2 feb 2011
First testcase: exact formula
Convert Fahrenheit to Celsius• input: Fahrenheit• output: Celsius• generates exact formula (score goes to 0)
GenPro – 2 feb 2011
GenPro inner working
GenPro – 2 feb 2011
Creating a random solution
Lib1Math
input1Integer
c3Integer
compareTo(Integer)
output1Integer
abs(int)c1
Integer
c2Integer
round(double)
junk DNA, aka intron
Integer c1 = Math.abs(input1);
Integer c2 = Math.round(c1);
Integer c3 = c2.compareTo(c1);
return c2;}
import java.lang.Math;
public Integer calc(Integer input1) {
ineffective DNA
Math.cos(…)Math.abs(…)…
input1
Call targets
Param values
Math.cos(…)Math.abs(…)c1.compareTo…
input1c1input1c1c2
input1c1c2c3
GenPro – 2 feb 2011
The heart of the machine: Method.invoke()
public void calc() {
// fetch parameter values Object[] paramObjects = new Object[paramCells.length];for (int i = 0; i < paramCells.length; i++) { paramObjects[i] = paramCells[i].getValue();}
// execute method on target object/class Object val = targetMethod.invoke(targetCell.getValue(),
paramObjects);// store valuesetValue(val);
}
GenPro – 2 feb 2011
Why reflection?
• Strong typed connecting• No compile time ( = faster @ small testsets )
• Easy extension, just add POJO’s• Use existing libraries• Reduces learning curve for user• Convertible to working Java code
GenPro – 2 feb 2011
Operators / libraries
public class NumberOperations {
public static double multiply(double a, double b) { return a * b; } public static double divide(double a, double b) { return a / b; } …
}
Lib
GenPro – 2 feb 2011
Back to reality ;)
GenPro – 2 feb 2011
Egg Boiling!
GenPro – 2 feb 2011
Second testcase: non-exact formula
Eggweight• input: width & height of egg• output: weight of egg • added lib: formula for volume of Egg
public class Egg {
public static double volume(double l, double b) {
return (0.6057 - 0.0018 * b) * l * b * b;
}
}
GenPro – 2 feb 2011
Search Space size
• Nr of cells• Nr of operators/methods• Nr of parameters• Nr of data types
GenPro – 2 feb 2011
Third Test case: search space
• Exact formula (score=0)• F(y)=x4+x3+x2-x
GenPro – 2 feb 2011
Forth Test case: Roman Figures
• Exact formula (score=0)• Strings to Integers• Does not give a correct solution yet.
GenPro – 2 feb 2011
Genpro current state
GenPro – 2 feb 2011
Program representations
• Tree based GP (TGP)• Linear based GP (LGP)• Graph based GP (GGP)
• OO based GP (OOGP)• Reflection based GP
Tree example
// 4 registers in rdouble lgp ( double[] r ) { r[0] = r[3] + 43 r[2] = r[1] + r[0] if( [r2] > 2) r[3] = sin ( r [2] ) r[0] = r[2] + r[3] return r[0];}
Linear example
GenPro – 2 feb 2011
Runtime Requirements
• GenPro Core: Java 1.5 or Android 1.0• GenPro Examples: Java 1.5 (Swing)• GenPro Android Example: Android 1.5
• Generated programs: Java 1.0, Android 1.0 or J2ME (CLDC 1.0)
GenPro – 2 feb 2011
Features
• Supported cell types: Integer, Double, Boolean, String
• Operators: – all numeric (+,-,/,*,%),
– boolean (&, ||, !),
– logic operators: none
• Constructs: if, switch (Integer & Double)
GenPro – 2 feb 2011
Wanted Features
• Cell type mutation (keep the if-cells!)• Runtime compiling of programs• Support for void (state-full objects!),
enumerations and arrays• enumeration switch• for (and while?) loop• support for tournaments (eg: Tic tact toe,
Robocode)
GenPro – 2 feb 2011
GenPro challenges you!
Real world application of GenPro• 2 days of free help getting your problem
solved.
More developers! • Join the architecture meeting!• Solve the multithreading bug!
GenPro – 2 feb 2011
Links
GenPro code: code.google.com/p/genpro
GenPro on twitter: @bluevoid
Groups: groups.google.com/group/genpro-gp
A scientists view on reflective GP: algoval.essex.ac.uk/rep/oogp/ReflectionBasedGP.pdf
Koza’s view on GP:
www.genetic-programming.com/sevendiffs.html
Contact Rob: [email protected]
GenPro – 2 feb 2011
Genpro, the origin of species (not covered on feb 2!)
GenPro – 2 feb 2011
Example Solution structure
cc2Integer
cc3Integer
Lib1Math
Input1Integer
abs(int)
round(double)
compareTo(Integer)
Output1Integer
cc1Integer
parametersMethod calls
GenPro – 2 feb 2011
Crossing of solutions
Int
Int
Math Int
Int
Int
Solution A
Int
Int
Math Int
Int
Int
Solution B
crossoverpoint
GenPro – 2 feb 2011
Creating children of A & B
Int
Int
Int
Int
Parent A
Int
Int
Int
Int
Parent B
Int
Int
Int
Int
Int
Int
Int
Int
Child A~B Child B~A
crossoverpoint
GenPro – 2 feb 2011
A2
B1
B2
A1
Crossover Reconnecting
Int
Int
Math Int
Int
Int
Child A~B
Int
Int
Math Int
Int
Int
Child B~A
GenPro – 2 feb 2011
Example Solution structure 2
cc2Integer
cc3Boolean
Lib1Math
Input1Integer
abs(int)
Output1Integer
cc1Double
in code:
setup.setCallCells(3, "cc", Double.class, Integer.class, Boolean.class );
GenPro – 2 feb 2011
Crossover on mixed value types
A2
B1
Int
Int
Math Int
Int
Dbl
Child B~A
compareTo(Integer)
Mismatch in param type!
Potential mismatch in return type and method call
GenPro – 2 feb 2011
Crossover in nature: the origin of species
Heart
Lung
“DNA” of Horse
Heart
Lung
Brain
“DNA” of Dog
cross over pointsdo not match!
GenPro – 2 feb 2011
Solving the species problem
1. prevent mismatch by pre-typing the solution template - specie-fy
2. use converters - Heart-brain bypass?
3. on misconnect: no child - allow specie groups as in nature
4. random reconnect - create new species
GenPro – 2 feb 2011
Crossover on mixed value types:random reconnect
A2
B1
Int
Int
Math Int
Int
Dbl
Child B~A
compareTo(Integer)
Moved connection
GenPro – 2 feb 2011
Here be dragons