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 ); } }
/************************************* * 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; }
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; }