--!movie
----------------------------------------
--
----------------------------------------
on startMovie
-- 2 = "arity", which means that we look for a program/function that accepts 2 arguments
gp = script("GP").new(2)
-- set params
gp.setParam(#MAX_DEPTH, 6)
gp.setParam(#MIN_CONST, 1)
gp.setParam(#MAX_CONST, 5)
-- add elementary function nodes ("genes")
gp.addFunction("add", 2, "res=c[1]+c[2]")
gp.addFunction("sub", 2, "res=c[1]-c[2]")
gp.addFunction("mul", 2, "res=c[1]*c[2]")
-- more example function nodes:
--gp.addFunction("if", 3, "if c[1] then res=c[2]"&RETURN&"else res=c[3]")
--gp.addFunction("eq", 2, "res=c[1]=c[2]")
--gp.addFunction("not", 1, "res=not c[1]")
--gp.addFunction("gt", 2, "res=c[1]>c[2]")
--gp.addFunction("dev", 2, "res=c[1]/c[2]")
--gp.addFunction("mod", 2, "res=c[1] mod c[2]")
--gp.addFunction("pow", 2, "res=power(c[1],c[2])")
--gp.addFunction("sin", 1, "res=sin(c[1])")
--gp.addFunction("cos", 1, "res=cos(c[1])")
--gp.addFunction("tan", 1, "res=tan(c[1])")
--gp.addFunction("atn", 1, "res=atan(c[1])")
-- create test dataset
dataSet = []
repeat with i = 1 to 200
x = random(41)-1 -- 0..40
y = random(41)-1
-- note: in this case using hiddenFunc(x,y) would also work - call(<func>, _movie, ...) is
-- a more abstract calling method that also works with functions represented as trees of nodes
dataSet.add([[x, y], call(#hiddenFunc, _movie, x, y)])
end repeat
gp.setDataSet(dataSet)
-- start running the GP algorithm (might loop forever, since no <maxGenerations> argument is passed)
prog = gp.run()
-- if a perfect solution was found, print it to the message window
prog.print()
end
----------------------------------------
-- The sample function that GP has to find
----------------------------------------
on hiddenFunc (x, y)
return x*x + 2*y + 3
end