Simulator.cpp
Go to the documentation of this file.
00001 
00009 #include "SimulationEngine.h"
00010 #include "GlobalResource.h"
00011 #include "SimulationRewardCollector.h"
00012 #include <string>
00013 #include <stdlib.h>
00014 #include <sstream>
00015 #include <fstream>
00016 #include <ctime>
00017 
00018 #include "CPTimer.h"
00019 
00020 #ifdef _MSC_VER
00021 #else
00022 //for timing
00023 #include <sys/param.h>
00024 #include <sys/types.h>
00025 #include <sys/times.h>
00026 //end for timing
00027 #endif
00028 
00029 #include "MOMDP.h"
00030 #include "ParserSelector.h"
00031 #include "AlphaVectorPolicy.h"
00032 
00033 using namespace std;
00034 using namespace momdp;
00035 
00036 void print_usage(const char* cmdName) 
00037 {
00038         cout << "Usage: " << cmdName << " POMDPModelFileName --policy-file policyFileName --simLen numberSteps \n" 
00039 <<"     --simNum numberSimulations [--fast] [--srand randomSeed] [--output-file outputFileName]\n" 
00040 <<"    or " << cmdName << " --help (or -h)  Print this help\n" 
00041 <<"    or " << cmdName << " --version     Print version information\n" 
00042 <<"\n"
00043 <<"Simulator options:\n"
00044 <<"  --policy-file policyFileName       Use policyFileName as the policy file name (compulsory).\n"
00045 <<"  --simLen numberSteps               Use numberSteps as the number of steps for each\n" 
00046 <<"                             simulation run (compulsory).\n"
00047 <<"  --simNum numberSimulations Use numberSimulations as the number of simulation runs\n" 
00048 <<"                             (compulsory).\n"
00049 <<"  -f or --fast                       Use fast (but very picky) alternate parser for .pomdp files.\n"
00050 <<"  --srand randomSeed         Set randomSeed as the random seed for simulation.\n" 
00051 <<"                             It is the current time by default.\n"
00052 //<<"  --lookahead yes/no               Set 'yes' ('no') to select action with (without) one-step\n" 
00053 //<<"                           look ahead. Action selection is with one-step look ahead\n" 
00054 //<<"                           by default.\n" 
00055 <<"\n"
00056 <<"Output options:\n"
00057 <<"  --output-file outputFileName       Use outputFileName as the name for the output file\n" 
00058 <<"                             that contains the simulation trace.\n"
00059                 << "Example:\n"
00060                 << "  " << cmdName << " --simLen 100 --simNum 100 --policy-file out.policy Hallway.pomdp\n";
00061 
00062 //      cout << "usage: binary [options] problem:\n"
00063 //              << "--help, print this message\n"
00064 //              << "--policy-file, policy file to be used\n"
00065 //              << "--output-file, output file to be used\n"
00066 //              << "--simLen, length of simulation\n"
00067 //              << "--simNum, number of simulations\n"
00068 //              << "--srand, random seed (default: current time)\n"
00069 //              << "--lookahead, use \"one-step look ahead\" when selecting action (default: yes)\n"
00070 //              << "Examples:\n"
00071 //              << " ./simulate --simLen 100 --simNum 100 --policy-file out.policy Hallway.pomdp\n";
00072 
00073 }
00074 
00075 void generateSimLog(SolverParams& p, double& globalExpRew, double& confInterval)
00076 {
00077     int length;
00078     char str1[102];
00079     string str_comb;
00080 
00081     int startpos = 0;
00082     int i;
00083     for (i = p.problemName.length() - 1; i >= 0; i--) {
00084         if (p.problemName[i] == '/') {
00085             startpos = i + 1;
00086             break;
00087         }
00088     }
00089 
00090     str_comb.append(p.problemName.begin() + startpos, p.problemName.end());
00091 
00092     str_comb.append("SimLog");
00093     cout << str_comb << endl;
00094 
00095     length = str_comb.copy(str1, 100);
00096     str1[length] = '\0';
00097 
00098     FILE *fp = fopen(str1, "a");
00099 
00100     //  FILE *fp = fopen("sim.log","a");
00101     if (fp == NULL) 
00102     {
00103         cerr << "cant open sim logfile\n";
00104         exit(1);
00105     }
00106 
00107     fprintf(fp, "%f ", globalExpRew);
00108     fprintf(fp, "%f ", globalExpRew - confInterval);
00109     fprintf(fp, "%f ", globalExpRew + confInterval);
00110     fprintf(fp, "\n");
00111     fclose(fp);
00112 
00113 
00114 }
00115 
00116 
00117 int main(int argc, char **argv) 
00118 {
00119 
00120     try
00121     {
00122         SolverParams* p = &GlobalResource::getInstance()->solverParams;
00123 
00124         bool parseCorrect = SolverParams::parseCommandLineOption(argc, argv, *p);
00125         if(!parseCorrect)
00126         {
00127             print_usage(p->cmdName); 
00128             exit(EXIT_FAILURE);
00129         }
00130 
00131         //check validity of options
00132         if (p->policyFile == "" || p->simLen == -1 || p->simNum == -1) 
00133         {
00134             print_usage(p->cmdName); 
00135             return 0;
00136         }
00137 
00138 
00139         bool enableFiling = false;
00140         if (p->outputFile.length() == 0) 
00141         {
00142             enableFiling = false;
00143         } 
00144         else 
00145         {
00146             enableFiling = true;
00147         }
00148 
00149         cout << "\nLoading the model ..." << endl << "  ";
00150         SharedPointer<MOMDP> problem = ParserSelector::loadProblem(p->problemName, *p);
00151 
00152         if(p->stateMapFile.length() > 0 )
00153         {
00154             // generate unobserved state to variable value map
00155             ofstream mapFile(p->stateMapFile.c_str());
00156             for(int i = 0 ; i < problem->YStates->size() ; i ++)
00157             {
00158                 mapFile << "State : " << i <<  endl;
00159                 map<string, string> obsState = problem->getFactoredUnobservedStatesSymbols(i);
00160                 for(map<string, string>::iterator iter = obsState.begin() ; iter != obsState.end() ; iter ++)
00161                 {
00162                     mapFile << iter->first << " : " << iter->second << endl ;
00163                 }
00164             }
00165             mapFile.close();
00166         }
00167 
00168         SharedPointer<AlphaVectorPolicy> policy = new AlphaVectorPolicy(problem);
00169 
00170         cout << "\nLoading the policy ..." << endl;
00171         cout << "  input file   : " << p->policyFile << endl;
00172         bool policyRead = policy->readFromFile(p->policyFile);
00173         if(!policyRead)
00174         {
00175             return 0;
00176         }
00177 
00178 
00179         cout << "\nSimulating ..." << endl;
00180         
00181         if(p->useLookahead)
00182         {
00183             cout << "  action selection :  one-step look ahead" << endl;
00184         }
00185         else
00186         {
00187         }
00188 
00189         SimulationRewardCollector rewardCollector;
00190         rewardCollector.setup(*p);
00191 
00192         ofstream * foutStream = NULL;
00193         srand(p->seed);//Seed for random number.  Xan
00194         //cout << p->seed << endl;
00195 
00196 
00197 
00198         if (enableFiling) 
00199         {
00200             foutStream = new ofstream(p->outputFile.c_str());
00201         }
00202 
00203         for (int currSim = 0; currSim < p->simNum; currSim++) 
00204         {
00205             SimulationEngine engine;
00206             engine.setup(problem, policy, p);
00207 
00208             double reward = 0, expReward = 0;
00209 
00210             int firstAction = engine.runFor(p->simLen, foutStream, reward, expReward);
00211             if(firstAction < 0)
00212             {
00213                 // something wrong happend, exit
00214                 return 0;
00215             }
00216 
00217             rewardCollector.addEntry(currSim, reward, expReward);
00218             rewardCollector.printReward(currSim);
00219 
00220         }
00221 
00222         if (enableFiling) 
00223         {
00224             foutStream->close();
00225         }
00226 
00227         rewardCollector.printFinalReward();
00228         DEBUG_LOG( generateSimLog(*p, rewardCollector.globalExpRew, rewardCollector.confInterval); );
00229 
00230     }
00231     catch(bad_alloc &e)
00232     {
00233         if(GlobalResource::getInstance()->solverParams.memoryLimit == 0)
00234         {
00235             cout << "Memory allocation failed. Exit." << endl;
00236         }
00237         else
00238         {
00239             cout << "Memory limit reached. Please try increase memory limit" << endl;
00240         }
00241 
00242     }
00243     catch(exception &e)
00244     {
00245         cout << "Exception: " << e.what() << endl ;
00246     }
00247     return 0;
00248 }
00249 


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