-
Notifications
You must be signed in to change notification settings - Fork 4
Add Street::meanSpeed and Dynamics::saveStreetSpeeds functions
#399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1665e90
c12f66d
b11c4d9
005ec13
fcb161c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -358,11 +358,18 @@ namespace dsf::mobility { | |||||||||||
|
|
||||||||||||
| /// @brief Save the street densities in csv format | ||||||||||||
| /// @param filename The name of the file (default is "{datetime}_{simulation_name}_street_densities.csv") | ||||||||||||
| /// @param normalized If true, the densities are normalized in [0, 1] | ||||||||||||
| /// @param separator The separator character (default is ';') | ||||||||||||
| /// @param normalized If true, the densities are normalized in [0, 1] dividing by the street capacity attribute | ||||||||||||
| void saveStreetDensities(std::string filename = std::string(), | ||||||||||||
| bool normalized = true, | ||||||||||||
| char const separator = ';') const; | ||||||||||||
| char const separator = ';', | ||||||||||||
| bool const normalized = true) const; | ||||||||||||
| /// @brief Save the street speeds in csv format | ||||||||||||
| /// @param filename The name of the file (default is "{datetime}_{simulation_name}_street_speeds.csv") | ||||||||||||
| /// @param separator The separator character (default is ';') | ||||||||||||
| /// @param bNormalized If true, the speeds are normalized in [0, 1] dividing by the street maxSpeed attribute | ||||||||||||
| void saveStreetSpeeds(std::string filename = std::string(), | ||||||||||||
| char const separator = ';', | ||||||||||||
| bool bNormalized = false) const; | ||||||||||||
| /// @brief Save the street input counts in csv format | ||||||||||||
| /// @param filename The name of the file | ||||||||||||
| /// @param reset If true, the input counts are cleared after the computation | ||||||||||||
|
|
@@ -742,7 +749,7 @@ namespace dsf::mobility { | |||||||||||
| timeTolerance, | ||||||||||||
| timeDiff); | ||||||||||||
| // Kill the agent | ||||||||||||
| this->m_killAgent(pStreet->dequeue(queueIndex)); | ||||||||||||
| this->m_killAgent(pStreet->dequeue(queueIndex, this->time_step())); | ||||||||||||
| ++m_nKilledAgents; | ||||||||||||
| continue; | ||||||||||||
| } | ||||||||||||
|
|
@@ -898,7 +905,8 @@ namespace dsf::mobility { | |||||||||||
| } | ||||||||||||
| } | ||||||||||||
| if (bArrived) { | ||||||||||||
| auto pAgent = this->m_killAgent(pStreet->dequeue(queueIndex)); | ||||||||||||
| auto pAgent = | ||||||||||||
| this->m_killAgent(pStreet->dequeue(queueIndex, this->time_step())); | ||||||||||||
| ++m_nArrivedAgents; | ||||||||||||
| if (reinsert_agents) { | ||||||||||||
| // reset Agent's values | ||||||||||||
|
|
@@ -920,7 +928,7 @@ namespace dsf::mobility { | |||||||||||
| *nextStreet); | ||||||||||||
| continue; | ||||||||||||
| } | ||||||||||||
| auto pAgent{pStreet->dequeue(queueIndex)}; | ||||||||||||
| auto pAgent{pStreet->dequeue(queueIndex, this->time_step())}; | ||||||||||||
| spdlog::debug( | ||||||||||||
| "{} at time {} has been dequeued from street {} and enqueued on street {} " | ||||||||||||
| "with free time {}.", | ||||||||||||
|
|
@@ -990,7 +998,7 @@ namespace dsf::mobility { | |||||||||||
| pNode->id(), | ||||||||||||
| nextStreet->id(), | ||||||||||||
| pAgent->freeTime()); | ||||||||||||
| nextStreet->addAgent(std::move(pAgent)); | ||||||||||||
| nextStreet->addAgent(std::move(pAgent), this->time_step()); | ||||||||||||
| it = intersection.agents().erase(it); | ||||||||||||
| break; | ||||||||||||
| } | ||||||||||||
|
|
@@ -1018,7 +1026,7 @@ namespace dsf::mobility { | |||||||||||
| nextStreet->id(), | ||||||||||||
| pAgent->freeTime(), | ||||||||||||
| *pAgent); | ||||||||||||
| nextStreet->addAgent(std::move(pAgent)); | ||||||||||||
| nextStreet->addAgent(std::move(pAgent), this->time_step()); | ||||||||||||
| } else { | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
|
|
@@ -1351,7 +1359,7 @@ namespace dsf::mobility { | |||||||||||
| this->setAgentSpeed(pAgent); | ||||||||||||
| pAgent->setFreeTime(this->time_step() + | ||||||||||||
| std::ceil(street->length() / pAgent->speed())); | ||||||||||||
| street->addAgent(std::move(pAgent)); | ||||||||||||
| street->addAgent(std::move(pAgent), this->time_step()); | ||||||||||||
| this->m_agents.pop_back(); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
@@ -2302,8 +2310,8 @@ namespace dsf::mobility { | |||||||||||
| template <typename delay_t> | ||||||||||||
| requires(is_numeric_v<delay_t>) | ||||||||||||
| void RoadDynamics<delay_t>::saveStreetDensities(std::string filename, | ||||||||||||
| bool normalized, | ||||||||||||
| char const separator) const { | ||||||||||||
| char const separator, | ||||||||||||
| bool const normalized) const { | ||||||||||||
| if (filename.empty()) { | ||||||||||||
| filename = | ||||||||||||
| this->m_safeDateTime() + '_' + this->m_safeName() + "_street_densities.csv"; | ||||||||||||
|
|
@@ -2333,6 +2341,48 @@ namespace dsf::mobility { | |||||||||||
| file << std::endl; | ||||||||||||
| file.close(); | ||||||||||||
| } | ||||||||||||
| template <typename delay_t> | ||||||||||||
| requires(is_numeric_v<delay_t>) | ||||||||||||
| void RoadDynamics<delay_t>::saveStreetSpeeds(std::string filename, | ||||||||||||
| char const separator, | ||||||||||||
| bool const bNormalized) const { | ||||||||||||
| if (filename.empty()) { | ||||||||||||
| filename = this->m_safeDateTime() + '_' + this->m_safeName() + "_street_speeds.csv"; | ||||||||||||
| } | ||||||||||||
| bool bEmptyFile{false}; | ||||||||||||
| { | ||||||||||||
| std::ifstream file(filename); | ||||||||||||
| bEmptyFile = file.peek() == std::ifstream::traits_type::eof(); | ||||||||||||
| } | ||||||||||||
| std::ofstream file(filename, std::ios::app); | ||||||||||||
| if (!file.is_open()) { | ||||||||||||
| throw std::runtime_error("Error opening file \"" + filename + "\" for writing."); | ||||||||||||
| } | ||||||||||||
| if (bEmptyFile) { | ||||||||||||
| file << "datetime" << separator << "time_step"; | ||||||||||||
| for (auto const& [streetId, pStreet] : this->graph().edges()) { | ||||||||||||
| file << separator << streetId; | ||||||||||||
| } | ||||||||||||
| file << std::endl; | ||||||||||||
| } | ||||||||||||
| file << this->strDateTime() << separator << this->time_step(); | ||||||||||||
| for (auto const& [streetId, pStreet] : this->graph().edges()) { | ||||||||||||
| auto const measure = pStreet->meanSpeed(true); | ||||||||||||
| file << separator; | ||||||||||||
| // If not valid, write empty value (less space w.r.t. NaN) | ||||||||||||
| if (!measure.is_valid) { | ||||||||||||
| continue; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| double speed{measure.mean}; | ||||||||||||
| if (bNormalized) { | ||||||||||||
| speed /= pStreet->maxSpeed(); | ||||||||||||
|
||||||||||||
| speed /= pStreet->maxSpeed(); | |
| const auto maxSpeed = pStreet->maxSpeed(); | |
| if (maxSpeed != 0.0) { | |
| speed /= maxSpeed; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should not happen
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -126,9 +126,10 @@ namespace dsf::mobility { | |||||||||||||||||||||||||||||||
| m_counter->reset(); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| void Street::addAgent(std::unique_ptr<Agent> pAgent) { | ||||||||||||||||||||||||||||||||
| void Street::addAgent(std::unique_ptr<Agent> pAgent, std::time_t const currentTime) { | ||||||||||||||||||||||||||||||||
| assert(!isFull()); | ||||||||||||||||||||||||||||||||
| spdlog::debug("Adding {} on {}", *pAgent, *this); | ||||||||||||||||||||||||||||||||
| spdlog::trace("Adding {} on {}", *pAgent, *this); | ||||||||||||||||||||||||||||||||
| m_agentsInsertionTimes[pAgent->id()] = currentTime; | ||||||||||||||||||||||||||||||||
| m_movingAgents.push(std::move(pAgent)); | ||||||||||||||||||||||||||||||||
| if (m_counter.has_value() && m_counterPosition == CounterPosition::ENTRY) { | ||||||||||||||||||||||||||||||||
| ++(*m_counter); | ||||||||||||||||||||||||||||||||
|
|
@@ -144,9 +145,15 @@ namespace dsf::mobility { | |||||||||||||||||||||||||||||||
| ++(*m_counter); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| std::unique_ptr<Agent> Street::dequeue(std::size_t const& index) { | ||||||||||||||||||||||||||||||||
| std::unique_ptr<Agent> Street::dequeue(std::size_t const& index, | ||||||||||||||||||||||||||||||||
| std::time_t const currentTime) { | ||||||||||||||||||||||||||||||||
| assert(!m_exitQueues[index].empty()); | ||||||||||||||||||||||||||||||||
| auto pAgent{std::move(m_exitQueues[index].front())}; | ||||||||||||||||||||||||||||||||
| // Keep track of average speed | ||||||||||||||||||||||||||||||||
| m_avgSpeeds.push_back(m_length / | ||||||||||||||||||||||||||||||||
| (currentTime - m_agentsInsertionTimes[pAgent->id()])); | ||||||||||||||||||||||||||||||||
| m_agentsInsertionTimes.erase(pAgent->id()); | ||||||||||||||||||||||||||||||||
|
Comment on lines
+153
to
+155
|
||||||||||||||||||||||||||||||||
| m_avgSpeeds.push_back(m_length / | |
| (currentTime - m_agentsInsertionTimes[pAgent->id()])); | |
| m_agentsInsertionTimes.erase(pAgent->id()); | |
| auto insertionIt = m_agentsInsertionTimes.find(pAgent->id()); | |
| if (insertionIt != m_agentsInsertionTimes.end()) { | |
| auto const delta = currentTime - insertionIt->second; | |
| if (delta > 0) { | |
| m_avgSpeeds.push_back(m_length / delta); | |
| } else { | |
| spdlog::warn( | |
| "Non-positive travel time ({} s) for {} on {}; skipping speed calculation.", | |
| delta, *pAgent, *this); | |
| } | |
| m_agentsInsertionTimes.erase(insertionIt); | |
| } |
Uh oh!
There was an error while loading. Please reload this page.