Go to the documentation of this file.00001 #include "MOMDP.h"
00002 #include "ParserSelector.h"
00003 #include "AlphaVectorPolicy.h"
00004 #include "BeliefForest.h"
00005 #include "Sample.h"
00006 #include "BeliefCache.h"
00007 #include "EvaluatorSampleEngine.h"
00008 #include "EvaluatorBeliefTreeNodeTuple.h"
00009
00010 #include "GlobalResource.h"
00011 #include "PolicyGraphGenerator.h"
00012
00013 #include <string>
00014 #include <stdlib.h>
00015 #include <sstream>
00016 #include <fstream>
00017 #include <ctime>
00018
00019 #include "CPTimer.h"
00020
00021 using namespace std;
00022 using namespace momdp;
00023
00024 void print_usage(const char* cmdName)
00025 {
00026 cout << "Usage: " << cmdName << " POMDPModelFileName --policy-file policyFileName --policy-graph policyGraphFileName \n"
00027 <<" [--fast] [--graph-max-depth maximumDepth] [--graph-max-branch maximumNoOfBranches]\n"
00028 <<" [--graph-min-prob minimumProbability]" << endl;
00029 cout << " or " << cmdName << " --help (or -h) Print this help."<< endl;
00030 cout << " or " << cmdName << " --version Print version information." << endl;
00031 cout << endl;
00032 cout << "Policy graph generator options:" << endl;
00033 cout << " --policy-file policyFileName Use policyFileName as the policy file name (compulsory)." << endl;
00034 cout << " -f or --fast Use fast (but very picky) alternate parser for .pomdp files.\n";
00035 cout << " --graph-max-depth maximumDepth Use maximumDepth as the maximum horizon of the generated policy\n"
00036 << " graph. There is no limit by default." << endl;
00037 cout << " --graph-max-branch maximumNoOfBranches Use maximumNoOfBranches as the maximum number of branches to show\n"
00038 << " in the policy graph. The branches shown are the top\n"
00039 << " maximumNofOfBranches branches in probability. There is no limit\n"
00040 << " by default." << endl;
00041 cout << " --graph-min-prob minimumProbability Use minimumProbability as the minimum probability threshold for a\n"
00042 << " branch to be shown in the policy graph. Branches with\n"
00043 << " probability less than the threshold are suppressed. The threshold\n"
00044 << " is zero by default." << endl;
00045 cout << "" << endl;
00046 cout << "Output options" << endl;
00047 cout << " --policy-graph policyGraphFileName Use policyGraphFileName as the name for the DOT file to be \n"
00048 << " generated (compulsory)." << endl;
00049 cout << "" << endl;
00050
00051
00052
00053 cout <<"Example:" <<endl;
00054 cout << " " << cmdName << " --policy-file hallway.policy --policy-graph Hallway.dot --graph-max-depth 5 Hallway.pomdp" << endl;
00055 }
00056
00057
00058 int main(int argc, char **argv)
00059 {
00060 try
00061 {
00062 SolverParams* p =&GlobalResource::getInstance()->solverParams;
00063 bool parseCorrect = SolverParams::parseCommandLineOption(argc, argv, *p);
00064 if(!parseCorrect)
00065 {
00066 print_usage(p->cmdName);
00067 exit(EXIT_FAILURE);
00068 }
00069
00070
00071 if (p->policyFile == "" || p->policyGraphFile.length() == 0)
00072 {
00073 print_usage(p->cmdName);
00074 return 0;
00075 }
00076
00077 SharedPointer<MOMDP> problem = ParserSelector::loadProblem(p->problemName, *p);
00078 SharedPointer<AlphaVectorPolicy> policy = new AlphaVectorPolicy(problem);
00079 bool policyRead = policy->readFromFile(p->policyFile);
00080 if(!policyRead)
00081 {
00082 return 0;
00083 }
00084
00085 if (policy->getValueAction() == -1 && !p->useLookahead) {
00086 cerr<<"Use the lookahead controller (--lookahead yes) for MDP policies."<<endl;
00087 exit(EXIT_FAILURE);
00088 }
00089
00090 if(p->useLookahead)
00091 {
00092 cout << "Use one-step look ahead" << endl;
00093 }
00094
00095 PolicyGraphParam graphParam;
00096 graphParam.useLookahead = p->useLookahead;
00097 graphParam.depth = p->graphDepth;
00098 graphParam.probThreshold = p->graphProbThreshold;
00099 graphParam.maxEdge = p->graphMaxBranch;
00100 ofstream dotFile(p->policyGraphFile.c_str());
00101 dotFile.precision(3);
00102
00103 PolicyGraphGenerator generator(problem, policy, graphParam);
00104 generator.generateGraph(dotFile);
00105
00106 dotFile.flush();
00107 dotFile.close();
00108 }
00109 catch(bad_alloc &e)
00110 {
00111 if(GlobalResource::getInstance()->solverParams.memoryLimit == 0)
00112 {
00113 cout << "Memory allocation failed. Exit." << endl;
00114 }
00115 else
00116 {
00117 cout << "Memory limit reached. Please try increase memory limit" << endl;
00118 }
00119
00120 }
00121 catch(exception &e)
00122 {
00123 cout << "Exception: " << e.what() << endl ;
00124 }
00125
00126 return 0;
00127 }
00128