devnology workshop genpro 2 feb 2011

48
GenPro Genetic Programming made simple Rob van der Veer Bluevoid

Upload: devnology

Post on 12-May-2015

790 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Devnology Workshop Genpro 2 feb 2011

GenPro Genetic Programming made simple

Rob van der Veer

Bluevoid

Page 2: Devnology Workshop Genpro 2 feb 2011

Bio

Java Development (Web, OSGi, Android, Swing)

Project Leader (o.a. Scrum)

[email protected]

Page 3: Devnology Workshop Genpro 2 feb 2011

Genpro preamble

Page 4: Devnology Workshop Genpro 2 feb 2011

GenPro – 2 feb 2011

Automated help: “Grrr”

Page 5: Devnology Workshop Genpro 2 feb 2011

GenPro – 2 feb 2011

Settings vs re-programmable logic

Page 6: Devnology Workshop Genpro 2 feb 2011

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 -

Page 7: Devnology Workshop Genpro 2 feb 2011

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

Page 8: Devnology Workshop Genpro 2 feb 2011

GenPro – 2 feb 2011

Program representations in GP

• bits• numerical weight vectors• machine / byte code• constructs• concepts?

Which granularity is better?

Page 9: Devnology Workshop Genpro 2 feb 2011

GenPro – 2 feb 2011

Genpro birth

Page 10: Devnology Workshop Genpro 2 feb 2011

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

Page 11: Devnology Workshop Genpro 2 feb 2011

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)

Page 12: Devnology Workshop Genpro 2 feb 2011

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

Page 13: Devnology Workshop Genpro 2 feb 2011

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

+

Page 14: Devnology Workshop Genpro 2 feb 2011

GenPro – 2 feb 2011

Working with GenPro

Page 15: Devnology Workshop Genpro 2 feb 2011

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

Page 16: Devnology Workshop Genpro 2 feb 2011

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

Page 17: Devnology Workshop Genpro 2 feb 2011

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);

Page 18: Devnology Workshop Genpro 2 feb 2011

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)

Page 19: Devnology Workshop Genpro 2 feb 2011

GenPro – 2 feb 2011

First testcase: exact formula

Convert Fahrenheit to Celsius• input: Fahrenheit• output: Celsius• generates exact formula (score goes to 0)

Page 20: Devnology Workshop Genpro 2 feb 2011

GenPro – 2 feb 2011

GenPro inner working

Page 21: Devnology Workshop Genpro 2 feb 2011

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

Page 22: Devnology Workshop Genpro 2 feb 2011

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);

}

Page 23: Devnology Workshop Genpro 2 feb 2011

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

Page 24: Devnology Workshop Genpro 2 feb 2011

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

Page 25: Devnology Workshop Genpro 2 feb 2011

GenPro – 2 feb 2011

Back to reality ;)

Page 26: Devnology Workshop Genpro 2 feb 2011

GenPro – 2 feb 2011

Egg Boiling!

Page 27: Devnology Workshop Genpro 2 feb 2011

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;

}

}

Page 28: Devnology Workshop Genpro 2 feb 2011

GenPro – 2 feb 2011

Search Space size

• Nr of cells• Nr of operators/methods• Nr of parameters• Nr of data types

Page 29: Devnology Workshop Genpro 2 feb 2011

GenPro – 2 feb 2011

Third Test case: search space

• Exact formula (score=0)• F(y)=x4+x3+x2-x

Page 30: Devnology Workshop Genpro 2 feb 2011

GenPro – 2 feb 2011

Forth Test case: Roman Figures

• Exact formula (score=0)• Strings to Integers• Does not give a correct solution yet.

Page 31: Devnology Workshop Genpro 2 feb 2011

GenPro – 2 feb 2011

Genpro current state

Page 32: Devnology Workshop Genpro 2 feb 2011

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

Page 33: Devnology Workshop Genpro 2 feb 2011

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)

Page 34: Devnology Workshop Genpro 2 feb 2011

GenPro – 2 feb 2011

Features

• Supported cell types: Integer, Double, Boolean, String

• Operators: – all numeric (+,-,/,*,%),

– boolean (&, ||, !),

– logic operators: none

• Constructs: if, switch (Integer & Double)

Page 35: Devnology Workshop Genpro 2 feb 2011

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)

Page 36: Devnology Workshop Genpro 2 feb 2011

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!

Page 37: Devnology Workshop Genpro 2 feb 2011

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]

Page 38: Devnology Workshop Genpro 2 feb 2011

GenPro – 2 feb 2011

Genpro, the origin of species (not covered on feb 2!)

Page 39: Devnology Workshop Genpro 2 feb 2011

GenPro – 2 feb 2011

Example Solution structure

cc2Integer

cc3Integer

Lib1Math

Input1Integer

abs(int)

round(double)

compareTo(Integer)

Output1Integer

cc1Integer

parametersMethod calls

Page 40: Devnology Workshop Genpro 2 feb 2011

GenPro – 2 feb 2011

Crossing of solutions

Int

Int

Math Int

Int

Int

Solution A

Int

Int

Math Int

Int

Int

Solution B

crossoverpoint

Page 41: Devnology Workshop Genpro 2 feb 2011

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

Page 42: Devnology Workshop Genpro 2 feb 2011

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

Page 43: Devnology Workshop Genpro 2 feb 2011

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 );

Page 44: Devnology Workshop Genpro 2 feb 2011

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

Page 45: Devnology Workshop Genpro 2 feb 2011

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!

Page 46: Devnology Workshop Genpro 2 feb 2011

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

Page 47: Devnology Workshop Genpro 2 feb 2011

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

Page 48: Devnology Workshop Genpro 2 feb 2011

GenPro – 2 feb 2011

Here be dragons