コード例 #1
0
ExactSolution *
game_position_mab_solve (const GamePositionX *const root,
                         const endgame_solver_env_t *const env,
                         const bool alpha_beta_pruning)
{
  g_assert(root);
  g_assert(env);

  GameTreeStack *stack = game_tree_stack_new();
  game_tree_stack_init(root, stack);

  LogEnv *const log_env = game_tree_log_init(env->log_file);

  if (log_env->log_is_on) {
    game_tree_log_open_h(log_env);
    stack->hash_is_on = true;
  }

  ExactSolution *result = exact_solution_new();
  exact_solution_set_solved_game_position_x(result, root);

  game_position_solve_impl(result, stack, log_env, alpha_beta_pruning);

  result->pv[0] = stack->nodes[1].best_move;
  result->outcome = stack->nodes[1].alpha;

  game_tree_stack_free(stack);
  game_tree_log_close(log_env);

  return result;
}
コード例 #2
0
ファイル: exact_solver.c プロジェクト: rcrr/reversi
/**
 * @brief Solves the game position returning a new exact solution pointer.
 *
 * @invariant Parameters `root` and `env` must be not `NULL`.
 *             The invariants are guarded by assertions.
 *
 * @param [in] root     the starting game position to be solved
 * @param [in] env      parameter envelope
 * @return              a pointer to a new exact solution structure
 */
ExactSolution *
game_position_es_solve (const GamePositionX *const root,
                        const endgame_solver_env_t *const env)
{
    g_assert(root);
    g_assert(env);

    ExactSolution *result;
    SearchNode    *sn;
    int            alpha;
    int            beta;

    pv_full_recording = env->pv_full_recording;

    log_env = game_tree_log_init(env->log_file);

    //GamePositionX *rootx = game_position_x_gp_to_gpx(root);
    pve = pve_new(root);
    //game_position_x_free(rootx);

    if (log_env->log_is_on) {
        gp_hash_stack[0] = 0;
        game_tree_log_open_h(log_env);
    }

    if (pv_full_recording) {
        alpha = out_of_range_defeat_score;
        beta = out_of_range_win_score;
    } else {
        alpha = worst_score;
        beta = best_score;
    }

    result = exact_solution_new();

    result->solved_game_position = game_position_clone(game_position_x_gpx_to_gp(root));

    sn = game_position_solve_impl(result,
                                  result->solved_game_position,
                                  alpha,
                                  beta,
                                  &(pve->root_line));


    if (sn) {
        result->pv[0] = sn->move;
        result->outcome = sn->value;
        pve_line_copy_to_exact_solution(pve, (const PVCell **const) pve->root_line, result);
        exact_solution_compute_final_board(result);
    }

    if (pv_full_recording && !env->pv_no_print) {
        printf("\n --- --- pve_line_with_variants_to_string() START --- ---\n");
        pve_line_with_variants_to_stream(pve, stdout);
        printf("\n --- --- pve_line_with_variants_to_string() COMPLETED --- ---\n");
    }

    if (pv_internals_to_stream) {
        printf("\nThe constant \"pv_internals_to_stream\", in source file \"exact_solver.c\", is TRUE. Printing PVE internals:\n");
        printf(" --- --- pve_is_invariant_satisfied() START --- ---\n");
        pve_error_code_t error_code = 0;
        pve_is_invariant_satisfied(pve, &error_code, 0xFF);
        if (error_code) {
            printf("error_code=%d\n", error_code);
            abort();
        }
        printf(" --- --- pve_is_invariant_satisfied() COMPLETED --- ---\n");

        printf("\n --- --- pve_internals_to_stream() START --- ---\n");
        switches_t shown_sections = 0x0000;
        shown_sections |= pve_internals_header_section;
        shown_sections |= pve_internals_index_section;
        shown_sections |= pve_internals_properties_section;
        shown_sections |= pve_internals_structure_section;
        shown_sections |= pve_internals_computed_properties_section;
        //shown_sections |= pve_internals_active_lines_section;
        shown_sections |= pve_internals_cells_segments_section;
        shown_sections |= pve_internals_sorted_cells_segments_section;
        shown_sections |= pve_internals_cells_section;
        shown_sections |= pve_internals_cells_stack_section;
        shown_sections |= pve_internals_lines_segments_section;
        shown_sections |= pve_internals_sorted_lines_segments_section;
        shown_sections |= pve_internals_lines_section;
        shown_sections |= pve_internals_lines_stack_section;
        pve_internals_to_stream(pve, stdout, shown_sections);
        printf("\n --- --- pve_internals_to_stream() COMPLETED --- ---\n");
    }

    if (env->pve_dump_file) {
        printf("\n --- --- pve_dump_to_binary_file() START --- ---\n");
        pve_dump_to_binary_file(pve, env->pve_dump_file);
        printf(" --- --- pve_dump_to_binary_file() COMPLETED --- ---\n");
    }

    search_node_free(sn);
    pve_free(pve);

    game_tree_log_close(log_env);

    return result;
}