void fc_solve_compact_allocator_extend( fcs_compact_allocator_t * allocator ) { /* Allocate a new pack */ if ((++allocator->num_packs) == allocator->max_num_packs) { allocator->packs = (char * *)SREALLOC( allocator->packs, allocator->max_num_packs += IA_STATE_PACKS_GROW_BY ); allocator->packs[allocator->num_packs-1] = SMALLOC(allocator->packs[allocator->num_packs-1], ALLOCED_SIZE); for (int i = allocator->num_packs ; i < allocator->max_num_packs ; i++) { allocator->packs[i] = NULL; } } else { if (! allocator->packs[allocator->num_packs - 1]) { allocator->packs[allocator->num_packs - 1] = SMALLOC( allocator->packs[allocator->num_packs - 1], ALLOCED_SIZE ); } } allocator->max_ptr = (allocator->ptr = allocator->rollback_ptr = allocator->packs[allocator->num_packs - 1] ) + ALLOCED_SIZE; }
WINDOW * newwin(int num_lines, int num_cols, int begy, int begx) { WINDOW *win; char *sp; int i, by, bx, nl, nc; int j; by = begy; bx = begx; nl = num_lines; nc = num_cols; if (nl == 0) nl = LINES - by; if (nc == 0) nc = COLS - bx; if ((win = makenew(nl, nc, by, bx)) == NULL) return (ERR); if ((win->_firstch = SMALLOC(nl * sizeof (win->_firstch[0]))) == NULL) { free(win->_y); free(win); return (NULL); } if ((win->_lastch = SMALLOC(nl * sizeof (win->_lastch[0]))) == NULL) { free(win->_y); free(win->_firstch); free(win); return (NULL); } win->_nextp = win; for (i = 0; i < nl; i++) { win->_firstch[i] = _NOCHANGE; win->_lastch[i] = _NOCHANGE; } for (i = 0; i < nl; i++) if ((win->_y[i] = malloc(nc * sizeof (win->_y[0]))) == NULL) { for (j = 0; j < i; j++) free(win->_y[j]); free(win->_firstch); free(win->_lastch); free(win->_y); free(win); return (ERR); } else for (sp = win->_y[i]; sp < win->_y[i] + nc; ) *sp++ = ' '; win->_ch_off = 0; #ifdef DEBUG fprintf(outf, "NEWWIN: win->_ch_off = %d\n", win->_ch_off); #endif return (win); }
static char * get_the_positions_by_rank_data__ss_generator( fc_solve_soft_thread_t * const soft_thread, const fcs_state_t * const the_state ) { fc_solve_instance_t * const instance = HT_INSTANCE(soft_thread->hard_thread); SET_GAME_PARAMS(); #define FCS_SS_POS_BY_RANK_WIDTH (13+1) #define FCS_POS_BY_RANK_LEN ( FCS_SS_POS_BY_RANK_WIDTH * 4 ) #define FCS_POS_BY_RANK_SIZE (sizeof(positions_by_rank[0]) * FCS_POS_BY_RANK_LEN) #define FCS_POS_IDX(rank, suit) ( (suit)*FCS_SS_POS_BY_RANK_WIDTH + (rank) ) pos_by_rank_t * const positions_by_rank = SMALLOC(positions_by_rank, FCS_POS_BY_RANK_LEN); memset(positions_by_rank, -1, FCS_POS_BY_RANK_SIZE); for (int ds = 0 ; ds < LOCAL_STACKS_NUM ; ds++) { const fcs_const_cards_column_t dest_col = fcs_state_get_col(*the_state, ds); const int dest_cards_num = fcs_col_len(dest_col); for (int dc = 0 ; dc < dest_cards_num ; dc++) { const fcs_card_t card = fcs_col_get_card(dest_col, dc); const int suit = fcs_card_suit(card); const int rank = fcs_card_rank(card); const pos_by_rank_t pos = {.col = ds, .height = dc}; positions_by_rank[FCS_POS_IDX(rank, suit)] = pos; } } return (char *)positions_by_rank; }
corpus_type *read_corpus(corpusflags_type *flags, FILE *in) { sentence_type s; feature_type fmax = 0; int nread, i = 0, maxnparses = 0, nloserparses = 0; Float sum_g = 0; /* allocate feature counts */ read_parse_nfc_max = MIN_NFC; read_parse_fcp = MALLOC(read_parse_nfc_max*sizeof(fc_type)); assert(read_parse_fcp != NULL); /* allocate features w/ 1 count */ read_parse_nf_max = MIN_NF; read_parse_fp = MALLOC(read_parse_nf_max*sizeof(feature_type)); assert(read_parse_fp != NULL); corpus_type *c = SMALLOC(sizeof(corpus_type)); assert(c != NULL); size_type nsentences; nread = fscanf(in, " S = %d ", &nsentences); assert(nread != EOF); c->sentence = MALLOC(nsentences*sizeof(sentence_type)); assert(c->sentence != NULL); while (read_sentence(flags, in, &s, &fmax, &maxnparses) != EOF) { if (i >= nsentences) { nsentences *= 2; c->sentence = REALLOC(c->sentence, nsentences*sizeof(sentence_type)); assert(c->sentence != NULL); } assert(i < nsentences); /* skip sentences with no winners but some parses -- these are typically parse failures. */ if (s.Px == 0.0 && s.nparses != 0) continue; c->sentence[i++] = s; sum_g += s.g; if (s.Px > 0) nloserparses += s.nparses - 1; } c->nsentences = i; c->sentence = SREALLOC(c->sentence, nsentences*sizeof(sentence_type), c->nsentences*sizeof(sentence_type)); assert(c->sentence != NULL); c->nfeatures = fmax+1; c->maxnparses = maxnparses; c->nloserparses = nloserparses; if (flags && flags->Px_propto_g) for (i = 0; i < c->nsentences; ++i) /* normalize Px */ c->sentence[i].Px *= c->nsentences * c->sentence[i].g / sum_g; FREE(read_parse_fcp); FREE(read_parse_fp); return c; } /* read_corpus() */
__inline__ static void* blockalloc_copy(void *p, size_t n) { void *new_p = SMALLOC(n); if (new_p == NULL) return NULL; if (n != 0) memcpy(new_p, p, n); return new_p; }
__inline__ static void* blockalloc_realloc(void *p, size_t old_n_char, size_t new_n_char) { void *new_p = SMALLOC(new_n_char); size_t n_move = old_n_char < new_n_char ? old_n_char : new_n_char; if (new_p == NULL) return NULL; if (n_move != 0) memcpy(new_p, p, n_move); FREE(p); return new_p; }
// The char * returned is malloc()ed and should be free()ed. DLLEXPORT char *fc_solve_user_INTERNAL_delta_states_enc_and_dec( const fcs_dbm_variant_type local_variant GCC_UNUSED, const char *const init_state_s, const char *const derived_state_s) { fcs_state_keyval_pair init_state, derived_state, new_derived_state; fcs_uchar enc_state[24]; fcs_bit_reader bit_r; fcs_state_locs_struct locs; fc_solve_init_locs(&locs); DECLARE_IND_BUF_T(indirect_stacks_buffer) DECLARE_IND_BUF_T(derived_stacks_buffer) DECLARE_IND_BUF_T(new_derived_indirect_stacks_buffer) fc_solve_initial_user_state_to_c(init_state_s, &init_state, FREECELLS_NUM, STACKS_NUM, DECKS_NUM, indirect_stacks_buffer); fc_solve_initial_user_state_to_c(derived_state_s, &derived_state, FREECELLS_NUM, STACKS_NUM, DECKS_NUM, derived_stacks_buffer); fcs_delta_stater delta; fc_solve_delta_stater_init(&delta, local_variant, &(init_state.s), STACKS_NUM, FREECELLS_NUM PASS_ON_NOT_FC_ONLY(FCS_SEQ_BUILT_BY_ALTERNATE_COLOR)); fc_solve_delta_stater_set_derived(&delta, &(derived_state.s)); fc_solve_state_init( &new_derived_state, STACKS_NUM, new_derived_indirect_stacks_buffer); fc_solve_bit_writer bit_w; fc_solve_bit_writer_init(&bit_w, enc_state); fc_solve_delta_stater_encode_composite(&delta, &bit_w); fc_solve_bit_reader_init(&bit_r, enc_state); fc_solve_delta_stater_decode(&delta, &bit_r, &(new_derived_state.s)); char *new_derived_as_str = SMALLOC(new_derived_as_str, 1000); FCS__RENDER_STATE(new_derived_as_str, &(new_derived_state.s), &locs); fc_solve_delta_stater_release(&delta); return new_derived_as_str; }
corpus_type *read_corpus(corpusflags_type *flags, FILE *in, int nsentences) { sentence_type s; feature_type fmax = 0; int nread, i = 0, maxnparses = 0; Float sum_g = 0; corpus_type *c = SMALLOC(sizeof(corpus_type)); assert(c != NULL); nread = fscanf(in, " S = %d ", &nsentences); assert(nread != EOF); c->sentence = MALLOC(nsentences*sizeof(sentence_type)); assert(c->sentence != NULL); while (read_sentence(flags, in, &s, &fmax, &maxnparses) != EOF) { if (i >= nsentences) { nsentences *= 2; c->sentence = REALLOC(c->sentence, nsentences*sizeof(sentence_type)); assert(c->sentence != NULL); } assert(i < nsentences); c->sentence[i++] = s; sum_g += s.g; } c->nsentences = i; c->sentence = SREALLOC(c->sentence, nsentences*sizeof(sentence_type), c->nsentences*sizeof(sentence_type)); assert(c->sentence != NULL); c->nfeatures = fmax+1; c->maxnparses = maxnparses; if (flags->Px_propto_g) for (i = 0; i < c->nsentences; ++i) /* normalize Px */ c->sentence[i].Px *= c->nsentences * c->sentence[i].g / sum_g; return c; } /* read_corpus() */
/* * The char * returned is malloc()ed and should be free()ed. */ DLLEXPORT int fc_solve_user_INTERNAL_calc_derived_states_wrapper( enum fcs_dbm_variant_type_t local_variant, const char * init_state_str_proto, int * const num_out_derived_states, fcs_derived_state_debug_t * * out_derived_states, const fcs_bool_t perform_horne_prune ) { 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; fcs_derived_state_t * derived_list = NULL; fcs_derived_state_t * derived_list_recycle_bin = NULL; fcs_compact_allocator_t allocator; fcs_meta_compact_allocator_t meta_alloc; int states_count = 0; fcs_derived_state_t * iter; fcs_derived_state_debug_t * debug_ret; int idx = 0; 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_meta_compact_allocator_init (&meta_alloc); fc_solve_compact_allocator_init( &allocator, &meta_alloc); instance_solver_thread_calc_derived_states( local_variant, &init_state, NULL, &derived_list, &derived_list_recycle_bin, &allocator, perform_horne_prune ); iter = derived_list; while (iter) { states_count++; iter = iter->next; } *(num_out_derived_states) = states_count; debug_ret = SMALLOC(debug_ret, states_count); *(out_derived_states) = debug_ret; fc_solve_init_locs(&locs); iter = derived_list; while (iter) { debug_ret[idx].state_string = fc_solve_state_as_string( &(iter->state.s), &locs, FREECELLS_NUM, STACKS_NUM, DECKS_NUM, 1, 0, 1 ); debug_ret[idx].move = iter->move; debug_ret[idx].core_irreversible_moves_count = iter->core_irreversible_moves_count; debug_ret[idx].num_non_reversible_moves_including_prune = iter->num_non_reversible_moves_including_prune; /* TODO : Put something meaningful there by passing it to the function. */ debug_ret[idx].which_irreversible_moves_bitmask = iter->which_irreversible_moves_bitmask; idx++; iter = iter->next; } assert(idx == states_count); fc_solve_compact_allocator_finish(&allocator); fc_solve_meta_compact_allocator_finish( &meta_alloc ); fc_solve_delta_stater_free (delta); return 0; }
int read_sentence(corpusflags_type *flags, FILE *in, sentence_type *s, feature_type *fmax, int *maxnparses) { int i, nread, best_fscore_index = -1, nwinners = 0; DataFloat fscore, best_logprob = -DATAFLOAT_MAX, best_fscore = -1; nread = fscanf(in, " G = " DATAFLOAT_FORMAT " ", &s->g); if (nread == EOF) return EOF; assert(nread == 0 || nread == 1); if (nread == 0) s->g = 1; nread = fscanf(in, " N = %d", &s->nparses); if (nread == EOF) return EOF; assert(nread == 1); assert(s->nparses >= 0); if (s->nparses > *maxnparses) *maxnparses = s->nparses; if (s->nparses > 0) { s->parse = SMALLOC(s->nparses*sizeof(parse_type)); assert(s->parse != NULL); } else s->parse = NULL; for (i = 0; i < s->nparses; ++i) { read_parse(in, &s->parse[i], fmax); // handle the admittedly strange case where a parse has no brackets if (s->parse[i].w == 0.0 || s->parse[i].p == 0.0) { fscore = 0.0; } else { fscore = 2 * s->parse[i].w/(s->parse[i].p+s->g); } if (fscore+DATAFLOAT_EPS >= best_fscore) { Float logprob = feature_value(&s->parse[i], 0); // logprob is feature 0 if (fabs(fscore-best_fscore) < 2*DATAFLOAT_EPS) { ++nwinners; // tied best f-scores if (logprob > best_logprob) { // pick candidate with best logprob best_fscore = fscore; best_logprob = logprob; best_fscore_index = i; } } else { best_fscore = fscore; best_logprob = logprob; best_fscore_index = i; nwinners = 1; } } } // skip sentences where the best f-score was 0 since there's nothing to learn from them if (best_fscore == 0.0) { best_fscore = -1; best_logprob = -DATAFLOAT_MAX; best_fscore_index = -1; nwinners = 0; } if (best_fscore_index >= 0) { /* is there a winner? */ Float sum_Pyx = 0; assert(nwinners > 0); assert(best_fscore_index < s->nparses); s->Px = 1.0; /* indicate that there is a winner */ s->correct_index = best_fscore_index; if (flags && flags->Pyx_factor > 1) { Float Z = 0; for (i = 0; i < s->nparses; ++i) { fscore = 2 * s->parse[i].w/(s->parse[i].p+s->g); Z += pow(flags->Pyx_factor, fscore - best_fscore); } for (i = 0; i < s->nparses; ++i) { fscore = 2 * s->parse[i].w/(s->parse[i].p+s->g); sum_Pyx += s->parse[i].Pyx = pow(flags->Pyx_factor, fscore - best_fscore) / Z; } } else if (flags && flags->Pyx_factor > 0) { /* Pyx_factor == 1; all winners get equal Pyx */ for (i = 0; i < s->nparses; ++i) { Float fscore = 2 * s->parse[i].w/(s->parse[i].p+s->g); if (fabs(best_fscore-fscore) < 2*DATAFLOAT_EPS) sum_Pyx += s->parse[i].Pyx = 1.0/nwinners; else sum_Pyx += s->parse[i].Pyx = 0; } } else { /* Pyx_factor == 0; f-score winner gets Pyx = 1, all others 0 */ for (i = 0; i < s->nparses; ++i) sum_Pyx += s->parse[i].Pyx = (i == best_fscore_index); } assert(fabs(sum_Pyx - 1) <= DATAFLOAT_EPS); } else { /* no winner, set Px to 0.0 to indicate this */ s->Px = 0.0; s->correct_index = s->nparses; for (i = 0; i < s->nparses; ++i) s->parse[i].Pyx = 0.0; /* We used to ensure that s->nparses == 0 here, but instead do nothing since read_corpus now knows to skip these sentences. These sentences show up when we have parse failures and the parser returns a flat bracketing. */ } return s->nparses; } /* read_sentence() */
int main(void) { TYPE max_value = 0; TYPE random_number = 0; TYPE flag_can_repeat = 0; TYPE index, k; /// get max value /// to use "%" operater with rand(), max value should less than RAND_MAX printf("MAX VALUE of the random number: %u\n", (unsigned)RAND_MAX - 1); printf("Please input the max value(excluded) of the random number: "); if(scanf("%u", &max_value) != 1) { DEBUG_PRINT_STATE; DEBUG_PRINT_STRING("can not get the right max value(excluded).\n"); DEBUG_PRINT_VALUE("%u", max_value); fflush(stdout); assert(0); exit(EXIT_FAILURE); } else if(max_value > (RAND_MAX - 1)) { DEBUG_PRINT_STATE; DEBUG_PRINT_STRING("value exceeds the limit max value.\n"); DEBUG_PRINT_VALUE("%u", max_value); fflush(stdout); assert(0); exit(EXIT_FAILURE); } /// get count printf("Please input the count of the random number: "); if(scanf("%u", &random_number) != 1) { DEBUG_PRINT_STATE; DEBUG_PRINT_STRING("can not get the right count.\n"); DEBUG_PRINT_VALUE("%u", random_number); fflush(stdout); assert(0); exit(EXIT_FAILURE); } else if(random_number > max_value) { DEBUG_PRINT_STATE; DEBUG_PRINT_STRING("count exceeds the max value.\n"); DEBUG_PRINT_VALUE("%u", max_value); fflush(stdout); assert(0); exit(EXIT_FAILURE); } printf("Please choose if data can be same in them(input 1(YES)/0(NO):"); if(scanf("%u", &flag_can_repeat) != 1) { DEBUG_PRINT_STATE; DEBUG_PRINT_STRING("can not get the choice.\n"); DEBUG_PRINT_VALUE("%u", flag_can_repeat); fflush(stdout); assert(0); exit(EXIT_FAILURE); } else if(flag_can_repeat != 0 && flag_can_repeat != 1) { DEBUG_PRINT_STATE; DEBUG_PRINT_STRING("can not get right choice(1 or 2).\n"); DEBUG_PRINT_VALUE("%u", flag_can_repeat); fflush(stdout); assert(0); exit(EXIT_FAILURE); } /// generate random number TYPE* list = SMALLOC(random_number, TYPE); srand(time(NULL)); printf("start generating random numbers...........\n"); if(!flag_can_repeat) { for(index = 0; index < random_number; index++) { TYPE flag = 1; while(flag) { list[index] = rand() % max_value; flag = 0; for(k = 0; k < index; k++) { if(list[k] == list[index]) { flag = 1; break; } } } } } else { for(index = 0; index < random_number; index++) { list[index] = rand() % max_value; } } /// write data to file FILE* fp = fopen("random_data.txt", "w"); if(fp == NULL) { perror("random_data.txt"); DEBUG_PRINT_STATE; fflush(stdout); assert(0); exit(EXIT_FAILURE); } for(index = 0; index < random_number; index++) { fprintf(fp, "%u\n", list[index]); } if(fclose(fp) != 0) { perror("random_data.txt"); DEBUG_PRINT_STATE; fflush(stdout); assert(0); exit(EXIT_FAILURE); } printf("random numbers saved to random_data.txt!\n"); /// free memory SFREE(&list); /// return return EXIT_SUCCESS; }
/* * The char * returned is malloc()ed and should be free()ed. */ DLLEXPORT int fc_solve_user_INTERNAL_calc_derived_states_wrapper( fcs_dbm_variant_type_t local_variant, const char *init_state_str_proto, int *const num_out_derived_states, fcs_derived_state_debug_t **out_derived_states, const fcs_bool_t perform_horne_prune) { fcs_state_keyval_pair_t init_state; fcs_encoded_state_buffer_t enc_state; fcs_state_locs_struct_t locs; fcs_derived_state_t *derived_list = NULL; fcs_derived_state_t *derived_list_recycle_bin = NULL; fcs_compact_allocator_t allocator; fcs_meta_compact_allocator_t meta_alloc; size_t states_count = 0; fcs_derived_state_t *iter; 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); fc_solve_delta_stater_t delta; fc_solve_delta_stater_init( &delta, &(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_meta_compact_allocator_init(&meta_alloc); fc_solve_compact_allocator_init(&allocator, &meta_alloc); instance_solver_thread_calc_derived_states(local_variant, &init_state, NULL, &derived_list, &derived_list_recycle_bin, &allocator, perform_horne_prune); iter = derived_list; while (iter) { states_count++; iter = iter->next; } *(num_out_derived_states) = states_count; fcs_derived_state_debug_t *const debug_ret = SMALLOC(debug_ret, states_count); *(out_derived_states) = debug_ret; fc_solve_init_locs(&locs); iter = derived_list; size_t idx = 0; while (iter) { debug_ret[idx] = (typeof(debug_ret[idx])){ .state_string = SMALLOC(debug_ret[idx].state_string, 1000), .move = iter->move, .core_irreversible_moves_count = iter->core_irreversible_moves_count, .num_non_reversible_moves_including_prune = iter->num_non_reversible_moves_including_prune, .which_irreversible_moves_bitmask = iter->which_irreversible_moves_bitmask}; FCS__RENDER_STATE(debug_ret[idx].state_string, &(iter->state.s), &locs); /* TODO : Put something meaningful there by passing it to the function. */ idx++; iter = iter->next; } assert(idx == states_count); fc_solve_compact_allocator_finish(&allocator); fc_solve_meta_compact_allocator_finish(&meta_alloc); fc_solve_delta_stater_release(&delta); return 0; } /* * The char * returned is malloc()ed and should be free()ed. */ DLLEXPORT int fc_solve_user_INTERNAL_perform_horne_prune( fcs_dbm_variant_type_t local_variant, const char *init_state_str_proto, char **ret_state_s) { fcs_state_keyval_pair_t init_state; fcs_state_locs_struct_t locs; int prune_ret; DECLARE_IND_BUF_T(indirect_stacks_buffer) fc_solve_init_locs(&locs); fc_solve_initial_user_state_to_c(init_state_str_proto, &init_state, FREECELLS_NUM, STACKS_NUM, DECKS_NUM, indirect_stacks_buffer); prune_ret = horne_prune__simple(local_variant, &init_state); *ret_state_s = SMALLOC(*ret_state_s, 1000); FCS__RENDER_STATE(*ret_state_s, &(init_state.s), &locs); return prune_ret; }
int read_sentence(corpusflags_type *flags, FILE *in, sentence_type *s, feature_type *fmax, int *maxnparses) { int i, nread, best_fscore_index = -1, nwinners = 0; DataFloat fscore, best_logprob = -DATAFLOAT_MAX, best_fscore = -1; nread = fscanf(in, " G = " DATAFLOAT_FORMAT " ", &s->g); if (nread == EOF) return EOF; assert(nread == 0 || nread == 1); if (nread == 0) s->g = 1; nread = fscanf(in, " N = %u", &s->nparses); if (nread == EOF) return EOF; assert(nread == 1); assert(s->nparses >= 0); if (s->nparses > *maxnparses) *maxnparses = s->nparses; if (s->nparses > 0) { s->parse = SMALLOC(s->nparses*sizeof(parse_type)); assert(s->parse != NULL); } else s->parse = NULL; for (i = 0; i < s->nparses; ++i) { read_parse(in, &s->parse[i], fmax); fscore = 2 * s->parse[i].w/(s->parse[i].p+s->g); if (fscore+DATAFLOAT_EPS >= best_fscore) { Float logprob = feature_value(&s->parse[i], 0); // logprob is feature 0 if (fabs(fscore-best_fscore) < 2*DATAFLOAT_EPS) { ++nwinners; // tied best f-scores if (logprob > best_logprob) { // pick candidate with best logprob best_fscore = fscore; best_logprob = logprob; best_fscore_index = i; } } else { best_fscore = fscore; best_logprob = logprob; best_fscore_index = i; nwinners = 1; } } } if (best_fscore_index >= 0) { /* is there a winner? */ Float sum_Pyx = 0; assert(nwinners > 0); s->Px = 1.0; /* indicate that there is a winner */ if (flags->Pyx_factor > 1) { Float Z = 0; for (i = 0; i < s->nparses; ++i) { fscore = 2 * s->parse[i].w/(s->parse[i].p+s->g); Z += pow(flags->Pyx_factor, fscore - best_fscore); } for (i = 0; i < s->nparses; ++i) { fscore = 2 * s->parse[i].w/(s->parse[i].p+s->g); sum_Pyx += s->parse[i].Pyx = pow(flags->Pyx_factor, fscore - best_fscore) / Z; } } else if (flags->Pyx_factor > 0) { /* Pyx_factor == 1; all winners get equal Pyx */ for (i = 0; i < s->nparses; ++i) { Float fscore = 2 * s->parse[i].w/(s->parse[i].p+s->g); if (fabs(best_fscore-fscore) < 2*DATAFLOAT_EPS) sum_Pyx += s->parse[i].Pyx = 1.0/nwinners; else sum_Pyx += s->parse[i].Pyx = 0; } } else { /* Pyx_factor == 0; f-score winner gets Pyx = 1, all others 0 */ for (i = 0; i < s->nparses; ++i) sum_Pyx += s->parse[i].Pyx = (i == best_fscore_index); } assert(fabs(sum_Pyx - 1) <= DATAFLOAT_EPS); } else { /* no winner, set Px to 0.0 to indicate this */ s->Px = 0.0; for (i = 0; i < s->nparses; ++i) s->parse[i].Pyx = 0.0; } return s->nparses; } /* read_sentence() */
/* * 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; }