int main(int argc, char **argv) 
{
     try
     {
	  SolverParams* p =&GlobalResource::getInstance()->solverParams;
	  bool parseCorrect = SolverParams::parseCommandLineOption(argc, argv, *p);
	  if(!parseCorrect)
	  {
	       print_usage(p->cmdName);
	       exit(EXIT_FAILURE);
	  }

	  //check validity of options
	  if (p->policyFile == "" || p->simLen == -1 || p->simNum == -1) 
	  {
	       print_usage(p->cmdName);
	       return 0;
	  }


	  bool enableFiling = false;

	  if (p->outputFile.length() == 0) 
	  {
	       enableFiling = false;
	  } 
	  else 
	  {
	       enableFiling = true;
	  }

	  cout << "\nLoading the model ..." << endl << "  ";
	  applSharedPointer<MOMDP> problem = ParserSelector::loadProblem(p->problemName, *p);

	  if (problem->initialBeliefStval->sval == -1) 
	  { 
	       cerr << "\nPlease use the simulator. Random initial value for the fully observable state variable is not supported in the evaluator.\n";
	       exit(1);
	   }

	  cout << "\nLoading the policy ..." << endl;
	  cout << "  input file   : " << p->policyFile << endl;
	  applSharedPointer<AlphaVectorPolicy> policy = new AlphaVectorPolicy(problem);
	  bool policyRead = policy->readFromFile(p->policyFile);
	  if(!policyRead)
	  {
	       return 0;
	  }

	  cout << "\nSimulating ..." << endl;
	  if(p->useLookahead)
	  {
	    cout << "  action selection :  one-step look ahead" << endl;
	  }
	  else
	  {
	  }

	  SimulationRewardCollector rewardCollector;
	  rewardCollector.setup(*p);


	  vector <BeliefCache *> beliefCacheSet;
	  int xStateNum = problem->XStates->size();
	  beliefCacheSet.resize(xStateNum);

	  for(States::iterator iter = problem->XStates->begin(); iter != problem->XStates->end(); iter ++ )
	  {
	       beliefCacheSet[iter.index()] = new BeliefCache();
	  }

	  BeliefForest* beliefForest = new BeliefForest();
	  EvaluatorSampleEngine* sample = new EvaluatorSampleEngine();

	  sample->setup(NULL, problem, &beliefCacheSet, beliefForest);
	  beliefForest->setup(problem, sample, &beliefCacheSet);
	  beliefForest->globalRootPrepare();


	  ofstream * foutStream = NULL;
	  srand(p->seed);//Seed for random number.  Xan
	  //cout << p->seed << endl;

	  //MOVED SYLTAG
	  // if the bvec field of problem.initialBeliefStval is not specified
	  applSharedPointer<BeliefWithState> startBeliefStval(new BeliefWithState());

	  copy(*startBeliefStval->bvec, *problem->initialBeliefStval->bvec);
	  startBeliefStval->sval = problem->initialBeliefStval->sval;

	  belief_vector startBel;
	  copy(startBel, *startBeliefStval->bvec);

	  //belief_vector startBel = problem.initialBelief;
	  if (startBel.filled() == 0) 
	  {
	       throw runtime_error("startBel.filled() == 0 !?");
	       int numStates = problem->getBeliefSize();
	       startBel.resize(numStates);
	       for (int i = 0; i < numStates; i++) 
	       {
		    startBel.push_back(i, ((double) 1) / (double(numStates)));
	       }
	       copy(*startBeliefStval->bvec, startBel);
	  }

	  //ADD SYLTAG
	  belief_vector startBeliefX;
	  // check if startBeliefStval->sval is specified or is it random start value for X
	  if (startBeliefStval->sval == -1) 
	  { 
	       // random start value for X
	       copy(startBeliefX, *problem->initialBeliefX);
	  } 
	  else 
	  { // for completeness we have a valid startBeliefX
	       startBeliefX.resize(problem->XStates->size());
	       startBeliefX.push_back(startBeliefStval->sval, 1.0);
	  }

	  //CPTimer simTimer;

	  bool hasMemory = true;
	  if (enableFiling) 
	  {
	       foutStream = new ofstream(p->outputFile.c_str());
	  }

	  for (int currSim = 0; currSim < p->simNum; currSim++) 
	  {
	       double reward = 0, expReward = 0;

	       if(hasMemory)
	       {
		    try
		    {
			 EvaluationEngine engine;
			 engine.setup(problem, policy, beliefForest, &beliefCacheSet, sample, p);
			 int firstAction = engine.runFor(p->simLen, *startBeliefStval, startBeliefX, foutStream, reward, expReward);
			 if(firstAction < 0)
			 {
			      // something wrong happend, exit
			      return 0;
			 }
		    }
		    catch(exception &e)
		    {
			 cout << "Memory limit reached, switch from evaluation to simulation and continue..." << endl;
			 hasMemory = false;
			 // TODO:: should free memory..., but for now, let's just remove the memory limit and continue
			 GlobalResource::getInstance()->solverParams.memoryLimit = 0;
			 delete beliefForest;
		    }
	       }

	       if(!hasMemory)
	       {
		    SimulationEngine engine;
		    engine.setup(problem, policy, p);
		    int firstAction = engine.runFor(p->simLen, foutStream, reward, expReward);

		    if(firstAction < 0)
		    {
			 // something wrong happend, exit
			 return 0;
		    }
	       }

	       rewardCollector.addEntry(currSim, reward, expReward);
	       rewardCollector.printReward(currSim);


	  }

	  if (enableFiling)
	  {
	       foutStream->close();
	  }


	  rewardCollector.printFinalReward();
	  DEBUG_LOG( generateSimLog(*p, rewardCollector.globalExpRew, rewardCollector.confInterval); );
     }
Пример #2
0
int main(int argc, char **argv) 
{

    try
    {
        SolverParams* p = &GlobalResource::getInstance()->solverParams;

        bool parseCorrect = SolverParams::parseCommandLineOption(argc, argv, *p);
        if(!parseCorrect)
        {
            print_usage(p->cmdName); 
            exit(EXIT_FAILURE);
        }

        //check validity of options
        if (p->policyFile == "" || p->simLen == -1 || p->simNum == -1) 
        {
            print_usage(p->cmdName); 
            return 0;
        }


        bool enableFiling = false;
        if (p->outputFile.length() == 0) 
        {
            enableFiling = false;
        } 
        else 
        {
            enableFiling = true;
        }

	cout << "\nLoading the model ..." << endl << "  ";
        SharedPointer<MOMDP> problem = ParserSelector::loadProblem(p->problemName, *p);

        if(p->stateMapFile.length() > 0 )
        {
            // generate unobserved state to variable value map
            ofstream mapFile(p->stateMapFile.c_str());
            for(int i = 0 ; i < problem->YStates->size() ; i ++)
            {
                mapFile << "State : " << i <<  endl;
                map<string, string> obsState = problem->getFactoredUnobservedStatesSymbols(i);
                for(map<string, string>::iterator iter = obsState.begin() ; iter != obsState.end() ; iter ++)
                {
                    mapFile << iter->first << " : " << iter->second << endl ;
                }
            }
            mapFile.close();
        }

        SharedPointer<AlphaVectorPolicy> policy = new AlphaVectorPolicy(problem);

	cout << "\nLoading the policy ..." << endl;
	cout << "  input file   : " << p->policyFile << endl;
        bool policyRead = policy->readFromFile(p->policyFile);
        if(!policyRead)
        {
            return 0;
        }


	cout << "\nSimulating ..." << endl;
	
        if(p->useLookahead)
        {
            cout << "  action selection :  one-step look ahead" << endl;
        }
        else
        {
        }

        SimulationRewardCollector rewardCollector;
        rewardCollector.setup(*p);

        ofstream * foutStream = NULL;
        srand(p->seed);//Seed for random number.  Xan
        //cout << p->seed << endl;



        if (enableFiling) 
        {
            foutStream = new ofstream(p->outputFile.c_str());
        }

        for (int currSim = 0; currSim < p->simNum; currSim++) 
        {
            SimulationEngine engine;
            engine.setup(problem, policy, p);

            double reward = 0, expReward = 0;

            int firstAction = engine.runFor(p->simLen, foutStream, reward, expReward);
            if(firstAction < 0)
            {
                // something wrong happend, exit
                return 0;
            }

            rewardCollector.addEntry(currSim, reward, expReward);
            rewardCollector.printReward(currSim);

        }

        if (enableFiling) 
        {
            foutStream->close();
        }

        rewardCollector.printFinalReward();
        DEBUG_LOG( generateSimLog(*p, rewardCollector.globalExpRew, rewardCollector.confInterval); );

    }