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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
|
/* Copyright (c) 2000 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.
*/
/*
* instance.c - instance/hard_thread/soft_thread functions for
* Freecell Solver.
*/
#define BUILDING_DLL 1
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#define NUM_CHECKED_STATES_STEP 50
#include "config.h"
#include "state.h"
#include "instance.h"
#include "scans.h"
#include "preset.h"
#include "unused.h"
#include "check_and_add_state.h"
#include "move_funcs_order.h"
#include "inline.h"
#include "likely.h"
#include "count.h"
#include "alloc_wrap.h"
#include "fcs_strndup.h"
/*
General use of this interface:
1. fc_solve_alloc_instance()
2. Set the parameters of the game
3. If you wish to revert, go to step #11.
4. fc_solve_init_instance()
5. Call fc_solve_solve_instance() with the initial board.
6. If it returns FCS_STATE_SUSPEND_PROCESS and you wish to proceed,
then increase the iteration limit and call
fc_solve_resume_instance().
7. Repeat Step #6 zero or more times.
8. If the solving was successful you can use the move stacks or the
intermediate stacks. (Just don't destroy them in any way).
9. Call fc_solve_finish_instance().
10. Call fc_solve_free_instance().
The library functions inside lib.c (a.k.a fcs_user()) give an
easier approach for embedding Freecell Solver into your library. The
intent of this comment is to document the code, rather than to be
a guideline for the user.
*/
const double fc_solve_default_befs_weights[FCS_NUM_BEFS_WEIGHTS] = {0.5, 0, 0.3, 0, 0.2, 0};
static GCC_INLINE void normalize_befs_weights(
fc_solve_state_weighting_t * const weighting
)
{
/* Normalize the Best-First-Search Weights, so the sum of all of them would be 1. */
double sum = 0;
#define my_befs_weights weighting->befs_weights
for (
int weight_idx = 0
;
weight_idx < COUNT(my_befs_weights)
;
weight_idx++
)
{
if (unlikely(my_befs_weights[weight_idx] < 0))
{
my_befs_weights[weight_idx] = fc_solve_default_befs_weights[weight_idx];
}
sum += my_befs_weights[weight_idx];
}
if (unlikely(sum < 1e-6))
{
sum = 1;
}
for (int weight_idx=0 ; weight_idx < COUNT(my_befs_weights) ; weight_idx++)
{
my_befs_weights[weight_idx] /= sum;
}
}
static GCC_INLINE void soft_thread_clean_soft_dfs(
fc_solve_soft_thread_t * const soft_thread
)
{
fcs_soft_dfs_stack_item_t * const soft_dfs_info = DFS_VAR(soft_thread, soft_dfs_info);
/* Check if a Soft-DFS-type scan was called in the first place */
if (! soft_dfs_info )
{
/* If not - do nothing */
return;
}
/* De-allocate the Soft-DFS specific stacks */
const fcs_soft_dfs_stack_item_t * info_ptr = soft_dfs_info;
const fcs_soft_dfs_stack_item_t * const max_info_ptr
= info_ptr + DFS_VAR(soft_thread, depth);
const fcs_soft_dfs_stack_item_t * const dfs_max_info_ptr
= info_ptr + DFS_VAR(soft_thread, dfs_max_depth);
for (; info_ptr < max_info_ptr ; info_ptr++)
{
free (info_ptr->derived_states_list.states);
free (info_ptr->derived_states_random_indexes);
free (info_ptr->positions_by_rank);
}
for ( ; info_ptr < dfs_max_info_ptr ; info_ptr++)
{
if (likely(info_ptr->derived_states_list.states))
{
free(info_ptr->derived_states_list.states);
free(info_ptr->derived_states_random_indexes);
}
}
free(soft_dfs_info);
DFS_VAR(soft_thread, soft_dfs_info) = NULL;
DFS_VAR(soft_thread, dfs_max_depth) = 0;
}
extern void fc_solve_free_soft_thread_by_depth_test_array(
fc_solve_soft_thread_t * const soft_thread
)
{
int depth_idx;
for (depth_idx = 0 ;
depth_idx < soft_thread->by_depth_tests_order.num ;
depth_idx++)
{
fc_solve_free_tests_order(&(soft_thread->by_depth_tests_order.by_depth_tests[depth_idx].tests_order));
}
soft_thread->by_depth_tests_order.num = 0;
free(soft_thread->by_depth_tests_order.by_depth_tests);
soft_thread->by_depth_tests_order.by_depth_tests = NULL;
return;
}
static GCC_INLINE void free_instance_soft_thread_callback(
fc_solve_soft_thread_t * const soft_thread
)
{
fc_solve_PQueueFree(
&(BEFS_VAR(soft_thread, pqueue))
);
fc_solve_release_tests_list(soft_thread);
fc_solve_free_soft_thread_by_depth_test_array(soft_thread);
if (likely(soft_thread->name != NULL))
{
free(soft_thread->name);
}
}
static GCC_INLINE void accumulate_tests_by_ptr(
int * const tests_order,
fcs_tests_order_t * const st_tests_order
)
{
const fcs_tests_order_group_t * group_ptr = st_tests_order->groups;
const fcs_tests_order_group_t * const groups_end = group_ptr + st_tests_order->num_groups;
for ( ; group_ptr < groups_end ; group_ptr++)
{
const int * test_ptr = group_ptr->tests;
const int * const tests_end = test_ptr + group_ptr->num;
for (; test_ptr < tests_end; test_ptr++)
{
*tests_order |= (1 << (*test_ptr));
}
}
}
static GCC_INLINE void accumulate_tests_order(
fc_solve_soft_thread_t * const soft_thread,
void * const context
)
{
accumulate_tests_by_ptr((int *)context, &(soft_thread->by_depth_tests_order.by_depth_tests[0].tests_order));
}
static GCC_INLINE void determine_scan_completeness(
fc_solve_soft_thread_t * const soft_thread,
void * const global_tests_order
)
{
int tests_order = 0;
accumulate_tests_by_ptr(&tests_order, &(soft_thread->by_depth_tests_order.by_depth_tests[0].tests_order));
STRUCT_SET_FLAG_TO(soft_thread, FCS_SOFT_THREAD_IS_A_COMPLETE_SCAN,
(tests_order == *(int *)global_tests_order)
);
}
void fc_solve_foreach_soft_thread(
fc_solve_instance_t * const instance,
const int callback_choice,
void * const context
)
{
for (int ht_idx = 0 ; ht_idx<=instance->num_hard_threads; ht_idx++)
{
fc_solve_hard_thread_t * hard_thread;
if (ht_idx < instance->num_hard_threads)
{
hard_thread = &(instance->hard_threads[ht_idx]);
}
else if (instance->optimization_thread)
{
hard_thread = instance->optimization_thread;
}
else
{
break;
}
ST_LOOP_START()
{
switch (callback_choice)
{
case FOREACH_SOFT_THREAD_CLEAN_SOFT_DFS:
soft_thread_clean_soft_dfs( soft_thread );
break;
case FOREACH_SOFT_THREAD_FREE_INSTANCE:
free_instance_soft_thread_callback( soft_thread );
break;
case FOREACH_SOFT_THREAD_ACCUM_TESTS_ORDER:
accumulate_tests_order( soft_thread, context );
break;
case FOREACH_SOFT_THREAD_DETERMINE_SCAN_COMPLETENESS:
determine_scan_completeness( soft_thread, context );
break;
}
}
}
}
static GCC_INLINE void clean_soft_dfs( fc_solve_instance_t * const instance )
{
fc_solve_foreach_soft_thread (
instance,
FOREACH_SOFT_THREAD_CLEAN_SOFT_DFS,
NULL
);
}
static GCC_INLINE void init_soft_thread(
fc_solve_hard_thread_t * const hard_thread,
fc_solve_soft_thread_t * const soft_thread
)
{
soft_thread->hard_thread = hard_thread;
soft_thread->id = (hard_thread->instance->next_soft_thread_id)++;
DFS_VAR(soft_thread, dfs_max_depth) = 0;
soft_thread->by_depth_tests_order.num = 1;
soft_thread->by_depth_tests_order.by_depth_tests =
SMALLOC1(soft_thread->by_depth_tests_order.by_depth_tests);
soft_thread->by_depth_tests_order.by_depth_tests[0].max_depth = INT_MAX;
soft_thread->by_depth_tests_order.by_depth_tests[0].tests_order.num_groups = 0;
soft_thread->by_depth_tests_order.by_depth_tests[0]
.tests_order.groups = NULL;
/* Initialize all the Soft-DFS stacks to NULL */
DFS_VAR(soft_thread, soft_dfs_info) = NULL;
DFS_VAR(soft_thread, depth) = 0;
DFS_VAR(soft_thread, tests_by_depth_array).num_units = 0;
DFS_VAR(soft_thread, tests_by_depth_array).by_depth_units = NULL;
DFS_VAR(soft_thread, rand_seed) = 24;
BEFS_M_VAR(soft_thread, tests_list) = NULL;
/* The default solving method */
soft_thread->method = FCS_METHOD_SOFT_DFS;
soft_thread->super_method_type = FCS_SUPER_METHOD_DFS;
BEFS_M_VAR(soft_thread, befs_positions_by_rank) = NULL;
memcpy(
BEFS_VAR(soft_thread, weighting.befs_weights),
fc_solve_default_befs_weights,
sizeof(fc_solve_default_befs_weights)
);
BEFS_VAR(soft_thread, pqueue).Elements = NULL;
BRFS_VAR(soft_thread, bfs_queue) =
BRFS_VAR(soft_thread, bfs_queue_last_item) =
NULL;
soft_thread->num_checked_states_step = NUM_CHECKED_STATES_STEP;
soft_thread->by_depth_tests_order.by_depth_tests[0].tests_order = tests_order_dup(&(soft_thread->hard_thread->instance->instance_tests_order));
fc_solve_reset_soft_thread(soft_thread);
soft_thread->name = NULL;
soft_thread->enable_pruning = FALSE;
}
void fc_solve_instance__init_hard_thread(
fc_solve_instance_t * const instance,
fc_solve_hard_thread_t * const hard_thread
)
{
hard_thread->instance = instance;
hard_thread->num_soft_threads = 0;
hard_thread->soft_threads = NULL;
fc_solve_new_soft_thread(hard_thread);
/* Set a limit on the Hard-Thread's scan. */
hard_thread->num_checked_states_step = NUM_CHECKED_STATES_STEP;
hard_thread->prelude_as_string = NULL;
hard_thread->prelude = NULL;
hard_thread->prelude_num_items = 0;
hard_thread->prelude_idx = 0;
fc_solve_reset_hard_thread(hard_thread);
fc_solve_compact_allocator_init(&(hard_thread->allocator), instance->meta_alloc);
fcs_move_stack_init(hard_thread->reusable_move_stack);
}
/*
This function allocates a Freecell Solver instance struct and set the
default values in it. After the call to this function, the program can
set parameters in it which are different from the default.
Afterwards fc_solve_init_instance() should be called in order
to really prepare it for solving.
*/
fc_solve_instance_t * fc_solve_alloc_instance(fcs_meta_compact_allocator_t * const meta_alloc)
{
fc_solve_instance_t * instance = SMALLOC1(instance);
instance->meta_alloc = meta_alloc;
#if (FCS_STATE_STORAGE == FCS_STATE_STORAGE_INDIRECT)
instance->num_indirect_prev_states = 0;
#endif
instance->num_checked_states = 0;
instance->num_states_in_collection = 0;
instance->active_num_states_in_collection = 0;
instance->max_num_checked_states = -1;
instance->effective_max_num_checked_states = INT_MAX;
instance->max_depth = -1;
instance->max_num_states_in_collection = -1;
instance->effective_max_num_states_in_collection = INT_MAX;
instance->trim_states_in_collection_from = -1;
instance->effective_trim_states_in_collection_from = LONG_MAX;
instance->instance_tests_order.num_groups = 0;
instance->instance_tests_order.groups = NULL;
instance->list_of_vacant_states = NULL;
STRUCT_CLEAR_FLAG(instance, FCS_RUNTIME_OPT_TESTS_ORDER_WAS_SET );
instance->opt_tests_order.num_groups = 0;
instance->opt_tests_order.groups = NULL;
instance->num_hard_threads = 0;
instance->hard_threads = NULL;
#ifndef FCS_FREECELL_ONLY
fc_solve_apply_preset_by_name(instance, "freecell");
#else
{
char * no_use;
fc_solve_apply_tests_order(
&(instance->instance_tests_order),
"[01][23456789]",
&no_use
);
}
#endif
/****************************************/
instance->debug_iter_output_func = NULL;
instance->next_soft_thread_id = 0;
fc_solve_new_hard_thread(instance);
instance->solution_moves.moves = NULL;
STRUCT_CLEAR_FLAG(instance, FCS_RUNTIME_OPTIMIZE_SOLUTION_PATH);
instance->optimization_thread = NULL;
STRUCT_CLEAR_FLAG(instance, FCS_RUNTIME_IN_OPTIMIZATION_THREAD);
instance->num_hard_threads_finished = 0;
STRUCT_CLEAR_FLAG(instance, FCS_RUNTIME_CALC_REAL_DEPTH);
STRUCT_CLEAR_FLAG(instance, FCS_RUNTIME_TO_REPARENT_STATES_PROTO );
/* Make the 1 the default, because otherwise scans will not cooperate
* with one another. */
STRUCT_TURN_ON_FLAG(instance, FCS_RUNTIME_SCANS_SYNERGY);
#define DEFAULT_MAX_NUM_ELEMENTS_IN_CACHE 10000
#ifdef FCS_RCS_STATES
instance->rcs_states_cache.max_num_elements_in_cache
= DEFAULT_MAX_NUM_ELEMENTS_IN_CACHE;
#endif
return instance;
}
#undef DEFAULT_MAX_NUM_ELEMENTS_IN_CACHE
enum
{
FCS_COMPILE_PRELUDE_OK,
FCS_COMPILE_PRELUDE_NO_AT_SIGN,
FCS_COMPILE_PRELUDE_UNKNOWN_SCAN_ID
};
static GCC_INLINE int compile_prelude(
fc_solve_hard_thread_t * const hard_thread
)
{
fcs_bool_t last_one = FALSE;
int num_items = 0;
fcs_prelude_item_t * prelude = NULL;
const char * p = hard_thread->prelude_as_string;
while (! last_one)
{
const int p_quota = atoi(p);
while((*p) && isdigit(*p))
{
p++;
}
if (*p != '@')
{
free(prelude);
return FCS_COMPILE_PRELUDE_NO_AT_SIGN;
}
p++;
const char * const p_scan = p;
while((*p) && ((*p) != ','))
{
p++;
}
if ((*p) == '\0')
{
last_one = TRUE;
}
char * const p_scan_copy = strndup(p_scan, p-p_scan);
p++;
ST_LOOP_START()
{
if (soft_thread->name && (!strcmp(soft_thread->name, p_scan_copy)))
{
break;
}
}
free (p_scan_copy);
if (ST_LOOP_FINISHED())
{
free(prelude);
return FCS_COMPILE_PRELUDE_UNKNOWN_SCAN_ID;
}
#define PRELUDE_GROW_BY 16
if (! (num_items & (PRELUDE_GROW_BY-1)))
{
prelude = SREALLOC(prelude, num_items+PRELUDE_GROW_BY);
}
prelude[num_items].scan_idx = ST_LOOP_INDEX();
prelude[num_items].quota = p_quota;
num_items++;
}
hard_thread->prelude = SREALLOC(prelude, num_items);
hard_thread->prelude_num_items = num_items;
hard_thread->prelude_idx = 0;
return FCS_COMPILE_PRELUDE_OK;
}
void fc_solve_init_instance(fc_solve_instance_t * instance)
{
/* Initialize the state packs */
HT_LOOP_START()
{
if (hard_thread->prelude_as_string)
{
if (!hard_thread->prelude)
{
compile_prelude(hard_thread);
}
}
hard_thread->num_checked_states_left_for_soft_thread =
hard_thread->soft_threads[0].num_checked_states_step;
}
{
int total_tests = 0;
fc_solve_foreach_soft_thread(
instance,
FOREACH_SOFT_THREAD_ACCUM_TESTS_ORDER,
&total_tests
);
fc_solve_foreach_soft_thread(
instance,
FOREACH_SOFT_THREAD_DETERMINE_SCAN_COMPLETENESS,
&total_tests
);
if (!STRUCT_QUERY_FLAG(
instance, FCS_RUNTIME_OPT_TESTS_ORDER_WAS_SET
))
{
/*
*
* What this code does is convert the bit map of total_tests
* to a valid tests order.
*
* */
int num_tests = 0;
int * tests = SMALLOC(tests, sizeof(total_tests)*8);
for (int bit_idx=0 ;
total_tests != 0 ;
bit_idx++, total_tests >>= 1)
{
if ((total_tests & 0x1) != 0)
{
tests[num_tests++] = bit_idx;
}
}
tests = SREALLOC(tests,
((num_tests & (~(TESTS_ORDER_GROW_BY - 1)))+TESTS_ORDER_GROW_BY)
);
instance->opt_tests_order.num_groups = 1;
instance->opt_tests_order.groups =
SMALLOC( instance->opt_tests_order.groups, TESTS_ORDER_GROW_BY);
instance->opt_tests_order.groups[0].tests = tests;
instance->opt_tests_order.groups[0].num =
num_tests;
instance->opt_tests_order.groups[0].shuffling_type = FCS_NO_SHUFFLING;
STRUCT_TURN_ON_FLAG(instance, FCS_RUNTIME_OPT_TESTS_ORDER_WAS_SET);
}
}
#ifdef FCS_RCS_STATES
{
fcs_lru_cache_t * cache = &(instance->rcs_states_cache);
#if (FCS_RCS_CACHE_STORAGE == FCS_RCS_CACHE_STORAGE_JUDY)
cache->states_values_to_keys_map = ((Pvoid_t) NULL);
#elif (FCS_RCS_CACHE_STORAGE == FCS_RCS_CACHE_STORAGE_KAZ_TREE)
cache->kaz_tree = fc_solve_kaz_tree_create(fc_solve_compare_lru_cache_keys, NULL, instance->meta_alloc);
#else
#error Unknown FCS_RCS_CACHE_STORAGE
#endif
fc_solve_compact_allocator_init(
&(cache->states_values_to_keys_allocator),
instance->meta_alloc
);
cache->lowest_pri = NULL;
cache->highest_pri = NULL;
cache->recycle_bin = NULL;
cache->count_elements_in_cache = 0;
}
#endif
}
/* These are all stack comparison functions to be used for the stacks
cache when using INDIRECT_STACK_STATES
*/
#if defined(INDIRECT_STACK_STATES)
#if ((FCS_STACK_STORAGE != FCS_STACK_STORAGE_GLIB_TREE) && (FCS_STACK_STORAGE != FCS_STACK_STORAGE_GLIB_HASH) && (FCS_STACK_STORAGE != FCS_STACK_STORAGE_JUDY))
#if (((FCS_STACK_STORAGE == FCS_STACK_STORAGE_INTERNAL_HASH) \
&& (defined(FCS_WITH_CONTEXT_VARIABLE)) \
&& (!defined(FCS_INLINED_HASH_COMPARISON))) \
|| \
(FCS_STACK_STORAGE == FCS_STACK_STORAGE_LIBAVL2_TREE) \
|| \
(FCS_STACK_STORAGE == FCS_STACK_STORAGE_LIBREDBLACK_TREE) \
)
static int fcs_stack_compare_for_comparison_with_context(
const void * v_s1,
const void * v_s2,
#if (FCS_STACK_STORAGE == FCS_STACK_STORAGE_LIBREDBLACK_TREE)
const
#endif
void * context GCC_UNUSED
)
{
return fc_solve_stack_compare_for_comparison(v_s1, v_s2);
}
#endif
#endif
#if (FCS_STACK_STORAGE == FCS_STACK_STORAGE_GLIB_HASH)
/* A hash calculation function for use in glib's hash */
static guint fc_solve_glib_hash_stack_hash_function (
gconstpointer key
)
{
guint hash_value_int;
/* Calculate the hash value for the stack */
/* This hash function was ripped from the Perl source code.
* (It is not derived work however). */
const char * s_ptr = (char*)key;
const char * const s_end = s_ptr+fcs_col_len((fcs_card_t *)key)+1;
hash_value_int = 0;
while (s_ptr < s_end)
{
hash_value_int += (hash_value_int << 5) + *(s_ptr++);
}
hash_value_int += (hash_value_int >> 5);
return hash_value_int;
}
static gint fc_solve_glib_hash_stack_compare (
gconstpointer a,
gconstpointer b
)
{
return !(fc_solve_stack_compare_for_comparison(a,b));
}
#endif /* (FCS_STACK_STORAGE == FCS_STACK_STORAGE_GLIB_HASH) */
#endif /* defined(INDIRECT_STACK_STATES) */
/*
* This function traces the solution from the final state down
* to the initial state
* */
extern void fc_solve_trace_solution(
fc_solve_instance_t * const instance
)
{
fcs_internal_move_t canonize_move = fc_solve_empty_move;
fcs_int_move_set_type(canonize_move, FCS_MOVE_TYPE_CANONIZE);
if (instance->solution_moves.moves != NULL)
{
fcs_move_stack_static_destroy(instance->solution_moves);
instance->solution_moves.moves = NULL;
}
fcs_move_stack_init(instance->solution_moves);
fcs_collectible_state_t * s1 = instance->final_state;
fcs_move_stack_t * const solution_moves_ptr = &(instance->solution_moves);
/* Retrace the step from the current state to its parents */
while (FCS_S_PARENT(s1) != NULL)
{
/* Mark the state as part of the non-optimized solution */
FCS_S_VISITED(s1) |= FCS_VISITED_IN_SOLUTION_PATH;
/* Each state->parent_state stack has an implicit CANONIZE
* move. */
fcs_move_stack_push(solution_moves_ptr, canonize_move);
/* Merge the move stack */
{
const fcs_move_stack_t * const stack = FCS_S_MOVES_TO_PARENT(s1);
const fcs_internal_move_t * const moves = stack->moves;
for (int move_idx=stack->num_moves-1 ; move_idx >= 0 ; move_idx--)
{
fcs_move_stack_push(solution_moves_ptr, moves[move_idx]);
}
}
/* Duplicate the state to a freshly malloced memory */
/* Move to the parent state */
s1 = FCS_S_PARENT(s1);
}
/* There's one more state than there are move stacks */
FCS_S_VISITED(s1) |= FCS_VISITED_IN_SOLUTION_PATH;
}
#ifdef FCS_RCS_STATES
#if ((FCS_STATE_STORAGE == FCS_STATE_STORAGE_LIBAVL2_TREE) || (FCS_STATE_STORAGE == FCS_STATE_STORAGE_KAZ_TREE))
static GCC_INLINE fcs_state_t * rcs_states_get_state(
fc_solve_instance_t * const instance,
fcs_collectible_state_t * const state
)
{
return (
(state == instance->tree_new_state)
? instance->tree_new_state_key
: fc_solve_lookup_state_key_from_val(
instance,
state
)
);
}
static int fc_solve_rcs_states_compare(const void * void_a, const void * void_b, void * param)
{
fc_solve_instance_t * instance = (fc_solve_instance_t *)param;
return fc_solve_state_compare(
rcs_states_get_state(instance, (fcs_collectible_state_t *)void_a),
rcs_states_get_state(instance, (fcs_collectible_state_t *)void_b)
);
}
#endif
#define STATE_STORAGE_TREE_COMPARE() fc_solve_rcs_states_compare
#define STATE_STORAGE_TREE_CONTEXT() instance
#else
#define STATE_STORAGE_TREE_COMPARE() fc_solve_state_compare_with_context
#define STATE_STORAGE_TREE_CONTEXT() NULL
#endif
/*
This function associates a board with an fc_solve_instance_t and
does other initialisations. After it, you must call
fc_solve_resume_instance() repeatedly.
*/
void fc_solve_start_instance_process_with_board(
fc_solve_instance_t * const instance,
fcs_state_keyval_pair_t * const init_state
)
{
fcs_state_keyval_pair_t * state_copy_ptr;
/* Allocate the first state and initialize it to init_state */
state_copy_ptr =
(fcs_state_keyval_pair_t *)
fcs_compact_alloc_ptr(
&(instance->hard_threads[0].allocator),
sizeof(*state_copy_ptr)
);
{
fcs_kv_state_t pass_copy, pass_init;
pass_copy.key = &(state_copy_ptr->s);
pass_copy.val = &(state_copy_ptr->info);
pass_init.key = &(init_state->s);
pass_init.val = &(init_state->info);
fcs_duplicate_kv_state( &(pass_copy), &(pass_init) );
}
#ifdef INDIRECT_STACK_STATES
{
int i;
char * buffer;
buffer = instance->hard_threads[0].indirect_stacks_buffer;
for ( i=0 ; i < INSTANCE_STACKS_NUM ; i++ )
{
fcs_copy_stack(state_copy_ptr->s, state_copy_ptr->info, i, buffer);
}
}
#endif
/* Initialize the state to be a base state for the game tree */
#ifndef FCS_WITHOUT_DEPTH_FIELD
state_copy_ptr->info.depth = 0;
#endif
state_copy_ptr->info.moves_to_parent = NULL;
state_copy_ptr->info.visited = 0;
state_copy_ptr->info.parent = NULL;
memset(&(state_copy_ptr->info.scan_visited), '\0', sizeof(state_copy_ptr->info.scan_visited));
instance->state_copy_ptr = state_copy_ptr;
/* Initialize the data structure that will manage the state collection */
#if (FCS_STATE_STORAGE == FCS_STATE_STORAGE_LIBREDBLACK_TREE)
instance->tree = rbinit(
fc_solve_state_extra_info_compare_with_context,
NULL
);
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_LIBAVL2_TREE)
instance->tree = fcs_libavl2_states_tree_create(
STATE_STORAGE_TREE_COMPARE(),
STATE_STORAGE_TREE_CONTEXT(),
NULL
);
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_KAZ_TREE)
instance->tree = fc_solve_kaz_tree_create(
STATE_STORAGE_TREE_COMPARE(), STATE_STORAGE_TREE_CONTEXT()
);
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_GLIB_TREE)
instance->tree = g_tree_new(fc_solve_state_compare);
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_JUDY)
instance->judy_array = ((Pvoid_t)NULL);
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_GLIB_HASH)
instance->hash = g_hash_table_new(
fc_solve_hash_function,
fc_solve_state_compare_equal
);
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_INTERNAL_HASH)
fc_solve_hash_init(
instance->meta_alloc,
&(instance->hash),
#ifdef FCS_INLINED_HASH_COMPARISON
FCS_INLINED_HASH__STATES
#else
#ifdef FCS_WITH_CONTEXT_VARIABLE
fc_solve_state_compare_with_context,
NULL
#else
fc_solve_state_compare
#endif
#endif
);
#ifdef FCS_RCS_STATES
instance->hash.instance = instance;
#endif
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_GOOGLE_DENSE_HASH)
instance->hash = fc_solve_states_google_hash_new();
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_INDIRECT)
instance->num_prev_states_margin = 0;
instance->indirect_prev_states = NULL;
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_INTERNAL_HASH)
/* Do nothing because it is allocated elsewhere. */
#else
#error not defined
#endif
/****************************************************/
#ifdef INDIRECT_STACK_STATES
/* Initialize the data structure that will manage the stack
collection */
#if FCS_STACK_STORAGE == FCS_STACK_STORAGE_INTERNAL_HASH
fc_solve_hash_init(
instance->meta_alloc,
&(instance->stacks_hash ),
#ifdef FCS_INLINED_HASH_COMPARISON
FCS_INLINED_HASH__COLUMNS
#else
#ifdef FCS_WITH_CONTEXT_VARIABLE
fcs_stack_compare_for_comparison_with_context,
NULL
#else
fc_solve_stack_compare_for_comparison
#endif
#endif
);
#elif (FCS_STACK_STORAGE == FCS_STACK_STORAGE_LIBAVL2_TREE)
instance->stacks_tree = fcs_libavl2_stacks_tree_create(
fcs_stack_compare_for_comparison_with_context,
NULL,
NULL
);
#elif (FCS_STACK_STORAGE == FCS_STACK_STORAGE_LIBREDBLACK_TREE)
instance->stacks_tree = rbinit(
fcs_stack_compare_for_comparison_with_context,
NULL
);
#elif (FCS_STACK_STORAGE == FCS_STACK_STORAGE_GLIB_TREE)
instance->stacks_tree = g_tree_new(fc_solve_stack_compare_for_comparison);
#elif (FCS_STACK_STORAGE == FCS_STACK_STORAGE_GLIB_HASH)
instance->stacks_hash = g_hash_table_new(
fc_solve_glib_hash_stack_hash_function,
fc_solve_glib_hash_stack_compare
);
#elif (FCS_STACK_STORAGE == FCS_STACK_STORAGE_GOOGLE_DENSE_HASH)
instance->stacks_hash = fc_solve_columns_google_hash_new();
#elif (FCS_STACK_STORAGE == FCS_STACK_STORAGE_JUDY)
instance->stacks_judy_array = NULL;
#else
#error FCS_STACK_STORAGE is not set to a good value.
#endif
#endif
/***********************************************/
#if (FCS_STATE_STORAGE == FCS_STATE_STORAGE_DB_FILE)
/* Not working - ignore */
db_open(
NULL,
DB_BTREE,
O_CREAT|O_RDWR,
0777,
NULL,
NULL,
&(instance->db)
);
#endif
{
fcs_kv_state_t no_use;
fcs_kv_state_t pass;
pass.key = &(state_copy_ptr->s);
pass.val = &(state_copy_ptr->info);
fc_solve_check_and_add_state(
instance->hard_threads,
&pass,
&no_use
);
}
instance->current_hard_thread = instance->hard_threads;
{
HT_LOOP_START()
{
if (hard_thread->prelude != NULL)
{
hard_thread->prelude_idx = 0;
hard_thread->st_idx = hard_thread->prelude[hard_thread->prelude_idx].scan_idx;
hard_thread->num_checked_states_left_for_soft_thread = hard_thread->prelude[hard_thread->prelude_idx].quota;
hard_thread->prelude_idx++;
}
else
{
hard_thread->st_idx = 0;
}
}
}
STRUCT_SET_FLAG_TO(instance,
FCS_RUNTIME_TO_REPARENT_STATES_REAL,
STRUCT_QUERY_FLAG(
instance, FCS_RUNTIME_TO_REPARENT_STATES_PROTO
)
);
return;
}
/***********************************************************/
/*
This function should be called after the user has retrieved the
results generated by the scan as it will destroy them.
*/
void fc_solve_finish_instance( fc_solve_instance_t * const instance )
{
/* De-allocate the state collection */
#if (FCS_STATE_STORAGE == FCS_STATE_STORAGE_LIBREDBLACK_TREE)
rbdestroy(instance->tree);
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_LIBAVL2_TREE)
fcs_libavl2_states_tree_destroy(instance->tree, NULL);
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_KAZ_TREE)
fc_solve_kaz_tree_free_nodes(instance->tree);
fc_solve_kaz_tree_destroy(instance->tree);
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_GLIB_TREE)
g_tree_destroy(instance->tree);
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_JUDY)
{
Word_t rc_word;
JHSFA(rc_word, instance->judy_array);
}
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_GLIB_HASH)
g_hash_table_destroy(instance->hash);
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_INTERNAL_HASH)
fc_solve_hash_free(&(instance->hash));
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_GOOGLE_DENSE_HASH)
fc_solve_states_google_hash_free(instance->hash);
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_INDIRECT)
instance->num_prev_states_margin = 0;
instance->num_indirect_prev_states = 0;
free(instance->indirect_prev_states);
instance->indirect_prev_states = NULL;
#else
#error not defined
#endif
/* De-allocate the stack collection while free()'ing the stacks
in the process */
#ifdef INDIRECT_STACK_STATES
#if (FCS_STACK_STORAGE == FCS_STACK_STORAGE_INTERNAL_HASH)
fc_solve_hash_free(&(instance->stacks_hash));
#elif (FCS_STACK_STORAGE == FCS_STACK_STORAGE_LIBAVL2_TREE)
fcs_libavl2_stacks_tree_destroy(instance->stacks_tree, NULL);
#elif (FCS_STACK_STORAGE == FCS_STACK_STORAGE_LIBREDBLACK_TREE)
rbdestroy(instance->stacks_tree);
#elif (FCS_STACK_STORAGE == FCS_STACK_STORAGE_GLIB_TREE)
g_tree_destroy(instance->stacks_tree);
#elif (FCS_STACK_STORAGE == FCS_STACK_STORAGE_GLIB_HASH)
g_hash_table_destroy(instance->stacks_hash);
#elif (FCS_STACK_STORAGE == FCS_STACK_STORAGE_GOOGLE_DENSE_HASH)
fc_solve_columns_google_hash_free(instance->stacks_hash);
#elif (FCS_STACK_STORAGE == FCS_STACK_STORAGE_JUDY)
{
Word_t rc_word;
JHSFA(rc_word, instance->stacks_judy_array);
}
#else
#error FCS_STACK_STORAGE is not set to a good value.
#endif
#endif
instance->num_states_in_collection = 0;
#if (FCS_STATE_STORAGE == FCS_STATE_STORAGE_DB_FILE)
instance->db->close(instance->db,0);
#endif
#ifdef FCS_RCS_STATES
#if (FCS_RCS_CACHE_STORAGE == FCS_RCS_CACHE_STORAGE_JUDY)
{
Word_t Rc_word;
JLFA(Rc_word,
instance->rcs_states_cache.states_values_to_keys_map
);
}
#elif (FCS_RCS_CACHE_STORAGE == FCS_RCS_CACHE_STORAGE_KAZ_TREE)
fc_solve_kaz_tree_free_nodes(instance->rcs_states_cache.kaz_tree);
fc_solve_kaz_tree_destroy(instance->rcs_states_cache.kaz_tree);
#else
#error Unknown FCS_RCS_CACHE_STORAGE
#endif
fc_solve_compact_allocator_finish(
&(instance->rcs_states_cache.states_values_to_keys_allocator)
);
#endif
clean_soft_dfs(instance);
}
fc_solve_soft_thread_t * fc_solve_new_soft_thread(
fc_solve_hard_thread_t * const hard_thread
)
{
/* Make sure we are not exceeding the maximal number of soft threads
* for an instance. */
if (hard_thread->instance->next_soft_thread_id == MAX_NUM_SCANS)
{
return NULL;
}
hard_thread->soft_threads =
SREALLOC(hard_thread->soft_threads, hard_thread->num_soft_threads+1);
fc_solve_soft_thread_t * ret;
init_soft_thread(
hard_thread,
(ret = &(hard_thread->soft_threads[hard_thread->num_soft_threads++]))
);
return ret;
}
|