SciQuest_csp

MCTS fork, Main

Apr 24th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.03 KB | None | 0 0
  1. red = color(255, 0, 0, 255)
  2. blue = color(0, 0, 255, 255)
  3. black = color(0, 0, 0, 255)
  4. white = color(255, 255, 255, 255)
  5. gray = color(127, 127, 127, 255)
  6.  
  7. function setupParams()
  8.  --   iparameter("Game", 0, 3)
  9.     iparameter("AIonline", 0, 1)
  10.     iparameter("AIstarts", 0, 1)
  11.     parameter("Difficulty", 0, 4)
  12.     iparameter("MaxSearchDepth", 0, 20)
  13.     
  14.  --   Game = readProjectData("game", 0)
  15.     AIonline = readProjectData("aiOnline", 1)
  16.     AIstarts = readProjectData("aiStarts", 0)
  17.     Difficulty = readProjectData("difficulty", 2)
  18.     MaxSearchDepth = readProjectData("maxSearchDepth", 6)
  19. end
  20.  
  21. function saveParams() 
  22.  --   saveProjectData("game", Game)
  23.     saveProjectData("aiOnline", AIonline)
  24.     saveProjectData("aiStarts", AIstarts)
  25.     saveProjectData("difficulty", Difficulty)
  26.     saveProjectData("maxSearchDepth", MaxSearchDepth)
  27. end
  28.  
  29. function setup()
  30.     wait = true
  31.     setupParams()
  32.     strokeWidth(5)
  33.  --   prevGame = 0
  34.     board = Othello( 8,8 )
  35.     sim = Othello( 8,8 )
  36. end
  37.  
  38. function startGame()
  39.  
  40. end
  41.  
  42. function draw()
  43.     --background(40, 40, 50)
  44.     saveParams()
  45.     
  46.    -- print(board:positionStrength())
  47.     board:draw() 
  48.     if board.turn == ai and AIonline == 1 then
  49.      --   print("random moving")
  50.         mcts()
  51.       --  board:randomMove()
  52.     end
  53.  
  54. end
  55.  
  56. function touched(t)
  57.     if t.state == ENDED then
  58.         print("move acknowledged. please wait...")
  59.         board:touched(t)
  60.         wait = true
  61.     end
  62. end
  63.  
  64. function mcts()
  65.     wait = true
  66.     local playouts = math.floor(10^Difficulty)
  67.     possibleMoves = {}
  68.     searchResults = {}
  69.     for k,v in pairs(board.moves[board.turn+1]) do
  70.        -- print(board.moves[self.turn+1][])
  71.         table.insert(possibleMoves, v)
  72.         table.insert(searchResults, 0)
  73.     end
  74.     local count = #possibleMoves
  75.  
  76.     print("number of possible moves is "..count)
  77.     if count == 0 then fin = true return end
  78. --    local sim = ConnectFour(board.numberOfRows, board.numberOfColumns, board.turn, board.goal)
  79.     for i = 1, count do
  80.         local sum = 0
  81.         for j = 1, playouts do
  82.             sim:copy(board)
  83.         --    print("moving "..i)
  84.         print(possibleMoves[i])
  85.             sim:move(possibleMoves[i].x, possibleMoves[j].y)
  86.          --   print("---playout "..j)
  87.             local playoutResult = playout(sim)
  88.           --  print(playoutResult)
  89.             sum = sum + playoutResult
  90.         end
  91.         sum = sum / playouts
  92.         searchResults[i] = sum
  93.         print("probability for move "..possibleMoves[i].." is "..sum)
  94.     end
  95.     -- we should want to maximize the search result 
  96.     bestResult = -99
  97.     bestIndex = -99
  98.     for i,r in pairs(searchResults) do
  99.       --  print("best "..bestResult)
  100.      --   print("r "..r)
  101.         if bestResult < r then
  102.             bestResult = r
  103.             bestIndex = i
  104.         end
  105.     end
  106.     print("bestResult is "..bestResult)
  107.     print("bestMove is "..possibleMoves[bestIndex])
  108.     if bestIndex == -99 then
  109.         print("oops! mcts failed? (thats not good)")
  110.     else
  111.         board:move(possibleMoves[bestIndex])
  112.     end
  113. end
  114.  
  115. function playout(b)
  116.     local result = b:gameOver()
  117.     local searchDepth = 0
  118.  --   print("playing out")
  119.     while (result == nil) do
  120.         local movesLeft = b:numberOfMoves()
  121.         if movesLeft == 0 or MaxSearchDepth <= searchDepth and MaxSearchDepth ~= 0 then 
  122.             return b:positionStrength()
  123.         end
  124.         b:randomMove( movesLeft )
  125.         searchDepth = searchDepth + 1
  126.         result = b:gameOver()
  127.     end
  128.     return result
  129. end
  130.  
  131. function flipTurn(b)
  132.     if b.turn == b.player then
  133.         b.turn = b.ai
  134.     else
  135.         b.turn = b.player
  136.     end
  137. end 
  138.  
  139. function randBool()
  140.     return math.random(2) == 1
  141. end
Advertisement
Add Comment
Please, Sign In to add comment