Skip to content

Instantly share code, notes, and snippets.

View Vaguery's full-sized avatar

Bill Tozier Vaguery

View GitHub Profile
@Vaguery
Vaguery / example_1.rb
Created November 12, 2010 20:55
The StickpigScript class for creating and evaluating random arithmetic expressions in a simple S-expression tree GP language, and two examples from the book I'm working on.
require './stickpig'
100.times do |i|
stick = StickpigScript.new(0.4)
puts "#{i}, #{stick.script.length}" +
[-20,-10,-1,0,1,10,20].inject("") {|line, x| line + ", #{stick.evaluate(x).round(5)}"}
end
@Vaguery
Vaguery / brickpig.rb
Created November 13, 2010 00:30
The BrickpigScript class for creating and evaluating random arithmetic expressions in a simple Cartesian GP language, and some accompanying files from the book I'm working on.
class Float
alias :add :+
alias :subtract :-
alias :multiply :*
alias :negate :-@
def divide(arg2)
arg2==0 ? 0.0 : self / arg2
end
end
@Vaguery
Vaguery / heaviside_data.csv
Created November 23, 2010 00:11
Datasets for symbolic regression exercises in Chapter 1 of "Pragmatic Genetic Programming: What to Do"
x y
-499.357 -5.173
-447.544 -5.173
-418.335 -5.173
-366.129 -5.173
-342.084 -5.173
-317.926 -5.173
-273.301 -5.173
-268.65 -5.173
-183.161 -5.173
def score(a=0,b=1,c=2,d=3,e=4)
puts "a: #{a.class}"
puts "b: #{b.class}"
puts "c: #{c.class}"
puts "d: #{d.class}"
puts "e: #{e.class}"
scores = Array.new
scores << a << b << c << d << e
Feature: Pile interpreter handles tokens as expected
In order to get consistent behavior when executing Pile scripts
As a symbolic regression modeler
I want each of the six basic token types to bring about expected changes in the Pile interpreter stack
Scenario: numerical constants are pushed onto the interpreter stack
Given the Pile script "-91.21"
When I execute it
Then there should be 1 item on the stack
Feature: random Pile script generator
In order to get sample arbitrary Pile solutions to a target problem
As a symbolic regression modeler
I want to be able to compose random Pile scripts, sampled from a set of tokens
Scenario: sample available tokens uniformly
When I generate 10000 random Pile scripts, each with 20 tokens
And the available tokens are ["a", "b", "c"]
Then the tokens should appear (on average) with equal frequency
Feature: Pile return values
In order to interpret a Pile script as a function of x
As a symbolic regression modeler
I want a scalar value to be returned from any executed script
Scenario: return value is the top stack item after execution
Given the Pile script "1.2 3.4 +"
When I execute it with x = 1
Then the returned value should be 4.6
Feature: Symbolic regression scoring against x+6 dataset
In order to evaluate how well a Pile script models a given dataset
As a symbolic regression modeler
I want the score of a given script against a dataset to be the RSS value
Scenario: score for a perfect program should be 0.0
Given the Pile script "x 6.0 +"
And the training data is "x_plus_6.csv"
When I score the script
Then the score should be 0.0
Feature: One-point uneven crossover works as specified
In order to combine "good ideas" that have arisen independently in different Pile scripts
As a symbolic regression modeler
I want crossover to snip scripts up and stick them back together
Scenario: crossover conserves the number of tokens
Given parent_1 is the Pile script "+ + + + + + + + + +"
And parent_2 is the Pile script "- - - - - - - - - -"
When I apply uneven one-point crossover to those parents
Then the result is a collection of 2 Pile scripts
Feature: Uniform mutation works as specified
In order to discover new beneficial variations of scripts that are already OK
As a symbolic regression modeler
I want uniform mutation to replace tokens in "parent" scripts with new random tokens
Scenario: uniform mutation replaces tokens with uniform random sample of available tokens
Given parent_1 is the Pile script "+ + + + + + + + + +"
And the available tokens are ["a", "b", "c", "d", "e"]
And the chance of mutation is p=1.0