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; }
/************************************************* * 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; }
Bool do_enforced_hill_climbing( State *start, State *end ) { static Bool first_call = TRUE; static State S, S_; int i, h, h_; if ( first_call ) { /* on first call, initialize plan hash table, search space, search hash table */ for ( i = 0; i < PLAN_HASH_SIZE; i++ ) { lplan_hash_entry[i] = NULL; } /* on subsequent calls, the start is already hashed, as it's the end of the previous calls */ hash_plan_state( start, 0 ); lehc_space_head = new_EhcNode(); lehc_space_end = lehc_space_head; for ( i = 0; i < EHC_HASH_SIZE; i++ ) { lehc_hash_entry[i] = NULL; lnum_ehc_hash_entry[i] = 0; lchanged_ehc_entry[i] = FALSE; } lnum_changed_ehc_entrys = 0; make_state( &S, gnum_ft_conn ); S.max_F = gnum_ft_conn; make_state( &S_, gnum_ft_conn ); S_.max_F = gnum_ft_conn; make_state( &lcurrent_goals, gnum_ft_conn ); lcurrent_goals.max_F = gnum_ft_conn; first_call = FALSE; } /* start enforced Hill-climbing */ source_to_dest( &lcurrent_goals, end ); source_to_dest( &S, start ); /*seems get a heuristic*/ h = get_1P_and_H( &S, &lcurrent_goals ); if ( h == INFINITY ) { return FALSE; } if ( h == 0 ) { return TRUE; } printf("\n\nCueing down from goal distance: %4d into depth", h); while ( h != 0 ) { if ( !search_for_better_state( &S, h, &S_, &h_ ) ) { return FALSE; } /* * modified by Jason Huang: return ealier */ if(is_solved_state( &S_ )) { printf("\nstate have seen. h = %d. return\n", h); return TRUE; } source_to_dest( &S, &S_ ); h = h_; printf("\n %4d ", 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; 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 do_enforced_hill_climbing( State *start, State *end ) { static Bool first_call = TRUE; static State S, S_; int i, h, h_; if ( first_call ) { /* on first call, initialize plan hash table, search space, search hash table */ for ( i = 0; i < PLAN_HASH_SIZE; i++ ) { lplan_hash_entry[i] = NULL; } lehc_space_head = new_EhcNode(); lehc_space_end = lehc_space_head; make_state( &S, gnum_ft_conn ); make_state( &S_, gnum_ft_conn ); make_state( &lscurrent_goals, gnum_ft_conn ); first_call = FALSE; } /* start enforced Hill-climbing */ source_to_dest( &lscurrent_goals, end ); source_to_dest( &S, start ); h = get_1P_and_H( &S, &lscurrent_goals, NULL, NULL, -1 ); if ( h == INFINITY ) { return FALSE; } if ( h == 0 ) { return TRUE; } printf("\n\nCueing down from goal distance: %4d into depth ", h); fflush(stdout); while ( h != 0 ) { if ( !search_for_better_state( &S, h, &S_, &h_ ) ) { /* printf("\nswitch to no H %4d ", h); */ /* get_A(&S); */ /* for ( i = 0; i < gnum_H; i++ ) { */ /* gop_conn[gH[i]].is_in_H = FALSE; */ /* } */ /* for ( i = 0; i < gnum_A; i++ ) { */ /* gH[i] = gA[i]; */ /* gop_conn[gH[i]].is_in_H = TRUE; */ /* } */ /* gnum_H = gnum_A; */ /* gcmd_line.help = FALSE; */ /* if ( !search_for_better_state( &S, h, &S_, &h_ ) ) { */ for ( i = 0; i < gnum_clauses; i++ ) { gclause_length[i] = 0; } gnum_fixed_clauses = 0; gnum_clauses = 0; for ( i = 1; i <= gnum_fixed_c; i++ ) { gcodes[gct[i]][gcf[i]] = -1; } gnum_fixed_c = 0; extend_fixed_clauses_base( 0, 0 ); extend_fixed_clauses_base_encoding( 0 ); return FALSE; /* } else { */ /* gcmd_line.help = TRUE; */ /* } */ } source_to_dest( &S, &S_ ); h = h_; printf("\n %4d ", h); fflush(stdout); } 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; }