Пример #1
0
void Simulator::run() {

	// setup algorithms, batteries and house copies
	for (AbstractAlgorithm* alg : algorithms) {
		alg->setSensor(sensor);
		batteryPerAlg[alg].setBatteryParams(config.BatteryCapacity, config.BatteryRechargeRate, config.BatteryConsumptionRate);
		housePerAlg[alg] = currHouse;

		currLocations[alg] = make_pair(currHouse.getDockRow(), currHouse.getDockCol());
	}

	int sum_dirt_in_house = currHouse.getTotalDust();

	int winner_num_steps = -1;
	int actual_pos = 1;

	// run all algorithms for MaxSteps steps
	int maxSteps = currHouse.getMaxSteps();
	int steps;
	for (steps = 0; steps < maxSteps; steps++) {
		
		int finished_this_round = 0; // how many algorithms finished this round (to not increment actual_pos again)
		
		if (steps == currHouse.getMaxSteps() - config.MaxStepsAfterWinner) {
			for (AbstractAlgorithm* alg : algorithms) {
				alg->aboutToFinish(config.MaxStepsAfterWinner);
			}
		}


		for (vector<AbstractAlgorithm*>::iterator alg_itr = algorithms.begin(); alg_itr != algorithms.end(); ) {
			int ret = simulateSingleStep(*alg_itr);
			if (ret == ALG_FINISHED) {
				
				finished_this_round++;
				
				if (winner_num_steps == -1) {
					winner_num_steps = steps + 1; // current algorithm is the winner

					for (AbstractAlgorithm* alg : algorithms) {
						alg->aboutToFinish(config.MaxStepsAfterWinner);
					}
					
					maxSteps = min(maxSteps, steps + config.MaxStepsAfterWinner); // update steps bound
				}

				int dirt_collected = housePerAlg[*alg_itr].getDustCleaned();
				scorePerAlg[*alg_itr] = Score(actual_pos, dirt_collected, sum_dirt_in_house, true);
				scorePerAlg[*alg_itr].setParams(winner_num_steps, steps + 1);

				alg_itr = algorithms.erase(alg_itr);
				continue;
			}

			if (ret == ALG_BAD_MOVE) {
			//	cout << "Algorithm did a bad move" << endl;
				scorePerAlg[*alg_itr].setScoreZero();

				alg_itr = algorithms.erase(alg_itr);
				continue;
			}

			if (ret == ALG_BATTERY_DEAD) {
			//	cout << "Algorithm battery dead: " << endl;
				int dirt_collected = housePerAlg[*alg_itr].getDustCleaned();
				scorePerAlg[*alg_itr] = Score(10, dirt_collected, sum_dirt_in_house, false);

				alg_itr = algorithms.erase(alg_itr);
				continue;
			}

			alg_itr++;
		}
		
		actual_pos += finished_this_round; // bump actual_pos to the next place in the competition
	}
	
	if (winner_num_steps == -1) { // if there's no winner
		winner_num_steps = steps;
	}

	// simulation done - check all remaining algorithms
	for (AbstractAlgorithm* alg : algorithms) {
		int dirt_collected = housePerAlg[alg].getDustCleaned();
		scorePerAlg[alg] = Score(10, dirt_collected, sum_dirt_in_house, false);
		scorePerAlg[alg].setParams(winner_num_steps, steps);
	}
	
	for (auto itr = scorePerAlg.begin(); itr != scorePerAlg.end(); itr++) {
		if (itr->second.getScore() == -1) { // only happens for algorithms with dead batteries
			 itr->second.setParams(winner_num_steps, steps);
		}
	}

}
Пример #2
0
void MainWindow::createActions()
{
    //loadAct
    loadSceneAct = new QAction(tr("&Load Scene"), this);
    loadSceneAct->setShortcut(Qt::CTRL + Qt::Key_O);
    loadSceneAct->setStatusTip(tr("Load scene"));
    connect(loadSceneAct, SIGNAL(triggered()), this, SLOT(loadScene()));

    //quickLoadAct
    quickLoadAct = new QAction(tr("&Quick Load"), this);
    quickLoadAct->setShortcut(Qt::SHIFT + Qt::CTRL + Qt::Key_Q);
    quickLoadAct->setStatusTip(tr("Load last scene"));
    connect(quickLoadAct, SIGNAL(triggered()), this, SLOT(quickLoad()));

    //saveSceneAct
    saveSceneAct = new QAction(tr("&Save Scene"), this);
    saveSceneAct->setShortcut(Qt::CTRL + Qt::Key_S);
    saveSceneAct->setStatusTip(tr("Save Scene"));
    connect(saveSceneAct, SIGNAL(triggered()), this, SLOT(saveScene()));

    loadPluginFileAct = new QAction(tr("&Load Plugin"), this);
    loadPluginFileAct->setShortcut(Qt::CTRL + Qt::Key_L);
    loadPluginFileAct->setStatusTip(tr("Load plugin file"));
    connect(loadPluginFileAct, SIGNAL(triggered()), this, SLOT(loadPluginFileWithDialog()));

    loadPluginDirAct = new QAction(tr("&Load Plugin Directory"), this);
    loadPluginDirAct->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_L);
    loadPluginDirAct->setStatusTip(tr("Load all plugins in directory"));
    connect(loadPluginDirAct, SIGNAL(triggered()), this, SLOT(loadPluginDirWithDialog()));

    /// save workspace configuration action
    saveWorkspaceConfigurationAct = new QAction(tr("Save Workspace"), this);
    saveWorkspaceConfigurationAct->setStatusTip(tr("Save the configuration of the workspace"));
    connect(saveWorkspaceConfigurationAct, SIGNAL(triggered()), this, SLOT(saveWorkspace()));

    /// save new workspace configuration action
    saveNewWorkspaceConfigurationAct = new QAction(tr("Save Workspace As..."), this);
    saveNewWorkspaceConfigurationAct->setStatusTip(tr("Save a new workspace configuration"));
    connect(saveNewWorkspaceConfigurationAct, SIGNAL(triggered()), this, SLOT(saveNewWorkspace()));

    /// load workspace configuration action
    loadWorkspaceConfigurationAct = new QAction(tr("Load Workspace"), this);
    loadWorkspaceConfigurationAct->setStatusTip(tr("Load a workspace configuration"));
    connect(loadWorkspaceConfigurationAct, SIGNAL(triggered()), this, SLOT(loadWorkspace()));

    //closeAct
    closeSceneAct = new QAction(tr("&Close"), this);
    closeSceneAct->setShortcut(Qt::CTRL + Qt::Key_W);
    closeSceneAct->setStatusTip(tr("Close Scene"));
    connect(closeSceneAct, SIGNAL(triggered()), this, SLOT(close()));

    //exitAct
    exitAct = new QAction(tr("&Exit"), this);
    exitAct->setShortcuts(QKeySequence::Quit);
    exitAct->setStatusTip(tr("Exit the application"));
    connect(exitAct, SIGNAL(triggered()), this, SLOT(exit()));

    //frontAct
    frontAct = new QAction(tr("Front"), this);
    frontAct->setStatusTip(tr("Camera Front View"));
    connect(frontAct, SIGNAL(triggered()), this, SLOT(front()));

    //topAct
    topAct = new QAction(tr("Top"), this);
    topAct->setStatusTip(tr("Camera Top View"));
    connect(topAct, SIGNAL(triggered()), this, SLOT(top()));

    //sideAct
    sideAct = new QAction(tr("Side"), this);
    sideAct->setStatusTip(tr("Camera Side View"));
    connect(sideAct, SIGNAL(triggered()), this, SLOT(side()));

    //startSimulationAct
    startSimulationAct = new QAction(tr("Start Simulation"), this);
    startSimulationAct->setShortcut(Qt::CTRL + Qt::Key_R);
    connect(startSimulationAct, SIGNAL(triggered()), this, SLOT(startSimulation()));

    //stopSimulationAct
    stopSimulationAct = new QAction(tr("Stop Simulation"), this);
    stopSimulationAct->setShortcut(Qt::CTRL + Qt::Key_C);
    connect(stopSimulationAct, SIGNAL(triggered()), this, SLOT(stopSimulation()));

    //simulateSingleStepAct
    simulateSingleStepAct = new QAction(tr("Simulate Single Step"), this);
    simulateSingleStepAct->setStatusTip(tr("Simulates one step at a time"));
    simulateSingleStepAct->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_R);
    connect(simulateSingleStepAct, SIGNAL(triggered()), this, SLOT(simulateSingleStep()));

    //renderDuringSimulationAct
    renderDuringSimulationAct = new QAction(tr("Render during Simulation"), this);
    connect(renderDuringSimulationAct, SIGNAL(triggered()), this, SLOT(renderDuringSimulation()));

    //whiteAct
    whiteAct = new QAction(tr("White"), this);
    connect(whiteAct, SIGNAL(triggered()), this, SLOT(white()));

    //grayAct
    grayAct = new QAction(tr("Gray"), this);
    connect(grayAct, SIGNAL(triggered()), this, SLOT(gray()));

    //BlackAct
    blackAct = new QAction(tr("Black"), this);
    connect(blackAct, SIGNAL(triggered()), this, SLOT(black()));

    //resetCameraAct
    resetCameraAct = new QAction(tr("Reset Camera"), this);
    resetCameraAct->setStatusTip(tr("Reset Camera Angle"));
    connect(resetCameraAct, SIGNAL(triggered()), this, SLOT(resetCamera()));

    //xga1024x768Act
    xga1024x768Act = new QAction(tr("XGA 1024 x 768"), this);
    xga1024x768Act->setCheckable(true);
    xga1024x768Act->setChecked(true);
    connect(xga1024x768Act, SIGNAL(triggered()), this, SLOT(xga1024x768()));

    //vga640x480Act
    vga640x480Act = new QAction(tr("VGA 640 x 768"), this);
    vga640x480Act->setCheckable(true);
    connect(vga640x480Act, SIGNAL(triggered()), this, SLOT(vga640x480()));

    //hd1280x720Act
    hd1280x720Act = new QAction(tr("HD 1280 x 720"), this);
    hd1280x720Act->setCheckable(true);
    connect(hd1280x720Act, SIGNAL(triggered()), this, SLOT(hd1280x720()));

    //aboutAct
    aboutAct = new QAction(tr("About"), this);
    aboutAct->setShortcut(Qt::Key_F1);
    connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
}