11#include < dsf/dsf.hpp>
22#include < array>
3+ #include < chrono>
34#include < cmath>
45#include < cstdint>
56#include < fstream>
@@ -45,13 +46,13 @@ void printLoadingBar(int const i, int const n) {
4546}
4647
4748int main (int argc, char ** argv) {
49+ auto const start = std::chrono::high_resolution_clock::now ();
4850 if (argc != 6 ) {
4951 std::cerr
5052 << " Usage: " << argv[0 ]
5153 << " <SEED> <ERROR_PROBABILITY> <OUT_FOLDER_BASE> <OPTIMIZE> <INIT_NAGENTS>\n " ;
5254 return 1 ;
5355 }
54- spdlog::set_level (spdlog::level::err); // turn off spdlog logging
5556
5657 const int SEED = std::stoi (argv[1 ]); // seed for random number generator
5758 const double ERROR_PROBABILITY{std::stod (argv[2 ])};
@@ -60,12 +61,6 @@ int main(int argc, char** argv) {
6061 BASE_OUT_FOLDER += OPTIMIZE ? " _op/" : " /" ;
6162 auto nAgents{std::stoul (argv[5 ])};
6263
63- std::string OUT_FOLDER{std::format (" {}output_sctl_{}_{}/" ,
64- BASE_OUT_FOLDER,
65- ERROR_PROBABILITY,
66- std::to_string (SEED))}; // output folder
67- constexpr auto MAX_TIME{static_cast <unsigned int >(5e5 )}; // maximum time of simulation
68-
6964 std::cout << " -------------------------------------------------\n " ;
7065 std::cout << " Input parameters:\n " ;
7166 std::cout << " Seed: " << SEED << ' \n ' ;
@@ -76,7 +71,11 @@ int main(int argc, char** argv) {
7671 std::cout << " Traffic light optimization ENABLED.\n " ;
7772 }
7873 std::cout << " -------------------------------------------------\n " ;
79-
74+ const std::string OUT_FOLDER{std::format (" {}output_sctl_{}_{}/" ,
75+ BASE_OUT_FOLDER,
76+ ERROR_PROBABILITY,
77+ std::to_string (SEED))}; // output folder
78+ constexpr auto MAX_TIME{static_cast <unsigned int >(5e5 )}; // maximum time of simulation
8079 // Clear output folder or create it if it doesn't exist
8180 if (!fs::exists (BASE_OUT_FOLDER)) {
8281 fs::create_directory (BASE_OUT_FOLDER);
@@ -151,81 +150,25 @@ int main(int argc, char** argv) {
151150 for (const auto & pair : graph.edges ()) {
152151 graph.makeSpireStreet (pair.first );
153152 }
154- // create gaussian random number generator
155- std::random_device rd;
156- std::mt19937 gen (rd ());
157- gen.seed (64313 );
158- std::normal_distribution d (60 ., 10 .);
159- std::array<uint8_t , 2 > sda{0 , 0 };
160- auto random = [&d, &gen]() { return std::round (d (gen)); };
161- std::cout << " Adjusting node capacities...\n " ;
162153 graph.adjustNodeCapacities ();
163154 std::cout << " Setting traffic light parameters..." << ' \n ' ;
164- for (const auto & [nodeId, pNode] : graph.nodes ()) {
165- if (!pNode->isTrafficLight ()) {
166- continue ;
167- }
168- auto & tl = dynamic_cast <TrafficLight&>(*pNode);
169- double value = -1 .;
170- while (value < 0 .) {
171- value = random ();
172- }
173- const auto & ingoingEdges = pNode->ingoingEdges ();
174- if (ingoingEdges.empty ()) {
175- spdlog::error (" Node {} has no ingoing edges." , nodeId);
176- continue ;
177- }
178- std::set<dsf::Id> streets;
179- if (!pNode->geometry ().has_value ()) {
180- std::cerr << " Warning: Node " << nodeId << " has no geometry, skipping.\n " ;
181- continue ;
182- }
183- const auto & refLat = (*pNode->geometry ()).y ();
184- for (const auto & inEdgeId : ingoingEdges) {
185- auto const & pEdge{graph.edge (inEdgeId)};
186- const auto & edgeGeometry = pEdge->geometry ();
187- if (edgeGeometry.empty ()) {
188- std::cerr << " Warning: Edge " << inEdgeId << " has empty geometry, skipping.\n " ;
189- continue ;
190- }
191- const auto & lat = edgeGeometry.front ().y ();
192- // std::cout << "Lat: " << lat << " RefLat: " << refLat << '\n';
193- if (std::abs (lat - refLat) < std::numeric_limits<double >::epsilon ()) {
194- streets.emplace (inEdgeId);
195- }
196- }
197- for (auto const & streetId : streets) {
198- tl.setCycle (streetId, dsf::Direction::ANY, {static_cast <dsf::Delay>(value), 0 });
199- }
200- for (const auto & streetId : ingoingEdges) {
201- if (!streets.contains (streetId)) {
202- tl.setComplementaryCycle (streetId, *streets.begin ());
203- }
204- }
205- ++sda[streets.size () - 1 ];
206- // std::cout << "Node id: " << nodeId << " has " << streets.size()
207- // << "streets.\n";
208- }
209- std::cout << " Nodes with one street: " << static_cast <int >(sda[0 ]) << ' \n ' ;
210- std::cout << " Nodes with two streets: " << static_cast <int >(sda[1 ]) << ' \n ' ;
155+ graph.initTrafficLights ();
211156 std::cout << " Done." << std::endl;
212157
213158 std::cout << " Creating dynamics...\n " ;
214159
215- Dynamics dynamics{graph, true , SEED, 0.6 };
216- std::size_t n{0 };
160+ Dynamics dynamics{graph, false , SEED, 0.6 };
217161 {
218162 std::vector<dsf::Id> destinationNodes;
219163 for (auto const & [nodeId, pNode] : dynamics.graph ().nodes ()) {
220164 if (pNode->outgoingEdges ().size () < 4 ) {
221- destinationNodes.push_back (pNode->id ());
222- ++n;
165+ destinationNodes.push_back (nodeId);
223166 }
224167 }
225168 dynamics.setDestinationNodes (destinationNodes);
169+ std::cout << " Number of exits: " << destinationNodes.size () << ' \n ' ;
226170 }
227171 dynamics.updatePaths ();
228- std::cout << " Number of exits: " << n << ' \n ' ;
229172
230173 dynamics.setErrorProbability (ERROR_PROBABILITY);
231174 // dynamics.setMaxFlowPercentage(0.69);
@@ -429,6 +372,10 @@ int main(int argc, char** argv) {
429372#ifdef __APPLE__
430373 t.join ();
431374#endif
432-
375+ std::cout << " Total elapsed time: "
376+ << std::chrono::duration_cast<std::chrono::milliseconds>(
377+ std::chrono::high_resolution_clock::now () - start)
378+ .count ()
379+ << " milliseconds\n " ;
433380 return 0 ;
434381}
0 commit comments