Commit cb8f16aa authored by VAN TOLL Wouter's avatar VAN TOLL Wouter

- CrowdSimulator::RunSimulationUntilEnd() now has parameters to enable/disable...

- CrowdSimulator::RunSimulationUntilEnd() now has parameters to enable/disable a progress bar and time measurements.
- Console application now measures and prints the running time.
- Removed CrowdSimulator::isConsoleApplication_.
- Fixed some Doxygen comments.
parent d856ec97
......@@ -65,7 +65,7 @@ int main( int argc, char * argv[] )
{
std::cerr << "Input error: " << paramName << " is not a valid parameter name." << std::endl;
printUsageInfo(argv[0]);
return 0;
return -1;
}
if (paramName == "-i" || paramName == "-input")
......@@ -81,7 +81,7 @@ int main( int argc, char * argv[] )
{
std::cerr << "Input error: Please specify an input scenario." << std::endl;
printUsageInfo(argv[0]);
return 0;
return -1;
}
// number of threads must be at least 1
......@@ -89,7 +89,7 @@ int main( int argc, char * argv[] )
{
std::cerr << "Input error: Please specify a positive number of threads." << std::endl;
printUsageInfo(argv[0]);
return 0;
return -1;
}
// output folder may be empty; if so, show a warning
......@@ -100,19 +100,17 @@ int main( int argc, char * argv[] )
}
// run the simulation
CrowdSimulator* cs = CrowdSimulator::FromConfigFile(configFile, true);
if (cs != nullptr)
{
cs->GetWorld()->SetNumberOfThreads(nrThreads);
if (outputFolder != "")
cs->StartCSVOutput(outputFolder, false); // false = don't save any files until the simulation ends
cs->RunSimulationUntilEnd();
}
CrowdSimulator* cs = CrowdSimulator::FromConfigFile(configFile);
if (cs == nullptr) // the simulation could not be loaded (for reasons already written to the console)
return -1;
std::cout << "Simulation done" << std::endl;
cs->GetWorld()->SetNumberOfThreads(nrThreads);
if (outputFolder != "")
cs->StartCSVOutput(outputFolder, false); // false = don't save any files until the simulation ends
delete cs;
std::cout << "---------------" << std::endl;
// run the full simulation; show a progress bar and measure the time
cs->RunSimulationUntilEnd(true, true);
delete cs;
return 0;
}
......@@ -58,7 +58,7 @@ public:
static void RegisterAllCostFunctions();
/// <summary>Creates and returns a new CostFunction instance whose name matches the given value.</summary>
/// <details>This method looks through the registry created in RegisterAllCostFunctions().</details>
/// <remarks>This method looks through the registry created in RegisterAllCostFunctions().</remarks>
/// <param name="name">The name of the cost function to create.</param>
/// <returns>A pointer to a new instance of the CostFunction subclass whose GetName() matches the "name" parameter.</returns>
static CostFunction* CreateCostFunction(const std::string& name)
......
......@@ -21,13 +21,13 @@
#include <core/crowdSimulator.h>
#include <tools/HelperFunctions.h>
#include <tools/csvwriter.h>
#include <core/worldInfinite.h>
#include <core/worldToric.h>
#include <core/costFunctionFactory.h>
CrowdSimulator::CrowdSimulator(bool isConsoleApplication)
: isConsoleApplication_(isConsoleApplication)
CrowdSimulator::CrowdSimulator()
{
CostFunctionFactory::RegisterAllCostFunctions();
writer_ = nullptr;
......@@ -35,7 +35,7 @@ CrowdSimulator::CrowdSimulator(bool isConsoleApplication)
}
void CrowdSimulator::StartCSVOutput(const std::string &dirname, bool flushImmediately)
{
{
// create the CSV writer if it did not exist yet
if (writer_ == nullptr)
writer_ = new CSVWriter(flushImmediately);
......@@ -91,7 +91,7 @@ void CrowdSimulator::RunSimulationSteps(int nrSteps)
}
}
void CrowdSimulator::RunSimulationUntilEnd()
void CrowdSimulator::RunSimulationUntilEnd(bool showProgressBar, bool measureTime)
{
if (end_time_ == MaxFloat || end_time_ <= 0)
{
......@@ -102,19 +102,52 @@ void CrowdSimulator::RunSimulationUntilEnd()
const int nrIterations_ = (int)ceilf(end_time_ / world_->GetDeltaTime());
int p = nrIterations_/50;
// get the current system time; useful for time measurements later on
const auto& startTime = HelperFunctions::GetCurrentTime();
if (isConsoleApplication_)
std::cout << "|--------------------------------------------------|100%" << std::endl << "[";
for (int i = 0; i < nrIterations_; ++i)
if (showProgressBar)
{
RunSimulationSteps(1);
if (isConsoleApplication_ && i%p == 0)
// do the simulation in K blocks, and show a progress bar with K blocks
const int nrProgressBarBlocks = 20;
int nrIterationsPerBlock = nrIterations_ / nrProgressBarBlocks;
int nrIterationsDone = 0;
// - print an empty progress bar first, to indicate how long the real bar will be
std::string progressBar = "Simulation progress: |";
for (int i = 0; i < nrProgressBarBlocks; ++i)
progressBar += "-";
progressBar += "|100%";
std::cout << std::endl << progressBar << std::endl;
// - on a new line, start writing a new progress bar
std::cout << " [";
for (int i = 0; i < nrProgressBarBlocks; ++i)
{
// run a block of iterations
int nrIterationsToDo = (i+1 == nrProgressBarBlocks ? nrIterations_ - nrIterationsDone : nrIterationsPerBlock);
RunSimulationSteps(nrIterationsToDo);
// augment the progress bar
std::cout << "#" << std::flush;
nrIterationsDone += nrIterationsToDo;
}
std::cout << "]" << std::endl << std::endl;
}
else
{
// do all steps at once without any printing
RunSimulationSteps(nrIterations_);
}
if (measureTime)
{
// report running times
const auto& endTime = HelperFunctions::GetCurrentTime();
const auto& timeSpent = HelperFunctions::GetIntervalMilliseconds(startTime, endTime);
if (isConsoleApplication_)
std::cout << "]" << std::endl;
std::cout << "Time simulated: " << world_->GetCurrentTime() << " seconds." << std::endl;
std::cout << "Computation time used: " << timeSpent / 1000 << " seconds." << std::endl;
}
if (writer_ != nullptr)
writer_->Flush();
......@@ -291,7 +324,7 @@ bool CrowdSimulator::FromConfigFile_loadObstaclesBlock(const tinyxml2::XMLElemen
bool CrowdSimulator::FromConfigFile_loadSinglePolicy(const tinyxml2::XMLElement* policyElement)
{
// --- Read mandatory parameters
// Unique policy ID
int policyID;
policyElement->QueryIntAttribute("id", &policyID);
......@@ -319,7 +352,7 @@ bool CrowdSimulator::FromConfigFile_loadSinglePolicy(const tinyxml2::XMLElement*
const char * res = policyElement->Attribute("SamplingType");
if (res != nullptr && !SamplingParameters::TypeFromString(res, params.type))
std::cerr << "Policy " << policyID << ": sampling type invalid, using default (regular)." << std::endl;
// base velocity (= origin of the cone or circle being sampled)
res = policyElement->Attribute("SamplingBase");
if (res != nullptr && !SamplingParameters::BaseFromString(res, params.base))
......@@ -477,7 +510,7 @@ bool CrowdSimulator::FromConfigFile_loadSingleObstacle(const tinyxml2::XMLElemen
return true;
}
CrowdSimulator* CrowdSimulator::FromConfigFile(const std::string& filename, bool isConsoleApplication)
CrowdSimulator* CrowdSimulator::FromConfigFile(const std::string& filename)
{
// Parse the XML into the property tree.
// If the path cannot be resolved, an exception is thrown.
......@@ -498,7 +531,7 @@ CrowdSimulator* CrowdSimulator::FromConfigFile(const std::string& filename, bool
{
// Location of the config file should be relative to the location of the *main* config file.
// Check if the main config file lies in a subfolder.
return CrowdSimulator::FromConfigFile(fileFolder + simConfigPathElement->Attribute("path"), isConsoleApplication);
return CrowdSimulator::FromConfigFile(fileFolder + simConfigPathElement->Attribute("path"));
}
// --- Otherwise, we assume that this is a "regular" config file that contains the simulation itself.
......@@ -510,7 +543,7 @@ CrowdSimulator* CrowdSimulator::FromConfigFile(const std::string& filename, bool
return nullptr;
}
CrowdSimulator* crowdsimulator = new CrowdSimulator(isConsoleApplication);
CrowdSimulator* crowdsimulator = new CrowdSimulator();
crowdsimulator->scenarioFilename_ = filename;
//
......@@ -546,7 +579,7 @@ CrowdSimulator* CrowdSimulator::FromConfigFile(const std::string& filename, bool
return nullptr;
}
crowdsimulator->GetWorld()->SetDeltaTime(delta_time);
// the total simulation time (optional)
float end_time = -1;
simulationElement->QueryFloatAttribute("end_time", &crowdsimulator->end_time_);
......
......@@ -39,10 +39,6 @@ private:
/// <summary>A pointer to an optional CSVWriter that can write the simulation output to CSV files.</summary>
CSVWriter* writer_;
/// <summary>Whether or not the simulation is running as a console application.
/// This affects whether the program can write progress to the console.</summary>
bool isConsoleApplication_;
/// <summary>An optional time at which the simulation should end.
/// Only used if this number is set in a configuration file.</summary>
float end_time_;
......@@ -58,10 +54,8 @@ public:
/// <summary>Creates a new CrowdSimulator object by loading a given configuration file.</summary>
/// <remarks>Note: The caller of this method is responsible for deleting the resulting CrowdSimulator object.</remarks>
/// <param name="filename">The name of the configuration file to load.</param>
/// <param name="isConsoleApplication">Whether or not the simulation will run inside a console application.
/// This affects whether or not the program will try to print progress information to the console.</param>
/// <returns>A pointer to new CrowdSimulator object, or nullptr if the loading failed for any reason.</returns>
static CrowdSimulator* FromConfigFile(const std::string& filename, bool isConsoleApplication);
static CrowdSimulator* FromConfigFile(const std::string& filename);
/// <summary>Destroys this CrowdSimulator object.</summary>
~CrowdSimulator();
......@@ -83,7 +77,9 @@ public:
/// <summary>Runs the crowd simulation for the number of iterations specified in the previously loaded config file.</summary>
/// <remarks>If the config file does not specify a number of iterations, then this method will do nothing.</remarks>
void RunSimulationUntilEnd();
/// <param name="showProgressBar">Whether or not to print a progress bar in the console.</param>
/// <param name="measureTime">Whether or not to measure the total computation time and report it in the console.</param>
void RunSimulationUntilEnd(bool showProgressBar, bool measureTime);
/// <summary>Returns a pointer to the world in which the simulation takes place.</summary>
WorldBase* GetWorld() { return world_.get(); }
......@@ -102,7 +98,7 @@ public:
inline bool HasPolicies() const { return !policies_.empty(); }
private:
CrowdSimulator(bool isConsoleApplication);
CrowdSimulator();
bool FromConfigFile_loadWorld(const tinyxml2::XMLElement* worldElement);
......
......@@ -41,7 +41,7 @@ struct Color
Color(unsigned short r, unsigned short g, unsigned short b, unsigned short a=255) : r(r), g(g), b(b), a(a) {}
/// <summary>Creates a Color based on HSV (Hue, Saturation, Value) values.</summary>
/// <details>Source: https://gist.github.com/kuathadianto/. </details>
/// <remarks>Source: https://gist.github.com/kuathadianto/.</remarks>
/// <param name="H">The H (Hue) component of the color, between 0 and 360.</param>
/// <param name="S">The S (Saturation) component of the color, between 0 and 1.</param>
/// <param name="V">The V (Vaue) component of the color, between 0 and 1.</param>
......
......@@ -26,7 +26,7 @@
#include <tools/vector2D.h>
/// <summary>A wrapper for a 2D polygon defined by a list of Vector2D points.</summary>
/// <details>Upon creation, a Polygon2D object pre-computes a list of edges *and* a triangulation.</details>
/// <remarks>Upon creation, a Polygon2D object pre-computes a list of edges *and* a triangulation.</remarks>
class Polygon2D
{
private:
......
......@@ -120,7 +120,7 @@ void UMANSOpenGLWidget::startNewSimulation(const std::string& scenarioFilename)
bool firstTime = simulator == nullptr;
// try to load a new simulation
CrowdSimulator* newSimulator = CrowdSimulator::FromConfigFile(scenarioFilename, false);
CrowdSimulator* newSimulator = CrowdSimulator::FromConfigFile(scenarioFilename);
if (newSimulator != nullptr)
{
// delete the current simulation, if applicable
......
......@@ -51,13 +51,12 @@ extern "C"
omp_set_num_threads(numberOfThreads);
// initialize a new crowd simulation; we'll fill it with the contents of the given config file
cs = CrowdSimulator::FromConfigFile(configFileName, false);
cs = CrowdSimulator::FromConfigFile(configFileName);
if (cs == nullptr)
{
return false;
}
cs->GetWorld()->SetNumberOfThreads(numberOfThreads);
// prepare the agentData array
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment