Exemple #1
0
void
full_learn_public_game( int length, int *moves, int cutoff,
			int deviation_depth, int exact, int wld ) {
  int i;
  int dummy;
  int side_to_move;
  FILE *stream;

  stream = fopen( LEARN_LOG_FILE_NAME, "a" );
  if ( stream != NULL ) {  /* Write the game learned to a log file. */
    for ( i = 0; i < length; i++ )
      fprintf( stream, "%c%c", TO_SQUARE( moves[i] ) );
    fputs( "\n", stream );
    fclose( stream );
  }

  clear_panic_abort();
  toggle_abort_check( FALSE );

  /* Copy the move list from the caller as it is modified below. */

  for ( i = 0; i < length; i++ )
    game_move[i] = moves[i];

  /* Determine side to move for all positions */

  game_init( NULL, &dummy );
  side_to_move = BLACKSQ;
  for ( i = 0; i < length; i++ ) {
    generate_all( side_to_move );
    if ( move_count[disks_played] == 0 ) {
      side_to_move = OPP( side_to_move );
      generate_all( side_to_move );
    }
    (void) make_move( side_to_move, game_move[i], TRUE );
    if ( side_to_move == WHITESQ )
      game_move[i] = -game_move[i];
    side_to_move = OPP( side_to_move );
  }

  /* Let the learning sub-routine in osfbook update the opening
     book and the dump it to file. */

  set_search_depth( deviation_depth );
  add_new_game( length, game_move, cutoff, exact, wld, TRUE, FALSE );
  if ( binary_database )
    write_binary_database( database_name );
  else
    write_text_database( database_name );

  toggle_abort_check( TRUE );
}
Exemple #2
0
void
learn_game( int game_length, int private_game, int save_database ) {
  int i;
  int dummy;
  int side_to_move;
  int full_solve, wld_solve;

  clear_panic_abort();
  toggle_abort_check( FALSE );

  full_solve = get_earliest_full_solve();
  wld_solve = get_earliest_wld_solve();

  game_init( NULL, &dummy );
  side_to_move = BLACKSQ;
  for ( i = 0; i < game_length; i++ ) {
    generate_all( side_to_move );
    if ( move_count[disks_played] == 0 ) {
      side_to_move = OPP( side_to_move );
      generate_all( side_to_move );
    }
    (void) make_move( side_to_move, game_move[i], TRUE );
    if ( side_to_move == WHITESQ )
      game_move[i] = -game_move[i];
    side_to_move = OPP( side_to_move );
  }

  set_search_depth( learn_depth );
  add_new_game( game_length, game_move, cutoff_empty, full_solve,
		wld_solve, TRUE, private_game );

  if ( save_database ) {
    if ( binary_database )
      write_binary_database( database_name );
    else
      write_text_database( database_name );
  }

  toggle_abort_check( TRUE );
}
Exemple #3
0
/**
 * @brief       Builds move tree.
 *
 * Builds a complete move tree recursively.
 *
 * @param[in]   color       Color to move
 * @param[out]  *i_selected Pointer to horizontal coordinate of selected move.
 * @param[out]  *j_selected Pointer to vertical coordinate of selected move.
 * @return      Nothing
 * @note        If no move is selected i and j are INVALID.
 */
