⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/dsf/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ PYBIND11_MODULE(dsf_cpp, m) {
.value("DOUBLE_TAIL", dsf::TrafficLightOptimization::DOUBLE_TAIL)
.export_values();

// Bind RoadStatus enum
pybind11::enum_<dsf::mobility::RoadStatus>(mobility, "RoadStatus")
.value("OPEN", dsf::mobility::RoadStatus::OPEN)
.value("CLOSED", dsf::mobility::RoadStatus::CLOSED)
.export_values();

// Bind spdlog log level enum
pybind11::enum_<spdlog::level::level_enum>(m, "LogLevel")
.value("TRACE", spdlog::level::trace)
Expand Down Expand Up @@ -243,6 +249,30 @@ PYBIND11_MODULE(dsf_cpp, m) {
pybind11::arg("cycleTime"),
pybind11::arg("counter"),
dsf::g_docstrings.at("dsf::mobility::RoadNetwork::makeTrafficLight").c_str())
.def(
"setStreetStatusById",
&dsf::mobility::RoadNetwork::setStreetStatusById,
pybind11::arg("streetId"),
pybind11::arg("status"),
dsf::g_docstrings.at("dsf::mobility::RoadNetwork::setStreetStatusById").c_str())
.def("setStreetStatusByName",
&dsf::mobility::RoadNetwork::setStreetStatusByName,
pybind11::arg("name"),
pybind11::arg("status"),
dsf::g_docstrings.at("dsf::mobility::RoadNetwork::setStreetStatusByName")
.c_str())
.def("changeStreetCapacityById",
&dsf::mobility::RoadNetwork::changeStreetCapacityById,
pybind11::arg("streetId"),
pybind11::arg("factor"),
dsf::g_docstrings.at("dsf::mobility::RoadNetwork::changeStreetCapacityById")
.c_str())
.def("changeStreetCapacityByName",
&dsf::mobility::RoadNetwork::changeStreetCapacityByName,
pybind11::arg("name"),
pybind11::arg("factor"),
dsf::g_docstrings.at("dsf::mobility::RoadNetwork::changeStreetCapacityByName")
.c_str())
.def("addCoil",
&dsf::mobility::RoadNetwork::addCoil,
pybind11::arg("streetId"),
Expand Down
2 changes: 1 addition & 1 deletion src/dsf/dsf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

static constexpr uint8_t DSF_VERSION_MAJOR = 4;
static constexpr uint8_t DSF_VERSION_MINOR = 7;
static constexpr uint8_t DSF_VERSION_PATCH = 5;
static constexpr uint8_t DSF_VERSION_PATCH = 6;

static auto const DSF_VERSION =
std::format("{}.{}.{}", DSF_VERSION_MAJOR, DSF_VERSION_MINOR, DSF_VERSION_PATCH);
Expand Down
37 changes: 35 additions & 2 deletions src/dsf/mobility/Road.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "../base/Edge.hpp"

#include <format>
#include <memory>
#include <optional>
#include <set>
Expand All @@ -15,6 +16,10 @@
TERTIARY = 3,
RESIDENTIAL = 4,
};
enum class RoadStatus : std::uint8_t {
OPEN = 0,

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
CLOSED = 1,

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
};

/// @brief The Road class represents a road in the network.
class Road : public Edge {
Expand All @@ -29,6 +34,7 @@
bool m_hasPriority = false;
std::set<Id> m_forbiddenTurns; // Stores the forbidden turns (road ids)
std::optional<RoadType> m_roadType{std::nullopt};
RoadStatus m_roadStatus = RoadStatus::OPEN;

public:
/// @brief Construct a new Road object
Expand Down Expand Up @@ -80,7 +86,10 @@
void setForbiddenTurns(std::set<Id> const& forbiddenTurns);
/// @brief Set the road type
/// @param roadType The road type
inline void setRoadType(RoadType roadType) { m_roadType = roadType; }
inline void setRoadType(RoadType const roadType) { m_roadType = roadType; }
/// @brief Set the road status
/// @param status The road status
inline void setStatus(RoadStatus const status) { m_roadStatus = status; }

/// @brief Get the length, in meters
/// @return double The length, in meters
Expand Down Expand Up @@ -111,6 +120,9 @@
/// @brief Get the road type
/// @return std::optional<RoadType> The road type
inline auto roadType() const noexcept { return m_roadType; }
/// @brief Get the road status
/// @return RoadStatus The road status
inline auto roadStatus() const noexcept { return m_roadStatus; }
/// @brief Get the road's turn direction given the previous road angle
/// @param previousStreetAngle The angle of the previous road
/// @return Direction The turn direction
Expand All @@ -127,4 +139,25 @@
virtual double nExitingAgents(Direction direction, bool normalizeOnNLanes) const = 0;
virtual double density(bool normalized = false) const = 0;
};
} // namespace dsf::mobility
} // namespace dsf::mobility

