38
38
#include "preset.h"
39
39
#include "fcs_user.h"
40
40
#include "move_funcs_order.h"
41
#include "fcs_user_internal.h"
42
43
#include "unused.h"
45
#include "indirect_buffer.h"
49
int num_states_in_collection;
45
52
/* A flare is an alternative scan algorithm to be tried. All flares in
46
53
* a single instance are being evaluated and then one picks the shortest
113
120
* The number of iterations this board started at.
115
int iterations_board_started_at;
122
fcs_stats_t iterations_board_started_at;
117
124
* The number of iterations that the current instance started solving from.
126
fcs_stats_t init_num_times;
121
128
* A pointer to the currently active instance out of the sequence
123
130
fc_solve_instance_t * fc_solve_obj;
124
131
fcs_state_keyval_pair_t state;
125
132
fcs_state_keyval_pair_t running_state;
126
#ifdef FCS_WITHOUT_LOCS_FIELDS
127
133
fcs_state_locs_struct_t state_locs;
128
134
fcs_state_locs_struct_t trace_solution_state_locs;
129
135
fcs_state_locs_struct_t initial_state_locs;
132
137
fcs_bool_t all_instances_were_suspended;
133
138
int state_validity_ret;
138
143
fc_solve_soft_thread_t * soft_thread;
140
#ifdef INDIRECT_STACK_STATES
141
char indirect_stacks_buffer[MAX_NUM_STACKS << 7];
145
DECLARE_IND_BUF_T(indirect_stacks_buffer)
143
146
char * state_string_copy;
145
148
#ifndef FCS_FREECELL_ONLY
157
160
void * lp_instance GCC_UNUSED,
158
#ifdef FCS_RCS_STATES
159
fcs_state_t * ptr_state_key,
161
fcs_collectible_state_t * ptr_state_val,
161
fcs_kv_state_t * ptr_state,
162
162
int parent_iter_num
212
212
user->current_iterations_limit = -1;
214
214
user->state_string_copy = NULL;
215
user->iterations_board_started_at = 0;
215
user->iterations_board_started_at.num_times = 0;
216
user->iterations_board_started_at.num_states_in_collection = 0;
216
217
user->all_instances_were_suspended = TRUE;
218
219
user->error_string = NULL;
386
int further_depth_idx;
387
for (further_depth_idx = depth_idx+1; further_depth_idx < user->soft_thread->by_depth_tests_order.num ; further_depth_idx++)
389
free(user->soft_thread->by_depth_tests_order.by_depth_tests[further_depth_idx].tests_order.tests);
390
user->soft_thread->by_depth_tests_order.by_depth_tests[further_depth_idx].tests_order.tests = NULL;
387
394
user->soft_thread->by_depth_tests_order.by_depth_tests =
389
396
user->soft_thread->by_depth_tests_order.by_depth_tests,
766
773
* not get initialized again, and now the num_times of the instance
769
user->init_num_times = 0;
776
user->init_num_times.num_times = 0;
777
user->init_num_times.num_states_in_collection = 0;
771
779
flare->ret_code = FCS_STATE_NOT_BEGAN_YET;
876
884
if (flare->ret_code == FCS_STATE_NOT_BEGAN_YET)
887
fcs_kv_state_t state_pass;
880
889
#if (!(defined(HARD_CODED_NUM_FREECELLS) && defined(HARD_CODED_NUM_STACKS) && defined(HARD_CODED_NUM_DECKS)))
881
890
fc_solve_instance_t * instance = user->fc_solve_obj;
887
896
INSTANCE_FREECELLS_NUM,
888
897
INSTANCE_STACKS_NUM,
890
#ifdef FCS_WITH_TALONS
891
,user->fc_solve_obj->talon_type
893
#ifdef INDIRECT_STACK_STATES
894
,user->indirect_stacks_buffer
899
user->indirect_stacks_buffer
898
902
if (status != FCS_USER_STATE_TO_C__SUCCESS)
918
919
return user->ret_code;
921
#ifdef FCS_WITHOUT_LOCS_FIELDS
924
for ( i=0 ; i<MAX_NUM_STACKS ; i++)
926
user->initial_state_locs.stack_locs[i] = (fcs_locs_t)i;
928
for ( i=0 ; i<MAX_NUM_FREECELLS ; i++)
930
user->initial_state_locs.fc_locs[i] = (fcs_locs_t)i;
922
fc_solve_init_locs(&(user->initial_state_locs));
933
923
user->state_locs = user->initial_state_locs;
936
/* running_state is a normalized state. So I'm duplicating
937
* state to it before state is canonized
925
state_pass.key = &(user->state.s);
926
state_pass.val = &(user->state.info);
927
/* running_state is a normalized state. So We're duplicating
928
* state to it before state state_pass is canonized
940
#ifdef FCS_RCS_STATES
941
&(user->running_state.s),
942
&(user->running_state.info),
946
&(user->running_state),
951
#ifdef FCS_WITHOUT_LOCS_FIELDS
954
#ifdef FCS_WITHOUT_LOCS_FIELDS
932
pass.key = &(user->running_state.s);
933
pass.val = &(user->running_state.info);
935
fcs_duplicate_kv_state(&pass, &state_pass);
955
938
fc_solve_canonize_state_with_locs
957
fc_solve_canonize_state
960
#ifdef FCS_RCS_STATES
966
#ifdef FCS_WITHOUT_LOCS_FIELDS
967
941
&(user->state_locs),
969
942
INSTANCE_FREECELLS_NUM,
970
943
INSTANCE_STACKS_NUM
973
#ifdef FCS_WITHOUT_LOCS_FIELDS
974
946
user->trace_solution_state_locs = user->state_locs;
977
948
fc_solve_init_instance(user->fc_solve_obj);
982
953
#define parameterized_fixed_limit(increment) \
983
(user->iterations_board_started_at + increment)
954
(user->iterations_board_started_at.num_times + increment)
984
955
#define parameterized_limit(increment) (((increment) < 0) ? (-1) : parameterized_fixed_limit(increment))
985
956
#define local_limit() \
986
957
(instance_item->limit)
1018
989
user->fc_solve_obj->max_num_times =
1019
990
user->fc_solve_obj->effective_max_num_times =
1020
(user->fc_solve_obj->num_times + mymin - user->iterations_board_started_at);
991
(user->fc_solve_obj->num_times + mymin - user->iterations_board_started_at.num_times);
1024
user->init_num_times = init_num_times = user->fc_solve_obj->num_times;
995
user->init_num_times.num_times = init_num_times.num_times = user->fc_solve_obj->num_times;
996
user->init_num_times.num_states_in_collection = init_num_times.num_states_in_collection = user->fc_solve_obj->num_states_in_collection;
1026
998
if (solve_start)
1038
1010
user->all_instances_were_suspended = FALSE;
1041
user->iterations_board_started_at += user->fc_solve_obj->num_times - init_num_times;
1042
user->init_num_times = user->fc_solve_obj->num_times;
1013
user->iterations_board_started_at.num_times += user->fc_solve_obj->num_times - init_num_times.num_times;
1014
user->iterations_board_started_at.num_states_in_collection += user->fc_solve_obj->num_states_in_collection - init_num_times.num_states_in_collection;
1015
user->init_num_times.num_times = user->fc_solve_obj->num_times;
1016
user->init_num_times.num_states_in_collection = user->fc_solve_obj->num_states_in_collection;
1044
1018
if (user->ret_code == FCS_STATE_WAS_SOLVED)
1020
fcs_kv_state_t pass;
1046
1022
#if (!(defined(HARD_CODED_NUM_FREECELLS) && defined(HARD_CODED_NUM_STACKS) && defined(HARD_CODED_NUM_DECKS)))
1047
1023
fc_solve_instance_t * instance =
1048
1024
user->fc_solve_obj;
1027
pass.key = &(user->state.s);
1028
pass.val = &(user->state.info);
1051
1030
* TODO : maybe only normalize the final moves' stack in
1052
1031
* order to speed things up.
1054
1033
fc_solve_move_stack_normalize(
1055
1034
&(user->fc_solve_obj->solution_moves),
1056
#ifdef FCS_RCS_STATES
1058
&(user->state.info),
1062
#ifdef FCS_WITHOUT_LOCS_FIELDS
1063
1036
&(user->trace_solution_state_locs),
1065
1037
INSTANCE_FREECELLS_NUM,
1066
1038
INSTANCE_STACKS_NUM,
1067
1039
INSTANCE_DECKS_NUM
1070
#ifdef FCS_WITHOUT_LOCS_FIELDS
1071
1042
user->trace_solution_state_locs = user->state_locs;
1074
1044
if (instance_item->minimal_solution_flare_idx < 0)
1092
1062
* and return now.
1094
1064
if (((user->current_iterations_limit >= 0) &&
1095
(user->iterations_board_started_at >=
1065
(user->iterations_board_started_at.num_times >=
1096
1066
user->current_iterations_limit)) ||
1097
1067
(user->fc_solve_obj->num_states_in_collection >=
1098
1068
user->fc_solve_obj->effective_max_num_states_in_collection)
1072
* We need to resume from the last flare in case we exceed
1073
* the board iterations limit.
1075
instance_item->current_plan_item_idx--;
1143
fcs_kv_state_t pass;
1144
pass.key = &(user->running_state.s);
1145
pass.val = &(user->running_state.info);
1168
1147
fc_solve_apply_move(
1169
#ifdef FCS_RCS_STATES
1170
&(user->running_state.s),
1171
&(user->running_state.info),
1173
&(user->running_state),
1175
#ifdef FCS_WITHOUT_LOCS_FIELDS
1178
1150
#ifdef FCS_USE_COMPACT_MOVE_TOKENS
1214
1186
fc_solve_state_as_string(
1215
#ifdef FCS_RCS_STATES
1216
1187
&(user->running_state.s),
1217
1188
&(user->running_state.info),
1219
&(user->running_state),
1221
#ifdef FCS_WITHOUT_LOCS_FIELDS
1222
1189
&(user->initial_state_locs),
1224
1190
INSTANCE_FREECELLS_NUM,
1225
1191
INSTANCE_STACKS_NUM,
1226
1192
INSTANCE_DECKS_NUM,
1333
1299
switch (method)
1301
case FCS_METHOD_RANDOM_DFS:
1302
case FCS_METHOD_SOFT_DFS:
1303
case FCS_METHOD_HARD_DFS:
1305
user->soft_thread->method_specific.soft_dfs.dfs_max_depth
1306
= user->soft_thread->method_specific.soft_dfs.depth
1307
= user->soft_thread->method_specific.soft_dfs.tests_by_depth_array.num_units
1309
user->soft_thread->method_specific.soft_dfs.rand_seed = 24;
1310
user->soft_thread->method_specific.soft_dfs.soft_dfs_info = NULL;
1311
user->soft_thread->method_specific.soft_dfs.tests_by_depth_array.by_depth_units = NULL;
1335
1314
case FCS_METHOD_A_STAR:
1337
1316
double * my_befs_weights;
1522
1501
user = (fcs_user_t *)api_instance;
1524
return user->iterations_board_started_at + user->fc_solve_obj->num_times - user->init_num_times;
1503
return user->iterations_board_started_at.num_times + user->fc_solve_obj->num_times - user->init_num_times.num_times;
1527
1506
int DLLEXPORT freecell_solver_user_get_limit_iterations(void * api_instance)
1736
1715
user = (fcs_user_t *)api_instance;
1738
1717
#define my_befs_weights soft_thread->method_specific.befs.meth.befs.befs_weights
1739
if ((index < 0) || (index >= (sizeof(user->my_befs_weights)/sizeof(user->my_befs_weights[0]))))
1718
if ((my_index < 0) || (my_index >= (sizeof(user->my_befs_weights)/sizeof(user->my_befs_weights[0]))))
1776
1755
void * lp_instance GCC_UNUSED,
1777
#ifdef FCS_RCS_STATES
1778
fcs_state_t * ptr_state_key,
1780
fcs_collectible_state_t * ptr_state_val,
1756
fcs_kv_state_t * ptr_state,
1781
1757
int parent_iter_num
1787
1763
user = (fcs_user_t *)api_instance;
1789
state_raw.s = ptr_state_val;
1790
#ifdef FCS_RCS_STATES
1791
state_raw.key = ptr_state_key;
1792
state_raw.val = ptr_state_val;
1794
#ifdef FCS_WITHOUT_LOCS_FIELDS
1797
for ( i=0 ; i<MAX_NUM_STACKS ; i++)
1799
state_raw.locs.stack_locs[i] = (fcs_locs_t)i;
1801
for ( i=0 ; i<MAX_NUM_FREECELLS ; i++)
1803
state_raw.locs.fc_locs[i] = (fcs_locs_t)i;
1810
fc_solve_instance_t * instance = user->instance;
1811
HT_LOOP_DECLARE_VARS();
1815
ST_LOOP_DECLARE_VARS();
1819
if (!strcmp(soft_thread->name, "11"))
1822
soft_thread->method_specific.befs
1823
.meth.befs.befs_weights
1826
printf("BeFS-Weights[\"11\"]=(%f,%f,%f,%f,%f)\n",
1827
w[0], w[1], w[2], w[3], w[4]
1765
state_raw.key = ptr_state->key;
1766
state_raw.val = ptr_state->val;
1767
fc_solve_init_locs(&(state_raw.locs));
1838
1769
user->iter_handler(
1773
(void *)(&state_raw),
1843
1774
parent_iter_num,
1844
1775
user->iter_handler_context
1850
void set_debug_iter_output_func_to_val(
1781
static void set_debug_iter_output_func_to_val(
1851
1782
fcs_user_t * user,
1853
void * api_instance,
1856
void * lp_instance GCC_UNUSED,
1857
#ifdef FCS_RCS_STATES
1858
fcs_state_t * ptr_state_key,
1860
fcs_collectible_state_t * ptr_state_val,
1783
fcs_instance_debug_iter_output_func_t value
1864
1786
FLARES_LOOP_DECLARE_VARS();
1865
1787
FLARES_LOOP_START()
1919
1841
fc_solve_state_as_string(
1920
#ifdef FCS_RCS_STATES
1921
1842
((fcs_standalone_state_ptrs_t *)ptr_state_void)->key,
1922
1843
((fcs_standalone_state_ptrs_t *)ptr_state_void)->val,
1924
((fcs_standalone_state_ptrs_t *)ptr_state_void)->s,
1926
#ifdef FCS_WITHOUT_LOCS_FIELDS
1927
1844
&(((fcs_standalone_state_ptrs_t *)ptr_state_void)->locs),
1929
1845
INSTANCE_FREECELLS_NUM,
1930
1846
INSTANCE_STACKS_NUM,
1931
1847
INSTANCE_DECKS_NUM,
1957
1873
user = (fcs_user_t *)api_instance;
1959
return user->fc_solve_obj->num_states_in_collection;
1875
return user->iterations_board_started_at.num_states_in_collection + user->fc_solve_obj->num_states_in_collection - user->init_num_times.num_states_in_collection;
1962
1878
void DLLEXPORT freecell_solver_user_limit_num_states_in_collection(
2191
2107
recycle_instance(user, i);
2110
* Removing because we are still interested to keep the current iterations
2193
2114
user->current_iterations_limit = -1;
2194
user->iterations_board_started_at = 0;
2116
user->iterations_board_started_at.num_times = 0;
2117
user->iterations_board_started_at.num_states_in_collection = 0;
2195
2118
if (user->state_string_copy != NULL)
2197
2120
free(user->state_string_copy);