1. --!movie
  2.  
  3. ----------------------------------------
  4. --
  5. ----------------------------------------
  6. on startMovie
  7.  
  8.   -- 2 = "arity", which means that we look for a program/function that accepts 2 arguments
  9.   gp = script("GP").new(2)
  10.  
  11.   -- set params
  12.   gp.setParam(#MAX_DEPTH, 6)
  13.   gp.setParam(#MIN_CONST, 1)
  14.   gp.setParam(#MAX_CONST, 5)
  15.  
  16.   -- add elementary function nodes ("genes")
  17.   gp.addFunction("add", 2, "res=c[1]+c[2]")
  18.   gp.addFunction("sub", 2, "res=c[1]-c[2]")
  19.   gp.addFunction("mul", 2, "res=c[1]*c[2]")
  20.  
  21.   -- more example function nodes:
  22.  
  23.   --gp.addFunction("if",  3, "if c[1] then res=c[2]"&RETURN&"else res=c[3]")
  24.   --gp.addFunction("eq",  2, "res=c[1]=c[2]")
  25.   --gp.addFunction("not", 1, "res=not c[1]")
  26.  
  27.   --gp.addFunction("gt",  2, "res=c[1]>c[2]")
  28.   --gp.addFunction("dev", 2, "res=c[1]/c[2]")
  29.   --gp.addFunction("mod", 2, "res=c[1] mod c[2]")
  30.   --gp.addFunction("pow", 2, "res=power(c[1],c[2])")
  31.  
  32.   --gp.addFunction("sin", 1, "res=sin(c[1])")
  33.   --gp.addFunction("cos", 1, "res=cos(c[1])")
  34.   --gp.addFunction("tan", 1, "res=tan(c[1])")
  35.   --gp.addFunction("atn", 1, "res=atan(c[1])")
  36.  
  37.   -- create test dataset
  38.   dataSet = []
  39.   repeat with i = 1 to 200
  40.     x = random(41)-1 -- 0..40
  41.     y = random(41)-1
  42.     -- note: in this case using hiddenFunc(x,y) would also work - call(<func>, _movie, ...) is
  43.     -- a more abstract calling method that also works with functions represented as trees of nodes
  44.     dataSet.add([[x, y], call(#hiddenFunc, _movie, x, y)])
  45.   end repeat
  46.   gp.setDataSet(dataSet)
  47.  
  48.   -- start running the GP algorithm (might loop forever, since no <maxGenerations> argument is passed)
  49.   prog = gp.run()
  50.  
  51.     -- if a perfect solution was found, print it to the message window
  52.   prog.print()
  53.  
  54. end
  55.  
  56. ----------------------------------------
  57. -- The sample function that GP has to find
  58. ----------------------------------------
  59. on hiddenFunc (x, y)
  60.   return x*x + 2*y + 3
  61. end
  62.  
[raw code]