Guest User

Untitled

a guest
Feb 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. require File.join(File.dirname(__FILE__), 'fastar_digger')
  2.  
  3. module FJC
  4. module Diggers
  5.  
  6. class FOOstarDigger < FAstarDigger
  7. def pick_move
  8. stash_visit_count
  9. super
  10. end
  11.  
  12. private
  13.  
  14. # stash a count of the number of times a position has been visited
  15. # by attaching an instance variable to the board location key
  16. def stash_visit_count
  17. position = @board.position
  18. board = @board.instance_variable_get("@board")
  19. keys = board.keys
  20. key = keys.find{|k| k == position}
  21.  
  22. visits = key.instance_variable_get("@visited").to_i
  23. key.instance_variable_set("@visited", visits + 1)
  24. end
  25.  
  26. # fetch the count of the number of times a position has been visited
  27. # from the inserted instance variable
  28. def fetch_visit_count key
  29. key.instance_variable_get("@visited")
  30. end
  31.  
  32. # cost is normal cost plus the function of the number of visits to that position
  33. def set_cost key, _
  34. visits = fetch_visit_count(key)
  35. visits ? super + visit_weight(visits).to_i : super
  36. end
  37.  
  38. # default visit weight function is to take the log
  39. def visit_weight visits
  40. Math::log(visits)
  41. end
  42. end
  43.  
  44. end
  45. end
Add Comment
Please, Sign In to add comment