Advertisement
justync7

neuroevolutioncc

Jun 29th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.80 KB | None | 0 0
  1. inputKeys = {
  2.     "a",
  3.     "b",
  4.     "c",
  5.     "d",
  6.     "e",
  7.     "f",
  8.     "g",
  9.     "h",
  10.     "i",
  11.     "j",
  12.     "k",
  13.     "l",
  14.     "m",
  15.     "n",
  16.     "o",
  17.     "p",
  18.     "q",
  19.     "r",
  20.     "s",
  21.     "t",
  22.     "u",
  23.     "v",
  24.     "w",
  25.     "x",
  26.     "y",
  27.     "z",
  28.     "\"",
  29.     "(",
  30.     ")"
  31. }
  32.  
  33. goal = "print(\"hello world\")"
  34.  
  35. function generateInput(thoughts, keys, fitness)
  36.     print(#thoughts..":"..fitness)
  37.     if not #thoughts then
  38.         math.randomseed(thoughts..keys..fitness)
  39.         return keys[math.random(1, #keys)]
  40.     elseif #thoughts > fitness then
  41.        return false
  42.     else
  43.         math.randomseed(thoughts..keys..fitness)
  44.         return keys[math.random(1, #keys)]
  45.     end
  46. end
  47.  
  48. function generateFitness(thoughts, goal)
  49.     fit = 0
  50.     for i,v in pairs(thoughts) do
  51.         if v == goal:sub(i,i) then
  52.             fit = fit + 1
  53.         else
  54.             return fit
  55.         end
  56.     end
  57. end
  58.  
  59. function generateLife()
  60.     return {}, 0
  61. end
  62.  
  63. term.clear()
  64. term.setCursorPos(1,1)
  65. term.setCursorBlink(true)
  66.  
  67. while true do
  68.     if not thoughts and not fitness then
  69.         thoughts = {}
  70.         fitness = 0
  71.         write("> ")
  72.     elseif generateFitness(thoughts, goal) == #goal then
  73.         print("hello world")
  74.         break
  75.     else
  76.         write("> ")
  77.         for i=1,#thoughts do
  78.             write(thoughts[i])
  79.             sleep(1)
  80.         end
  81.     end
  82.     alive = true
  83.     repeat
  84.         fitness = generateFitness(thoughts, goal)
  85.         inp = generateInput(thoughts, inputKeys, fitness)
  86.         if inp then
  87.             thoughts[#thoughts + 1] = inp
  88.             write(inp)
  89.             sleep(1)
  90.         else
  91.             write("\n")
  92.             print("Died at "..fitness.." fitness.")
  93.             alive = false
  94.         end
  95.     until alive == false
  96.    
  97. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement