operator.cpp
Go to the documentation of this file.
00001 #include "helper_functions.h"
00002 #include "operator.h"
00003 #include "variable.h"
00004 
00005 #include <cassert>
00006 #include <iostream>
00007 #include <fstream>
00008 using namespace std;
00009 
00010 Operator::Operator(istream &in, const vector<Variable *> &variables) {
00011     check_magic(in, "begin_operator");
00012     in >> ws;
00013     getline(in, name);
00014     int varNo;
00015     compoperator cop;
00016     in >> cop >> varNo;
00017     duration_cond = DurationCond(cop, variables[varNo]);
00018     int count;
00019     variables[varNo]->set_used_in_duration_condition();
00020     in >> count; // number of prevail at-start conditions
00021     for(int i = 0; i < count; i++) {
00022         int varNo, val;
00023         in >> varNo >> val;
00024         prevail_start.push_back(Prevail(variables[varNo], val));
00025     }
00026     in >> count; // number of prevail overall conditions
00027     for(int i = 0; i < count; i++) {
00028         int varNo, val;
00029         in >> varNo >> val;
00030         prevail_overall.push_back(Prevail(variables[varNo], val));
00031     }
00032     in >> count; // number of prevail at-end conditions
00033     for(int i = 0; i < count; i++) {
00034         int varNo, val;
00035         in >> varNo >> val;
00036         prevail_end.push_back(Prevail(variables[varNo], val));
00037     }
00038     in >> count; // number of pre_post_start conditions
00039     for(int i = 0; i < count; i++) {
00040         int eff_conds;
00041         vector<EffCond> ecs_start;
00042         vector<EffCond> ecs_overall;
00043         vector<EffCond> ecs_end;
00044         in >> eff_conds;
00045         for(int j = 0; j < eff_conds; j++) {
00046             int var, value;
00047             in >> var >> value;
00048             ecs_start.push_back(EffCond(variables[var], value));
00049         }
00050         in >> eff_conds;
00051         for(int j = 0; j < eff_conds; j++) {
00052             int var, value;
00053             in >> var >> value;
00054             ecs_overall.push_back(EffCond(variables[var], value));
00055         }
00056         in >> eff_conds;
00057         for(int j = 0; j < eff_conds; j++) {
00058             int var, value;
00059             in >> var >> value;
00060             ecs_end.push_back(EffCond(variables[var], value));
00061         }
00062         string effect;
00063         in >> effect;
00064         if(effect.at(0) != 'm') {
00065             int varNo = atoi(effect.c_str());
00066             if(variables[varNo]->is_functional()) {
00067                 foperator assignop;
00068                 int varNo2;
00069                 in >> assignop >> varNo2;
00070                 if(eff_conds)
00071                     numerical_effs_start.push_back(NumericalEffect(
00072                             variables[varNo], ecs_start, ecs_overall, ecs_end,
00073                             assignop, variables[varNo2]));
00074                 else
00075                     numerical_effs_start.push_back(NumericalEffect(
00076                             variables[varNo], assignop, variables[varNo2]));
00077             } else {
00078                 int val, newVal;
00079                 in >> val >> newVal;
00080                 if(eff_conds)
00081                     pre_post_start.push_back(PrePost(variables[varNo], ecs_start,
00082                             ecs_overall, ecs_end, val, newVal));
00083                 else
00084                     pre_post_start.push_back(PrePost(variables[varNo], val, newVal));
00085             }
00086         } else {
00087             if (eff_conds)
00088                 module_effs_start.push_back(ModuleEffect(effect, ecs_start,
00089                         ecs_overall, ecs_end));
00090             else
00091                 module_effs_start.push_back(ModuleEffect(effect));
00092         }
00093     }
00094     in >> count; // number of pre_post_end conditions
00095     for(int i = 0; i < count; i++) {
00096         int eff_conds;
00097         vector<EffCond> ecs_start;
00098         vector<EffCond> ecs_overall;
00099         vector<EffCond> ecs_end;
00100         in >> eff_conds;
00101         for(int j = 0; j < eff_conds; j++) {
00102             int var, value;
00103             in >> var >> value;
00104             ecs_start.push_back(EffCond(variables[var], value));
00105         }
00106         in >> eff_conds;
00107         for(int j = 0; j < eff_conds; j++) {
00108             int var, value;
00109             in >> var >> value;
00110             ecs_overall.push_back(EffCond(variables[var], value));
00111         }
00112         in >> eff_conds;
00113         for(int j = 0; j < eff_conds; j++) {
00114             int var, value;
00115             in >> var >> value;
00116             ecs_end.push_back(EffCond(variables[var], value));
00117         }
00118         string effect;
00119         in >> effect;
00120         if(effect.at(0) != 'm') {
00121             int varNo = atoi(effect.c_str());
00122             if(variables[varNo]->is_functional()) {
00123                 foperator assignop;
00124                 int varNo2;
00125                 in >> assignop >> varNo2;
00126                 if(eff_conds)
00127                     numerical_effs_end.push_back(NumericalEffect(variables[varNo],
00128                             ecs_start, ecs_overall, ecs_end, assignop,
00129                             variables[varNo2]));
00130                 else
00131                     numerical_effs_end.push_back(NumericalEffect(variables[varNo],
00132                             assignop, variables[varNo2]));
00133             } else {
00134                 int val, newVal;
00135                 in >> val >> newVal;
00136                 if(eff_conds)
00137                     pre_post_end.push_back(PrePost(variables[varNo], ecs_start,
00138                             ecs_overall, ecs_end, val, newVal));
00139                 else
00140                     pre_post_end.push_back(PrePost(variables[varNo], val, newVal));
00141             }
00142         } else {
00143             if (eff_conds)
00144                 module_effs_start.push_back(ModuleEffect(effect, ecs_start,
00145                         ecs_overall, ecs_end));
00146             else
00147                 module_effs_start.push_back(ModuleEffect(effect));
00148         }
00149     }
00150     check_magic(in, "end_operator");
00151 }
00152 
00153 void Operator::dump() const {
00154     cout << name << ":" << endl;
00155     cout << "duration:" << duration_cond.op << " "
00156         << duration_cond.var->get_name();
00157     cout << endl;
00158     cout << "prevail (at-start):";
00159     for(int i = 0; i < prevail_start.size(); i++)
00160         cout << "  " << prevail_start[i].var->get_name() << " := "
00161             << prevail_start[i].prev;
00162     cout << endl;
00163     cout << "prevail (overall):";
00164     for(int i = 0; i < prevail_overall.size(); i++)
00165         cout << "  " << prevail_overall[i].var->get_name() << " := "
00166             << prevail_overall[i].prev;
00167     cout << endl;
00168     cout << "prevail (at-end):";
00169     for(int i = 0; i < prevail_end.size(); i++)
00170         cout << "  " << prevail_end[i].var->get_name() << " := "
00171             << prevail_end[i].prev;
00172     cout << endl;
00173     cout << "pre-post (at-start):";
00174     for(int i = 0; i < pre_post_start.size(); i++) {
00175         if(pre_post_start[i].is_conditional_effect) {
00176             cout << "  if at-start(";
00177             for(int j = 0; j < pre_post_start[i].effect_conds_start.size(); j++)
00178                 cout << pre_post_start[i].effect_conds_start[j].var->get_name()
00179                     << " := "
00180                     << pre_post_start[i].effect_conds_start[j].cond;
00181             cout << ")";
00182             cout << "  if overall(";
00183             for(int j = 0; j < pre_post_start[i].effect_conds_overall.size(); j++)
00184                 cout
00185                     << pre_post_start[i].effect_conds_overall[j].var->get_name()
00186                     << " := "
00187                     << pre_post_start[i].effect_conds_overall[j].cond;
00188             cout << ")";
00189             cout << "  if at-end(";
00190             for(int j = 0; j < pre_post_start[i].effect_conds_end.size(); j++)
00191                 cout << pre_post_start[i].effect_conds_end[j].var->get_name()
00192                     << " := " << pre_post_start[i].effect_conds_end[j].cond;
00193             cout << ")";
00194         }
00195         cout << " " << pre_post_start[i].var->get_name() << " : "
00196             << pre_post_start[i].pre <<" -> "<< pre_post_start[i].post;
00197     }
00198     cout << endl;
00199     cout << "pre-post (at-end):";
00200     for(int i = 0; i < pre_post_end.size(); i++) {
00201         if(pre_post_end[i].is_conditional_effect) {
00202             cout << "  if at-start(";
00203             for(int j = 0; j < pre_post_end[i].effect_conds_start.size(); j++)
00204                 cout << pre_post_end[i].effect_conds_start[j].var->get_name()
00205                     << " := " << pre_post_end[i].effect_conds_start[j].cond;
00206             cout << ")";
00207             cout << "  if overall(";
00208             for(int j = 0; j < pre_post_end[i].effect_conds_overall.size(); j++)
00209                 cout << pre_post_end[i].effect_conds_overall[j].var->get_name()
00210                     << " := "
00211                     << pre_post_end[i].effect_conds_overall[j].cond;
00212             cout << ")";
00213             cout << "  if at-end(";
00214             for(int j = 0; j < pre_post_end[i].effect_conds_end.size(); j++)
00215                 cout << pre_post_end[i].effect_conds_end[j].var->get_name()
00216                     << " := " << pre_post_end[i].effect_conds_end[j].cond;
00217             cout << ")";
00218         }
00219         cout << " " << pre_post_end[i].var->get_name() << " : "
00220             << pre_post_end[i].pre <<" -> "<< pre_post_end[i].post;
00221     }
00222     cout << endl;
00223     cout << "numerical effects (at-start):";
00224     for(int i = 0; i< numerical_effs_start.size(); i++) {
00225         if(numerical_effs_start[i].is_conditional_effect) {
00226             cout << "  if at-start(";
00227             for(int j = 0; j
00228                     < numerical_effs_start[i].effect_conds_start.size(); j++)
00229                 cout
00230                     << numerical_effs_start[i].effect_conds_start[j].var->get_name()
00231                     << " := "
00232                     << numerical_effs_start[i].effect_conds_start[j].cond;
00233             cout << ")";
00234             cout << "  if overall(";
00235             for(int j = 0; j
00236                     < numerical_effs_start[i].effect_conds_overall.size(); j++)
00237                 cout
00238                     << numerical_effs_start[i].effect_conds_overall[j].var->get_name()
00239                     << " := "
00240                     << numerical_effs_start[i].effect_conds_overall[j].cond;
00241             cout << ")";
00242             cout << "  if at-end(";
00243             for(int j = 0; j
00244                     < numerical_effs_start[i].effect_conds_end.size(); j++)
00245                 cout
00246                     << numerical_effs_start[i].effect_conds_end[j].var->get_name()
00247                     << " := "
00248                     << numerical_effs_start[i].effect_conds_end[j].cond;
00249             cout << ")";
00250 
00251         }
00252         cout << " " << numerical_effs_start[i].var->get_name() << " "
00253             << numerical_effs_start[i].fop << " "
00254             << numerical_effs_start[i].foperand->get_name();
00255     }
00256     cout << endl;
00257     cout << "numerical effects (at-end):";
00258     for(int i = 0; i< numerical_effs_end.size(); i++) {
00259         if(numerical_effs_end[i].is_conditional_effect) {
00260             cout << "  if at-start(";
00261             for(int j = 0; j < numerical_effs_end[i].effect_conds_start.size(); j++)
00262                 cout
00263                     << numerical_effs_end[i].effect_conds_start[j].var->get_name()
00264                     << " := "
00265                     << numerical_effs_end[i].effect_conds_start[j].cond;
00266             cout << ")";
00267             cout << "  if overall(";
00268             for(int j = 0; j
00269                     < numerical_effs_end[i].effect_conds_overall.size(); j++)
00270                 cout
00271                     << numerical_effs_end[i].effect_conds_overall[j].var->get_name()
00272                     << " := "
00273                     << numerical_effs_end[i].effect_conds_overall[j].cond;
00274             cout << ")";
00275             cout << "  if at-end(";
00276             for(int j = 0; j < numerical_effs_end[i].effect_conds_end.size(); j++)
00277                 cout
00278                     << numerical_effs_end[i].effect_conds_end[j].var->get_name()
00279                     << " := "
00280                     << numerical_effs_end[i].effect_conds_end[j].cond;
00281             cout << ")";
00282         }
00283         cout << " " << numerical_effs_end[i].var->get_name() << " "
00284             << numerical_effs_end[i].fop << " "
00285             << numerical_effs_end[i].foperand->get_name();
00286     }
00287     cout << endl;
00288 }
00289 
00290 void Operator::strip_unimportant_effects() {
00291     int new_index = 0;
00292     for(int i = 0; i < pre_post_start.size(); i++) {
00293         if(pre_post_start[i].var->get_level() != -1)
00294             pre_post_start[new_index++] = pre_post_start[i];
00295     }
00296     pre_post_start.erase(pre_post_start.begin() + new_index,
00297             pre_post_start.end());
00298     new_index = 0;
00299     for(int i = 0; i < pre_post_end.size(); i++) {
00300         if(pre_post_end[i].var->get_level() != -1)
00301             pre_post_end[new_index++] = pre_post_end[i];
00302     }
00303     pre_post_end.erase(pre_post_end.begin() + new_index, pre_post_end.end());
00304     new_index = 0;
00305     for(int i = 0; i < numerical_effs_start.size(); i++) {
00306         if(numerical_effs_start[i].var->get_level() != -1)
00307             numerical_effs_start[new_index++] = numerical_effs_start[i];
00308     }
00309     numerical_effs_start.erase(numerical_effs_start.begin() + new_index, numerical_effs_start.end());
00310     new_index = 0;
00311     for(int i = 0; i < numerical_effs_end.size(); i++) {
00312         if(numerical_effs_end[i].var->get_level() != -1)
00313             numerical_effs_end[new_index++] = numerical_effs_end[i];
00314     }
00315     numerical_effs_end.erase(numerical_effs_end.begin() + new_index, numerical_effs_end.end());
00316 }
00317 
00318 bool Operator::is_redundant() const {
00319     return (pre_post_start.empty() && pre_post_end.empty()
00320             && numerical_effs_start.empty() && numerical_effs_end.empty());//FIXME: Is this correct?
00321 }
00322 
00323 void strip_operators(vector<Operator> &operators) {
00324     int old_count = operators.size();
00325     int new_index = 0;
00326     for(int i = 0; i < operators.size(); i++) {
00327         operators[i].strip_unimportant_effects();
00328         if(!operators[i].is_redundant())
00329             operators[new_index++] = operators[i];
00330     }
00331     operators.erase(operators.begin() + new_index, operators.end());
00332     cout << operators.size() << " of " << old_count << " operators necessary."
00333         << endl;
00334 }
00335 void Operator::write_prevails(ostream &outfile, const vector<Prevail> &prevails) const {
00336     outfile << prevails.size() << endl;
00337     for(int i = 0; i < prevails.size(); i++) {
00338         assert(prevails[i].var->get_level() != -1);
00339         if(prevails[i].var->get_level() != -1)
00340             outfile << prevails[i].var->get_level() << " "
00341                 << prevails[i].prev << endl;
00342     }
00343 }
00344 
00345 void Operator::write_effect_conds(ostream &outfile, const vector<EffCond> &conds) const {
00346     outfile << conds.size() << endl;
00347     for (int j = 0; j < conds.size(); j++)
00348         outfile << conds[j].var->get_level() << " " << conds[j].cond << endl;
00349 }
00350 
00351 void Operator::write_pre_posts(ostream &outfile, const vector<PrePost> &pre_posts) const {
00352     outfile << pre_posts.size() << endl;
00353     for (int i = 0; i < pre_posts.size(); i++) {
00354         assert(pre_posts[i].var->get_level() != -1);
00355         write_effect_conds(outfile, pre_posts[i].effect_conds_start);
00356         write_effect_conds(outfile, pre_posts[i].effect_conds_overall);
00357         write_effect_conds(outfile, pre_posts[i].effect_conds_end);
00358         outfile << pre_posts[i].var->get_level() << " " << pre_posts[i].pre << " "
00359             << pre_posts[i].post << endl;
00360     }
00361 }
00362 
00363 void Operator::write_num_effect(ostream &outfile,
00364         const vector<NumericalEffect> &num_effs) const {
00365     outfile << num_effs.size() << endl;
00366     for (int i = 0; i < num_effs.size(); i++) {
00367         assert(num_effs[i].var->get_level() != -1);
00368         write_effect_conds(outfile, num_effs[i].effect_conds_start);
00369         write_effect_conds(outfile, num_effs[i].effect_conds_overall);
00370         write_effect_conds(outfile, num_effs[i].effect_conds_end);
00371         outfile << num_effs[i].var->get_level() << " " << num_effs[i].fop
00372             << " " << num_effs[i].foperand->get_level() << endl;
00373     }
00374 }
00375 
00376 void Operator::write_module_effect(ostream &outfile,
00377         const vector<ModuleEffect> &mod_effs) const
00378 {
00379     outfile << mod_effs.size() << endl;
00380     for (int i = 0; i < mod_effs.size(); ++i) {
00381         // whoever C&P this - fix it    assert(numeffs[i].var->get_level() != -1);
00382         write_effect_conds(outfile, mod_effs[i].effect_conds_start);
00383         write_effect_conds(outfile, mod_effs[i].effect_conds_overall);
00384         write_effect_conds(outfile, mod_effs[i].effect_conds_end);
00385         outfile << mod_effs[i].name << endl;
00386     }
00387 }
00388 
00389 void Operator::generate_cpp_input(ostream &outfile) const {
00390     // beim Einlesen in search feststellen, ob leerer Operator
00391     outfile << "begin_operator" << endl;
00392     outfile << name << endl;
00393     //duration
00394     outfile << duration_cond.op << " " << duration_cond.var->get_level()
00395         << endl;
00396     write_prevails(outfile, prevail_start);
00397     write_prevails(outfile, prevail_overall);
00398     write_prevails(outfile, prevail_end);
00399 
00400     write_pre_posts(outfile, pre_post_start);
00401     write_pre_posts(outfile, pre_post_end);
00402 
00403     write_num_effect(outfile, numerical_effs_start);
00404     write_num_effect(outfile, numerical_effs_end);
00405 
00406     write_module_effect(outfile, module_effs_start);
00407     write_module_effect(outfile, module_effs_end);
00408 
00409     outfile << "end_operator" << endl;
00410 }


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