convertor.cpp
Go to the documentation of this file.
00001 #include "MOMDP.h"
00002 #include "SparseVector.h"
00003 #include "SparseMatrix.h"
00004 #include "Parser.h"
00005 #include "POMDP.h"
00006 #include "GlobalResource.h"
00007 
00008 #include <string>
00009 #include <stdlib.h>
00010 #include <sstream>
00011 #include <fstream>
00012 
00013 using namespace std;
00014 using namespace momdp;
00015 
00016 void print_usage(const char* cmdName) 
00017 {
00018     cout << "Usage: " << cmdName << " POMDPModelFileName\n " << endl; 
00019     cout <<"Example:" <<endl;
00020     cout << "  " << cmdName << " Hallway.pomdp" << endl;
00021 }
00022 
00023 void writeSparseVector(ostream& out, SparseVector& sv, int numStates) {
00024 
00025     int index = 0;
00026     for(vector<SparseVector_Entry>::const_iterator bi = sv.data.begin(); bi != sv.data.end(); bi++)
00027     {
00028         if ( index == (signed) bi->index )
00029         {
00030             //non zero value
00031             out << bi->value;
00032         }
00033         else
00034         {
00035             //buffer up zero values
00036             for ( ;index < (signed) bi->index; index ++ )
00037             {
00038                 out << "0 ";
00039             }
00040             out << bi->value;
00041         }
00042 
00043         if ( index != numStates-1 )
00044         {
00045             out << " ";
00046         }
00047         index ++;
00048     }
00049 
00050     if ( index < numStates )
00051     {
00052         for ( ;index < numStates-1; index ++ )
00053         {
00054             out << "0 ";
00055         }
00056         out << "0";
00057     }
00058 }
00059 
00060 //writeout SparseMatrix to POMDPX entries
00061 void writeSparseMatrix(ostream& out, SparseMatrix& sm, SparseMatrix& smtr, int action, char type, int numStates) {
00062     
00063     //check sparsity of matrix
00064     if(sm.data.size() < (sm.size1_ * sm.size2_)/20)
00065     {
00066         vector<SparseVector_Entry>::const_iterator  di;
00067         FOR (c, sm.size2_) {
00068         SparseCol col = sm.col(c);
00069             for (di = col.begin(); di != col.end(); di++) {
00070 
00071                 out << "\n<Entry>\n<Instance>";
00072                 out << "a" << action << " " << "s" << di->index << " " << type << c;
00073                 out << "</Instance>\n<ProbTable>" << di->value << "</ProbTable></Entry>";
00074             }
00075         }
00076     }
00077     else{
00078         //use transposed matrix for dumping dense matrix
00079         
00080         vector<SparseVector_Entry>::const_iterator  di, col_end;
00081         out << "\n<Entry>\n<Instance>";
00082         out << "a" << action << " - - </Instance>\n<ProbTable>" ;
00083 
00084         FOR (c, smtr.size2_) {
00085             int index=0;
00086             SparseCol col = smtr.col(c);
00087             for (di = col.begin(); di != col.end(); di++) {
00088 
00089                 if ( index == (signed) di->index )
00090                 {
00091                     //non zero value
00092                     out << di->value;
00093                 }
00094                 else
00095                 {
00096                     //buffer up zero values
00097                     for ( ;index < (signed) di->index; index ++ )
00098                     {
00099                         out << "0 ";
00100                     }
00101                     out << di->value;
00102                 }
00103 
00104                 if ( index != numStates-1 )
00105                 {
00106                     out << " ";
00107                 }
00108                 index ++;
00109             }
00110 
00111             //ensure dense matrix is of correct dimension
00112             if ( index < numStates )
00113             {
00114                 for ( ;index < numStates-1; index ++ )
00115                 {
00116                     out << "0 ";
00117                 }
00118                 out << "0";
00119             }
00120             out << endl;
00121         }
00122         
00123         out << "</ProbTable></Entry>";
00124     }
00125 }
00126 
00127 //writeout SparseMatrix to POMDPX reward entries
00128 void writeSparseMatrixReward(ostream& out, SparseMatrix& sm)
00129 {
00130     vector<SparseVector_Entry>::const_iterator  di, col_end;
00131     FOR (c, sm.size2_) {
00132         SparseCol col = sm.col(c);
00133         for (di = col.begin(); di != col.end(); di++) {
00134 
00135             out << "\n<Entry>\n<Instance>";
00136             out << "a" << c << " s" << di->index;
00137             out << "</Instance>\n<ValueTable>" << di->value << "</ValueTable></Entry>";
00138         }
00139     }
00140 }
00141 
00142 void convertToPomdpx(POMDP* problem, ofstream& pomdpxfile){
00143 
00144     pomdpxfile << "<?xml version='1.0' encoding='ISO-8859-1'?>\n \
00145         \n\
00146         \n\
00147         <pomdpx version='0.1' id='autogenerated' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='pomdpx.xsd'>\n\
00148         \n\
00149         \n<Description>This is an auto-generated POMDPX file</Description>\
00150         \n<Discount>" << problem->getDiscount() << "</Discount>           \
00151         \n\
00152         \n<Variable>\
00153         \n\
00154         \n<StateVar vnamePrev=\"state_0\" vnameCurr=\"state_1\" fullyObs=\"false\">\
00155         \n<NumValues>" << problem->numStates << "</NumValues>\
00156         \n</StateVar>\
00157         \n\
00158         \n<ObsVar vname=\"obs_sensor\">\
00159         \n<NumValues>" << problem->numObservations << "</NumValues>\
00160         \n</ObsVar>\
00161         \n\
00162         \n<ActionVar vname=\"action_agent\">\
00163         \n<NumValues>" << problem->T.size() << "</NumValues>\
00164         \n</ActionVar>\
00165         \n\
00166         \n<RewardVar vname=\"reward_agent\"/>\
00167         \n</Variable>\
00168         \n\
00169         \n\
00170         \n<InitialStateBelief>\
00171         \n<CondProb>\
00172         \n<Var>state_0</Var>\
00173         \n<Parent>null</Parent>\
00174         \n<Parameter type = \"TBL\">\
00175         \n<Entry>\
00176         \n<Instance>-</Instance>\
00177         \n<ProbTable>";
00178     writeSparseVector(pomdpxfile, problem->getInitialBelief(), problem->numStates);
00179 
00180     pomdpxfile << "</ProbTable>\n \
00181         \n</Entry>\
00182         \n</Parameter>\
00183         \n</CondProb>\
00184         \n</InitialStateBelief>";
00185 
00186     pomdpxfile << "<StateTransitionFunction>\n \
00187         \n<CondProb>\
00188         \n<Var>state_1</Var>\
00189         \n<Parent>action_agent state_0</Parent>\
00190         \n<Parameter type = \"TBL\">";
00191 
00192     for (unsigned int i=0; i < problem->T.size(); i++) {
00193         SparseMatrix sm = problem->T[i];
00194         SparseMatrix smtr = problem->Ttr[i];
00195         writeSparseMatrix(pomdpxfile, sm, smtr, i, 's', problem->numStates);
00196     }
00197 
00198     pomdpxfile << "\
00199         \n</Parameter>\
00200         \n</CondProb>\
00201         \n</StateTransitionFunction>\n\n";
00202 
00203     pomdpxfile << "<ObsFunction>\n \
00204         \n<CondProb>\
00205         \n<Var>obs_sensor</Var>\
00206         \n<Parent>action_agent state_1</Parent>\
00207         \n<Parameter type = \"TBL\">";
00208 
00209     for (unsigned int i=0; i < problem->O.size(); i++) {
00210         SparseMatrix sm = problem->O[i];
00211         SparseMatrix smtr = problem->Otr[i];
00212         writeSparseMatrix(pomdpxfile, sm, smtr, i, 'o', problem->numObservations);
00213     }
00214 
00215     pomdpxfile << "\
00216         \n</Parameter>\
00217         \n</CondProb>\
00218         \n</ObsFunction>\n\n";
00219 
00220     pomdpxfile << "<RewardFunction>\n \
00221         \n<Func>\
00222         \n<Var>reward_agent</Var>\
00223         \n<Parent>action_agent state_0</Parent>\
00224         \n<Parameter type = \"TBL\">";
00225 
00226     SparseMatrix sm = problem->getRewardMatrix();
00227     writeSparseMatrixReward(pomdpxfile, sm);
00228     pomdpxfile << "\
00229         \n</Parameter>\
00230         \n</Func>\
00231         \n</RewardFunction>";
00232 
00233     pomdpxfile << "</pomdpx>";
00234 }
00235 
00236 
00237 int main(int argc, char **argv) 
00238 {
00239     try
00240     {
00241         SolverParams* p =&GlobalResource::getInstance()->solverParams;
00242         bool parseCorrect = SolverParams::parseCommandLineOption(argc, argv, *p);
00243         if(!parseCorrect)
00244         {
00245             print_usage(p->cmdName);
00246             exit(EXIT_FAILURE);
00247         }
00248         Parser* parser = new Parser();
00249         POMDP* pomdpProblem = parser->parse(p->problemName, p->useFastParser);
00250 
00251         ofstream pomdpxFile((p->problemName.append("x")).c_str());
00252         convertToPomdpx(pomdpProblem, pomdpxFile);      
00253         pomdpxFile.flush();
00254         pomdpxFile.close();
00255     }
00256     catch(bad_alloc &e)
00257     {
00258         if(GlobalResource::getInstance()->solverParams.memoryLimit == 0)
00259         {
00260             cout << "Memory allocation failed. Exit." << endl;
00261         }
00262         else
00263         {
00264             cout << "Memory limit reached. Please try increase memory limit" << endl;
00265         }
00266 
00267     }
00268     catch(exception &e)
00269     {
00270         cout << "Exception: " << e.what() << endl ;
00271     }
00272 
00273     return 0;
00274 }
00275 


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:28