AlphaPlanePoolSet.cpp
Go to the documentation of this file.
00001 #include "Const.h"
00002 #include "AlphaPlanePoolSet.h"
00003 #include "tinyxml.h"
00004 #include <exception>
00005 #include <fstream>
00006 using namespace std;
00007 
00008 
00009 REAL_VALUE AlphaPlanePoolSet::getValue(SharedPointer<BeliefWithState>& bns)
00010 {
00011         state_val sval = bns->sval;
00012         double bestVal;
00013         bestVal = set[sval]->getValue( bns->bvec);
00014 
00015         return bestVal;
00016 }
00017 
00018 // TODO : this is for the first time visited belief only
00019 SharedPointer<AlphaPlane> AlphaPlanePoolSet::getValueAlpha(SharedPointer<BeliefWithState>& bns)
00020 {
00021         state_val sval = bns->sval;
00022         return set[sval]->getValueAlpha( bns->bvec);
00023 }
00024 
00025 SharedPointer<AlphaPlane> AlphaPlanePoolSet::backup(BeliefTreeNode * node)
00026 {
00027         SharedPointer<AlphaPlane> result = backupEngine->backup(node);
00028         for(int i = 0 ; i < onBackup.size(); i++)
00029         {
00030                 (*onBackup[i])(solver, node, result);
00031         }
00032         return result;
00033 }
00034 
00035 
00036 SharedPointer<AlphaPlane> AlphaPlanePoolSet::getBestAlphaPlane(BeliefWithState& beliefandState)
00037 {
00038         state_val sval = beliefandState.sval;
00039         SharedPointer<AlphaPlane> bestAlp;
00040         bestAlp =  set[sval]->getBestAlphaPlane(beliefandState.bvec);
00041         return bestAlp;
00042 
00043 }
00044 
00045 
00046 SharedPointer<AlphaPlane> AlphaPlanePoolSet::getBestAlphaPlane(BeliefTreeNode& cn)
00047 {
00048         state_val sval = cn.s->sval;
00049         SharedPointer<AlphaPlane> bestAlp;
00050 
00051         bestAlp =  set[sval]->getBestAlphaPlane(cn);
00052         return bestAlp;
00053 }
00054 
00055 SharedPointer<AlphaPlane> AlphaPlanePoolSet::getBestAlphaPlane1(BeliefWithState& beliefandState)
00056 {
00057         state_val sval = beliefandState.sval;
00058         SharedPointer<AlphaPlane>bestAlp;
00059         bestAlp =  set[sval]->getBestAlphaPlane1(beliefandState.bvec);
00060         return bestAlp;
00061 }
00062 
00063 //write out policy in new xml format
00064 void AlphaPlanePoolSet::writeToFile(const std::string& outFileName, string problemName) 
00065 {
00066         ofstream out(outFileName.c_str());
00067         if (!out) 
00068         {
00069                 cerr << "ERROR: Bounds::writeToFile: couldn't open " << outFileName << " for writing " << endl;
00070                 exit(EXIT_FAILURE);
00071         }
00072         
00073         string declaration = 
00074         "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<Policy version=\"0.1\" type=\"value\" model=\""+ problemName +"\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"policyx.xsd\">";
00075         out << declaration << endl ;
00076         int totalnumPlanes = 0;
00077         FOR (sval, set.size()) 
00078         {
00079                 totalnumPlanes += set[sval]->planes.size();
00080         }
00081         int vectorLength = problem->YStates->size();
00082         int numObsValue = set.size();
00083         out << "<AlphaVector vectorLength=\"" << vectorLength << "\" numObsValue=\"" << numObsValue << "\" numVectors=\"" << totalnumPlanes << "\">" << endl;
00084         FOR (sval, set.size())
00085         {
00086                 list<SharedPointer<AlphaPlane> >::const_iterator pi = set[sval]->planes.begin();
00087                 FOR (i, set[sval]->planes.size()) 
00088                 {
00089                         (*pi)->write(out);
00090                         pi++;
00091                             
00092                 }
00093         }
00094         out << "</AlphaVector> </Policy>" << endl;
00095 }
00096 
00097 //write out policy in old format
00098 /*
00099 void AlphaPlanePoolSet::writeToFile(const std::string& outFileName) 
00100 {
00101         ofstream out(outFileName.c_str());
00102         if (!out) 
00103         {
00104                 cerr << "ERROR: Bounds::writeToFile: couldn't open " << outFileName << " for writing " << endl;
00105                 exit(EXIT_FAILURE);
00106         }
00107 
00108         out <<
00109                 "# This file is a POMDP policy, represented as a set of \"lower bound\n"
00110                 "# planes\", each of which consists of the value of the observed state\n"
00111                 "# variable, an alpha vector and a corresponding action.\n"
00112                 "# Given a particular value of the observed state variable x\n"
00113                 "# and a belief b, this information can be used to answer two queries\n"
00114                 "# of interest:\n"
00115                 "#\n"
00116                 "#   1. What is a lower bound on the expected long-term reward starting\n"
00117                 "#        from state value x and belief b?\n"
00118                 "#   2. What is an action that achieves that expected reward lower bound?\n"
00119                 "#\n"
00120                 "# Each lower bound plane is only defined over a subset of the belief\n"
00121                 "# simplex--it is defined for those beliefs b such that the non-zero\n"
00122                 "# entries of b are a subset of the entries present in the plane's alpha\n"
00123                 "# vector.  If this condition holds we say the plane is 'applicable' to b.\n"
00124                 "#\n"
00125                 "# Given a belief b, both of the queries above can be answered by the\n"
00126                 "# following process: first, throw out all the planes that are not\n"
00127                 "# applicable to b.  Then, for each of the remaining planes, take the inner\n"
00128                 "# product of the plane's alpha vector with b.  The highest inner product\n"
00129                 "# value is the expected long-term reward lower bound, and the action label\n"
00130                 "# for that plane is the action that achieves the bound.\n"
00131                 "\n"
00132                 ;
00133         out << "{" << endl;
00134         out << "  policyType => \"MaxPlanesLowerBoundWithObservedState\"," << endl;
00135 
00136         int totalnumPlanes = 0;
00137         FOR (sval, set.size()) 
00138         {
00139                 totalnumPlanes += set[sval]->planes.size();
00140         }
00141 
00142         out << "  numPlanes => " << totalnumPlanes << "," << endl;
00143         out << "  planes => [" << endl;
00144 
00145         FOR (sval, set.size()-1)
00146         {
00147                 list<SharedPointer<AlphaPlane> >::const_iterator pi = set[sval]->planes.begin();
00148                 FOR (i, set[sval]->planes.size()) 
00149                 {
00150                         (*pi)->write(out);
00151                         out << "," << endl;
00152                         pi++;
00153                 }
00154         }
00155 
00156         list<SharedPointer<AlphaPlane> >::const_iterator pi = set.back()->planes.begin();
00157 
00158         FOR (i, set.back()->planes.size()-1) 
00159         {
00160                 (*pi)->write(out);
00161                 out << "," << endl;
00162                 pi++;
00163         }
00164         (*pi)->write(out);
00165         out << endl;
00166 
00167         out << "  ]" << endl;
00168         out << "}" << endl;
00169 
00170         out.close();
00171 }
00172 */


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