operator.cpp
Go to the documentation of this file.
00001 #include "globals.h"
00002 #include "operator.h"
00003 #include "module.h"
00004 #include "best_first_search.h"
00005 #include "plannerParameters.h"
00006 
00007 #include <iostream>
00008 using namespace std;
00009 
00010 Prevail::Prevail(istream &in)
00011 {
00012     in >> var >> prev;
00013 }
00014 
00015 bool Prevail::is_applicable(const TimeStampedState &state, bool allowRelaxed) const
00016 {
00017     assert(var >= 0 && var < g_variable_name.size());
00018     assert((prev >= 0 && prev < g_variable_domain[var]) || (g_variable_types[var] == module));
00019     if(g_variable_types[var] == module) {
00020         g_setModuleCallbackState(&state);
00021         predicateCallbackType pct = getPreds;
00022         numericalFluentCallbackType nct = getFuncs;
00023         double cost = g_condition_modules[var]->checkCondition(
00024                 g_condition_modules[var]->params, pct, nct, allowRelaxed);
00025         return cost < INFINITE_COST;
00026     } else {
00027         return double_equals(state[var], prev);
00028     }
00029 }
00030 
00031 PrePost::PrePost(istream &in)
00032 {
00033     int cond_count;
00034     in >> cond_count;
00035     for(int i = 0; i < cond_count; i++)
00036         cond_start.push_back(Prevail(in));
00037     in >> cond_count;
00038     for(int i = 0; i < cond_count; i++)
00039         cond_overall.push_back(Prevail(in));
00040     in >> cond_count;
00041     for(int i = 0; i < cond_count; i++)
00042         cond_end.push_back(Prevail(in));
00043     in >> var;
00044     if(is_functional(var)) {
00045         in >> fop >> var_post;
00046         // HACK: just use some arbitrary values for pre and post
00047         // s.t. they do not remain uninitialized
00048         pre = post = -1;
00049     } else {
00050         in >> pre >> post;
00051         // HACK: just use some arbitrary values for var_post and fop
00052         // s.t. they do not remain uninitialized
00053         var_post = -1;
00054         fop = assign;
00055     }
00056 }
00057 
00058 ModuleEffect::ModuleEffect(istream &in)
00059 {
00060     int cond_count;
00061     in >> cond_count;
00062     for(int i = 0; i < cond_count; i++)
00063         cond_start.push_back(Prevail(in));
00064     in >> cond_count;
00065     for(int i = 0; i < cond_count; i++)
00066         cond_overall.push_back(Prevail(in));
00067     in >> cond_count;
00068     for(int i = 0; i < cond_count; i++)
00069         cond_end.push_back(Prevail(in));
00070     string name;
00071     in >> name;
00072     name = name.substr(3);
00073     module = g_effect_modules[atoi(name.c_str())];
00074 }
00075 
00076 bool PrePost::is_applicable(const TimeStampedState &state) const
00077 {
00078     assert(var >= 0 && var < g_variable_name.size());
00079     assert(pre == -1 || (pre >= 0 && pre < g_variable_domain[var]));
00080     return pre == -1 || (double_equals(state[var], pre));
00081 }
00082 
00083 void Operator::sort_prevails(vector<Prevail> &prevails)
00084 {
00085     int swapIndex = prevails.size() - 1;
00086     for(int i = 0; i <= swapIndex; ++i) {
00087         if(g_variable_types[prevails[i].var] == module) {
00088             std::swap(prevails[i], prevails[swapIndex]);
00089             i--;
00090             swapIndex--;
00091         }
00092     }
00093 }
00094 
00095 Operator::Operator(istream &in)
00096 {
00097     check_magic(in, "begin_operator");
00098     in >> ws;
00099     getline(in, name);
00100     int count;
00101     binary_op bop;
00102     in >> bop >> duration_var;
00103     if(bop != eq) {
00104         cout << "Error: The duration constraint must be of the form\n";
00105         cout << "       (= ?duration (arithmetic_term))" << endl;
00106         exit(1);
00107     }
00108 
00109     in >> count; //number of prevail at-start conditions
00110     for(int i = 0; i < count; i++)
00111         prevail_start.push_back(Prevail(in));
00112     in >> count; //number of prevail overall conditions
00113     for(int i = 0; i < count; i++)
00114         prevail_overall.push_back(Prevail(in));
00115     in >> count; //number of prevail at-end conditions
00116     for(int i = 0; i < count; i++)
00117         prevail_end.push_back(Prevail(in));
00118     in >> count; //number of pre_post_start conditions (symbolical)
00119     for(int i = 0; i < count; i++)
00120         pre_post_start.push_back(PrePost(in));
00121     in >> count; //number of pre_post_end conditions (symbolical)
00122     for(int i = 0; i < count; i++)
00123         pre_post_end.push_back(PrePost(in));
00124     in >> count; //number of pre_post_start conditions (functional)
00125     for(int i = 0; i < count; i++)
00126         pre_post_start.push_back(PrePost(in));
00127     in >> count; //number of pre_post_end conditions (functional)
00128     for(int i = 0; i < count; i++)
00129         pre_post_end.push_back(PrePost(in));
00130 
00131     // sort prevails such that conditions on module variables come last
00132     sort_prevails(prevail_start);
00133     sort_prevails(prevail_overall);
00134     sort_prevails(prevail_end);
00135 
00136     in >> count; //numer of module start effects
00137     for(int i = 0; i < count; ++i) {
00138         mod_effs_start.push_back(ModuleEffect(in));
00139     }
00140     in >> count; //number of module end effects
00141     for(int i = 0; i < count; ++i) {
00142         mod_effs_end.push_back(ModuleEffect(in));
00143     }
00144     check_magic(in, "end_operator");
00145 }
00146 
00147 Operator::Operator(bool uses_concrete_time_information)
00148 {
00149     prevail_start   = vector<Prevail>();
00150     prevail_overall = vector<Prevail>();
00151     prevail_end     = vector<Prevail>();
00152     pre_post_start  = vector<PrePost>();
00153     pre_post_end    = vector<PrePost>();
00154     if(!uses_concrete_time_information) {
00155         name = "let_time_pass";
00156         duration_var = -1;
00157     } else {
00158         name = "wait";
00159         duration_var = -2;
00160     }
00161 }
00162 
00163 void Prevail::dump() const
00164 {
00165     cout << g_variable_name[var] << ": " << prev << endl;
00166 }
00167 
00168 void PrePost::dump() const
00169 {
00170     cout << "var: " << g_variable_name[var] << ", pre: " << pre
00171         << " , var_post: " << var_post << ", post: " << post << endl;
00172 }
00173 
00174 void Operator::dump() const
00175 {
00176     cout << name << endl;
00177     cout << "Prevails start:" << endl;
00178     for(int i = 0; i < prevail_start.size(); ++i) {
00179         prevail_start[i].dump();
00180     }
00181     cout << "Prevails overall:" << endl;
00182     for(int i = 0; i < prevail_overall.size(); ++i) {
00183         prevail_overall[i].dump();
00184     }
00185     cout << "Prevails end:" << endl;
00186     for(int i = 0; i < prevail_end.size(); ++i) {
00187         prevail_end[i].dump();
00188     }
00189     cout << "Preposts start:" << endl;
00190     for(int i = 0; i < pre_post_start.size(); ++i) {
00191         pre_post_start[i].dump();
00192     }
00193     cout << "Preposts end:" << endl;
00194     for(int i = 0; i < pre_post_end.size(); ++i) {
00195         pre_post_end[i].dump();
00196     }
00197     cout << endl;
00198 }
00199 
00200 bool Operator::is_applicable(const TimeStampedState & state, bool allowRelaxed,
00201         TimedSymbolicStates* timedSymbolicStates) const
00202 {
00203     if(g_parameters.disallow_concurrent_actions && !state.operators.empty())
00204         return false;
00205 
00206     if(g_parameters.epsilonize_internally) {
00207         for(unsigned int i = 0; i < state.operators.size(); ++i) {
00208             double time_increment = state.operators[i].time_increment;
00209             if(double_equals(time_increment, EPS_TIME)) {
00210                 return false;
00211             }
00212         }
00213     }
00214 
00215     if(g_parameters.use_cost_modules_for_applicability || (g_variable_types[duration_var] != costmodule)) {
00216         double duration = get_duration(&state, allowRelaxed);
00217         if(duration < 0 || duration >= INFINITE_COST)
00218             return false;
00219     }
00220 
00221     for(int i = 0; i < prevail_start.size(); i++)
00222         if(!prevail_start[i].is_applicable(state, allowRelaxed))
00223             return false;
00224     for(int i = 0; i < pre_post_start.size(); i++)
00225         if(!pre_post_start[i].is_applicable(state))
00226             return false;
00227 
00228     // There may be no simultaneous applications of two instances of the
00229     // same ground operator (for technical reasons, to simplify the task
00230     // of keeping track of durations committed to at the start of the
00231     // operator application)
00232     for(int i = 0; i < state.operators.size(); i++)
00233         if(state.operators[i].get_name() == get_name())
00234             return false;
00235 
00236     return TimeStampedState(state, *this).is_consistent_when_progressed(timedSymbolicStates);
00237 }
00238 
00239 bool Operator::isDisabledBy(const Operator* other) const
00240 {
00241     if(name.compare(other->name) == 0)
00242         return false;
00243     if(deletesPrecond(prevail_start, other->pre_post_start))
00244         return true;
00245     if(deletesPrecond(prevail_start, other->pre_post_end))
00246         return true;
00247     if(deletesPrecond(prevail_overall, other->pre_post_start))
00248         return true;
00249     if(deletesPrecond(prevail_overall, other->pre_post_end))
00250         return true;
00251     if(deletesPrecond(prevail_end, other->pre_post_start))
00252         return true;
00253     if(deletesPrecond(prevail_end, other->pre_post_end))
00254         return true;
00255     if(deletesPrecond(pre_post_start, other->pre_post_start))
00256         return true;
00257     if(deletesPrecond(pre_post_start, other->pre_post_end))
00258         return true;
00259     if(deletesPrecond(pre_post_end, other->pre_post_start))
00260         return true;
00261     if(deletesPrecond(pre_post_end, other->pre_post_end))
00262         return true;
00263     //    if(writesOnSameVar(pre_post_start,other->pre_post_start)) return true;
00264     //    if(writesOnSameVar(pre_post_start,other->pre_post_end)) return true;
00265     //    if(writesOnSameVar(pre_post_end,other->pre_post_start)) return true;
00266     //    if(writesOnSameVar(pre_post_end,other->pre_post_end)) return true;
00267 
00268     return false;
00269 }
00270 
00271 bool Operator::enables(const Operator* other) const
00272 {
00273     if(name.compare(other->name) == 0)
00274         return false;
00275     if(achievesPrecond(pre_post_start, other->prevail_start))
00276         return true;
00277     if(achievesPrecond(pre_post_start, other->prevail_overall))
00278         return true;
00279     if(achievesPrecond(pre_post_start, other->prevail_end))
00280         return true;
00281     if(achievesPrecond(pre_post_end, other->prevail_start))
00282         return true;
00283     if(achievesPrecond(pre_post_end, other->prevail_overall))
00284         return true;
00285     if(achievesPrecond(pre_post_end, other->prevail_end))
00286         return true;
00287     if(achievesPrecond(pre_post_start, other->pre_post_start))
00288         return true;
00289     if(achievesPrecond(pre_post_start, other->pre_post_end))
00290         return true;
00291     if(achievesPrecond(pre_post_end, other->pre_post_start))
00292         return true;
00293     if(achievesPrecond(pre_post_end, other->pre_post_end))
00294         return true;
00295     return false;
00296 }
00297 
00298 bool Operator::achievesPrecond(const vector<PrePost>& effects, const vector<
00299         Prevail>& conds) const
00300 {
00301     for(int i = 0; i < effects.size(); ++i) {
00302         for(int j = 0; j < conds.size(); ++j) {
00303             if(effects[i].var == conds[j].var && double_equals(
00304                         effects[i].post, conds[j].prev)) {
00305                 return true;
00306             }
00307         }
00308     }
00309     return false;
00310 }
00311 
00312 bool Operator::achievesPrecond(const vector<PrePost>& effs1, const vector<
00313         PrePost>& effs2) const
00314 {
00315     for(int i = 0; i < effs1.size(); ++i) {
00316         for(int j = 0; j < effs2.size(); ++j) {
00317             if(effs1[i].var == effs2[j].var && double_equals(effs1[i].post,
00318                         effs2[j].pre)) {
00319                 return true;
00320             }
00321         }
00322     }
00323     return false;
00324 }
00325 
00326 //FIXME: numerical effects?? conditional effects?? axioms??
00327 bool Operator::deletesPrecond(const vector<Prevail>& conds, const vector<
00328         PrePost>& effects) const
00329 {
00330     for(int i = 0; i < conds.size(); ++i) {
00331         for(int j = 0; j < effects.size(); ++j) {
00332             if(conds[i].var == effects[j].var && !double_equals(conds[i].prev,
00333                         effects[j].post)) {
00334                 return true;
00335             }
00336         }
00337     }
00338     return false;
00339 }
00340 
00341 bool Operator::deletesPrecond(const vector<PrePost>& effs1, const vector<
00342         PrePost>& effs2) const
00343 {
00344     for(int i = 0; i < effs1.size(); ++i) {
00345         for(int j = 0; j < effs2.size(); ++j) {
00346             if(effs1[i].var == effs2[j].var && !double_equals(effs1[i].pre,
00347                         effs2[j].post)) {
00348                 //        if(effs1[i].var == effs2[j].var &&
00349                 //           !double_equals(effs1[i].pre,effs2[j].post) &&
00350                 //           !double_equals(effs1[i].pre,-1.0)) 
00351                 return true;
00352             }
00353         }
00354     }
00355     return false;
00356 }
00357 
00358 bool Operator::writesOnSameVar(const vector<PrePost>& effs1, const vector<
00359         PrePost>& effs2) const
00360 {
00361     for(int i = 0; i < effs1.size(); ++i) {
00362         for(int j = 0; j < effs2.size(); ++j) {
00363             if(effs1[i].var == effs2[j].var/* && effs1[i].post != effs2[j].post*/) {
00364                 return true;
00365             }
00366         }
00367     }
00368     return false;
00369 }
00370 
00371 double Operator::get_duration(const TimeStampedState* state, bool relaxed) const
00372 {
00373     assert(duration_var >= 0);
00374     assert(state != NULL);
00375 
00376     if(g_variable_types[duration_var] == costmodule) {
00377         g_setModuleCallbackState(state);
00378         predicateCallbackType pct = getPreds;
00379         numericalFluentCallbackType nct = getFuncs;
00380         double duration = g_cost_modules[duration_var]->checkCost(
00381                 g_cost_modules[duration_var]->params, pct, nct, relaxed);
00382         //printf("Duration from module: %f\n", duration);
00383         return duration;
00384     }
00385 
00386     // default behaviour: duration is defined by duration_var
00387     return (*state)[duration_var];
00388 }
00389 
00390 bool Operator::operator<(const Operator &other) const
00391 {
00392     return name < other.name;
00393 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


tfd_modules
Author(s): Maintained by Christian Dornhege (see AUTHORS file).
autogenerated on Tue Jan 22 2013 12:25:03