int expand_A_first_node( int h ) { int h_, i; State S_; h_ = get_1P_and_AH( lsearch_space[lspace_start].S ); if ( h_ == INFINITY ) { lspace_start++; return h_; } if ( h_ < h ) { return h_; } for ( i = 0; i < gnum_A; i++ ) { result_to_dest( &S_, lsearch_space[lspace_start].S, gA[i] ); add_to_search_space( S_, gA[i], lspace_start ); } lspace_start++; return h_; }
void forward_propagation (int from_level, int to_level) { int i; for (i = from_level; i < to_level; i++) result_to_dest (&(gplan_states[i + 1]), &(gplan_states[i]), gplan_ops[i]); }
Bool search_A_for_better_state( State S, int h, State *S_, int *h_ ) { int i, h__; State S__; lspace_start = 0; lspace_end = 0; lspace_size = 0; hash_state( S ); for ( i = 0; i < gnum_A; i++ ) { result_to_dest( &S__, S, gA[i] ); add_to_search_space( S__, gA[i], -1 ); } while ( TRUE ) { if ( lspace_start == lspace_end ) { return FALSE; } h__ = expand_A_first_node( h ); if ( LESS( h__, h ) ) { break; } } reset_hash_entrys(); extract_plan_fragment(); source_to_dest( S_, lsearch_space[lspace_start].S ); *h_ = h__; if ( lsearch_space[lspace_start].depth > gmax_search_depth ) { gmax_search_depth = lsearch_space[lspace_start].depth; } if ( lspace_size > gmax_search_size ) { gmax_search_size = lspace_size; } return TRUE; }
int expand_first_node( int h ) { static Bool fc = TRUE; static State S_; int h_, i, g; if ( fc ) { make_state( &S_, gnum_ft_conn ); S_.max_F = gnum_ft_conn; fc = FALSE; } h_ = get_1P_and_H( &(lehc_current_start->S), &lcurrent_goals ); if ( h_ == INFINITY ) { lehc_current_start = lehc_current_start->next; return h_; } if ( lehc_current_start->new_goal != -1 && new_goal_gets_deleted( lehc_current_start ) ) { lehc_current_start = lehc_current_start->next; return INFINITY; } if ( h_ < h ) { return h_; } for ( i = 0; i < gnum_H; i++ ) { g = result_to_dest( &S_, &(lehc_current_start->S), gH[i] ); add_to_ehc_space( &S_, gH[i], lehc_current_start, g ); } lehc_current_start = lehc_current_start->next; return h_; }
void extract_plan_fragment( void ) { int i, seen; int ops[MAX_PLAN_LENGTH], num_ops; num_ops = 0; for ( i = lspace_start; i != -1; i = lsearch_space[i].father ) { if ( num_ops == MAX_PLAN_LENGTH ) { printf("\nincrease MAX_PLAN_LENGTH! currently %d\n\n", MAX_PLAN_LENGTH); exit( 1 ); } ops[num_ops++] = lsearch_space[i].op; } for ( i = num_ops - 1; i > -1; i-- ) { /* SCHNELLERE METHODE, hashing oder UBTree verwenden ! */ if ( gnum_plan_ops == MAX_PLAN_LENGTH ) { printf("\nincrease MAX_PLAN_LENGTH! currently %d\n\n", MAX_PLAN_LENGTH); exit( 1 ); } result_to_dest( &(gplan_states[gnum_plan_ops + 1]), gplan_states[gnum_plan_ops], ops[i] ); gplan_states_sum[gnum_plan_ops + 1] = state_sum( gplan_states[gnum_plan_ops + 1] ); if ( (seen = state_seen( gnum_plan_ops + 1)) != -1 ) { gnum_plan_ops = seen; printf("\ngoing back to %d", seen); continue; } gplan_ops[gnum_plan_ops++] = ops[i]; } }
int expand_first_node( int h ) { static Bool fc = TRUE; static State S_; int h_, i; if ( fc ) { make_state( &S_, gnum_ft_conn ); fc = FALSE; } h_ = get_1P_and_H( &(lehc_current_start->S), &lscurrent_goals, lehc_current_start->father, NULL, lehc_current_start->op ); if ( h_ == INFINITY ) { lehc_current_start = lehc_current_start->next; return h_; } if ( h_ < h ) { return h_; } for ( i = 0; i < gnum_H; i++ ) { if ( !result_to_dest( &S_, lehc_current_start, NULL, gH[i] ) ) continue; add_to_ehc_space( &S_, gH[i], lehc_current_start ); } lehc_current_start = lehc_current_start->next; return h_; }
/************************************* * Jovi: update for multiple purpose * * BEST FIRST SEARCH IMPLEMENTATION * *************************************/ Bool do_best_first_search_for_multiple_purpose ( void ) { static Bool fc_for_multiple_purpose = TRUE; /*first round*/ static State S; BfsNode *first; int i, min = INFINITY; Bool start = TRUE; if ( fc_for_multiple_purpose ) { make_state( &S, gnum_ft_conn ); S.max_F = gnum_ft_conn; fc_for_multiple_purpose = FALSE; } lbfs_space_head = new_BfsNode(); lbfs_space_had = NULL; for ( i = 0; i < BFS_HASH_SIZE; i++ ) { lbfs_hash_entry[i] = NULL; } add_to_bfs_space_for_multiple_purpose( &ginitial_state, -1, NULL ); while ( TRUE ) { if ( (first = lbfs_space_head->next) == NULL ) { printf("\n\nbest first search space empty! problem proven unsolvable.\n\n"); return FALSE; } lbfs_space_head->next = first->next; if ( first->next ) { first->next->prev = lbfs_space_head; } if ( LESS( first->h, min ) ) { min = first->h; if ( start ) { printf("\nadvancing to distance : %4d", min); start = FALSE; } else { printf("\n %4d", min); } /* * modified by JC: return ealier */ if(is_solved_state( &first->S )){ printf("\nstate have seen. return!\n"); break; } } if ( first->h == 0 ) { break; } get_A( &(first->S) ); for ( i = 0; i < gnum_A; i++ ) { /*if(g_is_strong){*/ /*JC: dismiss invalid actions*/ int k; Bool found = FALSE; for(k = 0; k < gnum_IV; k++){ if(gA[i] == gInvActs[k].act){ found = TRUE; break; } } /*}*/ if(found) continue; result_to_dest( &S, &(first->S), gA[i] ); add_to_bfs_space_for_multiple_purpose( &S, gA[i], first ); } first->next = lbfs_space_had; lbfs_space_had = first; } extract_plan( first ); return TRUE; }
/* breath first search */ int expand_first_node( int h ) { static Bool fc = TRUE; static State S_; int h_, i, g; if ( fc ) { make_state( &S_, gnum_ft_conn ); S_.max_F = gnum_ft_conn; fc = FALSE; } h_ = get_1P_and_H( &(lehc_current_start->S), &lcurrent_goals ); if ( h_ == INFINITY ) { lehc_current_start = lehc_current_start->next; return h_; } if ( lehc_current_start->new_goal != -1 && new_goal_gets_deleted( lehc_current_start ) ) { lehc_current_start = lehc_current_start->next; return INFINITY; } if ( h_ < h ) { return h_; } for ( i = 0; i < gnum_H; i++ ) { /*JC: dismiss invalid actions*/ int k; Bool found = FALSE; for(k = 0; k < gnum_IV; k++){ if(same_state(&gInvActs[k].state, &(lehc_current_start->S)) && gH[i] == gInvActs[k].act){ found = TRUE; break; } } if(found) continue; if(is_D_action(gop_conn[gH[i]].action->name)) continue; g = result_to_dest( &S_, &(lehc_current_start->S), gH[i] ); add_to_ehc_space( &S_, gH[i], lehc_current_start, g ); } for ( i = 0; i < gnum_H; i++ ) { /*JC: dismiss invalid actions*/ int k; Bool found = FALSE; for(k = 0; k < gnum_IV; k++){ if(same_state(&gInvActs[k].state, &(lehc_current_start->S)) && gH[i] == gInvActs[k].act){ found = TRUE; break; } } if(found) continue; if(!is_D_action(gop_conn[gH[i]].action->name)) continue; g = result_to_dest( &S_, &(lehc_current_start->S), gH[i] ); add_to_ehc_space( &S_, gH[i], lehc_current_start, g ); } lehc_current_start = lehc_current_start->next; return h_; }
/************************************************* * FUNCTIONS FOR BREADTH FIRST SEARCH IN H SPACE * *************************************************/ Bool search_for_better_state_for_multiple_purpose ( State *S, int h, State *S_, int *h_ ) { static Bool first_call_for_multiple_purpose = TRUE; static State S__; int i, h__, depth = 0, g; EhcNode *tmp; if ( first_call_for_multiple_purpose ) { make_state( &S__, gnum_ft_conn ); S__.max_F = gnum_ft_conn; first_call_for_multiple_purpose = FALSE; } /* don't hash states, but search nodes. * this way, don't need to keep states twice in memory */ tmp = new_EhcNode(); copy_source_to_dest( &(tmp->S), S); hash_ehc_node( tmp ); lehc_current_end = lehc_space_head->next; for ( i = 0; i < gnum_H; i++ ) { int k; Bool found = FALSE; for(k = 0; k < gnum_IV; k++){ if(same_state(&gInvActs[k].state, S) && gH[i] == gInvActs[k].act){ found = TRUE; break; } } if(found) continue; if(is_D_action(gop_conn[gH[i]].action->name)) continue; g = result_to_dest( &S__, S, gH[i] ); add_to_ehc_space( &S__, gH[i], NULL, g ); } for ( i = 0; i < gnum_H; i++ ) { int k; Bool found = FALSE; for(k = 0; k < gnum_IV; k++){ if(same_state(&gInvActs[k].state, S) && gH[i] == gInvActs[k].act){ found = TRUE; break; } } if(found) continue; if(!is_D_action(gop_conn[gH[i]].action->name)) continue; /* D_cation, not in the gH */ g = result_to_dest( &S__, S, gH[i] ); add_to_ehc_space( &S__, gH[i], NULL, g ); } lehc_current_start = lehc_space_head->next; while ( TRUE ) { if ( lehc_current_start == lehc_current_end ) { reset_ehc_hash_entrys(); free( tmp ); return FALSE; } if ( lehc_current_start->depth > depth ) { depth = lehc_current_start->depth; if ( depth > gmax_search_depth ) { gmax_search_depth = depth; } printf("[%d]", depth); fflush( stdout ); } h__ = expand_first_node( h ); if ( LESS( h__, h ) ) { break; } } reset_ehc_hash_entrys(); free( tmp ); extract_plan_fragment( S ); source_to_dest( S_, &(lehc_current_start->S) ); *h_ = h__; return TRUE; }
void manual_control( void ) { static Bool fc = TRUE; static State S; int i, j, h, choice; BfsNode *tmp, *curr; if ( fc ) { make_state( &S, gnum_ft_conn ); fc = FALSE; } tmp = new_BfsNode(); copy_source_to_dest( &(tmp->S), &ginitial_state ); tmp->op = -1; curr = tmp; if ( gcmd_line.dominating ) { hash_bfs_node( curr ); } while ( TRUE ) { if ( !curr ) break; h = get_1P_and_H( &(curr->S), &ggoal_state, NULL, curr->father, curr->op ); get_A( &(curr->S) ); while ( TRUE ) { printf("\n\n\n-------------state h = %d", h); if ( h > 0 ) printf(" (resp. %d actions)", h - 1); printf(", info level %d, %d applicable actions", gcmd_line.debug, gnum_A); if ( gcmd_line.debug >= 1 ) { print_state( curr->S ); } if ( 0 ) { printf("\nH:"); for ( i = 0; i < gnum_H; i++ ) { printf(" "); print_op_name( gH[i] ); } } printf("\n"); for ( i = 0; i < gnum_A; i++ ) { printf("\n%3d ", i); for ( j = 0; j < gnum_H; j++ ) { if ( gA[i] == gH[j] ) break; } if ( j < gnum_H ) { printf("H: "); } else { printf(" : "); } print_op_name( gA[i] ); } printf("\n\n -1: retract last choice"); printf("\n -2: set info level"); printf("\n\nchoice: "); scanf("%d", &choice); if ( choice >= -2 && choice < gnum_A ) break; } if ( choice >= 0 ) { if ( !result_to_dest( &S, NULL, curr, gA[choice] ) ) { printf("\naction not applicable!"); continue; } if ( gcmd_line.dominating && bfs_state_hashed( &S, curr, gA[choice] ) ) { printf("\nthis state is dominated!\n\n"); } tmp = new_BfsNode(); copy_source_to_dest( &(tmp->S), &S ); tmp->father = curr; tmp->op = gA[choice]; curr = tmp; if ( gcmd_line.dominating ) { hash_bfs_node( curr ); } continue; } if ( choice == -1 ) { curr = curr->father; continue; } printf("\nlevel : "); scanf("%d", &gcmd_line.debug); } }
Bool do_best_first_search( void ) { static Bool fc = TRUE; static State S; BfsNode *first; int i, min = INFINITY; Bool start = TRUE; if ( fc ) { make_state( &S, gnum_ft_conn ); fc = FALSE; } lbfs_space_head = new_BfsNode(); lbfs_space_had = NULL; add_to_bfs_space( &ginitial_state, -1, NULL ); while ( TRUE ) { if ( (first = lbfs_space_head->next) == NULL ) { printf("\n\nbest first search space empty! problem proven unsolvable.\n\n"); return FALSE; } lbfs_space_head->next = first->next; if ( first->next ) { first->next->prev = lbfs_space_head; } if ( LESS( first->h, min ) ) { min = first->h; if ( start ) { printf("\nadvancing to distance : %4d", min); start = FALSE; fflush(stdout); } else { printf("\n %4d", min); fflush(stdout); } } if ( first->h == 0 ) { break; } get_A( &(first->S) ); for ( i = 0; i < gnum_A; i++ ) { if ( !result_to_dest( &S, NULL, first, gA[i] ) ) continue; add_to_bfs_space( &S, gA[i], first ); } first->next = lbfs_space_had; lbfs_space_had = first; } extract_plan( first ); return TRUE; }
Bool search_for_better_state( State *S, int h, State *S_, int *h_ ) { static Bool first_call = TRUE; static State S__; int i, h__, depth = 0; EhcNode *tmp; if ( first_call ) { make_state( &S__, gnum_ft_conn ); first_call = FALSE; } /* don't hash states, but search nodes. * this way, don't need to keep states twice in memory */ tmp = new_EhcNode(); copy_source_to_dest( &(tmp->S), S); hash_ehc_node( tmp ); lehc_current_end = lehc_space_head->next; for ( i = 0; i < gnum_H; i++ ) { /* see result-to-dest params explanation at fn header */ if ( !result_to_dest( &S__, tmp, NULL, gH[i] ) ) continue; add_to_ehc_space( &S__, gH[i], tmp ); } lehc_current_start = lehc_space_head->next; while ( TRUE ) { if ( lehc_current_start == lehc_current_end ) { reset_ehc_hash_entrys(); free( tmp ); return FALSE; } if ( lehc_current_start->depth > depth ) { depth = lehc_current_start->depth; if ( depth > gmax_search_depth ) { gmax_search_depth = depth; } printf("[%d]", depth); fflush( stdout ); /* HACK: if this is going to more than 15, then most likely this is just a stupid * effect of helpful actions pruning, and we won't get anywhere anyway. */ if ( depth > 15 ) { reset_ehc_hash_entrys(); free( tmp ); return FALSE; } } h__ = expand_first_node( h ); if ( LESS( h__, h ) ) { break; } } reset_ehc_hash_entrys(); free( tmp ); extract_plan_fragment( S ); source_to_dest( S_, &(lehc_current_start->S) ); *h_ = h__; return TRUE; }
Bool search_for_better_state( State *S, int h, State *S_, int *h_ ) { static Bool first_call = TRUE; static State S__; int i, h__, depth = 0, g; EhcNode *tmp; if ( first_call ) { make_state( &S__, gnum_ft_conn ); S__.max_F = gnum_ft_conn; first_call = FALSE; } /* don't hash states, but search nodes. * this way, don't need to keep states twice in memory */ tmp = new_EhcNode(); copy_source_to_dest( &(tmp->S), S); hash_ehc_node( tmp ); lehc_current_end = lehc_space_head->next; for ( i = 0; i < gnum_H; i++ ) { g = result_to_dest( &S__, S, gH[i] ); add_to_ehc_space( &S__, gH[i], NULL, g ); } lehc_current_start = lehc_space_head->next; while ( TRUE ) { if ( lehc_current_start == lehc_current_end ) { reset_ehc_hash_entrys(); free( tmp ); return FALSE; } if ( lehc_current_start->depth > depth ) { depth = lehc_current_start->depth; if ( depth > gmax_search_depth ) { gmax_search_depth = depth; } printf("[%d]", depth); fflush( stdout ); } h__ = expand_first_node( h ); if ( LESS( h__, h ) ) { break; } } reset_ehc_hash_entrys(); free( tmp ); extract_plan_fragment( S ); source_to_dest( S_, &(lehc_current_start->S) ); *h_ = h__; return TRUE; }