helper_functions.cpp
Go to the documentation of this file.
00001 #include <cstdlib>
00002 #include <iostream>
00003 #include <fstream>
00004 #include <cassert>
00005 
00006 #include <string>
00007 #include <vector>
00008 using namespace std;
00009 
00010 #include "helper_functions.h"
00011 #include "state.h"
00012 #include "operator.h"
00013 #include "axiom.h"
00014 #include "variable.h"
00015 #include "successor_generator.h"
00016 #include "domain_transition_graph.h"
00017 
00018 void check_magic(istream &in, string magic) {
00019     string word;
00020     in >> word;
00021     if(word != magic) {
00022         cout << "Failed to match magic word '" << magic << "'." << endl;
00023         cout << "Got '" << word << "'." << endl;
00024         exit(1);
00025     }
00026 }
00027 
00028 void read_variables(istream &in, vector<Variable> &internal_variables,
00029         vector<Variable *> &variables) {
00030     check_magic(in, "begin_variables");
00031     int count;
00032     in >> count;
00033     internal_variables.reserve(count);
00034     // Important so that the iterators stored in variables are valid.
00035     for(int i = 0; i < count; i++) {
00036         internal_variables.push_back(Variable(in));
00037         variables.push_back(&internal_variables.back());
00038     }
00039     check_magic(in, "end_variables");
00040 }
00041 
00042 void read_n_strings(istream &in, vector<string> & output)
00043 {
00044     int count;
00045     in >> count;
00046 
00047     // read the rest of the count line (to get rid of trailing \n for consecutive getlines
00048     string dummy;
00049     getline(in, dummy);
00050 
00051     for (int i = 0; i < count; i++) {
00052         string str;
00053         getline(in, str);
00054         for (int i = 0; i < 3; ++i) {
00055             int start = str.find("@");
00056             if (start == -1)
00057                 break;
00058             str.replace(start, 1, " ");
00059         }
00060         output.push_back(str);
00061     }
00062 }
00063 
00064 void read_constants(istream &in, vector<string> & predConst,
00065         vector<string> & numConst)
00066 {
00067     check_magic(in, "begin_constant_facts");
00068     read_n_strings(in, predConst);
00069     read_n_strings(in, numConst);
00070     check_magic(in, "end_constant_facts");
00071 }
00072 
00073 void read_modules(istream &in, vector<string> & moduleInits,
00074         vector<string> & subplanGenerators,
00075         vector<ConditionModule> &condModules,
00076         vector<EffectModule> &effectModules,
00077         vector<ConditionModule> & costModules,
00078         const vector<Variable*> variables)
00079 {
00080     check_magic(in, "begin_modules");
00081     read_n_strings(in, moduleInits);
00082     read_n_strings(in, subplanGenerators);
00083     int count;
00084     in >> count;
00085     condModules.reserve(count);
00086     for (int i = 0; i < count; ++i) {
00087         condModules.push_back(ConditionModule(in, variables));
00088     }
00089     in >> count;
00090     effectModules.reserve(count);
00091     for (int i = 0; i < count; ++i) {
00092         effectModules.push_back(EffectModule(in, variables));
00093     }
00094     in >> count;
00095     costModules.reserve(count);
00096     for (int i = 0; i < count; i++) {
00097         costModules.push_back(ConditionModule(in, variables));
00098     }
00099     check_magic(in, "end_modules");
00100 }
00101 
00102 void read_oplinits(istream &in, vector<string>& oplinits)
00103 {
00104     check_magic(in, "begin_oplinits");
00105     read_n_strings(in, oplinits);
00106     check_magic(in, "end_oplinits");
00107 }
00108 
00109 void read_objects(istream &in, vector<string>& objects)
00110 {
00111     check_magic(in, "begin_objects");
00112     read_n_strings(in, objects);
00113     check_magic(in, "end_objects");
00114 }
00115 
00116 void read_translations(istream &in,
00117         vector<TranslatePredicate> &predicateTranslations, vector<
00118                 TranslateFunction> &functionTranslations,
00119         vector<Variable*> variables)
00120 {
00121     check_magic(in, "begin_pddl_translation");
00122     int count;
00123     in >> count;
00124     predicateTranslations.reserve(count);
00125     for (int i = 0; i < count; ++i) {
00126         predicateTranslations.push_back(TranslatePredicate(in, variables));
00127     }
00128     in >> count;
00129     functionTranslations.reserve(count);
00130     for (int i = 0; i < count; ++i) {
00131         functionTranslations.push_back(TranslateFunction(in, variables));
00132     }
00133     check_magic(in, "end_pddl_translation");
00134 
00135 }
00136 
00137 void read_goal(istream &in, const vector<Variable *> &variables,
00138         vector<pair<Variable*, int> > &goals) {
00139     check_magic(in, "begin_goal");
00140     int count;
00141     in >> count;
00142     for(int i = 0; i < count; i++) {
00143         int varNo, val;
00144         in >> varNo >> val;
00145         goals.push_back(make_pair(variables[varNo], val));
00146     }
00147     check_magic(in, "end_goal");
00148 }
00149 
00150 void dump_goal(const vector<pair<Variable*, int> > &goals) {
00151     cout << "Goal Conditions:" << endl;
00152     for(int i = 0; i < goals.size(); i++)
00153         cout << "  " << goals[i].first->get_name() << ": " << goals[i].second
00154             << endl;
00155 }
00156 
00157 void read_operators(istream &in, const vector<Variable *> &variables,
00158         vector<Operator> &operators) {
00159     int count;
00160     in >> count;
00161     for(int i = 0; i < count; i++)
00162         operators.push_back(Operator(in, variables));
00163 }
00164 
00165 void read_axioms_rel(istream &in, const vector<Variable *> &variables,
00166         vector<Axiom_relational> &axioms_rel) {
00167     int count;
00168     in >> count;
00169     for(int i = 0; i < count; i++)
00170         axioms_rel.push_back(Axiom_relational(in, variables));
00171 }
00172 
00173 void read_axioms_comp(istream &in, const vector<Variable *> &variables,
00174         vector<Axiom_functional> &axioms_func) {
00175     int count;
00176     in >> count;
00177     for(int i = 0; i < count; i++)
00178         axioms_func.push_back(Axiom_functional(in, variables, true));
00179 }
00180 
00181 void read_axioms_func(istream &in, const vector<Variable *> &variables,
00182         vector<Axiom_functional> &axioms_func) {
00183     int count;
00184     in >> count;
00185     for(int i = 0; i < count; i++)
00186         axioms_func.push_back(Axiom_functional(in, variables, false));
00187 }
00188 
00189 void read_preprocessed_problem_description(istream &in,
00190         vector<Variable> &internal_variables, vector<Variable *> &variables,
00191         State &initial_state, vector<pair<Variable*, int> > &goals,
00192         vector<Operator> &operators, vector<Axiom_relational> &axioms_rel,
00193         vector<Axiom_functional> &axioms_func,
00194         vector<string> &moduleInits,
00195         vector<string> &subplanGenerators,
00196         vector<ConditionModule> &condModules,
00197         vector<EffectModule> &effectModules,
00198         vector<ConditionModule> & costModules,
00199         vector<TranslatePredicate> &predicateTranslations, vector<
00200                 TranslateFunction> &functionTranslations,
00201         vector<string> & pred_constants, vector<string> & num_constants,
00202         vector<string>& objects,
00203         vector<string>& oplinits)
00204 {
00205     read_variables(in, internal_variables, variables);
00206     read_oplinits(in, oplinits);
00207     read_objects(in, objects);
00208     read_translations(in, predicateTranslations, functionTranslations,
00209             variables);
00210     read_constants(in, pred_constants, num_constants);
00211     read_modules(in, moduleInits, subplanGenerators, condModules,
00212             effectModules, costModules, variables);
00213     initial_state = State(in, variables);
00214     read_goal(in, variables, goals);
00215     read_operators(in, variables, operators);
00216     read_axioms_rel(in, variables, axioms_rel);
00217     read_axioms_comp(in, variables, axioms_func);
00218     read_axioms_func(in, variables, axioms_func);
00219 }
00220 
00221 void dump_preprocessed_problem_description(const vector<Variable *> &variables,
00222         const State &initial_state, const vector<pair<Variable*, int> > &goals,
00223         const vector<Operator> &operators,
00224         const vector<Axiom_relational> &axioms_rel,
00225         const vector<Axiom_functional> &axioms_func) {
00226 
00227     cout << "Variables (" << variables.size() << "):" << endl;
00228     for(int i = 0; i < variables.size(); i++)
00229         variables[i]->dump();
00230 
00231     cout << "Initial State:" << endl;
00232     initial_state.dump();
00233     dump_goal(goals);
00234 
00235     for(int i = 0; i < operators.size(); i++)
00236         operators[i].dump();
00237     for(int i = 0; i < axioms_rel.size(); i++)
00238         axioms_rel[i].dump();
00239     for(int i = 0; i < axioms_func.size(); i++)
00240         axioms_func[i].dump();
00241 }
00242 
00243 void dump_DTGs(const vector<Variable *> &ordering,
00244         vector<DomainTransitionGraph*> &transition_graphs) {
00245     for(int i = 0; i < transition_graphs.size(); i++) {
00246         cout << "Domain transition graph for variable " << ordering[i]->get_level()
00247             << " (original name: " << ordering[i]->get_name() << endl;
00248         transition_graphs[i]->dump();
00249     }
00250 }
00251 
00252 void generate_cpp_input(bool solveable_in_poly_time,
00253         const vector<Variable *> & ordered_vars,
00254         const vector<string> & moduleInits,
00255         const vector<string> & subplanGenerators,
00256         const vector<ConditionModule> &cond_modules,
00257         const vector<EffectModule> &eff_modules,
00258         const vector<ConditionModule> &cost_modules, const vector<
00259                 TranslatePredicate> &pred_translations, const vector<
00260                 TranslateFunction> &func_translations,
00261         const vector<string> & predConstants,
00262         const vector<string> & numConstants,
00263         const State &initial_state,
00264         const vector<pair<Variable*, int> > &goals,
00265         const vector<Operator> & operators,
00266         const vector<Axiom_relational> &axioms_rel,
00267         const vector<Axiom_functional> &axioms_func, const SuccessorGenerator &sg,
00268         const vector<DomainTransitionGraph*> transition_graphs,
00269         const CausalGraph &cg,
00270         const vector<string>& objects,
00271         const vector<string>& oplinits, ostream& outfile)
00272 {
00273     //ostream outfile;
00274     //outfile.open("output", ios::out);
00275     outfile << solveable_in_poly_time << endl; // 1 if true, else 0
00276     int var_count = ordered_vars.size();
00277     outfile << "begin_variables" << endl;
00278     outfile << var_count << endl;
00279     for(int i = 0; i < var_count; i++)
00280         outfile << ordered_vars[i]->get_name() << " "
00281             << ordered_vars[i]->get_range() << " " << ordered_vars[i]->get_layer()
00282             << endl;
00283     outfile << "end_variables" << endl;
00284     outfile << "begin_oplinits" << endl;
00285     outfile << oplinits.size() << endl;
00286     for (vector<string>::const_iterator it = oplinits.begin(); it
00287             != oplinits.end(); it++) {
00288         outfile << *it << endl;
00289     }
00290     outfile << "end_oplinits" << endl;
00291     outfile << "begin_objects" << endl;
00292     outfile << objects.size() << endl;
00293     for (vector<string>::const_iterator it = objects.begin(); it
00294             != objects.end(); it++) {
00295         outfile << *it << endl;
00296     }
00297     outfile << "end_objects" << endl;
00298     outfile << "begin_pddl_translation" << endl;
00299     int count = pred_translations.size();
00300     outfile << count << endl;
00301     for (int i = 0; i < count; ++i) {
00302         pred_translations[i].generate_cpp_input(outfile);
00303     }
00304     count = func_translations.size();
00305     outfile << count << endl;
00306     for (int i = 0; i < count; ++i) {
00307         func_translations[i].generate_cpp_input(outfile);
00308     }
00309     outfile << "end_pddl_translation" << endl;
00310     outfile << "begin_constant_facts" << endl;
00311     outfile << predConstants.size() << endl;
00312     for (vector<string>::const_iterator it = predConstants.begin(); it
00313             != predConstants.end(); it++) {
00314         outfile << *it << endl;
00315     }
00316     outfile << numConstants.size() << endl;
00317     for (vector<string>::const_iterator it = numConstants.begin(); it
00318             != numConstants.end(); it++) {
00319         outfile << *it << endl;
00320     }
00321     outfile << "end_constant_facts" << endl;
00322     outfile << "begin_modules" << endl;
00323     int modInitCount = moduleInits.size();
00324     outfile << modInitCount << endl;
00325     for (int i = 0; i < modInitCount; i++) {
00326         outfile << moduleInits.at(i) << endl;
00327     }
00328     int spgCount = subplanGenerators.size();
00329     outfile << spgCount << endl;
00330     for (int i = 0; i < spgCount; i++) {
00331         outfile << subplanGenerators.at(i) << endl;
00332     }
00333     int cond_mod_count = cond_modules.size();
00334     outfile << cond_mod_count << endl;
00335     for (int i = 0; i < cond_mod_count; ++i) {
00336         cond_modules[i].generate_cpp_input(outfile);
00337     }
00338     int eff_mod_count = eff_modules.size();
00339     outfile << eff_mod_count << endl;
00340     for (int i = 0; i < eff_mod_count; ++i) {
00341         eff_modules[i].generate_cpp_input(outfile);
00342     }
00343     int cost_mod_count = cost_modules.size();
00344     outfile << cost_mod_count << endl;
00345     for (int i = 0; i < cost_mod_count; ++i) {
00346         cost_modules[i].generate_cpp_input(outfile);
00347     }
00348     outfile << "end_modules" << endl;
00349     outfile << "begin_state" << endl;
00350     for(int i = 0; i < var_count; i++)
00351         outfile << initial_state[ordered_vars[i]] << endl; // for axioms default value
00352     outfile << "end_state" << endl;
00353 
00354     vector<int> ordered_goal_values;
00355     ordered_goal_values.resize(var_count, -1);
00356     for(int i = 0; i < goals.size(); i++) {
00357         int var_index = goals[i].first->get_level();
00358         ordered_goal_values[var_index] = goals[i].second;
00359     }
00360     outfile << "begin_goal" << endl;
00361     outfile << goals.size() << endl;
00362     for(int i = 0; i < var_count; i++)
00363         if(ordered_goal_values[i] != -1)
00364             outfile << i << " " << ordered_goal_values[i] << endl;
00365     outfile << "end_goal" << endl;
00366 
00367     outfile << operators.size() << endl;
00368     for(int i = 0; i < operators.size(); i++)
00369         operators[i].generate_cpp_input(outfile);
00370 
00371     outfile << axioms_rel.size() << endl;
00372     for(int i = 0; i < axioms_rel.size(); i++)
00373         axioms_rel[i].generate_cpp_input(outfile);
00374 
00375     outfile << axioms_func.size() << endl;
00376     for(int i = 0; i < axioms_func.size(); i++)
00377         axioms_func[i].generate_cpp_input(outfile);
00378 
00379     outfile << "begin_SG" << endl;
00380     cout << "printing SG " << endl;
00381     sg.generate_cpp_input(outfile);
00382     outfile << "end_SG" << endl;
00383 
00384     outfile << "begin_CG" << endl;
00385     cg.generate_cpp_input(outfile, ordered_vars);
00386     outfile << "end_CG" << endl;
00387 
00388     cout <<  var_count << endl;
00389     for(int i = 0; i < var_count; i++) {
00390         outfile << "begin_DTG" << endl;
00391         transition_graphs[i]->generate_cpp_input(outfile);
00392         outfile << "end_DTG" << endl;
00393     }
00394 
00395     //outfile.close();
00396 }
00397 
00398 compoperator get_inverse_op(compoperator op) {
00399     switch (op) {
00400         case lt:
00401             return ge;
00402             break;
00403         case le:
00404             return gt;
00405             break;
00406         case eq:
00407             return ue;
00408             break;
00409         case ge:
00410             return lt;
00411             break;
00412         case gt:
00413             return le;
00414             break;
00415         case ue:
00416             return eq;
00417             break;
00418         default:
00419             cout << "inverse requestet for " << op << endl;
00420             assert(false);
00421             return eq; // arbitrary foperator
00422     }
00423 }
00424 
00425 istream& operator>>(istream &is, foperator &fop) {
00426     string strVal;
00427     is >> strVal;
00428     if(!strVal.compare("="))
00429         fop = assign;
00430     else if(!strVal.compare("+"))
00431         fop = increase;
00432     else if(!strVal.compare("-"))
00433         fop = decrease;
00434     else if(!strVal.compare("*"))
00435         fop = scale_up;
00436     else if(!strVal.compare("/"))
00437         fop = scale_down;
00438     else
00439         assert(false);
00440     return is;
00441 }
00442 
00443 ostream& operator<<(ostream &os, const foperator &fop) {
00444     switch (fop) {
00445         case assign:
00446             os << "=";
00447             break;
00448         case scale_up:
00449             os << "*";
00450             break;
00451         case scale_down:
00452             os << "/";
00453             break;
00454         case increase:
00455             os << "+";
00456             break;
00457         case decrease:
00458             os << "-";
00459             break;
00460         default:
00461             cout << (int)fop << " was read" << endl;
00462             assert(false);
00463     }
00464     return os;
00465 }
00466 
00467 istream& operator>>(istream &is, compoperator &fop) {
00468     string strVal;
00469     is >> strVal;
00470     if(!strVal.compare("<"))
00471         fop = lt;
00472     else if(!strVal.compare("<="))
00473         fop = le;
00474     else if(!strVal.compare("="))
00475         fop = eq;
00476     else if(!strVal.compare(">="))
00477         fop = ge;
00478     else if(!strVal.compare(">"))
00479         fop = gt;
00480     else if(!strVal.compare("!="))
00481         fop = ue;
00482     else
00483         assert(false);
00484     return is;
00485 }
00486 
00487 ostream& operator<<(ostream &os, const compoperator &fop) {
00488     switch (fop) {
00489         case lt:
00490             os << "<";
00491             break;
00492         case le:
00493             os << "<=";
00494             break;
00495         case eq:
00496             os << "=";
00497             break;
00498         case ge:
00499             os << ">=";
00500             break;
00501         case gt:
00502             os << ">";
00503             break;
00504         case ue:
00505             os << "!=";
00506             break;
00507         default:
00508             cout << fop << " WAS READ" << endl;
00509             assert(false);
00510     }
00511     return os;
00512 }
00513 
00514 istream& operator>>(istream &is, trans_type &tt) {
00515     string strVal;
00516     is >> strVal;
00517     if(!strVal.compare("s"))
00518         tt = start;
00519     else if(!strVal.compare("e"))
00520         tt = end;
00521     else if(!strVal.compare("c"))
00522         tt = compressed;
00523     else if(!strVal.compare("a"))
00524         tt = ax_rel;
00525     else {
00526         cout << strVal << " was read." << endl;
00527         assert(false);
00528     }
00529     return is;
00530 }
00531 
00532 ostream& operator<<(ostream &os, const trans_type &tt) {
00533     switch (tt) {
00534         case start:
00535             os << "s";
00536             break;
00537         case end:
00538             os << "e";
00539             break;
00540         case compressed:
00541             os << "c";
00542             break;
00543         case ax_rel:
00544             os << "a";
00545         default:
00546             // cout << "Error: Encountered binary operator " << bop << "." << endl;
00547             assert(false);
00548             break;
00549     }
00550     return os;
00551 }
00552 
00553 istream& operator>>(istream &is, condition_type &ct) {
00554     string strVal;
00555     is >> strVal;
00556     if(!strVal.compare("s"))
00557         ct = start_cond;
00558     else if(!strVal.compare("o"))
00559         ct = overall_cond;
00560     else if(!strVal.compare("e"))
00561         ct = end_cond;
00562     else if(!strVal.compare("a"))
00563         ct = ax_cond;
00564     else
00565         assert(false);
00566     return is;
00567 }
00568 
00569 ostream& operator<<(ostream &os, const condition_type &ct) {
00570     switch (ct) {
00571         case start_cond:
00572             os << "s";
00573             break;
00574         case overall_cond:
00575             os << "o";
00576             break;
00577         case end_cond:
00578             os << "e";
00579             break;
00580         case ax_cond:
00581             os << "a";
00582             break;
00583         default:
00584             assert(false);
00585     }
00586     return os;
00587 }


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