void search_tree( int color, int *i_selected, int *j_selected )
{
    // Index variables:
    int k;
    int d;
    //int m;   //DEBUG
    int i, j;

    // Variables for search tree:
    int depth = 0;
    int best_value;
    int search_level_incr;
    int alpha;
    int beta;

    // Variables for move list:
    int valid_moves[BOARD_SIZE_MAX * BOARD_SIZE_MAX][4];
    int nr_of_valid_moves;
    int nr_of_valid_moves_cut;

    // Variables for measuring time:
    time_t start;
    time_t stop;
    time_t diff_time;

    // Variables needed for logging:
    char x[2];
    char y[3];

    // Setting to root level values:
    alpha = INT_MIN;
    beta  = INT_MAX;
    hash_hit    = 0;
    alpha_break = 0;
    beta_break  = 0;
    search_level_incr = 0;
    count_quiet_search = 0;

    //init_brains();
    init_search_stats();
    //init_hash_table();

    best_value = ( color == BLACK ) ? INT_MIN : INT_MAX;

    node_count = 0;

    if ( do_log ) {
        log_file = fopen( LOG_FILE, "w" );
        if ( log_file == NULL ) {
            printf( "# Cannot open log file\n" );
            exit(1);
        }
    }

    (void) time(&start);

    nr_of_valid_moves     = get_valid_move_list( color, valid_moves );
    nr_of_valid_moves_cut = nr_of_valid_moves;

    // Loop start:
    search_level_incr = get_search_depth();
    for ( d = 0; d <= search_level_incr; d++ ) {
        set_search_depth(d);
        //set_search_level(search_level_incr);
        //l = search_level_incr;

        // Go through move list:
        for ( k = 0; k < nr_of_valid_moves_cut; k++ ) {
            i = valid_moves[k][0];
            j = valid_moves[k][1];

            // Make move:
            node_count++;
            //printf( "# Level: %d make: %d,%d value: %d\n", l, i, j, best_value );
            make_move( color, i, j );
            //i_to_x( i, x );
            //j_to_y( j, y );
            //printf( "## %s%s\n", x, y );

            // Start recursion:
            valid_moves[k][2] = add_node( color * -1, depth, alpha, beta );

            if ( color  == BLACK ) {
                // For black: remember highest value
                if ( valid_moves[k][2] > best_value ) {
                    best_value = valid_moves[k][2];
                    if ( best_value > alpha ) {
                        alpha = best_value;
                    }
                }
            }
            else {
                // For white: remember lowest value
                if ( valid_moves[k][2] < best_value ) {
                    best_value = valid_moves[k][2];
                    if ( best_value < beta ) {
                        beta = best_value;
                    }
                }
            }

            if ( do_log ) {
                i_to_x( i, x );
                j_to_y( j, y );
                fprintf( log_file, "%s%s (%d) (a: %d, b: %d)\n"
                    , x, y, valid_moves[k][2], alpha, beta );
            }

            undo_move();
        }

        // Sort move list by value:
        if ( color == BLACK ) {
            qsort( valid_moves, (size_t)nr_of_valid_moves_cut, sizeof(valid_moves[0]), compare_value_black );
        }
        else {
            qsort( valid_moves, (size_t)nr_of_valid_moves_cut, sizeof(valid_moves[0]), compare_value_white );
        }

        // DEBUG:
        /*
        printf( "# Level: %d (%d) - ", l, nr_of_valid_moves_cut );
        for ( m = 0; m < nr_of_valid_moves_cut; m++ ) {
            i_to_x( valid_moves[m][0], x );
            j_to_y( valid_moves[m][1], y );
            printf( "%s%s (%d,%d), ", x, y, valid_moves[m][2], valid_moves[m][3] );
        }
        printf("\n");
        */

        if ( nr_of_valid_moves_cut / 2 > 5 ) {
            nr_of_valid_moves_cut = nr_of_valid_moves_cut / 2;
        }

    }
    // Loop end

    (void) time(&stop);

    diff_time = stop - start;
    if ( diff_time == 0 ) {
        diff_time = 1;
    }

    // Save some stats about this search:
    search_stats.color[0] = '\0';
    if ( color == BLACK ) {
        my_strcpy( search_stats.color, "Black", 6 );
    }
    else {
        my_strcpy( search_stats.color, "White", 6 );
    }
    i_to_x( valid_moves[0][0], x );
    j_to_y( valid_moves[0][1], y );
    search_stats.move[0] = '\0';
    strcat( search_stats.move, x );
    strcat( search_stats.move, y );
    search_stats.level         = search_depth;
    search_stats.duration      = stop - start;
    search_stats.node_count    = node_count;
    search_stats.nodes_per_sec = node_count / diff_time;
    search_stats.qsearch_count = count_quiet_search;
    search_stats.hash_hit      = hash_hit;
    search_stats.alpha_cut     = alpha_break;
    search_stats.beta_cut      = beta_break;
    search_stats.value         = valid_moves[0][2];

    *i_selected = valid_moves[0][0];
    *j_selected = valid_moves[0][1];

    if ( log_file != NULL ) {
        fclose(log_file);
    }

    return;
}