main.cpp 3.92 KB
Newer Older
1
2
/* UMANS: Unified Microscopic Agent Navigation Simulator
** Copyright (C) 2018-2020  Inria Rennes Bretagne Atlantique - Rainbow - Julien Pettré
3
**
4
5
6
7
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
8
**
9
10
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
11
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
13
** GNU General Public License for more details.
**
14
** You should have received a copy of the GNU General Public License
15
** along with this program. If not, see <https://www.gnu.org/licenses/>.
16
**
17
18
19
** Contact: crowd_group@inria.fr
** Website: https://project.inria.fr/crowdscience/
** See the file AUTHORS.md for a list of all contributors.
20
*/
21

22
23
#include <core/crowdSimulator.h>

24
25
#include <vector>
#include <iostream>
26
#include <fstream>
27

28
using namespace std;
29

30
31
32
33
34
35
36
37
38
39
40
41
void printBasicInfo()
{
	std::cout
		<< "-----------------------------------------------------" << std::endl
		<< "UMANS: Unified Microscopic Agent Navigation Simulator" << std::endl
		<< "Copyright(C) 2018 - 2020 Inria Rennes Bretagne Atlantique - Rainbow - Julien Pettre" << std::endl
		<< "-----------------------------------------------------" << std::endl << std::endl;
}

void printUsageInfo(const std::string& programName)
{
	std::cout
42
		<< "Usage: " << programName << " -i [-o] [-t]" << std::endl
43
44
		<< "  -i (or -input)     = An XML file describing the simulation scenario to run." << std::endl
		<< "                       For help on creating scenario files, please see the UMANS documentation." << std::endl
45
		<< "  -o (or -output)    = (optional) Name of a folder to which the simulation output will be written." << std::endl
46
		<< "                       The program will write a CSV file for each agent's trajectory." << std::endl
47
		<< "                       If you omit this, the program will run faster, but no results will be saved." << std::endl
48
49
50
		<< "  -t (or -nrThreads) = (optional, default=1) The number of parallel threads to use." << std::endl << std::endl;
}

51
52
int main( int argc, char * argv[] )
{
53
	printBasicInfo();
54
	
55
56
57
58
59
	std::string configFile = "", outputFolder = "";
	int nrThreads = 1;

	// parse the arguments one by one
	for (int i = 1; i + 1 < argc; i += 2)
60
	{
61
62
63
64
		std::string paramName(argv[i]);
		std::string paramValue(argv[i + 1]);

		if (paramName[0] != '-')
65
		{
66
67
68
			std::cerr << "Input error: " << paramName << " is not a valid parameter name." << std::endl;
			printUsageInfo(argv[0]);
			return 0;
BONNEAU Alexandre's avatar
BONNEAU Alexandre committed
69
		}
70

71
72
73
74
75
76
		if (paramName == "-i" || paramName == "-input")
			configFile = paramValue;
		else if (paramName == "-o" || paramName == "-output")
			outputFolder = paramValue;
		else if (paramName == "-t" || paramName == "-nrThreads")
			nrThreads = atoi(paramValue.c_str());
BONNEAU Alexandre's avatar
BONNEAU Alexandre committed
77
	}
78

79
80
	// input file is mandatory
	if (configFile == "")
81
	{
82
		std::cerr << "Input error: Please specify an input scenario." << std::endl;
83
84
		printUsageInfo(argv[0]);
		return 0;
BONNEAU Alexandre's avatar
BONNEAU Alexandre committed
85
	}
86

87
	// number of threads must be at least 1
88
89
90
91
92
93
	if (nrThreads < 1)
	{
		std::cerr << "Input error: Please specify a positive number of threads." << std::endl;
		printUsageInfo(argv[0]);
		return 0;
	}
94
95
96
97
98
99
100

	// output folder may be empty; if so, show a warning
	if (outputFolder == "")
	{
		std::cout << "Input warning: No output folder specified." << std::endl
			<< "The program will not write any simulation results to CSV files." << std::endl;
	}
VAN TOLL Wouter's avatar
VAN TOLL Wouter committed
101
	
102
	// run the simulation
VAN TOLL Wouter's avatar
VAN TOLL Wouter committed
103
	CrowdSimulator* cs = CrowdSimulator::FromConfigFile(configFile, true);
104
105
	if (cs != nullptr)
	{
106
		cs->GetWorld()->SetNumberOfThreads(nrThreads);
107
		if (outputFolder != "")
VAN TOLL Wouter's avatar
VAN TOLL Wouter committed
108
			cs->StartCSVOutput(outputFolder, false); // false = don't save any files until the simulation ends
109
110
		cs->RunSimulationUntilEnd();
	}
111

112
	std::cout << "Simulation done" << std::endl;
113

VAN TOLL Wouter's avatar
VAN TOLL Wouter committed
114
	delete cs;
115

VAN TOLL Wouter's avatar
VAN TOLL Wouter committed
116
	std::cout << "---------------" << std::endl;
117

118
}