std::set<mu_0_boolean*> upm_rpg::get_fact_layer(state* st){ std::set<mu_0_boolean*> f_list; state* temps = new state(workingstate); StateCopy(workingstate, st); // workingstate->print(); std::vector<mu_0_boolean*> v_temp = st->get_mu_bools(); std::vector<mu_0_boolean*> v_tempp = st->get_mu_bool_arrays(); v_temp.insert(v_temp.end(), v_tempp.begin(), v_tempp.end()); for (int ix2 = 0; ix2 < (v_temp.size()); ix2++){ // cout << v_temp.at(ix2)->Name() << endl; if (v_temp.at(ix2)->value() == 1){ f_list.insert(v_temp.at(ix2)); } } StateCopy(workingstate, temps); return (f_list); }
char *StartStateManager::StateName(StatePtr p) { state nextstate; if (!p.isStart()) Error.Notrace ("Internal: Cannot find startstate name for non startstate"); for (what_startstate = 0; what_startstate < numstartstates; what_startstate++) { StartState(); StateCopy(&nextstate, workingstate); if (StateEquivalent(&nextstate, p)) return LastStateName(); } // Norris: it is very funny, but the following code is supposed to work, but it doesn't // // state * nextstate; // for(what_startstate=0; what_startstate<numstartstates; what_startstate++) // { // nextstate = StartState(); // nextstate points to internal data at theworld.getstate() // if (p.compare(nextstate)) // return LastStateName(); // } Error.Notrace ("Internal: Cannot find startstate name for funny startstate"); return NULL; }
std::map<mu__real*, std::pair<double, double> > upm_staged_rpg::get_initial_bounds(){ std::map<mu__real*, std::pair<double, double> > bs; static state* temp_numr = new state; StateCopy(temp_numr, workingstate); std::vector<mu__real*> v_temp = workingstate->get_mu_nums(); std::vector<mu__real*> v_tempp = workingstate->get_mu_num_arrays(); v_temp.insert(v_temp.end(), v_tempp.begin(), v_tempp.end()); for (int ix2 = 0; ix2 < (v_temp.size()); ix2++){ bs[v_temp.at(ix2)] = (std::make_pair(v_temp.at(ix2)->value(), v_temp.at(ix2)->value())); } StateCopy(workingstate, temp_numr); return bs; }
/******************** Canonicalization by fast exhaustive generation of all permutations ********************/ void SymmetryClass::Exhaustive_Fast_Canonicalize(state* s) { int i; static state temp; Perm.ResetToExplicit(); StateCopy(&temp, workingstate); ResetBestResult(); for (i=0; i<Perm.count; i++) if (Perm.In(i)) { StateCopy(workingstate, &temp); mu_v.Permute(Perm,i); if (args->multiset_reduction.value) mu_v.MultisetSort(); SetBestResult(i, workingstate); } StateCopy(workingstate, &BestPermutedState); };
std::set<mu__any*> upm_staged_rpg::get_fact_layer(state* st){ std::set<mu__any*> f_list; state* temps = new state(workingstate); StateCopy(workingstate, st); std::vector<mu_0_boolean*> v_temp = st->get_mu_bools(); std::vector<mu_0_boolean*> v_tempp = st->get_mu_bool_arrays(); v_temp.insert(v_temp.end(), v_tempp.begin(), v_tempp.end()); for (int ix2 = 0; ix2 < (v_temp.size()); ix2++){ if (v_temp.at(ix2)->value() == 1){ f_list.insert(v_temp.at(ix2)); } } StateCopy(workingstate, temps); delete temps; return (f_list); }
state(state * s) { StateCopy(this, s); };
double upm_staged_rpg::compute_rpg(){ double curr_mu_time = mu_TIME.value(); std::set<int> ha; bounds = get_initial_bounds(); double curr_time = 0; action_pre.clear(); achieved_by.clear(); achieved_by_first.clear(); rpg_state_facts.clear(); rpg_facts.clear(); init_facts.clear(); temporal_layers.clear(); if (bool_goal_fact_layer.size() == 0 && num_goal_fact_layer.size() == 0) return 999999; double rpg_length = 999; static state* orig = new state; StateCopy(orig, workingstate); state* temp_ws = new state(workingstate); state* backup_state_lower = new state(workingstate); state* backup_state_upper = new state(workingstate); state* next_rpg_state_lower = new state(workingstate); StateCopy(backup_state_lower, workingstate); state* next_rpg_state_upper = new state(workingstate); StateCopy(backup_state_upper, workingstate); state* trpg0 = new state(workingstate); temporal_layers.push_back( std::make_pair( std::make_pair(trpg0, trpg0), mu_TIME.value()) ); init_facts = get_fact_layer(temp_ws); std::set<mu_0_boolean*> f_l; while (true){ std::map<mu__any*, std::set<int> > curr_ach_by; std::map<int, std::vector<mu__any*> > curr_a_pre; std::map<mu__any*, int> curr_ach_by_first; achieved_by.push_back(curr_ach_by); action_pre.push_back(curr_a_pre); achieved_by_first.push_back(curr_ach_by_first); StateCopy(backup_state_lower, temporal_layers.at(temporal_layers.size()-1).first.first); StateCopy(backup_state_upper, temporal_layers.at(temporal_layers.size()-1).first.second); StateCopy(workingstate, backup_state_lower); // curr_time += (10*mu_T); for (int ix = 0; ix < RULES_IN_WORLD; ix++){ StateCopy(workingstate, backup_state_lower); if (metric_condition(ix) == true){ action_pre.at(action_pre.size()-1).insert(std::make_pair(ix, Rules->generator->all_precond_array(ix))); std::vector<mu__any*> aef = Rules->generator->effects_all_array(ix); for (int ix44 = 0; ix44 < aef.size(); ix44++){ if (init_facts.count(aef.at(ix44))==0 && ix != 0){ achieved_by_first.at(achieved_by_first.size()-1).insert(std::make_pair(aef.at(ix44), ix)); StateCopy(workingstate, orig); if (Rules->generator->Condition(ix)){ achieved_by.at(achieved_by.size()-1)[aef.at(ix44)].insert(ix); } StateCopy(workingstate, backup_state_lower); } } StateCopy(workingstate, next_rpg_state_lower); Rules->generator->Code_numeric_ff_minus(ix); StateCopy(next_rpg_state_lower, workingstate); StateCopy(workingstate, next_rpg_state_upper); Rules->generator->Code_numeric_ff_plus(ix); StateCopy(next_rpg_state_upper, workingstate); } } // FOR LOOP END StateCopy(workingstate, next_rpg_state_upper); workingstate->fire_processes_plus(1); mu_event_check(); StateCopy(next_rpg_state_upper, workingstate); StateCopy(workingstate, next_rpg_state_lower); workingstate->fire_processes_minus(1); mu_event_check(); StateCopy(next_rpg_state_lower, workingstate); state* trpg_l = new state(next_rpg_state_lower); // cout << "\n\n*****LOWER STATE PRINT*****\n" << endl; // next_rpg_state_lower->print(); update_lower_bound(bounds); update_lower_bound(goal_bounds); StateCopy(workingstate, next_rpg_state_upper); state* trpg_u = new state(next_rpg_state_upper); // cout << "\n\n*****UPPER STATE PRINT*****\n" << endl; // next_rpg_state_upper->print(); update_upper_bound(bounds); update_upper_bound(goal_bounds); temporal_layers.push_back(std::make_pair(std::make_pair(trpg_l, trpg_u), mu_TIME.value())); // print_bounds(); if (goal_check()){ delete trpg_u; delete trpg_l; break; } if ( (mu_TIME.value() > args->SRPG_horizon.value) ){ StateCopy(workingstate, temp_ws); workingstate->set_h_val(999); workingstate->set_g_val(0); workingstate->set_f_val(); delete trpg0; delete backup_state_lower; delete backup_state_upper; delete next_rpg_state_upper; delete next_rpg_state_lower; delete temp_ws; delete trpg_u; delete trpg_l; // cout << "\n\nENDING THE LOOP WITH NO GOAL!\n\n" << endl; return 999; } delete trpg_u; delete trpg_l; } // WHILE END /************************* * * BACKWARDS SEARCH * */ std::set<mu__any*> backwards = all_goal_fact_layer; std::vector<std::set<int> >rpg_final_all; int curr_action_layer = achieved_by_first.size()-1; std::set<mu__any*> g1 = get_fact_layer(temporal_layers.at(1).first.second); std::set<mu__any*>::iterator itt; std::set<mu__any*> temp_set; while (curr_action_layer >= 0 ){ std::set<int>rpg_final; rpg_final.clear(); for (itt=backwards.begin(); itt!=backwards.end(); ++itt){ if ((*itt)->get_mu_type() == "mu_0_boolean" && curr_action_layer >= 1 && achieved_by_first.at(curr_action_layer-1).count((*itt)) > 0) { temp_set.insert((*itt)); continue; } if (rpg_final.count(achieved_by_first.at(curr_action_layer)[*itt]) >= 1) { continue; } std::copy(action_pre.at(curr_action_layer)[achieved_by_first.at(curr_action_layer)[*itt]].begin(), action_pre.at(curr_action_layer)[achieved_by_first.at(curr_action_layer)[*itt]].end(), std::inserter(temp_set, temp_set.end())); if (init_facts.count(*itt) == 0 ){ rpg_final.insert(achieved_by_first.at(curr_action_layer)[*itt]); if (g1.count(*itt)==1){ ha.insert(achieved_by.at(curr_action_layer)[(*itt)].begin(), achieved_by.at(curr_action_layer)[(*itt)].end()); } } } rpg_final_all.push_back(rpg_final); backwards = temp_set; curr_action_layer--; temp_set.clear(); if (backwards.size() == 0) break; } if (ha.size() < 1){ ha.insert(0); } for (int i = 1; i < rpg_final_all.size()-1; i++){ if (rpg_final_all.at(i).size() == 0){ rpg_final_all.at(i).insert(0); } } rpg_length = 0; for (int i = 0; i < rpg_final_all.size(); i++){ rpg_length += rpg_final_all.at(i).size(); } StateCopy(workingstate, temp_ws); workingstate->set_h_val(rpg_length); workingstate->set_g_val(0); workingstate->set_f_val(); state * tend = new state; StateCopy(tend, workingstate); helpful_actions[tend] = ha; delete next_rpg_state_upper; delete next_rpg_state_lower; delete backup_state_lower; delete backup_state_upper; delete temp_ws; delete trpg0; delete tend; return (rpg_length); }
/******************** Auxiliary function for error trace printing ********************/ bool match(state* ns, StatePtr p) { int i; static PermSet Perm; static state temp; StateCopy(&temp, ns); if (args->symmetry_reduction.value) { if ( args->sym_alg.mode == argsym_alg::Exhaustive_Fast_Canonicalize) { Perm.ResetToExplicit(); for (i=0; i<Perm.count; i++) if (Perm.In(i)) { if (ns != workingstate) StateCopy(workingstate, ns); mu_v.Permute(Perm,i); if (args->multiset_reduction.value) mu_v.MultisetSort(); if (p.compare(workingstate)) { StateCopy(workingstate,&temp); return TRUE; } } StateCopy(workingstate,&temp); return FALSE; } else { Perm.ResetToSimple(); Perm.SimpleToOne(); if (ns != workingstate) StateCopy(workingstate, ns); mu_v.Permute(Perm,0); if (args->multiset_reduction.value) mu_v.MultisetSort(); if (p.compare(workingstate)) { StateCopy(workingstate,&temp); return TRUE; } while (Perm.NextPermutation()) { if (ns != workingstate) StateCopy(workingstate, ns); mu_v.Permute(Perm,0); if (args->multiset_reduction.value) mu_v.MultisetSort(); if (p.compare(workingstate)) { StateCopy(workingstate,&temp); return TRUE; } } StateCopy(workingstate,&temp); return FALSE; } } if (!args->symmetry_reduction.value && args->multiset_reduction.value) { if (ns != workingstate) StateCopy(workingstate, ns); mu_v.MultisetSort(); if (p.compare(workingstate)) { StateCopy(workingstate,&temp); return TRUE; } StateCopy(workingstate,&temp); return FALSE; } return (p.compare(ns)); }
double upm_rpg::compute_rpg(){ std::set<int> ha; // helpful_actions.clear(); // clock_t start_total = clock(); action_pre.clear(); achieved_by.clear(); achieved_by_first.clear(); rpg_state_facts.clear(); rpg_facts.clear(); init_facts.clear(); if (bool_goal_fact_layer.size() == 0) return 999999; double rpg_length; static state* orig = new state; StateCopy(orig, workingstate); state* temp_ws = new state(workingstate); state* backup_state = new state(); std::vector<int> temp_rem_rules = remaining_rules; std::set<int> temp_rem_rules2(remaining_rules.begin(), remaining_rules.end()); state* next_rpg_state = new state(workingstate); StateCopy(backup_state, next_rpg_state); state* trpg0 = new state(workingstate); rpg_state_facts.push_back(temp_ws); init_facts = get_fact_layer(temp_ws); std::set<mu_0_boolean*> f_l; while (true){ StateCopy(backup_state, workingstate); for (int ix = 0; ix < temp_rem_rules.size(); ix++){ int ixxx = temp_rem_rules.at(ix); StateCopy(workingstate, backup_state); if (Rules->generator->Condition(ixxx)){ action_pre.insert(std::make_pair(ixxx, Rules->generator->bool_precond_array(ixxx))); // action_pre[r_name] = Rules->generator->precond_array(ixxx); std::vector<mu_0_boolean*> aef = Rules->generator->effects_add_bool_array(ixxx); for (int ix44 = 0; ix44 < aef.size(); ix44++){ if (init_facts.count(aef.at(ix44))==0){ achieved_by_first.insert(std::make_pair(aef.at(ix44), ixxx)); // achieved_by_first[aef.at(ix44)] = ixxx; StateCopy(workingstate, orig); if (Rules->generator->Condition(ixxx)){ achieved_by[aef.at(ix44)].insert(ixxx); StateCopy(workingstate, backup_state); } } } StateCopy(workingstate, next_rpg_state); Rules->generator->Code_ff(ixxx); temp_rem_rules2.erase(ixxx); StateCopy(next_rpg_state, workingstate); } } // FOR LOOP END StateCopy(workingstate, next_rpg_state); if (temp_rem_rules.size() == temp_rem_rules2.size()){ StateCopy(workingstate, temp_ws); // //// cout << "\n\nTWO RULE ARRAYS ARE THE SAME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" << endl; // //// if ((new StatePtr(workingstate))->isStart()){ return 999999; //// } // //// return (workingstate->previous.get_sp()->get_h_val()); } if ((rpg_state_facts.size() > 1 && StateCmp(rpg_state_facts.at(rpg_state_facts.size()-1), rpg_state_facts.at(rpg_state_facts.size()-2)) == 0) || rpg_state_facts.size() > 60 ){ // cout << "\n\nTWO LAYERS ARE THE SAME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" << endl; StateCopy(workingstate, temp_ws); return 999999; } temp_rem_rules.resize(temp_rem_rules2.size()); std::copy(temp_rem_rules2.begin(), temp_rem_rules2.end(), temp_rem_rules.begin()); StateCopy(backup_state, workingstate); state* trpg = new state(workingstate); rpg_state_facts.push_back(trpg); if (mu__goal__00()) { // cout << " GOAL " << endl; break; } } // WHILE END /************************* * * BACKWARDS SEARCH * */ std::set<mu_0_boolean*> backwards = bool_goal_fact_layer; std::set<int> rpg_final; int curr_action_layer = rpg_state_facts.size(); std::set<mu_0_boolean*> g1 = get_fact_layer(rpg_state_facts.at(1)); std::set<mu_0_boolean*>::iterator itt; std::set<mu_0_boolean*> temp_set; // ha.clear(); while (curr_action_layer >= 0 ){ for (itt=backwards.begin(); itt!=backwards.end(); ++itt){ if ( rpg_final.count(achieved_by_first[*itt]) >= 1) continue; std::copy(action_pre[achieved_by_first[*itt]].begin(), action_pre[achieved_by_first[*itt]].end(), std::inserter(temp_set, temp_set.end())); if (init_facts.count(*itt) == 0){ rpg_final.insert(achieved_by_first[*itt]); // if (achieved_by[*itt].size() == 1){ // rpg_final.insert(achieved_by_first[*itt]); // } else if (achieved_by[*itt].size() > 1){ // std::set<int>::iterator itt2; // for (itt2=achieved_by[*itt].begin(); itt2!=achieved_by[*itt].begin(); ++itt2){ // if ((*itt2) == achieved_by_first[*itt]){ // rpg_final.insert((*itt2)); // } // } // } // cout << (*itt)->name << endl; // cout << Rules->RuleName(achieved_by_first[(*itt)]) << endl; if (g1.count(*itt)==1){ ha.insert(achieved_by[(*itt)].begin(), achieved_by[(*itt)].end()); } } } // cout << "------------------------" << endl; backwards = temp_set; curr_action_layer--; temp_set.clear(); if (backwards.size() == 0) break; } // cout << "---------------------------------\n\n\n" << endl; rpg_length = rpg_final.size(); // helpful_actions[temp_ws] = ha; StateCopy(workingstate, temp_ws); workingstate->set_h_val(rpg_length); // workingstate->set_g_val(workingstate->previous.get_sp()->get_g_val()+1); workingstate->set_g_val(0); workingstate->set_f_val(); state * tend = new state; StateCopy(tend, workingstate); helpful_actions[tend] = ha; // cout << "\nNEW STATE: \n" << endl; // temp_ws->print(); // std::set<int>::iterator itrpg; // cout << "\n\n" << endl; // for (itrpg=rpg_final.begin(); itrpg!=rpg_final.end(); ++itrpg){ // cout << Rules->RuleName(*itrpg) << endl; // } // cout << "\n*****************\n\n" << endl; // // std::set<int>::iterator itha; // for (itha=ha.begin(); itha!=ha.end(); ++itha){ // cout << Rules->RuleName(*itha) << endl; // } // cout << "\n*****************\n\n" << endl; // // cout << "CURRENT STATE HEURISTIC VALUE: " << rpg_length << endl; // // // cout << "\n\n\n---------------------------END OF RPG---------------------------\n\n\n\n" << endl; delete next_rpg_state; // delete temp_ws; delete backup_state; // start_total = clock() - start_total; // cout << "\nTOTAL RPG TIME: " << (((float)start_total)/CLOCKS_PER_SEC) << " (" << start_total << " clicks)\n\n ----------------------------------------------------" << endl; return (rpg_length); }