Controller.cpp
Go to the documentation of this file.
00001 #include "SimulationEngine.h"
00002 #include "AlphaVectorPolicy.h"
00003 #include "solverUtils.h"
00004 #include "Controller.h"
00005 
00006 using namespace std;
00007 using namespace momdp;
00008 
00009 namespace momdp
00010 {
00011     Controller::Controller(SharedPointer<MOMDP> problem,
00012                            SharedPointer<AlphaVectorPolicy> policy,
00013                            SolverParams* solverParams,
00014                            int initialBeliefStvalX):
00015             problem(problem),
00016             policy(policy),
00017             solverParams(solverParams),
00018             currBelSt(new BeliefWithState()),
00019             firstAction(true), initialBeliefStvalX(initialBeliefStvalX)
00020     {
00021         reset(initialBeliefStvalX);
00022     }
00023 
00024     void Controller::reset(int xstate)
00025     {
00026         firstAction = true;
00027 
00028         //if (problem->initialBeliefStval->sval == -1 && initialBeliefStvalX == -1) {
00029             //cerr << "An initial observed state is not specified by the model. You must provide it by passing the parameter initialBeliefStvalX." << endl;
00030             //throw runtime_error("missing initial state");
00031         //}
00032 
00033         // Initialize the starting state of X
00034         //currBelSt->sval = initialBeliefStvalX == -1 ?
00035                 //problem->initialBeliefStval->sval :
00036                 //initialBeliefStvalX;
00037         currBelSt->sval = xstate;
00038 
00039         cout << "inital state: " << currBelSt->sval << endl;
00040 
00041         // Initialize the belief vector for Y
00042         SharedPointer<SparseVector> startBeliefVec;
00043         if (problem->initialBeliefStval->bvec)
00044             startBeliefVec = problem->initialBeliefStval->bvec;
00045         else
00046             startBeliefVec = problem->initialBeliefYByX[currBelSt->sval];
00047 
00048         copy(*currBelSt->bvec, *startBeliefVec);
00049     
00050     }
00051 
00052     const int Controller::nextAction(ObsDefine currObservation,
00053                                      int nextStateX)
00054     {
00055         if (firstAction) {
00056             firstAction = false;
00057         }
00058         else
00059         {
00060             // If this is not the first action, we first update the
00061             // belief according to the last action and the current
00062             // observation
00063 
00064             // Next belief with state
00065             SharedPointer<BeliefWithState> nextBelSt;
00066 
00067             // Create next belief
00068             nextBelSt = problem->beliefTransition->nextBelief(currBelSt, lastAction, currObservation, nextStateX);
00069 
00070             // Assign to current belief
00071             copy(*currBelSt->bvec, *nextBelSt->bvec);
00072             currBelSt->sval = nextBelSt->sval;
00073         }
00074 
00075         // Belief conditioning on X. May not be used.
00076         DenseVector currBelX;
00077 
00078         int currAction;
00079         if (solverParams->useLookahead)
00080             currAction = policy->getBestActionLookAhead(*currBelSt);
00081         else
00082             currAction = policy->getBestAction(*currBelSt);
00083 
00084         if (currAction < 0)
00085         {
00086             cerr << "You are using a MDP Policy, please make sure you are using a MDP policy together with one-step look ahead option turned on" << endl;
00087             return -1;
00088         }
00089 
00090         lastAction = currAction;
00091 
00092         return currAction;
00093     }
00094 
00095     SharedPointer<BeliefWithState> Controller::currBelief() const
00096     {
00097         return currBelSt;
00098     }
00099 }


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