void add_to_bfs_space( State *S, int op, BfsNode *father ) { static int max_d = 0; BfsNode *tmp, *i; int h, num; /* see if state is already a part of this search space */ if ( op >= 0 ) {/* intial state has op -1 */ if ( gcmd_line.dominating && bfs_state_hashed( S, father, op ) ) { return; } } if ( gcmd_line.A && gcmd_line.debug ) { num = 0; for ( i = father; i; i = i->father ) { num++; } if ( num > max_d ) { max_d = num; printf("\ndepth: %d", num); } } h = get_1P( S, &ggoal_state, NULL, father, op ); if ( h == INFINITY ) { return; } for ( i = lbfs_space_head; i->next; i = i->next ) { if ( gcmd_line.breadth_bfs ) { if ( i->next->h > h ) break;/* stop only when next one is strictly worse */ } else { if ( i->next->h >= h ) break;/* stop already when next one is equally good */ } } tmp = new_BfsNode(); copy_source_to_dest( &(tmp->S), S ); tmp->op = op; tmp->h = h; tmp->father = father; tmp->next = i->next; tmp->prev = i; i->next = tmp; if ( tmp->next ) { tmp->next->prev = tmp; } if ( gcmd_line.dominating ) { hash_bfs_node( tmp ); } }
void add_to_ehc_space( State *S, int op, EhcNode *father, int new_goal ) { /* see if state is already a part of this search space */ if ( ehc_state_hashed( S ) ) { return; } if ( !lehc_current_end ) { lehc_current_end = new_EhcNode(); lehc_space_end->next = lehc_current_end; lehc_space_end = lehc_current_end; } copy_source_to_dest( &(lehc_current_end->S), S ); lehc_current_end->op = op; lehc_current_end->father = father; if ( !father ) { lehc_current_end->depth = 1; } else { lehc_current_end->depth = father->depth + 1; } lehc_current_end->new_goal = new_goal; hash_ehc_node( lehc_current_end ); lehc_current_end = lehc_current_end->next; }
/*add the state S into hash table*/ PlanHashEntry *hash_plan_state( State *S, int step ) { int sum, index; PlanHashEntry *h, *tmp; sum = state_sum( S ); index = sum & PLAN_HASH_BITS; for ( h = lplan_hash_entry[index]; h; h = h->next ) { if ( h->sum != sum ) continue; if ( same_state( S, &(h->S) ) ) break; } /*found the same state in the hash table*/ if ( h ) { if ( h->step != -1 ) { printf("\n\n[error]:reencountering a state that is already in plan! debug me\n\n"); exit( 1 ); } h->step = step; return h; } /* not found the same state in hash table*/ /* h points to the last one*/ /* jovi: comments by jovi, seems it is not necessary * This is for addding a nes state */ for ( h = lplan_hash_entry[index]; h && h->next; h = h->next ) ; tmp = new_PlanHashEntry(); tmp->sum = sum; copy_source_to_dest( &(tmp->S), S ); tmp->step = step; if ( h ) { h->next = tmp; } else { lplan_hash_entry[index] = tmp; } return tmp; }
PlanHashEntry *hash_plan_state( State *S, int step ) { int sum, index; PlanHashEntry *h, *tmp; sum = state_sum( S ); index = sum & PLAN_HASH_BITS; for ( h = lplan_hash_entry[index]; h; h = h->next ) { if ( h->sum != sum ) continue; if ( same_state( S, &(h->S) ) ) break; } if ( h ) { if ( h->step != -1 ) { printf("\n\nreencountering a state that is already in plan! debug me\n\n"); exit( 1 ); } h->step = step; return h; } for ( h = lplan_hash_entry[index]; h && h->next; h = h->next ); tmp = new_PlanHashEntry(); tmp->sum = sum; copy_source_to_dest( &(tmp->S), S ); tmp->step = step; if ( h ) { h->next = tmp; } else { lplan_hash_entry[index] = tmp; } return tmp; }
/************************************************* * 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 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; }