Pendulum::Pendulum(SimulationEngine& engine, const opal::Matrix44r& transform) : LearningEntity(10), mMaxAngle(180), mMaxVel(500), mMaxTorque(2) { createAgent(); opal::Simulator* sim = engine.getSimulator(); // Create the pendulum Solid. We use radius, length, and density // values that produce a pendulum mass of ~1. mLength = 1; mRadius = (opal::real)0.05; mPendulumSolid = sim->createSolid(); mPendulumSolid->setSleepiness(0); // Make the pendulum "point" away from the joint anchor. opal::Matrix44r pendulumTransform = transform; pendulumTransform.rotate(-90, 1, 0, 0); mPendulumSolid->setTransform(pendulumTransform); opal::CapsuleShapeData capsule; capsule.length = mLength; capsule.radius = mRadius; capsule.material.density = 127; mPendulumSolid->addShape(capsule); engine.createPhysicalEntity("pole", "Plastic/Green", mPendulumSolid); //// TESTING //mPendulumSolid->setLinearDamping(0.1); // Setup the Joint that connects the pendulum to the static // environment. mHinge = sim->createJoint(); opal::JointAxis axis; axis.direction.set(0, 0, 1); axis.limitsEnabled = false; opal::JointData jointData; jointData.setType(opal::HINGE_JOINT); jointData.contactsEnabled = false; jointData.solid0 = mPendulumSolid; jointData.solid1 = NULL; opal::Point3r anchor = transform.getPosition(); anchor[1] += (opal::real)0.5 * mLength; jointData.anchor = anchor; jointData.axis[0] = axis; mHinge->init(jointData); // Save the initial Solid configurations for resetting. mInitSolidData = mPendulumSolid->getData(); }
void PendulumTest::handleInput(opal::real elapsedRealTime, const SimulationEngine& engine) { OIS::Keyboard* keyboard = engine.getKeyboard(); // This variable can be used to keep keys from repeating too fast. static Ogre::Real toggleTimer = 0; if (toggleTimer >= 0) { toggleTimer -= elapsedRealTime; } // Toggle GUI. if (keyboard->isKeyDown(OIS::KC_G) && toggleTimer <= 0) { Ogre::Overlay* debugOverlay = Ogre::OverlayManager::getSingleton(). getByName("Verve/TrialNumber"); if (debugOverlay->isVisible()) { debugOverlay->hide(); mAgentDebugger->setDisplayEnabled(false); } else { debugOverlay->show(); mAgentDebugger->setDisplayEnabled(true); } toggleTimer = 0.5; } }
void mainLoop() { //while (mCurrentTrialTime < mTrialLength) while (true) { Ogre::Real elapsedSimTime = 0; Ogre::Real elapsedRealTime = 0; gEngine.update(elapsedSimTime, elapsedRealTime); handleInput(elapsedRealTime); if (gEngine.quitApp()) { return; } // Update sound effects at 50 Hz. const Ogre::Real soundUpdatePeriod = 0.02; static Ogre::Real soundUpdateTimer = 0; soundUpdateTimer -= elapsedSimTime; if (soundUpdateTimer <= 0) { gRobot->updateSoundEffects(soundUpdatePeriod); gCar->updateSoundEffects(soundUpdatePeriod); soundUpdateTimer = soundUpdatePeriod; } gRobot->updateVisuals(elapsedSimTime); updateOverlay(); gAgentDebugger->updateVisuals(); } //mAvgRewardPerStep = mAvgRewardPerStep * mPhysicsStepSize / // mCurrentTrialTime; //dataFile.storeData("trial", trial, (double)trial); //dataFile.storeData("avg reward per step", trial, // mAvgRewardPerStep); //printTrialAndRunStatus(run, trial, mAvgRewardPerStep); //std::cout << "Agent age = " << mPendulum->getAgent()->getAgeString() // << std::endl; //dataFile.printToFile("./results/pendulum-performance.dat"); }
void PendulumTest::startTest() { SimulationEngine engine; if (!engine.init()) { return; } // Get a pointer to the trial number overlay and make it visible. Ogre::OverlayManager::getSingleton().getByName("Verve/TrialNumber")->show(); engine.setUpdateMode(SimulationEngine::SIMULATE_REAL_TIME_MULTIPLE, 1); engine.setCameraMoveSpeed(5); //// Set to capture frames at 29.97 fps. //engine.setUpdateMode(SIMULATE_CONSTANT_CHUNK, 0.0333667); engine.getSimulator()->setStepSize(mPhysicsStepSize); engine.getSimulator()->setGravity(opal::Vec3r(0, -9.81, 0)); // Make sure we get notified at the end of each step. engine.getSimulator()->addPostStepEventHandler(this); // Create a static box for a ground plane. opal::Solid* boxSolid = engine.getSimulator()->createSolid(); boxSolid->setStatic(true); boxSolid->setPosition(0, -1.5, 0); opal::BoxShapeData data; data.dimensions.set(6, 3, 6); boxSolid->addShape(data); engine.createPhysicalEntity("ground", "Plastic/Gray", boxSolid); DataFile dataFile(mNumTrialsPerRun); opal::Matrix44r initialTransform; initialTransform.translate(0, 1.2, 0); assert(NULL == mPendulum); mPendulum = new Pendulum(engine, initialTransform); mAgentDebugger = new AgentVisualDebugger(engine.getSceneManager()); for (unsigned int run = 0; run < mNumRuns; ++run) { mPendulum->resetBodyAndCreateNewAgent(); mAgentDebugger->setAgent(mPendulum->getAgent()); for (unsigned int trial = 0; trial < mNumTrialsPerRun; ++trial) { updateOverlayData(trial); mPendulum->resetBodyAndSTM(); mPendulum->randomizeState(); mAvgRewardPerStep = 0; mCurrentTrialTime = 0; while (mCurrentTrialTime < mTrialLength) { Ogre::Real elapsedSimTime = 0; Ogre::Real elapsedRealTime = 0; engine.update(elapsedSimTime, elapsedRealTime); handleInput(elapsedRealTime, engine); if (engine.quitApp()) { return; } mAgentDebugger->updateVisuals(); } mAvgRewardPerStep = mAvgRewardPerStep * mPhysicsStepSize / mCurrentTrialTime; dataFile.storeData("trial", trial, (float)trial); dataFile.storeData("avg reward per step", trial, mAvgRewardPerStep); printTrialAndRunStatus(run, trial, mAvgRewardPerStep); //// Print value function data. //if (0 == run && // (trial == 0 || trial == 4 || trial == 19 || trial == 99)) //{ // char fileStr[1024]; // sprintf(fileStr, "./results/pendulum-trial%d-value.dat", trial); // mPendulum->getAgent()->saveValueData(400, fileStr); // sprintf(fileStr, "./results/pendulum-trial%d-RBF.dat", trial); // mPendulum->getAgent()->saveStateRBFData(fileStr); //} } std::cout << "Agent age = " << mPendulum->getAgent()->getAgeString() << std::endl; } dataFile.printToFile("./results/pendulum-performance.dat"); }
int main(int argc, char* argv[]) { srand((unsigned int)time(NULL)); SDL_Init(SDL_INIT_JOYSTICK); if(SDL_NumJoysticks() > 0) { // Setup the joystick. std::cout << "========================================" << std::endl; std::cout << "Initializing game controller: " << SDL_JoystickName(0) << std::endl; gGamePad = SDL_JoystickOpen(0); std::cout << SDL_JoystickNumAxes(gGamePad) << " axes" << std::endl; std::cout << SDL_JoystickNumBalls(gGamePad) << " trackballs" << std::endl; std::cout << SDL_JoystickNumHats(gGamePad) << " hats" << std::endl; std::cout << SDL_JoystickNumButtons(gGamePad) << " buttons" << std::endl; std::cout << "========================================" << std::endl; } else { std::cout << "========================================" << std::endl; std::cout << "No game controller detected" << std::endl; std::cout << "========================================" << std::endl; } //Ogre::Overlay* trialOverlay; ///// The current amount of elapsed time within a trial. //Ogre::Real mCurrentTrialTime; ///// The length of each trial in seconds. //Ogre::Real mTrialLength; ///// The rewards received during a single trial, in rewards per step. //verve::real mAvgRewardPerStep; if (!gEngine.init()) { return 0; } gEngine.setUpdateMode(SimulationEngine::SIMULATE_REAL_TIME_MULTIPLE, 1); //// Set to capture frames at 29.97 fps. //engine.setUpdateMode(SIMULATE_CONSTANT_CHUNK, 0.0333667); // Use feet for this simulation. gEngine.getSimulator()->setGravity(opal::Vec3r(0, -30, 0)); gEngine.getSimulator()->setStepSize(gPhysicsStepSize); // Make sure we get notified at the end of each step. gEngine.getSimulator()->addPostStepEventHandler(&gPostStepEventHandler); // Create the robot. opal::Matrix44r robotTransform; robotTransform.translate(0, 1, 0); gRobot = new Robot(gEngine, 5); gRobot->init("../data/blueprints/robot1.xml", "Plastic/LightBlue", 0.5, robotTransform, 2); gRobot->resetBodyAndCreateNewAgent(); gRobot->resetBodyAndSTM(); gRobot->randomizeState(); gRobot->getFLMotor()->setMaxTorque((opal::real)2); gRobot->getFRMotor()->setMaxTorque((opal::real)2); gRobot->getFLMotor()->setMaxVelocity(1000); gRobot->getFRMotor()->setMaxVelocity(1000); // Create the car. opal::Matrix44r carTransform; carTransform.translate(-12, 2, 4); gCar = new Car(gEngine); gCar->init("../data/blueprints/car1.xml", "Plastic/Blue", 1, carTransform, 1); //DataFile dataFile(mNumTrialsPerRun); //updateOverlayData(trial); //mAvgRewardPerStep = 0; //mCurrentTrialTime = 0; gAgentDebugger = new AgentVisualDebugger(gEngine.getSceneManager()); gAgentDebugger->setAgent(gRobot->getAgent()); gAgentDebugger->setDisplayEnabled(false); Ogre::OverlayManager::getSingleton().getByName("Verve/Debug")->hide(); Ogre::OverlayManager::getSingleton().getByName("Core/DebugOverlay")->hide(); // Setup camera. gEngine.getCamera()->setPosition(opal::Point3r(0, 25, 25)); gEngine.getCamera()->lookAt(opal::Point3r(0, (opal::real)0.1, 0)); gEngine.setCameraMoveSpeed(15); setupEnvironment(); mainLoop(); delete gRobot; delete gCar; delete gAgentDebugger; return 0; }
void handleInput(Ogre::Real elapsedRealTime) { // This variable can be used to keep keys from repeating too fast. static Ogre::Real toggleTimer = 0; if (toggleTimer >= 0) { toggleTimer -= elapsedRealTime; } OIS::Keyboard* keyboard = gEngine.getKeyboard(); if (keyboard->isKeyDown(OIS::KC_W)) { gCar->forward(); } else if (keyboard->isKeyDown(OIS::KC_S)) { gCar->reverse(); } else { gCar->idle(); } if (keyboard->isKeyDown(OIS::KC_A)) { gCar->setSteering(-1); } else if (keyboard->isKeyDown(OIS::KC_D)) { gCar->setSteering(1); } else { gCar->setSteering(0); } // If available, get data from the game controller. if (gGamePad) { // Update the game controller state. SDL_JoystickUpdate(); Ogre::Real joy0X = (Ogre::Real)SDL_JoystickGetAxis(gGamePad, 0) / (Ogre::Real)32768; Ogre::Real joy0Y = (Ogre::Real)SDL_JoystickGetAxis(gGamePad, 1) / (Ogre::Real)32768; Ogre::Real joy1X = (Ogre::Real)SDL_JoystickGetAxis(gGamePad, 4) / (Ogre::Real)32768; Ogre::Real joy1Y = (Ogre::Real)SDL_JoystickGetAxis(gGamePad, 3) / (Ogre::Real)32768; if (fabs(joy0Y) > 0.1) { gCar->setThrottle(-joy0Y); } else { gCar->idle(); } if (fabs(joy0X) > 0.1) { gCar->setSteering(joy0X); } else { gCar->setSteering(0); } if (joy1X > 0.2 || joy1X < -0.2) { Ogre::Degree rotAroundY = -Ogre::Degree(joy1X); gEngine.getCamera()->yawRelative(rotAroundY.valueDegrees()); } if (joy1Y > 0.2 || joy1Y < -0.2) { Ogre::Degree rotAroundX = -Ogre::Degree(joy1Y); gEngine.getCamera()->pitchRelative(rotAroundX.valueDegrees()); } } // Toggle GUI. if (keyboard->isKeyDown(OIS::KC_G) && toggleTimer <= 0) { Ogre::Overlay* debugOverlay = Ogre::OverlayManager::getSingleton(). getByName("Verve/Debug"); if (debugOverlay->isVisible()) { debugOverlay->hide(); gAgentDebugger->setDisplayEnabled(false); } else { debugOverlay->show(); gAgentDebugger->setDisplayEnabled(true); } toggleTimer = 0.5; } }
void setupEnvironment() { // Main static arena. opal::Blueprint arenaBlueprint; opal::loadFile(arenaBlueprint, "../data/blueprints/arena1.xml"); opal::BlueprintInstance arenaBPInstance; gEngine.getSimulator()->instantiateBlueprint(arenaBPInstance, arenaBlueprint, opal::Matrix44r(), 1); gEngine.createPhysicalEntity("staticEnvironment", "Plastic/Gray", arenaBPInstance.getSolid("staticEnvironment")); gEngine.createPhysicalEntity("toy1", "Plastic/Green", arenaBPInstance.getSolid("toy1")); gEngine.createPhysicalEntity("toy2", "Plastic/Green", arenaBPInstance.getSolid("toy2")); gEngine.createPhysicalEntity("toy3", "Plastic/Green", arenaBPInstance.getSolid("toy3")); // Seesaw. opal::Blueprint seesawBP; opal::loadFile(seesawBP, "../data/blueprints/seesaw.xml"); opal::BlueprintInstance seesawBPInstance; opal::Matrix44r seesawTransform; seesawTransform.translate(8, 0, 0); gEngine.getSimulator()->instantiateBlueprint(seesawBPInstance, seesawBP, seesawTransform, 1); gEngine.createPhysicalEntity("seesawSupport", "Plastic/Black", seesawBPInstance.getSolid("seesawSupport")); gEngine.createPhysicalEntity("seesawPanel", "Plastic/Orange", seesawBPInstance.getSolid("seesawPanel")); // Add an initial torque to bring one end of the seesaw to the // ground. seesawBPInstance.getJoint("seesawHinge")->addTorque(0, 100, 0, true); // Merry-go-round. gEngine.createPhysicalEntity("merryGoRound", "Plastic/Yellow", arenaBPInstance.getSolid("merryGoRound")); // Curtains. opal::Blueprint curtainsBP; opal::loadFile(curtainsBP, "../data/blueprints/blockCurtain.xml"); opal::BlueprintInstance curtainsBPInstance; opal::Matrix44r curtainsTransform; curtainsTransform.rotate(45, 0, 1, 0); curtainsTransform.translate(-10, 0, 0); gEngine.getSimulator()->instantiateBlueprint(curtainsBPInstance, curtainsBP, curtainsTransform, 1); gEngine.createPhysicalEntity("curtainBase", "Plastic/Red", curtainsBPInstance.getSolid("curtainBase")); gEngine.createPhysicalEntity("curtainPiece0", "Plastic/Black", curtainsBPInstance.getSolid("curtainPiece0")); gEngine.createPhysicalEntity("curtainPiece1", "Plastic/Black", curtainsBPInstance.getSolid("curtainPiece1")); gEngine.createPhysicalEntity("curtainPiece2", "Plastic/Black", curtainsBPInstance.getSolid("curtainPiece2")); gEngine.createPhysicalEntity("curtainPiece3", "Plastic/Black", curtainsBPInstance.getSolid("curtainPiece3")); gEngine.createPhysicalEntity("curtainPiece4", "Plastic/Black", curtainsBPInstance.getSolid("curtainPiece4")); gEngine.createPhysicalEntity("curtainPiece5", "Plastic/Black", curtainsBPInstance.getSolid("curtainPiece5")); //// Ragdoll. //opal::Blueprint ragdollBP; //opal::loadFile(ragdollBP, "../data/blueprints/ragdoll.xml"); //opal::BlueprintInstance ragdollBPInstance; //opal::Matrix44r ragdollTransform; //ragdollTransform.translate(10, 5, 0); //gEngine.getSimulator()->instantiateBlueprint(ragdollBPInstance, // ragdollBP, ragdollTransform, 2); //for (unsigned int i = 0; i < ragdollBPInstance.getNumSolids(); ++i) //{ // opal::Solid* s = ragdollBPInstance.getSolid(i); // gEngine.createPhysicalEntity(s->getName(), "Plastic/Red", s); //} //// TESTING: Simple goal box. //opal::Solid* boxSolid = gEngine.getSimulator()->createSolid(); //boxSolid->setStatic(false); //boxSolid->setSleepiness(0); //boxSolid->setPosition(15.5, 10, -7); //opal::BoxShapeData data; //data.dimensions.set(1.5, 1.5, 1.5); //data.material.friction = 0.1; //data.material.density = 0.5; //boxSolid->addShape(data); //gEngine.createPhysicalEntity("goal box", "Plastic/Green", boxSolid); //// TESTING: Make a volume sensor to detect the goal. //opal::VolumeSensorData goalSensorData; //goalSensorData.solid = gRobot->getChassis(); //goalSensorData.transform.makeTranslation(0, 0, -2); //gGoalSensor = gEngine.getSimulator()->createVolumeSensor(); //gGoalSensor->init(goalSensorData); //gGoalSensorVolume = gEngine.getSimulator()->createSolid(); //gGoalSensorVolume->setStatic(true); ////opal::Matrix44r m = gRobot->getChassis()->getTransform(); ////m.translate(0, 0, -2); //opal::Matrix44r m; //m.translate(0, 100, 0); //gGoalSensorVolume->setTransform(m); //opal::BoxShapeData sensorVolumeShape; //sensorVolumeShape.dimensions.set(2, 1, 2); ////gEngine.getSimulator()->setupContactGroup(5, false); ////sensorVolumeShape.contactGroup = 5; //sensorVolumeShape.material.density = 0.01; //gGoalSensorVolume->addShape(sensorVolumeShape); ////gEngine.createPhysicalEntityBox("goal sensor", "Translucent/Blue", boxDim, //// gGoalSensorVolume); //opal::JointData fixedJointData; //fixedJointData.setType(opal::FIXED_JOINT); //fixedJointData.solid0 = gRobot->getChassis(); //fixedJointData.solid1 = gGoalSensorVolume; //opal::Joint* fixedJoint = gEngine.getSimulator()->createJoint(); //fixedJoint->init(fixedJointData); }
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); ); }
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); ); }
void initializeOptionsFromCommandLine( int argc, char **argv, SimulationOptions & simulationOptions ) { // // BEFORE PARSING NORMAL COMMAND LINE: check for special-case command-line options. // // - if a config file was specified by the command line, // the config file should be loaded first, so that all other // command-line options can override the config file options. // // - if the user requested to list modules, print them out and exit. // std::string inputConfigFileName = ""; bool listModulesAndExit = false; CommandLineParser specialOpts; specialOpts.addOption("-config", &inputConfigFileName, OPTION_DATA_TYPE_STRING); specialOpts.addOption("-listModules", NULL, OPTION_DATA_TYPE_NO_DATA, 0, &listModulesAndExit, true); specialOpts.addOption("-listmodules", NULL, OPTION_DATA_TYPE_NO_DATA, 0, &listModulesAndExit, true); specialOpts.parse(argc, argv, true, false); // If a config file was specified, initialize options from it. if (inputConfigFileName != "") { simulationOptions.loadOptionsFromConfigFile(inputConfigFileName); } // If user requested to list available modules, then do that and exit. if (listModulesAndExit) { // instantiate a dummy engine SimulationEngine * dummyEngine = new SimulationEngine(); std::vector<std::string> moduleNames; dummyEngine->getListOfKnownBuiltInModules(moduleNames); std::cout << "Built-in modules:\n"; for(unsigned int i=0; i<moduleNames.size(); ++i) { std::cout << " " << moduleNames[i] << "\n"; } // simulate the same semantics as the real parsing // above, hard-coded defaults may have been overridden by loading an input config file // here, command line options may override the module search path again. std::string searchPath = simulationOptions.engineOptions.moduleSearchPath; CommandLineParser opts; opts.addOption( "-moduleSearchPath", &searchPath, OPTION_DATA_TYPE_STRING); opts.addOption( "-modulesearchpath", &searchPath, OPTION_DATA_TYPE_STRING); opts.parse(argc, argv, true, false); moduleNames.clear(); dummyEngine->getListOfKnownPlugInModules(moduleNames,searchPath); std::cout << "Plug-in modules:\n"; if (moduleNames.size() == 0 ) { std::cout << " <no modules found>\n"; } for(unsigned int i=0; i<moduleNames.size(); ++i) { std::cout << " " << moduleNames[i] << "\n"; } delete dummyEngine; exit(0); } // // parse the command line options. // // Previously the following code only parsed some of the options, // and left other options to be parsed by engine drivers (or the engine itself). // Now this has changed, ALL command-line options must be parsed here, // and only a SimulationOptions data structure is passed to the engine driver. // bool qtSpecified = false; bool glfwSpecified = false; bool commandLineSpecified = false; std::string engineDriverName = ""; std::string generateConfigFilename = ""; std::string aiModuleName = ""; std::string testCaseFilename = ""; std::string recordingFilename = ""; std::string replayingFilename = ""; std::vector<std::string> modulesGivenOnCommandLine; CommandLineParser opts; // The following several options must be "post-processed" to figure out exactly // what to put into the options data structure. opts.addOption("-Qt", NULL, OPTION_DATA_TYPE_NO_DATA, 0, &qtSpecified, true); opts.addOption("-qt", NULL, OPTION_DATA_TYPE_NO_DATA, 0, &qtSpecified, true); opts.addOption("-glfw", NULL, OPTION_DATA_TYPE_NO_DATA, 0, &glfwSpecified, true); opts.addOption("-GLFW", NULL, OPTION_DATA_TYPE_NO_DATA, 0, &glfwSpecified, true); opts.addOption("-commandLine", NULL, OPTION_DATA_TYPE_NO_DATA, 0, &commandLineSpecified, true); opts.addOption("-commandline", NULL, OPTION_DATA_TYPE_NO_DATA, 0, &commandLineSpecified, true); opts.addOption("-engineDriver", &engineDriverName, OPTION_DATA_TYPE_STRING); opts.addOption("-enginedriver", &engineDriverName, OPTION_DATA_TYPE_STRING); opts.addOption("-generateConfig", &generateConfigFilename, OPTION_DATA_TYPE_STRING); opts.addOption("-generateconfig", &generateConfigFilename, OPTION_DATA_TYPE_STRING); opts.addOption( "-module", &modulesGivenOnCommandLine, OPTION_DATA_TYPE_MULTI_INSTANCE_STRING); opts.addOption( "-testfile", &testCaseFilename, OPTION_DATA_TYPE_STRING); opts.addOption( "-testFile", &testCaseFilename, OPTION_DATA_TYPE_STRING); opts.addOption( "-testcase", &testCaseFilename, OPTION_DATA_TYPE_STRING); opts.addOption( "-testCase", &testCaseFilename, OPTION_DATA_TYPE_STRING); opts.addOption( "-ai", &aiModuleName, OPTION_DATA_TYPE_STRING); opts.addOption( "-storeSimulation", &recordingFilename, OPTION_DATA_TYPE_STRING); opts.addOption( "-storesimulation", &recordingFilename, OPTION_DATA_TYPE_STRING); opts.addOption( "-playback", &replayingFilename, OPTION_DATA_TYPE_STRING); opts.addOption( "-replay", &replayingFilename, OPTION_DATA_TYPE_STRING); // The following several options can initialize the options data structure directly. opts.addOption( "-numFrames", &simulationOptions.engineOptions.numFramesToSimulate, OPTION_DATA_TYPE_UNSIGNED_INT); opts.addOption( "-numframes", &simulationOptions.engineOptions.numFramesToSimulate, OPTION_DATA_TYPE_UNSIGNED_INT); opts.addOption( "-numThreads", &simulationOptions.engineOptions.numThreads, OPTION_DATA_TYPE_UNSIGNED_INT); opts.addOption( "-numthreads", &simulationOptions.engineOptions.numThreads, OPTION_DATA_TYPE_UNSIGNED_INT); opts.addOption( "-testCaseSearchPath", &simulationOptions.engineOptions.testCaseSearchPath, OPTION_DATA_TYPE_STRING); opts.addOption( "-testcasesearchpath", &simulationOptions.engineOptions.testCaseSearchPath, OPTION_DATA_TYPE_STRING); opts.addOption( "-testCasePath", &simulationOptions.engineOptions.testCaseSearchPath, OPTION_DATA_TYPE_STRING); opts.addOption( "-testcasepath", &simulationOptions.engineOptions.testCaseSearchPath, OPTION_DATA_TYPE_STRING); opts.addOption( "-moduleSearchPath", &simulationOptions.engineOptions.moduleSearchPath, OPTION_DATA_TYPE_STRING); opts.addOption( "-modulesearchpath", &simulationOptions.engineOptions.moduleSearchPath, OPTION_DATA_TYPE_STRING); opts.addOption( "-modulePath", &simulationOptions.engineOptions.moduleSearchPath, OPTION_DATA_TYPE_STRING); opts.addOption( "-modulepath", &simulationOptions.engineOptions.moduleSearchPath, OPTION_DATA_TYPE_STRING); opts.addOption( "-startPaused", NULL, OPTION_DATA_TYPE_NO_DATA, 0, &simulationOptions.glfwEngineDriverOptions.pausedOnStart, true); opts.addOption( "-startpaused", NULL, OPTION_DATA_TYPE_NO_DATA, 0, &simulationOptions.glfwEngineDriverOptions.pausedOnStart, true); // Dummy option parsing for the special options, but these are used earlier and ignored at this point. opts.addOption("-config", NULL, OPTION_DATA_TYPE_STRING); // This will parse the command line and initialize all the above variables as appropriate. opts.parse(argc, argv, true, true); // // NEXT: many of these command-line options need to be error-checked and "interpreted" // to initialize the simulationOptions data structure // // figure out which engine the user specified on the command line, or use the default. // this code might seem "inflated", but actually it is an attempt to have user-friendly semantics // for several different ways of specifying the engine driver on the command line. if ( engineDriverName != "" ) { // convert the user's command-line input into lower case for the engine driver std::transform(engineDriverName.begin(), engineDriverName.end(), engineDriverName.begin(), (int(*)(int))tolower); if (engineDriverName == "qt") qtSpecified = true; else if (engineDriverName == "glfw") glfwSpecified = true; else if (engineDriverName == "commandline") commandLineSpecified = true; } unsigned int numGUIOptionsSpecified = 0; // // NOTE CAREFULLY!!!! The following MUST BE SEPARATE if statements, they MUST NOT be if-else statements. // if (qtSpecified) { numGUIOptionsSpecified++; engineDriverName = "qt"; } if (glfwSpecified) { numGUIOptionsSpecified++; engineDriverName = "glfw"; } if (commandLineSpecified) { numGUIOptionsSpecified++; engineDriverName = "commandline"; } if (numGUIOptionsSpecified > 1) { throw GenericException("Multiple engine drivers were specified:" + toString(commandLineSpecified ? " commandLine" : "") + toString(glfwSpecified ? " glfw" : "") + toString(qtSpecified ? " qt" : "") + "; Please specify only one engine driver."); } // finally, after all that, if there is an engine driver name, then set the option, otherwise, // leave the hard-coded default. if (engineDriverName != "") { simulationOptions.globalOptions.engineDriver = engineDriverName; } else { // in this case, leave the simulationOptions.globalOptions.engineDriver // with the hard coded default. } // // take care of some shortcut options; these will be overridden by module options if the user specified conflicting options in both places. // if (testCaseFilename != "") { simulationOptions.moduleOptionsDatabase["testCasePlayer"]["testcase"] = testCaseFilename; simulationOptions.engineOptions.startupModules.insert("testCasePlayer"); simulationOptions.engineOptions.startupModules.erase("recFilePlayer"); // removes the recFilePlayer module so that the overriding command-line option doesn't cause a conflict with default options. } if (aiModuleName != "") { simulationOptions.moduleOptionsDatabase["testCasePlayer"]["ai"] = aiModuleName.substr(0,aiModuleName.find(',')); modulesGivenOnCommandLine.push_back(aiModuleName); simulationOptions.engineOptions.startupModules.insert("testCasePlayer"); simulationOptions.engineOptions.startupModules.erase("recFilePlayer"); // removes the recFilePlayer module so that the overriding command-line option doesn't cause a conflict with default options. } if (replayingFilename != "") { simulationOptions.moduleOptionsDatabase["recFilePlayer"]["recfile"] = replayingFilename; simulationOptions.engineOptions.startupModules.insert("recFilePlayer"); simulationOptions.engineOptions.startupModules.erase("testCasePlayer"); // removes the testCasePlayer module so that the overriding command-line option doesn't cause a conflict with default options. } if (recordingFilename != "") { simulationOptions.moduleOptionsDatabase["simulationRecorder"]["recfile"] = recordingFilename; simulationOptions.engineOptions.startupModules.insert("simulationRecorder"); } //separate the modules names and possible options for (unsigned int i=0; i<modulesGivenOnCommandLine.size(); i++) { // First, split the command-line between the module name and the string of options. string::size_type firstComma = modulesGivenOnCommandLine[i].find(','); std::string moduleName = modulesGivenOnCommandLine[i].substr(0, firstComma); std::string moduleOptions = ""; if (firstComma != string::npos) { moduleOptions = modulesGivenOnCommandLine[i].substr(firstComma+1, string::npos); } else { // no comma was found, so there are no options. moduleOptions = ""; } // Next, add these options to the options database. simulationOptions.mergeModuleOptions(moduleName, moduleOptions); // add it to the list of startup modules; its a set so will only exist once, anyway. simulationOptions.engineOptions.startupModules.insert(moduleName); } // If user requested to store a config file, do that and exit. if (generateConfigFilename != "") { simulationOptions.generateConfigFile(generateConfigFilename); exit(0); } }