template <>
struct std::formatter<dsf::mobility::RoadStatus> {
constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(dsf::mobility::RoadStatus const& status, FormatContext&& ctx) const {
std::string_view name;
switch (status) {
case dsf::mobility::RoadStatus::OPEN:
name = "OPEN";
break;
case dsf::mobility::RoadStatus::CLOSED:
name = "CLOSED";
break;
default:
name = "UNKNOWN";
break;
}
return std::format_to(ctx.out(), "{}", name);
}
};
20 changes: 10 additions & 10 deletions src/dsf/mobility/RoadDynamics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,10 +499,10 @@ namespace dsf::mobility {
pItinerary->setPath(path);
auto const newSize{pItinerary->path().size()};
if (oldSize > 0 && newSize != oldSize) {
spdlog::warn("Path for itinerary {} changed size from {} to {}",
pItinerary->id(),
oldSize,
newSize);
spdlog::debug("Path for itinerary {} changed size from {} to {}",
pItinerary->id(),
oldSize,
newSize);
}
if (m_bCacheEnabled) {
pItinerary->save(std::format("{}{}.ity", CACHE_FOLDER, pItinerary->id()));
Expand Down Expand Up @@ -731,7 +731,7 @@ namespace dsf::mobility {
auto const timeTolerance{m_timeToleranceFactor.value() *
std::ceil(pStreet->length() / pStreet->maxSpeed())};
if (timeDiff > timeTolerance) {
spdlog::warn(
spdlog::debug(
"Time-step {} - {} currently on {} ({} turn - Traffic Light? {}), "
"has been still for more than {} seconds ({} seconds). Killing it.",
this->time_step(),
Expand Down Expand Up @@ -1302,7 +1302,7 @@ namespace dsf::mobility {
m_nAddedAgents += nAgents;
if (m_timeToleranceFactor.has_value() && !m_agents.empty()) {
auto const nStagnantAgents{m_agents.size()};
spdlog::warn(
spdlog::debug(
"Removing {} stagnant agents that were not inserted since the previous call to "
"addAgentsUniformly().",
nStagnantAgents);
Expand Down Expand Up @@ -1414,7 +1414,7 @@ namespace dsf::mobility {
m_nAddedAgents += nAgents;
if (m_timeToleranceFactor.has_value() && !m_agents.empty()) {
auto const nStagnantAgents{m_agents.size()};
spdlog::warn(
spdlog::debug(
"Removing {} stagnant agents that were not inserted since the previous call to "
"addAgentsRandomly().",
nStagnantAgents);
Expand Down Expand Up @@ -1520,9 +1520,9 @@ namespace dsf::mobility {
// Check if destination is reachable from source
auto const& itinerary = itineraryIt->second;
if (!itinerary->path().contains(*srcId)) {
spdlog::warn("Destination {} not reachable from source {}. Skipping agent.",
*dstId,
*srcId);
spdlog::debug("Destination {} not reachable from source {}. Skipping agent.",
*dstId,
*srcId);
--nAgents;
continue;
}
Expand Down
55 changes: 54 additions & 1 deletion src/dsf/mobility/RoadNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ namespace dsf::mobility {
}

if (priorityRoads.size() < 2) {
spdlog::warn("Node {}: unable to auto-assign road priorities", pNode->id());
spdlog::warn("{}: unable to auto-assign road priorities", *pNode);
return;
}

Expand Down Expand Up @@ -934,6 +934,59 @@ namespace dsf::mobility {
addEdge<Street>(std::move(street));
}

void RoadNetwork::setStreetStatusById(Id const streetId, RoadStatus const status) {
try {
edge(streetId)->setStatus(status);
} catch (const std::out_of_range&) {
throw std::out_of_range(std::format("Street with id {} not found", streetId));
}
}
void RoadNetwork::setStreetStatusByName(std::string const& streetName,
RoadStatus const status) {
std::atomic<std::size_t> nAffectedRoads{0};
std::for_each(DSF_EXECUTION m_edges.cbegin(),
m_edges.cend(),
[this, &streetName, &status, &nAffectedRoads](auto const& pair) {
auto const& pStreet = pair.second;
if (pStreet->name().find(streetName) != std::string::npos) {
pStreet->setStatus(status);
++nAffectedRoads;
}
});
spdlog::info("Set status {} to {} streets with name containing \"{}\"",
status,
nAffectedRoads.load(),
streetName);
}
void RoadNetwork::changeStreetCapacityById(Id const streetId, double const factor) {
try {
auto const& pStreet{edge(streetId)};
auto const& currentCapacity{pStreet->capacity()};
pStreet->setCapacity(std::ceil(currentCapacity * factor));
} catch (const std::out_of_range&) {
throw std::out_of_range(std::format("Street with id {} not found", streetId));
}
}
void RoadNetwork::changeStreetCapacityByName(std::string const& streetName,
double const factor) {
std::atomic<std::size_t> nAffectedRoads{0};
std::for_each(DSF_EXECUTION m_edges.cbegin(),
m_edges.cend(),
[this, &streetName, &factor, &nAffectedRoads](auto const& pair) {
auto const& pStreet = pair.second;
if (pStreet->name().find(streetName) != std::string::npos) {
auto const& currentCapacity = pStreet->capacity();
pStreet->setCapacity(std::ceil(currentCapacity * factor));
++nAffectedRoads;
}
});
spdlog::info(
"Changed capacity by factor {} to {} streets with name containing \"{}\"",
factor,
nAffectedRoads.load(),
streetName);
}

void RoadNetwork::setStreetStationaryWeights(
std::unordered_map<Id, double> const& weights) {
std::for_each(DSF_EXECUTION m_edges.cbegin(),
Expand Down
26 changes: 26 additions & 0 deletions src/dsf/mobility/RoadNetwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,24 @@ namespace dsf::mobility {
requires is_street_v<std::remove_reference_t<T1>> &&
(is_street_v<std::remove_reference_t<Tn>> && ...)
void addStreets(T1&& street, Tn&&... streets);

/// @brief Set the street's status by its id
/// @param streetId The id of the street
/// @param status The status to set
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

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

setStreetStatusById calls edge(streetId) which uses unordered_map::at and will throw std::out_of_range if the id doesn’t exist (see src/dsf/base/Network.hpp:224-230). Consider either documenting this in the comment or catching it and throwing a more specific exception (e.g., std::invalid_argument) consistent with other public RoadNetwork APIs.

Suggested change
/// @param status The status to set
/// @param status The status to set
/// @throws std::out_of_range if the given street id does not exist in the network

Copilot uses AI. Check for mistakes.
void setStreetStatusById(Id const streetId, RoadStatus const status);
/// @brief Set the street's status of all streets with the given name
/// @param name The name to match
/// @param status The status to set
void setStreetStatusByName(std::string const& name, RoadStatus const status);
/// @brief Change the street's capacity by its id
/// @param streetId The id of the street
/// @param factor The factor to multiply the capacity by
void changeStreetCapacityById(Id const streetId, double const factor);
/// @brief Change the street's capacity of all streets with the given name
/// @param name The name to match
/// @param factor The factor to multiply the capacity by
void changeStreetCapacityByName(std::string const& name, double const factor);

/// @brief Set the streets' stationary weights
/// @param streetWeights A map where the key is the street id and the value is the street stationary weight. If a street id is not present in the map, its stationary weight is set to 1.0.
void setStreetStationaryWeights(std::unordered_map<Id, double> const& streetWeights);
Expand Down Expand Up @@ -366,6 +384,10 @@ namespace dsf::mobility {
// Explore all incoming edges (nodes that can reach currentNode)
auto const& inEdges = node(currentNode)->ingoingEdges();
for (auto const& inEdgeId : inEdges) {
// Skip closed roads
if (edge(inEdgeId)->roadStatus() == RoadStatus::CLOSED) {
continue;
}
Id neighborId = edge(inEdgeId)->source();

// Calculate the weight of the edge from neighbor to currentNode using the dynamics function
Expand Down Expand Up @@ -471,6 +493,10 @@ namespace dsf::mobility {
// Explore all incoming edges (nodes that can reach currentNode)
auto const& inEdges = node(currentNode)->ingoingEdges();
for (auto const& inEdgeId : inEdges) {
// Skip closed roads
if (edge(inEdgeId)->roadStatus() == RoadStatus::CLOSED) {
continue;
}
Id neighborId = edge(inEdgeId)->source();

// Calculate the weight of the edge from neighbor to currentNode using the dynamics function
Expand Down
Empty file removed src/dsf/py.typed
Empty file.
Loading
Loading