Skip to content

Conversation

@Grufoony
Copy link
Collaborator

@Grufoony Grufoony commented May 9, 2025

No description provided.

@Grufoony Grufoony requested a review from Copilot May 9, 2025 09:06
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the TrafficLight cycle logic to optimize non‐local phase adjustments, and adds new utility functions for traffic light and road network management.

  • Refactored isGreen() implementation in TrafficLightCycle for handling cycle wraparound
  • Introduced increasePhases() in TrafficLight to update cycle phases
  • Added importTrafficLights() in RoadNetwork to load traffic light configurations from a file
  • Updated related header declarations and modified agent queue and optimization handling in RoadDynamics
  • Bumped DSM version from 2.6.25 to 2.6.26

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/dsm/sources/TrafficLight.cpp Updated isGreen() logic and added increasePhases() implementation
src/dsm/sources/RoadNetwork.cpp Improved traffic light auto-init logic and added importTrafficLights()
src/dsm/headers/TrafficLight.hpp Declared new increasePhases() method
src/dsm/headers/RoadNetwork.hpp Declared new importTrafficLights() method
src/dsm/headers/RoadDynamics.hpp Enhanced agent street-id checks and optimized double tail logic
src/dsm/dsm.hpp Updated DSM version patch number
Comments suppressed due to low confidence (3)

src/dsm/sources/TrafficLight.cpp:140

  • [nitpick] The parameter 'phase' in increasePhases() could be more descriptive (e.g., 'phaseOffset') to clarify its purpose in adjusting the current phase.
