globals.cpp
Go to the documentation of this file.
00001 #include "globals.h"
00002 
00003 #include <cstdlib>
00004 #include <iostream>
00005 #include <string>
00006 #include <vector>
00007 #include <map>
00008 #include <cassert>
00009 #include <cmath>
00010 using namespace std;
00011 
00012 #include "axioms.h"
00013 //#include "causal_graph.h"
00014 #include "domain_transition_graph.h"
00015 #include "operator.h"
00016 #include "state.h"
00017 #include "successor_generator.h"
00018 #include "plannerParameters.h"
00019 
00020 void PlanStep::dump() const
00021 {
00022     cout << start_time << ": " << op->get_name() << "[" << duration << "]"
00023         << endl;
00024 }
00025 
00026 void check_magic(istream &in, string magic)
00027 {
00028     string word;
00029     in >> word;
00030     if(word != magic) {
00031         cout << "Failed to match magic word '" << magic << "'." << endl;
00032         cout << "Got '" << word << "'." << endl;
00033         exit(1);
00034     }
00035 }
00036 
00037 void read_variables(istream &in)
00038 {
00039     check_magic(in, "begin_variables");
00040     int count;
00041     in >> count;
00042     for(int i = 0; i < count; i++) {
00043         string name;
00044         in >> name;
00045         g_variable_name.push_back(name);
00046         int range;
00047         in >> range;
00048         g_variable_domain.push_back(range);
00049         int layer;
00050         in >> layer;
00051         g_axiom_layers.push_back(layer);
00052         //identify variable type
00053         if(range == -3) {
00054             g_variable_types.push_back(costmodule);
00055         } else if (range == -2) {
00056             g_variable_types.push_back(module);
00057         } else if (range != -1) {
00058             g_variable_types.push_back(logical);
00059             //changes to comparison if a comparison axiom is detected
00060         } else {
00061             g_variable_types.push_back(primitive_functional);
00062             //changes to subterm_functional if a numeric axiom is detected
00063         }
00064     }
00065     check_magic(in, "end_variables");
00066 }
00067 
00068 void read_goal(istream &in)
00069 {
00070     check_magic(in, "begin_goal");
00071     int count;
00072     in >> count;
00073     for(int i = 0; i < count; i++) {
00074         int var;
00075         double val;
00076         in >> var >> val;
00077         g_goal.push_back(make_pair(var, val));
00078     }
00079     check_magic(in, "end_goal");
00080 }
00081 
00082 void dump_goal()
00083 {
00084     cout << "Goal Conditions:" << endl;
00085     for(int i = 0; i < g_goal.size(); i++)
00086         cout << "  " << g_variable_name[g_goal[i].first] << ": "
00087             << g_goal[i].second << endl;
00088 }
00089 
00090 void read_operators(istream &in)
00091 {
00092     int count;
00093     in >> count;
00094     for(int i = 0; i < count; i++)
00095         g_operators.push_back(Operator(in));
00096 }
00097 
00098 void read_logic_axioms(istream &in)
00099 {
00100     int count;
00101     in >> count;
00102     for(int i = 0; i < count; i++) {
00103         LogicAxiom *ax = new LogicAxiom(in);
00104         g_axioms.push_back(ax);
00105     }
00106 }
00107 
00108 void read_numeric_axioms(istream &in)
00109 {
00110     int count;
00111     in >> count;
00112     for(int i = 0; i < count; i++) {
00113         NumericAxiom *ax = new NumericAxiom(in);
00114         g_axioms.push_back(ax);
00115         // ax->dump();
00116     }
00117 }
00118 
00119 void evaluate_axioms_in_init()
00120 {
00121     g_axiom_evaluator = new AxiomEvaluator;
00122     g_axiom_evaluator->evaluate(*g_initial_state);
00123 }
00124 
00125 void read_everything(istream &in)
00126 {
00127     read_variables(in);
00128     read_oplinits(in);
00129     read_objects(in);
00130     read_pddl_translation(in);
00131     read_constant_facts(in);
00132     read_modules(in);
00133 
00134     g_initial_state = new TimeStampedState(in);
00135     //g_initial_state->dump();
00136     read_goal(in);
00137     read_operators(in);
00138     read_logic_axioms(in);
00139     read_numeric_axioms(in);
00140     evaluate_axioms_in_init();
00141     check_magic(in, "begin_SG");
00142     g_successor_generator = read_successor_generator(in);
00143     check_magic(in, "end_SG");
00144     g_causal_graph = new CausalGraph(in);
00145     DomainTransitionGraph::read_all(in);
00146 }
00147 
00148 void dump_everything()
00149 {
00150     cout << "Variables (" << g_variable_name.size() << "):" << endl;
00151     for(int i = 0; i < g_variable_name.size(); i++)
00152         cout << "  " << g_variable_name[i] << " (range "
00153             << g_variable_domain[i] << ")" << endl;
00154     cout << "Initial State:" << endl;
00155     g_initial_state->dump(true);
00156     dump_goal();
00157     cout << "Successor Generator:" << endl;
00158     g_successor_generator->dump();
00159     for(int i = 0; i < g_variable_domain.size(); i++)
00160         g_transition_graphs[i]->dump();
00161 }
00162 
00163 void dump_DTGs()
00164 {
00165     for(int i = 0; i < g_variable_domain.size(); i++) {
00166         cout << "DTG of variable " << i;
00167         g_transition_graphs[i]->dump();
00168     }
00169 }
00170 
00171 int g_last_arithmetic_axiom_layer;
00172 int g_comparison_axiom_layer;
00173 int g_first_logic_axiom_layer;
00174 int g_last_logic_axiom_layer;
00175 vector<string> g_variable_name;
00176 vector<int> g_variable_domain;
00177 vector<int> g_axiom_layers;
00178 vector<double> g_default_axiom_values;
00179 vector<variable_type> g_variable_types;
00180 ObjectTypeMap g_objectTypes;
00181 TimeStampedState *g_initial_state;
00182 vector<pair<int, double> > g_goal;
00183 vector<Operator> g_operators;
00184 vector<Axiom*> g_axioms;
00185 AxiomEvaluator *g_axiom_evaluator;
00186 SuccessorGenerator *g_successor_generator;
00187 vector<DomainTransitionGraph *> g_transition_graphs;
00188 CausalGraph *g_causal_graph;
00189 
00190 PlannerParameters g_parameters;
00191 
00192 Operator *g_let_time_pass;
00193 Operator *g_wait_operator;
00194 
00195 istream& operator>>(istream &is, assignment_op &aop)
00196 {
00197     string strVal;
00198     is >> strVal;
00199     if(!strVal.compare("="))
00200         aop = assign;
00201     else if(!strVal.compare("+"))
00202         aop = increase;
00203     else if(!strVal.compare("-"))
00204         aop = decrease;
00205     else if(!strVal.compare("*"))
00206         aop = scale_up;
00207     else if(!strVal.compare("/"))
00208         aop = scale_down;
00209     else {
00210         cout << "SEVERE ERROR: expected assignment operator, read in " << strVal << endl;
00211         assert(false);
00212     }
00213     return is;
00214 }
00215 
00216 ostream& operator<<(ostream &os, const assignment_op &aop)
00217 {
00218     switch (aop) {
00219         case assign:
00220             os << ":=";
00221             break;
00222         case scale_up:
00223             os << "*=";
00224             break;
00225         case scale_down:
00226             os << "/=";
00227             break;
00228         case increase:
00229             os << "+=";
00230             break;
00231         case decrease:
00232             os << "-=";
00233             break;
00234         default:
00235             cout << "Error: aop has value " << (int)aop << endl;
00236 
00237             assert(false);
00238             break;
00239     }
00240     return os;
00241 }
00242 
00243 istream& operator>>(istream &is, binary_op &bop)
00244 {
00245     string strVal;
00246     is >> strVal;
00247     if(!strVal.compare("+"))
00248         bop = add;
00249     else if(!strVal.compare("-"))
00250         bop = subtract;
00251     else if(!strVal.compare("*"))
00252         bop = mult;
00253     else if(!strVal.compare("/"))
00254         bop = divis;
00255     else if(!strVal.compare("<"))
00256         bop = lt;
00257     else if(!strVal.compare("<="))
00258         bop = le;
00259     else if(!strVal.compare("="))
00260         bop = eq;
00261     else if(!strVal.compare(">="))
00262         bop = ge;
00263     else if(!strVal.compare(">"))
00264         bop = gt;
00265     else if(!strVal.compare("!="))
00266         bop = ue;
00267     else {
00268         cout << strVal << " was read" << endl;
00269         assert(false);
00270     }
00271     return is;
00272 }
00273 
00274 ostream& operator<<(ostream &os, const binary_op &bop)
00275 {
00276     switch (bop) {
00277         case mult:
00278             os << "*";
00279             break;
00280         case divis:
00281             os << "/";
00282             break;
00283         case add:
00284             os << "+";
00285             break;
00286         case subtract:
00287             os << "-";
00288             break;
00289         case lt:
00290             os << "<";
00291             break;
00292         case le:
00293             os << "<=";
00294             break;
00295         case eq:
00296             os << "=";
00297             break;
00298         case ge:
00299             os << ">=";
00300             break;
00301         case gt:
00302             os << ">";
00303             break;
00304         case ue:
00305             os << "!=";
00306             break;
00307         default:
00308             assert(false);
00309             break;
00310     }
00311     return os;
00312 }
00313 
00314 istream& operator>>(istream &is, trans_type &tt)
00315 {
00316     string strVal;
00317     is >> strVal;
00318     if(!strVal.compare("s"))
00319         tt = start;
00320     else if(!strVal.compare("e"))
00321         tt = end;
00322     else if(!strVal.compare("c"))
00323         tt = compressed;
00324     else if(!strVal.compare("a"))
00325         tt = ax;
00326     else {
00327         cout << strVal << " was read." << endl;
00328         assert(false);
00329     }
00330     return is;
00331 }
00332 
00333 ostream& operator<<(ostream &os, const trans_type &tt)
00334 {
00335     switch (tt) {
00336         case start:
00337             os << "s";
00338             break;
00339         case end:
00340             os << "e";
00341             break;
00342         case compressed:
00343             os << "c";
00344             break;
00345         case ax:
00346             os << "a";
00347         default:
00348             // cout << "Error: Encountered binary operator " << bop << "." << endl;
00349             assert(false);
00350             break;
00351     }
00352     return os;
00353 }
00354 
00355 istream& operator>>(istream &is, condition_type &ct)
00356 {
00357     string strVal;
00358     is >> strVal;
00359     if(!strVal.compare("s"))
00360         ct = start_cond;
00361     else if(!strVal.compare("o"))
00362         ct = overall_cond;
00363     else if(!strVal.compare("e"))
00364         ct = end_cond;
00365     else if(!strVal.compare("a"))
00366         ct = ax_cond;
00367     else
00368         assert(false);
00369     return is;
00370 }
00371 
00372 ostream& operator<<(ostream &os, const condition_type &ct)
00373 {
00374     switch (ct) {
00375         case start_cond:
00376             os << "s";
00377             break;
00378         case overall_cond:
00379             os << "o";
00380             break;
00381         case end_cond:
00382             os << "e";
00383             break;
00384         case ax_cond:
00385             os << "a";
00386             break;
00387         default:
00388             assert(false);
00389     }
00390     return os;
00391 }
00392 
00393 void printSet(const set<int> s)
00394 {
00395     set<int>::const_iterator it;
00396     for(it = s.begin(); it != s.end(); ++it)
00397         cout << *it << ",";
00398     cout << endl;
00399 }
 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