Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. public class ParallelSearcher<M extends Move<M>, B extends Board<M, B>> extends AbstractSearcher<M, B> {
  2.  
  3. public static final ForkJoinPool POOL = new ForkJoinPool();
  4. private static int DIVIDE_CUTOFF = 3;
  5.  
  6. public M getBestMove(B board, int myTime, int opTime) {
  7. List<M> moves = board.generateMoves();
  8. SearchTask task = new SearchTask(this.evaluator, board, moves, this.ply, 0,
  9. moves.size());
  10. return POOL.invoke(task).move;
  11. }
  12.  
  13. public class SearchTask extends RecursiveTask<BestMove<M>> {
  14.  
  15. public Evaluator<B> evaluator;
  16. public B board;
  17. public int depth, lo, hi;
  18. private static final long serialVersionUID = -4025688100707858050L;
  19. private List<M> moves;
  20.  
  21. public SearchTask(Evaluator<B> evaluator, B board, List<M> moves, int depth, int lo, int hi) {
  22. /* if (hi == lo) {
  23. throw new IllegalArgumentException("hi: " + hi + " lo: " + lo + " depth: " + depth + " moves: " + moves.size());
  24. }
  25. if (moves.size() == 0) {
  26. throw new IllegalArgumentException("moves.size() == " + moves.size());
  27. } */
  28. this.evaluator = evaluator;
  29. this.board = board;
  30. this.depth = depth;
  31. this.lo = lo;
  32. this.hi = hi;
  33. this.moves = moves;
  34. }
  35.  
  36. @Override
  37. protected BestMove<M> compute() {
  38. if (depth <= cutoff || moves.isEmpty()) {
  39. return SimpleSearcher.minimax(evaluator, board, depth);
  40. } else {
  41. // if we're done splitting the moves on this level
  42. if (hi - lo <= DIVIDE_CUTOFF) {
  43. // B boardCopy = board.copy();
  44.  
  45. SearchTask[] tasks = (SearchTask[]) new ParallelSearcher<?, ?>.SearchTask[hi - lo - 1];
  46. for (int i = lo; i < hi - 1; i++) {
  47.  
  48. /* boardCopy.applyMove(moves.get(i));
  49. List<M> newMoves = boardCopy.generateMoves();
  50. SearchTask taskAfterMove = new SearchTask(evaluator, boardCopy, newMoves, depth - 1, 0, newMoves.size());
  51. BestMove<M> ret = taskAfterMove.compute().negate();
  52. boardCopy.undoMove(); */
  53.  
  54. B boardCopy = board.copy();
  55. boardCopy.applyMove(moves.get(i));
  56. List<M> newMoves = boardCopy.generateMoves();
  57.  
  58. SearchTask taskAfterMove = new SearchTask(evaluator, boardCopy, newMoves, depth - 1, 0, newMoves.size());
  59. tasks[i - lo] = taskAfterMove;
  60. taskAfterMove.fork();
  61.  
  62. /* if (ret.value > best.value) {
  63. best.value = ret.value;
  64. best.move = moves.get(i);
  65. } */
  66. }
  67. /* return best; */
  68.  
  69. // compute the last move
  70. B boardCopy = board.copy();
  71. boardCopy.applyMove(moves.get(hi - 1));
  72. List<M> newMoves = boardCopy.generateMoves();
  73. SearchTask taskAfterMove = new SearchTask(evaluator, boardCopy, newMoves, depth - 1, 0, newMoves.size());
  74. BestMove<M> best = taskAfterMove.compute().negate();
  75. best.move = moves.get(hi - 1);
  76.  
  77. for (int i = lo; i < hi - 1; i++) {
  78. BestMove<M> ret = tasks[i - lo].join().negate();
  79. if (ret.value > best.value) {
  80. best.value = ret.value;
  81. best.move = moves.get(i);
  82. }
  83. }
  84. return best;
  85. }
  86. // divide the moves
  87. if (hi - 1 == lo) {
  88. throw new IllegalArgumentException("hi and lo are one apart");
  89. }
  90. int mid = lo + (hi - lo) / 2;
  91.  
  92. SearchTask left = new SearchTask(evaluator, board, moves, depth, lo, mid);
  93. SearchTask right = new SearchTask(evaluator, board, moves, depth, mid, hi);
  94.  
  95. right.fork();
  96. BestMove<M> leftRet = left.compute();
  97. BestMove<M> rightRet = right.join();
  98.  
  99. // return the best
  100. return rightRet.value > leftRet.value ? rightRet : leftRet;
  101. }
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement