1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
/* Copyright (c) 2012 Shlomi Fish
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* fcc_brfs_test.c - provide utility routines to test fcc_brfs.h
*/
#define BUILDING_DLL 1
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "config.h"
#undef FCS_RCS_STATES
#include "dbm_common.h"
#include "fcs_dllexport.h"
#include "delta_states_any.h"
#include "fcc_brfs.h"
#include "fcc_brfs_test.h"
static void fc_solve_state_string_to_enc(
enum fcs_dbm_variant_type_t local_variant,
fc_solve_delta_stater_t * delta,
const char * state_s_proto,
fcs_encoded_state_buffer_t * enc_state
)
{
fcs_state_keyval_pair_t state;
DECLARE_IND_BUF_T(state_buffer)
fc_solve_initial_user_state_to_c(
state_s_proto,
&state,
FREECELLS_NUM,
STACKS_NUM,
DECKS_NUM,
state_buffer
);
fcs_init_and_encode_state(
delta,
local_variant,
&(state),
enc_state
);
}
/*
* The char * returned is malloc()ed and should be free()ed.
*/
DLLEXPORT int fc_solve_user_INTERNAL_find_fcc_start_points(
enum fcs_dbm_variant_type_t local_variant,
const char * init_state_str_proto,
const int start_state_moves_count,
const fcs_fcc_move_t * const start_state_moves,
fcs_FCC_start_point_result_t * * out_fcc_start_points,
long * out_num_new_positions
)
{
fcs_state_keyval_pair_t init_state;
fc_solve_delta_stater_t * delta;
fcs_encoded_state_buffer_t enc_state;
fcs_state_locs_struct_t locs;
int i;
fcs_meta_compact_allocator_t meta_alloc;
fcs_FCC_start_points_list_t start_points_list;
dict_t * do_next_fcc_start_points_exist;
dict_t * does_min_by_sorting_exist;
fcs_lru_cache_t does_state_exist_in_any_FCC_cache;
const int max_num_elements_in_cache = 1000;
fcs_bool_t is_min_by_sorting_new;
fcs_encoded_state_buffer_t min_by_sorting;
fcs_fcc_moves_seq_allocator_t moves_list_allocator;
fcs_fcc_moves_seq_t start_state_moves_seq;
int states_count = 0;
fcs_FCC_start_point_t * iter;
fcs_FCC_start_point_result_t * ret;
add_start_point_context_t add_start_point_context;
void * tree_recycle_bin = NULL;
fcs_compact_allocator_t moves_list_compact_alloc;
DECLARE_IND_BUF_T(indirect_stacks_buffer)
fc_solve_initial_user_state_to_c(
init_state_str_proto,
&init_state,
FREECELLS_NUM,
STACKS_NUM,
DECKS_NUM,
indirect_stacks_buffer
);
delta = fc_solve_delta_stater_alloc(
&(init_state.s),
STACKS_NUM,
FREECELLS_NUM
#ifndef FCS_FREECELL_ONLY
, FCS_SEQ_BUILT_BY_ALTERNATE_COLOR
#endif
);
fcs_init_and_encode_state(
delta,
local_variant,
&(init_state),
&enc_state
);
start_points_list.list = NULL;
start_points_list.recycle_bin = NULL;
fc_solve_meta_compact_allocator_init( &meta_alloc );
fc_solve_compact_allocator_init(&(start_points_list.allocator), &meta_alloc);
do_next_fcc_start_points_exist =
fc_solve_kaz_tree_create(fc_solve_compare_encoded_states, NULL, &meta_alloc, &tree_recycle_bin);
does_min_by_sorting_exist =
fc_solve_kaz_tree_create(fc_solve_compare_encoded_states, NULL, &meta_alloc, &tree_recycle_bin);
cache_init(&does_state_exist_in_any_FCC_cache, max_num_elements_in_cache, &meta_alloc);
fc_solve_compact_allocator_init(&(moves_list_compact_alloc), &(meta_alloc));
moves_list_allocator.recycle_bin = NULL;
moves_list_allocator.allocator = &(moves_list_compact_alloc);
start_state_moves_seq.count = start_state_moves_count;
start_state_moves_seq.moves_list = NULL;
{
fcs_fcc_moves_list_item_t * * moves_iter = &(start_state_moves_seq.moves_list);
for ( i=0 ; i < start_state_moves_count ; )
{
if (i % FCS_FCC_NUM_MOVES_IN_ITEM == 0)
{
*(moves_iter) = fc_solve_fcc_alloc_moves_list_item(&moves_list_allocator);
}
(*moves_iter)->data.s[i % FCS_FCC_NUM_MOVES_IN_ITEM] = start_state_moves[i];
if ((++i) % FCS_FCC_NUM_MOVES_IN_ITEM == 0)
{
moves_iter = &((*moves_iter)->next);
}
}
}
add_start_point_context.do_next_fcc_start_points_exist = do_next_fcc_start_points_exist;
add_start_point_context.next_start_points_list = &start_points_list;
add_start_point_context.moves_list_allocator = &moves_list_allocator;
perform_FCC_brfs(
local_variant,
&(init_state),
enc_state,
&start_state_moves_seq,
fc_solve_add_start_point_in_mem,
&add_start_point_context,
&is_min_by_sorting_new,
&min_by_sorting,
does_min_by_sorting_exist,
&does_state_exist_in_any_FCC_cache,
out_num_new_positions,
&moves_list_allocator,
&meta_alloc
);
iter = start_points_list.list;
while (iter)
{
states_count++;
iter = iter->next;
}
*out_fcc_start_points = ret = SMALLOC(ret, states_count+1);
ret[states_count].count = 0;
fc_solve_init_locs(&locs);
iter = start_points_list.list;
for (i = 0; i < states_count ; i++)
{
fcs_state_keyval_pair_t state;
DECLARE_IND_BUF_T(state_indirect_stacks_buffer)
ret[i].count = iter->moves_seq.count;
ret[i].moves = SMALLOC(ret[i].moves, ret[i].count);
{
int moves_idx;
fcs_fcc_moves_list_item_t * moves_iter = iter->moves_seq.moves_list;
for (moves_idx = 0 ; moves_idx < ret[i].count ; )
{
ret[i].moves[moves_idx] = moves_iter->data.s[moves_idx % FCS_FCC_NUM_MOVES_IN_ITEM];
if ((++moves_idx) % FCS_FCC_NUM_MOVES_IN_ITEM == 0)
{
moves_iter = moves_iter->next;
}
}
}
fc_solve_delta_stater_decode_into_state(delta, iter->enc_state.s, &(state), state_indirect_stacks_buffer);
ret[i].state_as_string =
fc_solve_state_as_string(
&(state.s),
&locs,
FREECELLS_NUM,
STACKS_NUM,
DECKS_NUM,
1,
0,
1
);
iter = iter->next;
}
fc_solve_compact_allocator_finish(&(start_points_list.allocator));
fc_solve_compact_allocator_finish(&(moves_list_compact_alloc));
fc_solve_delta_stater_free (delta);
fc_solve_kaz_tree_destroy(do_next_fcc_start_points_exist);
fc_solve_kaz_tree_destroy(does_min_by_sorting_exist);
cache_destroy(&does_state_exist_in_any_FCC_cache);
fc_solve_meta_compact_allocator_finish( &meta_alloc );
return 0;
}
DLLEXPORT int fc_solve_user_INTERNAL_is_fcc_new(
enum fcs_dbm_variant_type_t local_variant,
const char * init_state_str_proto,
const char * start_state_str_proto,
/* NULL-terminated */
const char * * min_states,
/* NULL-terminated */
const char * * states_in_cache,
fcs_bool_t * out_is_fcc_new
)
{
fcs_state_keyval_pair_t init_state;
fc_solve_delta_stater_t * delta;
fcs_encoded_state_buffer_t enc_state;
fcs_encoded_state_buffer_t start_enc_state;
fcs_meta_compact_allocator_t meta_alloc;
fcs_FCC_start_points_list_t start_points_list;
dict_t * do_next_fcc_start_points_exist;
dict_t * does_min_by_sorting_exist;
fcs_compact_allocator_t temp_allocator;
fcs_fcc_moves_seq_allocator_t moves_list_allocator;
const int max_num_elements_in_cache = 1000000;
fcs_lru_cache_t does_state_exist_in_any_FCC_cache;
fcs_encoded_state_buffer_t min_by_sorting;
long num_new_positions_temp;
fcs_fcc_moves_seq_t init_moves_seq;
add_start_point_context_t add_start_point_context;
void * tree_recycle_bin = NULL;
DECLARE_IND_BUF_T(indirect_stacks_buffer)
fc_solve_initial_user_state_to_c(
init_state_str_proto,
&init_state,
FREECELLS_NUM,
STACKS_NUM,
DECKS_NUM,
indirect_stacks_buffer
);
delta = fc_solve_delta_stater_alloc(
&(init_state.s),
STACKS_NUM,
FREECELLS_NUM
#ifndef FCS_FREECELL_ONLY
, FCS_SEQ_BUILT_BY_ALTERNATE_COLOR
#endif
);
fcs_init_and_encode_state(
delta,
local_variant,
&(init_state),
&enc_state
);
fc_solve_state_string_to_enc(
local_variant,
delta,
start_state_str_proto,
&(start_enc_state)
);
fc_solve_meta_compact_allocator_init( &meta_alloc );
start_points_list.list = NULL;
start_points_list.recycle_bin = NULL;
fc_solve_compact_allocator_init(&(start_points_list.allocator), &meta_alloc);
do_next_fcc_start_points_exist =
fc_solve_kaz_tree_create(fc_solve_compare_encoded_states, NULL, &meta_alloc, &tree_recycle_bin);
does_min_by_sorting_exist =
fc_solve_kaz_tree_create(fc_solve_compare_encoded_states, NULL, &meta_alloc, &tree_recycle_bin);
fc_solve_compact_allocator_init(&(temp_allocator), &meta_alloc);
moves_list_allocator.recycle_bin = NULL;
moves_list_allocator.allocator = &(temp_allocator);
/* Populate does_min_by_sorting_exist from min_states */
{
const char * * min_states_iter = min_states;
for (; *(min_states_iter) ; min_states_iter++)
{
fcs_encoded_state_buffer_t * min_enc_state;
min_enc_state = (fcs_encoded_state_buffer_t *)
fcs_compact_alloc_ptr(
&(temp_allocator),
sizeof (*min_enc_state)
);
fc_solve_state_string_to_enc(
local_variant,
delta,
*(min_states_iter),
min_enc_state
);
fc_solve_kaz_tree_alloc_insert(
does_min_by_sorting_exist,
min_enc_state
);
}
}
cache_init(&does_state_exist_in_any_FCC_cache, max_num_elements_in_cache, &meta_alloc);
/* Populate does_state_exist_in_any_FCC_cache from states_in_cache */
{
const char * * min_states_iter = states_in_cache;
for (; *(min_states_iter) ; min_states_iter++)
{
fcs_encoded_state_buffer_t * min_enc_state;
min_enc_state = (fcs_encoded_state_buffer_t *)
fcs_compact_alloc_ptr(
&(temp_allocator),
sizeof (*min_enc_state)
);
fc_solve_state_string_to_enc(
local_variant,
delta,
*(min_states_iter),
min_enc_state
);
cache_insert (&does_state_exist_in_any_FCC_cache, min_enc_state, NULL, '\0');
}
}
init_moves_seq.moves_list = NULL;
init_moves_seq.count = 0;
add_start_point_context.do_next_fcc_start_points_exist = do_next_fcc_start_points_exist;
add_start_point_context.next_start_points_list = &start_points_list;
add_start_point_context.moves_list_allocator = &moves_list_allocator;
perform_FCC_brfs(
local_variant,
&(init_state),
start_enc_state,
&init_moves_seq,
fc_solve_add_start_point_in_mem,
&add_start_point_context,
out_is_fcc_new,
&min_by_sorting,
does_min_by_sorting_exist,
&does_state_exist_in_any_FCC_cache,
&num_new_positions_temp,
&moves_list_allocator,
&meta_alloc
);
fc_solve_compact_allocator_finish(&(start_points_list.allocator));
fc_solve_compact_allocator_finish(&(temp_allocator));
fc_solve_delta_stater_free (delta);
fc_solve_kaz_tree_destroy(do_next_fcc_start_points_exist);
fc_solve_kaz_tree_destroy(does_min_by_sorting_exist);
cache_destroy(&does_state_exist_in_any_FCC_cache);
fc_solve_meta_compact_allocator_finish( &meta_alloc );
return 0;
}
|