void TrafficLight::increasePhases(Delay const phase) {

src/dsm/sources/RoadNetwork.cpp:964

  • [nitpick] The storedGreenTimes map is declared inside the loop, which means its state is reset for each line. If multiple entries for the same traffic light are expected, consider declaring it outside the loop or clarifying the expected file format.
std::unordered_map<Id, Delay> storedGreenTimes;

src/dsm/headers/RoadDynamics.hpp:1265

  • Ensure that using pStreet->id() as the key in m_queuesAtTrafficLights is consistent with how keys are initialized elsewhere, to avoid mismatches in queue lookups.
if (!m_queuesAtTrafficLights.contains(pStreet->id())) {

@codecov
Copy link

codecov bot commented May 9, 2025

Codecov Report

Attention: Patch coverage is 25.32468% with 115 lines in your changes missing coverage. Please review.

Project coverage is 84.78%. Comparing base (e7005b2) to head (5992b91).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/dsm/sources/RoadNetwork.cpp 22.38% 52 Missing ⚠️
src/dsm/headers/RoadDynamics.hpp 20.33% 47 Missing ⚠️
src/dsm/utility/Logger.hpp 23.07% 10 Missing ⚠️
src/dsm/sources/TrafficLight.cpp 45.45% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #301      +/-   ##
==========================================
- Coverage   85.57%   84.78%   -0.79%     
==========================================
  Files          38       38              
  Lines        5503     5574      +71     
  Branches      565      572       +7     
==========================================
+ Hits         4709     4726      +17     
- Misses        794      848      +54     
Flag Coverage Δ
unittests 84.78% <25.32%> (-0.79%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cppcheck (reported by Codacy) found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

@Grufoony Grufoony requested a review from Copilot May 21, 2025 09:33
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors traffic light cycle logic, adds utility functions for phase adjustments, implements CSV-based traffic light import, extends node export with new columns, migrates queue data structures to TBB concurrent containers, and enhances the double-tail optimization in road dynamics.

  • Simplify and correct wraparound logic in TrafficLightCycle::isGreen and add increasePhases
  • Introduce importTrafficLights and update exportNodes format in RoadNetwork
  • Switch to tbb::concurrent_unordered_map in RoadDynamics, fix agent street‐ID checks, and improve optimization flow
  • Bump DSM patch version to 2.6.26

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/dsm/sources/TrafficLight.cpp Rewrite isGreen, add increasePhases implementation
src/dsm/headers/TrafficLight.hpp Declared increasePhases method
src/dsm/sources/RoadNetwork.cpp Added importTrafficLights, enhanced auto-init, updated CSV export
src/dsm/headers/RoadNetwork.hpp Declared importTrafficLights in header
src/dsm/headers/RoadDynamics.hpp Migrated m_queuesAtTrafficLights to TBB, adjusted agent and queue logic
src/dsm/sources/RoadDynamics.cpp Fixed ID misuse, removed capacity guard in loop, added zero‐division risk
src/dsm/dsm.hpp Incremented patch version
Comments suppressed due to low confidence (4)

src/dsm/sources/RoadDynamics.cpp:834

  • This message logs missing streetId but the check is on nextStreetId; consider updating the error text to reference nextStreetId for accuracy.
Logger::error(std::format("Agent {} has no street id", pAgentTemp->id()));

src/dsm/sources/RoadDynamics.cpp:1841

  • Potential divide-by-zero if n is zero (no incoming traffic-light nodes found); add a check if (n > 0) before performing the division.
density /= n;

src/dsm/headers/TrafficLight.hpp:138

  • [nitpick] The parameter name phase is ambiguous against the member m_phase; consider renaming to deltaPhase or offset for clarity.
void increasePhases(Delay const phase);

src/dsm/sources/RoadDynamics.cpp:1053

  • Removing the capacity check may cause an infinite loop if all edges are full; reintroduce a guard or fallback to avoid potential hangs.
} while (this->graph().edge(streetId)->isFull());

/// @brief Decrease the green times of the traffic light for priority streets and increase the green times for non-priority streets
/// @param delta Delay, the time to increase or decrease the green times
void decreaseGreenTimes(Delay const delta);

Copy link

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a brief Doxygen comment above increasePhases explaining its purpose and parameters to keep the public API documented.

Suggested change
/// @brief Increase the phase times of the traffic light cycles
/// @param phase Delay, the amount of time to increase the phase for each cycle

Copilot uses AI. Check for mistakes.
Comment on lines 959 to 964
while (!file.eof()) {
std::getline(file, line);
if (line.empty()) {
continue;
}
std::unordered_map<Id, Delay> storedGreenTimes;
Copy link

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

storedGreenTimes is reinitialized on every loop iteration, preventing multi-line grouping; move its declaration outside the while to accumulate across all lines.

Suggested change
while (!file.eof()) {
std::getline(file, line);
if (line.empty()) {
continue;
}
std::unordered_map<Id, Delay> storedGreenTimes;
std::unordered_map<Id, Delay> storedGreenTimes;
while (!file.eof()) {
std::getline(file, line);
if (line.empty()) {
continue;
}

Copilot uses AI. Check for mistakes.
Comment on lines 959 to 960
while (!file.eof()) {
std::getline(file, line);
Copy link

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using while (!file.eof()) is error-prone; prefer while (std::getline(file, line)) to handle file input more robustly.

Suggested change
while (!file.eof()) {
std::getline(file, line);
while (std::getline(file, line)) {

Copilot uses AI. Check for mistakes.
std::ofstream file{path};
// Column names
file << "id;source_id;target_id;name;coil_code;geometry\n";
file << "id;source_id;target_id;length;capacity;name;coil_code;geometry\n";
Copy link

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CSV header was extended to include length and capacity; update the function comment above exportNodes in the header to reflect the new columns.

Copilot uses AI. Check for mistakes.
/// - forbiddenTurns: The forbidden turns of the street, encoding information about street into which the street cannot output agents. The format is a string "sourceId1-targetid1, sourceId2-targetid2,..."
/// - coilcode: An integer code to identify the coil located on the street
void importOSMEdges(const std::string& fileName);

Copy link

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a Doxygen comment describing importTrafficLights, its expected file format, and behavior on parsing errors.

Suggested change
/**
* @brief Import traffic light data from a file.
* @param fileName The path to the file containing traffic light data.
* @details The file format is csv-like with the ';' separator. Supported columns (in order):
* - id: The unique identifier of the traffic light
* - junctionId: The id of the junction where the traffic light is located
* - state: The initial state of the traffic light (e.g., "red", "green", "yellow")
* - timing: The timing configuration for the traffic light, in seconds
*
* Parsing errors, such as missing or malformed columns, will be logged as warnings,
* and the corresponding traffic light entry will be skipped.
*/

Copilot uses AI. Check for mistakes.
@Grufoony Grufoony requested a review from Copilot May 27, 2025 09:14
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the traffic light optimization to support a non-local “green-wave” approach and adds helper methods for phase adjustment and CSV import/export enhancements.

  • Simplified isGreen logic and added increasePhases to TrafficLight
  • Normalized and sorted street angles, plus a new importTrafficLights CSV loader
  • Updated optimizeTrafficLights to include ratio-based non-local optimization and bumped DSM version

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/dsm/sources/TrafficLight.cpp Refactored isGreen logic; added increasePhases
src/dsm/headers/TrafficLight.hpp Declared increasePhases
src/dsm/sources/RoadNetwork.cpp Normalized/ordered street angles; CSV import/export
src/dsm/headers/RoadNetwork.hpp Added importTrafficLights signature and docs
src/dsm/headers/RoadDynamics.hpp Switched to tbb::concurrent_unordered_map; updated optimizer signature and logic
src/dsm/sources/RoadDynamics.cpp Adapted non-local optimization and queue tracking
src/dsm/dsm.hpp Bumped version minor/patch to 27.0
examples/slow_charge_tl.cpp Updated optimizeTrafficLights invocation to new signature
Comments suppressed due to low confidence (3)

src/dsm/sources/RoadDynamics.cpp:835

  • This check logs an error for missing streetId but then continues to use nextStreetId; either log and continue or check nextStreetId before dereferencing to avoid invalid state.
        if (!pAgentTemp->streetId().has_value()) {

src/dsm/sources/RoadNetwork.cpp:1055

  • The original capacity check (&& this->nAgents() < this->graph().maxCapacity()) was removed, so this loop may never terminate if all edges are full; reintroduce a guard against exceeding max capacity.
      } while (this->graph().edge(streetId)->isFull());

src/dsm/sources/TrafficLight.cpp:140

  • [nitpick] The new increasePhases method does not appear to have accompanying unit tests; consider adding tests to verify phase updates for both wraparound and non-wraparound cases.
  void TrafficLight::increasePhases(Delay const phase) {

Comment on lines +159 to +168
std::vector<std::pair<Id, double>> sortedAngles;
std::copy(
streetAngles.begin(), streetAngles.end(), std::back_inserter(sortedAngles));
std::sort(sortedAngles.begin(),
sortedAngles.end(),
[](auto const& a, auto const& b) { return a.second < b.second; });
streetAngles.clear();
for (auto const& [streetId, angle] : sortedAngles) {
streetAngles.emplace(streetId, angle);
}
Copy link

Copilot AI May 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Reinserting sorted entries into an unordered_map does not guarantee stable iteration order; if ordering matters later, consider using a vector or an ordered map structure instead.

Suggested change
std::vector<std::pair<Id, double>> sortedAngles;
std::copy(
streetAngles.begin(), streetAngles.end(), std::back_inserter(sortedAngles));
std::sort(sortedAngles.begin(),
sortedAngles.end(),
[](auto const& a, auto const& b) { return a.second < b.second; });
streetAngles.clear();
for (auto const& [streetId, angle] : sortedAngles) {
streetAngles.emplace(streetId, angle);
}
std::sort(streetAngles.begin(),
streetAngles.end(),
[](auto const& a, auto const& b) { return a.second < b.second; });

Copilot uses AI. Check for mistakes.
@Grufoony
Copy link
Collaborator Author

It is not clear why macos examples fail... I don't really care about mac at all

@Grufoony Grufoony merged commit 4fab95e into main May 28, 2025
29 of 31 checks passed
@Grufoony Grufoony deleted the greenWaving branch May 28, 2025 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants