示例#1
0
// Evaluate the move by performing a search.
void evaluateMove(searchNode *node, move_t mv, move_t killer_a,
                  move_t killer_b, searchType_t type,
                  uint64_t *node_count_serial, moveEvaluationResult* result) {
  int ext = 0;  // extensions
  bool blunder = false;  // shoot our own piece

  result->next_node.subpv[0] = 0;
  result->next_node.parent = node;

  // Make the move, and get any victim pieces.
  bool isko = make_move(&(node->position), &(result->next_node.position), mv);

  // Check whether this move changes the board state.
  //   such moves are not legal.
  if (isko) {
    result->type = MOVE_ILLEGAL;
    return;
  }

  victims_t* victims = &result->next_node.position.victims;

  // Check whether the game is over.
  if (is_game_over(victims, node->pov, node->ply)) {
    // Compute the end-game score.
    result->type = MOVE_GAMEOVER;
    result->score = get_game_over_score(victims, node->pov, node->ply);
    return;
  }

  // Ignore noncapture moves when in quiescence.
  if (zero_victims(victims) && node->quiescence) {
    result->type = MOVE_IGNORE;
    return;
  }

  // Check whether the board state has been repeated, this results in a draw.
  if (is_repeated(&(result->next_node.position), node->ply)) {
    result->type = MOVE_GAMEOVER;
    result->score = get_draw_score(&(result->next_node.position), node->ply);
    return;
  }

  tbassert(victims->stomped == 0
           || color_of(victims->stomped) != node->fake_color_to_move,
           "stomped = %d, color = %d, fake_color_to_move = %d\n",
           victims->stomped, color_of(victims->stomped),
           node->fake_color_to_move);


  // Check whether we caused our own piece to be zapped. This isn't considered
  //   a blunder if we also managed to stomp an enemy piece in the process.
  if (victims->stomped == 0 &&
      victims->zapped > 0 &&
      color_of(victims->zapped) == node->fake_color_to_move) {
    blunder = true;
  }

  // Do not consider moves that are blunders while in quiescence.
  if (node->quiescence && blunder) {
    result->type = MOVE_IGNORE;
    return;
  }

  // Extend the search-depth by 1 if we captured a piece, since that means the
  //   move was interesting.
  if (victim_exists(victims) && !blunder) {
    ext = 1;
  }

  // Late move reductions - or LMR. Only done in scout search.
  int next_reduction = 0;
  if (type == SEARCH_SCOUT && node->legal_move_count + 1 >= LMR_R1 && node->depth > 2 &&
      zero_victims(victims) && mv != killer_a && mv != killer_b) {
    if (node->legal_move_count + 1 >= LMR_R2) {
      next_reduction = 2;
    } else {
      next_reduction = 1;
    }
  }

  result->type = MOVE_EVALUATED;
  int search_depth = ext + node->depth - 1;

  // Check if we need to perform a reduced-depth search.
  //
  // After a reduced-depth search, a full-depth search will be performed if the
  //  reduced-depth search did not trigger a cut-off.
  if (next_reduction > 0) {
    search_depth -= next_reduction;
    int reduced_depth_score = -scout_search(&(result->next_node), search_depth,
                                            node_count_serial);
    if (reduced_depth_score < node->beta) {
      result->score = reduced_depth_score;
      return;
    }
    search_depth += next_reduction;
  }

  // Check if we should abort due to time control.
  if (abortf) {
    result->score = 0;
    result->type = MOVE_IGNORE;
    return;
  }


  if (type == SEARCH_SCOUT) {
    result->score = -scout_search(&(result->next_node), search_depth,
                                 node_count_serial);
  } else {
    if (node->legal_move_count == 0 || node->quiescence) {
      result->score = -searchPV(&(result->next_node), search_depth, node_count_serial);
    } else {
      result->score = -scout_search(&(result->next_node), search_depth,
                            node_count_serial);
      if (result->score > node->alpha) {
        result->score = -searchPV(&(result->next_node), node->depth + ext - 1, node_count_serial);
      }
    }
  }
}
示例#2
0
score_t searchRoot(position_t *p, score_t alpha, score_t beta, int depth,
                   int ply, move_t *pv, uint64_t *node_count_serial,
                   FILE *OUT) {
  static int num_of_moves = 0;  // number of moves in list
  // hopefully, more than we will need
  static sortable_move_t move_list[MAX_NUM_MOVES];

  if (depth == 1) {
    // we are at depth 1; generate all possible moves
    num_of_moves = generate_all_opt(p, move_list, false);
    // shuffle the list of moves
    for (int i = 0; i < num_of_moves; i++) {
      int r = myrand() % num_of_moves;
      sortable_move_t tmp = move_list[i];
      move_list[i] = move_list[r];
      move_list[r] = tmp;
    }
  }

  searchNode rootNode;
  rootNode.parent = NULL;
  initialize_root_node(&rootNode, alpha, beta, depth, ply, p);


  assert(rootNode.best_score == alpha);  // initial conditions

  searchNode next_node;
  next_node.subpv[0] = 0;
  next_node.parent = &rootNode;

  score_t score;

  for (int mv_index = 0; mv_index < num_of_moves; mv_index++) {
    move_t mv = get_move(move_list[mv_index]);

    if (TRACE_MOVES) {
      print_move_info(mv, ply);
    }

    (*node_count_serial)++;

    // make the move.
    victims_t x = make_move(&(rootNode.position), &(next_node.position), mv);

    if (is_KO(x)) {
      continue;  // not a legal move
    }

    if (is_game_over(x, rootNode.pov, rootNode.ply)) {
      score = get_game_over_score(x, rootNode.pov, rootNode.ply);
      next_node.subpv[0] = 0;
      goto scored;
    }

    if (is_repeated(&(next_node.position), rootNode.ply)) {
      score = get_draw_score(&(next_node.position), rootNode.ply);
      next_node.subpv[0] = 0;
      goto scored;
    }

    if (mv_index == 0 || rootNode.depth == 1) {
      // We guess that the first move is the principle variation
      score = -searchPV(&next_node, rootNode.depth-1, node_count_serial);

      // Check if we should abort due to time control.
      if (abortf) {
        return 0;
      }
    } else {
      score = -scout_search(&next_node, rootNode.depth-1, node_count_serial);

      // Check if we should abort due to time control.
      if (abortf) {
        return 0;
      }

      // If its score exceeds the current best score,
      if (score > rootNode.alpha) {
        score = -searchPV(&next_node, rootNode.depth-1, node_count_serial);
        // Check if we should abort due to time control.
        if (abortf) {
          return 0;
        }
      }
    }

  scored:
    // only valid for the root node:
    tbassert((score > rootNode.best_score) == (score > rootNode.alpha),
             "score = %d, best = %d, alpha = %d\n", score, rootNode.best_score, rootNode.alpha);

    if (score > rootNode.best_score) {
      tbassert(score > rootNode.alpha, "score: %d, alpha: %d\n", score, rootNode.alpha);

      rootNode.best_score = score;
      pv[0] = mv;
      memcpy(pv+1, next_node.subpv, sizeof(move_t) * (MAX_PLY_IN_SEARCH - 1));
      pv[MAX_PLY_IN_SEARCH - 1] = 0;

      // Print out based on UCI (universal chess interface)
      double et = elapsed_time();
      char   pvbuf[MAX_PLY_IN_SEARCH * MAX_CHARS_IN_MOVE];
      getPV(pv, pvbuf, MAX_PLY_IN_SEARCH * MAX_CHARS_IN_MOVE);
      if (et < 0.00001) {
        et = 0.00001;  // hack so that we don't divide by 0
      }

      uint64_t nps = 1000 * *node_count_serial / et;
      fprintf(OUT, "info depth %d move_no %d time (microsec) %d nodes %" PRIu64
              " nps %" PRIu64 "\n",
              depth, mv_index + 1, (int) (et * 1000), *node_count_serial, nps);
      fprintf(OUT, "info score cp %d pv %s\n", score, pvbuf);

      // Slide this move to the front of the move list
      for (int j = mv_index; j > 0; j--) {
        move_list[j] = move_list[j - 1];
      }
      move_list[0] = mv;
    }

    // Normal alpha-beta logic: if the current score is better than what the
    // maximizer has been able to get so far, take that new value.  Likewise,
    // score >= beta is the beta cutoff condition
    if (score > rootNode.alpha) {
      rootNode.alpha = score;
    }
    if (score >= rootNode.beta) {
      tbassert(0, "score: %d, beta: %d\n", score, rootNode.beta);
      break;
    }
  }

  return rootNode.best_score;
}