state.h
Go to the documentation of this file.
00001 #ifndef STATE_H
00002 #define STATE_H
00003 
00004 #include <algorithm>
00005 #include <cassert>
00006 #include <iostream>
00007 #include <vector>
00008 #include <set>
00009 #include "globals.h"
00010 #include "module.h"
00011 
00012 using namespace std;
00013 
00014 class Operator;
00015 class TimeStampedState;
00016 
00017 struct Prevail
00018 {
00019     int var;
00020     double prev;
00021     Prevail(istream &in);
00022     Prevail(int v, double p) :
00023         var(v), prev(p)
00024     {
00025     }
00026     virtual ~Prevail()
00027     {
00028     }
00029 
00030     bool is_applicable(const TimeStampedState & state, bool allowRelaxed = false) const;
00031 
00032     void dump() const;
00033 
00034     bool operator<(const Prevail &other) const {
00035         if(var < other.var)
00036             return true;
00037         if(var > other.var)
00038             return false;
00039         return prev < other.prev;
00040     }
00041 };
00042 
00043 struct PrePost
00044 {
00045     int var;
00046     double pre;
00047     int var_post;
00048     double post;
00049     vector<Prevail> cond_start;
00050     vector<Prevail> cond_overall;
00051     vector<Prevail> cond_end;
00052     assignment_op fop;
00053 
00054     PrePost()
00055     {
00056     } // Needed for axiom file-reading constructor, unfortunately.
00057     PrePost(std::istream &in);
00058     PrePost(int v, double pr, int vpo, double po, const std::vector<Prevail> &co_start,
00059             const std::vector<Prevail> &co_oa,
00060             const std::vector<Prevail> &co_end, assignment_op fo = assign) :
00061         var(v), pre(pr), var_post(vpo), post(po),
00062         cond_start(co_start), cond_overall(co_oa), cond_end(co_end), fop(fo)
00063     {
00064     }
00065     PrePost(int v, double p) : var(v), post(p) {}
00066     virtual ~PrePost() {}
00067 
00068     bool is_applicable(const TimeStampedState &state) const;
00069 
00070     bool does_fire(const TimeStampedState &state) const {
00071         for(unsigned int i = 0; i < cond_start.size(); i++)
00072             if(!cond_start[i].is_applicable(state))
00073                 return false;
00074         return true;
00075     }
00076 
00077     void dump() const;
00078 };
00079 
00080 struct ModuleEffect
00081 {
00082     vector<Prevail> cond_start;
00083     vector<Prevail> cond_overall;
00084     vector<Prevail> cond_end;
00085     EffectModule *module;
00086     ModuleEffect(std::istream &in);
00087     ModuleEffect(vector<Prevail> &_cond_start,
00088             vector<Prevail> &_cond_overall, vector<Prevail> &_cond_end,
00089             EffectModule *_module) :
00090         cond_start(_cond_start), cond_overall(_cond_overall), cond_end(
00091                 _cond_end), module(_module)
00092     {
00093     }
00094 
00095     bool does_fire(const TimeStampedState &state) const
00096     {
00097         for(unsigned int i = 0; i < cond_start.size(); i++)
00098             if(!cond_start[i].is_applicable(state))
00099                 return false;
00100         return true;
00101     }
00102 };
00103 
00104 struct ScheduledEffect : public PrePost
00105 {
00106     double time_increment;
00107     ScheduledEffect(double t, vector<Prevail> &cas, vector<Prevail> &coa, vector<Prevail> &cae,
00108         int va, int vi, assignment_op op) :
00109         PrePost(va, -1.0, vi, -1.0, cas, coa, cae, op), time_increment(t)
00110     {
00111         initialize();
00112     }
00113     ScheduledEffect(double t, const PrePost& pp) : PrePost(pp), time_increment(t)
00114     {
00115         initialize();
00116     }
00117     void initialize()
00118     {
00119         sort(cond_start.begin(), cond_start.end());
00120         sort(cond_overall.begin(), cond_overall.end());
00121         sort(cond_end.begin(), cond_end.end());
00122     }
00123     bool operator<(const ScheduledEffect &other) const
00124     {
00125         if(time_increment < other.time_increment)
00126             return true;
00127         if(time_increment > other.time_increment)
00128             return false;
00129         if(var < other.var)
00130             return true;
00131         if(var > other.var)
00132             return false;
00133         if(pre < other.pre)
00134             return true;
00135         if(pre > other.pre)
00136             return false;
00137         if(var_post < other.var_post)
00138             return true;
00139         if(var_post > other.var_post)
00140             return false;
00141         if(post < other.post)
00142             return true;
00143         if(post > other.post)
00144             return false;
00145         if(fop < other.fop)
00146             return true;
00147         if(fop > other.fop)
00148             return false;
00149         if(cond_start.size() < other.cond_start.size())
00150             return true;
00151         if(cond_start.size() > other.cond_start.size())
00152             return false;
00153         if(cond_overall.size() < other.cond_overall.size())
00154             return true;
00155         if(cond_overall.size() > other.cond_overall.size())
00156             return false;
00157         if(cond_end.size() < other.cond_end.size())
00158             return true;
00159         if(cond_end.size() > other.cond_end.size())
00160             return false;
00161         if(lexicographical_compare(cond_start.begin(), cond_start.end(),
00162                     other.cond_start.begin(), other.cond_start.end()))
00163             return true;
00164         if(lexicographical_compare(other.cond_start.begin(), other.cond_start.end(),
00165             cond_start.begin(), cond_start.end()))
00166             return false;
00167         if(lexicographical_compare(cond_overall.begin(), cond_overall.end(),
00168             other.cond_overall.begin(), other.cond_overall.end()))
00169             return true;
00170         if(lexicographical_compare(other.cond_overall.begin(), other.cond_overall.end(),
00171             cond_overall.begin(), cond_overall.end()))
00172             return false;
00173         if(lexicographical_compare(cond_end.begin(), cond_end.end(),
00174                     other.cond_end.begin(), other.cond_end.end()))
00175             return true;
00176         if(lexicographical_compare(other.cond_end.begin(), other.cond_end.end(),
00177             cond_end.begin(), cond_end.end()))
00178             return false;
00179         return false;
00180     }
00181 };
00182 
00183 struct ScheduledModuleEffect : public ModuleEffect
00184 {
00185     double time_increment;
00186     ScheduledModuleEffect(double t, vector<Prevail> &cas,
00187             vector<Prevail> &coa, vector<Prevail> &cae,
00188             EffectModule *module) :
00189         ModuleEffect(cas, coa, cae, module), time_increment(t)
00190     {
00191         initialize();
00192     }
00193 
00194     ScheduledModuleEffect(double _time_increment,
00195             const ModuleEffect &_mod_eff) :
00196         ModuleEffect(_mod_eff), time_increment(_time_increment)
00197     {
00198     }
00199 
00200     void initialize()
00201     {
00202         sort(cond_start.begin(), cond_start.end());
00203         sort(cond_overall.begin(), cond_overall.end());
00204         sort(cond_end.begin(), cond_end.end());
00205     }
00206 
00207     bool operator<(const ScheduledModuleEffect &other) const
00208     {
00209         if(time_increment < other.time_increment)
00210             return true;
00211         if(time_increment > other.time_increment)
00212             return false;
00213         if(cond_start.size() < other.cond_start.size())
00214             return true;
00215         if(cond_start.size() > other.cond_start.size())
00216             return false;
00217         if(cond_overall.size() < other.cond_overall.size())
00218             return true;
00219         if(cond_overall.size() > other.cond_overall.size())
00220             return false;
00221         if(cond_end.size() < other.cond_end.size())
00222             return true;
00223         if(cond_end.size() > other.cond_end.size())
00224             return false;
00225         if(lexicographical_compare(cond_start.begin(), cond_start.end(),
00226                     other.cond_start.begin(), other.cond_start.end()))
00227             return true;
00228         if(lexicographical_compare(other.cond_start.begin(),
00229                     other.cond_start.end(), cond_start.begin(),
00230                     cond_start.end()))
00231             return false;
00232         if(lexicographical_compare(cond_overall.begin(),
00233                     cond_overall.end(), other.cond_overall.begin(),
00234                     other.cond_overall.end()))
00235             return true;
00236         if(lexicographical_compare(other.cond_overall.begin(),
00237                     other.cond_overall.end(), cond_overall.begin(),
00238                     cond_overall.end()))
00239             return false;
00240         if(lexicographical_compare(cond_end.begin(), cond_end.end(),
00241                     other.cond_end.begin(), other.cond_end.end()))
00242             return true;
00243         if(lexicographical_compare(other.cond_end.begin(),
00244                     other.cond_end.end(), cond_end.begin(), cond_end.end()))
00245             return false;
00246         return (module->internal_name.compare(other.module->internal_name));
00247     }
00248 };
00249 
00250 struct ScheduledCondition : public Prevail
00251 {
00252     double time_increment;
00253     ScheduledCondition(double t, int v, double p) :
00254         Prevail(v, p), time_increment(t)
00255     {
00256     }
00257     ScheduledCondition(double t, const Prevail &prev) : Prevail(prev), time_increment(t)
00258     {
00259     }
00260     bool operator<(const ScheduledCondition &other) const
00261     {
00262         if(time_increment < other.time_increment)
00263             return true;
00264         if(time_increment > other.time_increment)
00265             return false;
00266         if(var < other.var)
00267             return true;
00268         if(var > other.var)
00269             return false;
00270         return prev < other.prev;
00271     }
00272 };
00273 
00274 class ScheduledOperator;
00275 
00276 typedef std::pair<std::vector<double>, double> TimedSymbolicState;
00277 typedef std::vector<TimedSymbolicState> TimedSymbolicStates;
00278 
00279 class TimeStampedState
00280 {
00281     friend class RelaxedState;
00282     friend class AxiomEvaluator;
00283     friend struct PrePost;
00284     friend struct Prevail;
00285     friend class Operator;
00286     friend class NoHeuristic;
00287     friend class CyclicCGHeuristic;
00288     friend class ConsistencyCache;
00289     friend struct TssCompareIgnoreTimestamp;
00290 
00291     private:
00292         bool satisfies(const Prevail& cond) const
00293         {
00294             return cond.is_applicable(*this);
00295         }
00296 
00297         bool satisfies(const vector<Prevail>& conds) const
00298         {
00299             for(unsigned int i = 0; i < conds.size(); i++)
00300                 if(!satisfies(conds[i]))
00301                     return false;
00302             return true;
00303         }
00304 
00305         bool satisfies(const pair<int, double>& goal) const
00306         {
00307             return double_equals(state[goal.first], goal.second);
00308         }
00309 
00310         void apply_numeric_effect(int lhs, assignment_op op, int rhs)
00311         {
00312             switch(op) {
00313                 case assign:
00314                     state[lhs] = state[rhs];
00315                     break;
00316                 case scale_up:
00317                     state[lhs] *= state[rhs];
00318                     break;
00319                 case scale_down:
00320                     state[lhs] /= state[rhs];
00321                     break;
00322                 case increase:
00323                     state[lhs] += state[rhs];
00324                     break;
00325                 case decrease:
00326                     state[lhs] -= state[rhs];
00327                     break;
00328                 default:
00329                     assert(false);
00330                     break;
00331             }
00332         }
00333 
00334         void apply_discrete_effect(int var, double post)
00335         {
00336             state[var] = post;
00337         }
00338 
00339         void apply_module_effect(string internal_name);
00340 
00341         void apply_effect(int lhs, assignment_op op, int rhs, double post)
00342         {
00343             if(is_functional(lhs)) {
00344                 apply_numeric_effect(lhs, op, rhs);
00345             } else {
00346                 apply_discrete_effect(lhs, post);
00347             }
00348         }
00349 
00350         void initialize()
00351         {
00352             sort(scheduled_effects.begin(), scheduled_effects.end());
00353             sort(conds_over_all.begin(), conds_over_all.end());
00354             sort(conds_at_end.begin(), conds_at_end.end());
00355         }
00356         double eps_time(double offset) const;
00357 
00358     public:
00359         vector<double> state;
00360         vector<ScheduledEffect> scheduled_effects;
00361         vector<ScheduledModuleEffect> scheduled_module_effects;
00362         vector<ScheduledCondition> conds_over_all;
00363         vector<ScheduledCondition> conds_at_end;
00364 
00365         double timestamp;
00366         vector<ScheduledOperator> operators;
00367 
00368         TimeStampedState(istream &in);
00369         // apply an operator
00370         TimeStampedState(const TimeStampedState &predecessor, const Operator &op);
00371         // let time pass without applying an operator
00372         TimeStampedState let_time_pass(
00373             bool go_to_intermediate_between_now_and_next_happening = false,
00374             bool skip_eps_steps = false) const;
00375 
00376         TimeStampedState increase_time_stamp_by(double increment) const;
00377 
00378         double &operator[](int index)
00379         {
00380             return state[index];
00381         }
00382         double operator[](int index) const
00383         {
00384             return state[index];
00385         }
00386         void dump(bool verbose) const;
00387 
00388         void scheduleEffect(ScheduledEffect effect);
00389 
00390         double next_happening() const;
00391 
00392         bool is_consistent_now() const;
00393         bool is_consistent_when_progressed(TimedSymbolicStates* timedSymbolicStates) const;
00394 
00395         const double &get_timestamp() const
00396         {
00397             return timestamp;
00398         }
00399 
00400         bool satisfies(const vector<pair<int, double> >& goal) const
00401         {
00402             for(unsigned int i = 0; i < goal.size(); i++)
00403                 if(!satisfies(goal[i]))
00404                     return false;
00405             return true;
00406         }
00407 };
00408 
00409 TimeStampedState &buildTestState(TimeStampedState &state);
00410 
00411 #endif


tfd_modules
Author(s): Maintained by Christian Dornhege (see AUTHORS file).
autogenerated on Mon Oct 6 2014 07:52:06