Beispiel #1
0
void fc_solve_dbm_store_init(fcs_dbm_store *const store, const char *const path,
    void **const recycle_bin_ptr)
{
    fcs_dbm *db = SMALLOC1(db);

    int ret;

    if ((ret = db_create(&(db->dbp), NULL, 0)) != 0)
    {
        fc_solve_err("db_create: %s\n", db_strerror(ret));
    }

    if ((ret = db->dbp->open(
             db->dbp, NULL, path, NULL, DB_BTREE, DB_CREATE, 0664)) != 0)
    {
        db->dbp->err(db->dbp, ret, "%s", path);
        goto err;
    }
    memset(&(db->key), 0, sizeof(db->key));
    memset(&(db->data), 0, sizeof(db->data));
    *store = (fcs_dbm_store)db;
    return;

err:
    my_close(db->dbp, ret);
}
Beispiel #2
0
extern void fc_solve_dbm_store_destroy(fcs_dbm_store store)
{
    fcs_dbm *const db = (fcs_dbm *)store;
    int ret;
    if ((ret = db->dbp->close(db->dbp, 0)) != 0)
    {
        fc_solve_err("DB close failed with ret=%d\n", ret);
    }
    free(db);
}
Beispiel #3
0
int main(int argc, char *argv[])
{
    fcs_dbm_variant_type_t local_variant = FCS_DBM_VARIANT_2FC_FREECELL;
    const long delta_limit = 100000;
    const int max_num_elements_in_cache = 8000000;
    const char *filename = argv[1];
    FILE *const fh = fopen(filename, "r");
    if (fh == NULL)
    {
        fc_solve_err("Could not open file '%s' for input.\n", filename);
    }
    const fcs_user_state_str_t user_state = read_state(fh);
    fcs_state_keyval_pair_t init_state_pair;
    fc_solve_initial_user_state_to_c(
        user_state.s, &init_state_pair, FREECELLS_NUM, STACKS_NUM, 1, NULL);

    fcs_dbm_solver_instance_t instance;

    instance_init(
        &instance, local_variant, &init_state_pair, max_num_elements_in_cache);

#define LOG_FILENAME "fc-solve-pseudo-dfs.log.txt"

    {
        FILE *const last_line_fh = popen(("tail -1 " LOG_FILENAME), "r");

        if (last_line_fh)
        {
            long count_num_processed;
            if (fscanf(last_line_fh, "At %ld iterations Coords=[",
                    &count_num_processed) == 1)
            {
                instance__load_coords_from_fh(&instance, last_line_fh);
                /*
                 * instance__inspect_new_state increments count_num_processed
                 * so let's set it after loading the coordinates.
                 * */
                instance.count_num_processed = count_num_processed;
            }
        }
        pclose(last_line_fh);
    }

    instance.max_count_num_processed =
        instance.count_num_processed + delta_limit;

    while (instance.max_count_num_processed % delta_limit != 0)
    {
        instance.max_count_num_processed +=
            delta_limit - (instance.max_count_num_processed % delta_limit);
    }

    while (instance.should_terminate == DONT_TERMINATE)
    {
        instance_run(&instance);

        FILE *const log_fh = fopen(LOG_FILENAME, "at");
        instance__print_coords_to_log(&instance, log_fh);
        fclose(log_fh);

        instance.max_count_num_processed =
            instance.count_num_processed + delta_limit;
    }

    if (instance.should_terminate == SOLUTION_FOUND_TERMINATE)
    {
        printf("%s\n", "Solution was found.");
    }
    else
    {
        printf("%s\n", "I could not solve it.");
    }

    instance_free(&instance);

    return 0;
}