Skip to content

Commit 99ba82a

Browse files
committed
Dump
1 parent 20d2863 commit 99ba82a

File tree

12 files changed

+118
-417
lines changed

12 files changed

+118
-417
lines changed

examples/slow_charge_rb.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ int main(int argc, char** argv) {
8282
std::cout << "Using dsf version: " << dsf::version() << '\n';
8383
RoadNetwork graph{};
8484
std::cout << "Importing matrix.dat...\n";
85-
graph.importEdges("./data/manhattan_edges.csv");
86-
graph.importNodeProperties("./data/manhattan_nodes.csv");
85+
graph.importEdges("../test/data/manhattan_edges.csv");
86+
graph.importNodeProperties("../test/data/manhattan_nodes.csv");
8787
std::cout << "Setting street parameters..." << '\n';
8888

8989
std::cout << "Number of nodes: " << graph.nNodes() << '\n';

examples/slow_charge_tl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ int main(int argc, char** argv) {
8989
std::cout << "Using dsf version: " << dsf::version() << '\n';
9090
RoadNetwork graph{};
9191
std::cout << "Importing matrix.dat...\n";
92-
graph.importEdges("./data/manhattan_edges.csv");
93-
graph.importNodeProperties("./data/manhattan_nodes.csv");
92+
graph.importEdges("../test/data/manhattan_edges.csv");
93+
graph.importNodeProperties("../test/data/manhattan_nodes.csv");
9494
std::cout << "Setting street parameters..." << '\n';
9595

9696
// graph.addStreet(Street(100002, std::make_pair(0, 108)));

profiling/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ using Dynamics = dsf::FirstOrderDynamics;
1515
int main() {
1616
RoadNetwork graph{};
1717
std::cout << "Importing matrix.dat...\n";
18-
graph.importMatrix("../test/data/rawMatrix.dat", false);
18+
graph.importEdges("../examples/data/manhattan_edges.csv");
1919
std::cout << "Number of nodes: " << graph.nNodes() << '\n'
2020
<< "Number of streets: " << graph.nEdges() << '\n';
2121
for (auto const& pair : graph.edges()) {

src/dsf/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
from dsf_cpp import *
22
from dsf_cpp import __version__
3-
from .python.cartography import (get_cartography, graph_from_gdfs, graph_to_gdfs, create_manhattan_cartography)
3+
from .python.cartography import (
4+
get_cartography,
5+
graph_from_gdfs,
6+
graph_to_gdfs,
7+
create_manhattan_cartography,
8+
)

src/dsf/binding.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,6 @@ PYBIND11_MODULE(dsf_cpp, m) {
160160
.def("autoMapStreetLanes",
161161
&dsf::RoadNetwork::autoMapStreetLanes,
162162
dsf::g_docstrings.at("dsf::RoadNetwork::autoMapStreetLanes").c_str())
163-
.def("importMatrix",
164-
&dsf::RoadNetwork::importMatrix,
165-
pybind11::arg("fileName"),
166-
pybind11::arg("isAdj") = true,
167-
pybind11::arg("defaultSpeed") = 13.8888888889,
168-
dsf::g_docstrings.at("dsf::RoadNetwork::importMatrix").c_str())
169-
.def("importCoordinates",
170-
&dsf::RoadNetwork::importCoordinates,
171-
pybind11::arg("fileName"),
172-
dsf::g_docstrings.at("dsf::RoadNetwork::importCoordinates").c_str())
173163
.def(
174164
"importEdges",
175165
[](dsf::RoadNetwork& self, const std::string& fileName) {

src/dsf/headers/RoadDynamics.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,11 @@ namespace dsf {
673673
bool overtimed{false};
674674
{
675675
auto const timeDiff{this->time_step() - pAgentTemp->freeTime()};
676-
auto const timeTolerance{3 *
677-
std::ceil(pStreet->length() / pStreet->maxSpeed())};
676+
// A minute of delay has never hurt anyone, right?
677+
auto const timeTolerance{
678+
std::max(static_cast<std::time_t>(60),
679+
static_cast<std::time_t>(
680+
3 * std::ceil(pStreet->length() / pStreet->maxSpeed())))};
678681
if (timeDiff > timeTolerance) {
679682
overtimed = true;
680683
spdlog::warn(

src/dsf/headers/RoadNetwork.hpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,6 @@ namespace dsf {
9292
/// @details For example, if one street has the right turn forbidden, then the right lane becomes a straight one
9393
void autoMapStreetLanes();
9494

95-
/// @brief Import the graph's adjacency matrix from a file.
96-
/// If the file is not of a supported format, it will read the file as a matrix with the first two elements being
97-
/// the number of rows and columns and the following elements being the matrix elements.
98-
/// @param fileName The name of the file to import the adjacency matrix from.
99-
/// @param isAdj A boolean value indicating if the file contains the adjacency matrix or the distance matrix.
100-
/// @param defaultSpeed The default speed limit for the streets
101-
/// @throws std::invalid_argument if the file is not found or invalid
102-
/// The matrix format is deduced from the file extension. Currently only .dsm files are supported.
103-
void importMatrix(const std::string& fileName,
104-
bool isAdj = true,
105-
double defaultSpeed = 13.8888888889);
106-
/// @brief Import the graph's nodes from a file
107-
/// @param fileName The name of the file to import the nodes from.
108-
/// @throws std::invalid_argument if the file is not found, invalid or the format is not supported
109-
/// @details The file format is deduced from the file extension. Currently only .dsm files are supported.
110-
/// The first input number is the number of nodes, followed by the coordinates of each node.
111-
/// In the i-th row of the file, the (i - 1)-th node's coordinates are expected.
112-
[[deprecated]] void importCoordinates(const std::string& fileName);
113-
11495
/// @brief Import the graph's streets from a file
11596
/// @param fileName The name of the file to import the streets from.
11697
/// @details Supports csv, json and geojson file formats.
@@ -157,13 +138,6 @@ namespace dsf {
157138
/// and the speed limit, if such data is available in the file.
158139
void importTrafficLights(const std::string& fileName);
159140

160-
/// @brief Export the graph's adjacency matrix to a file
161-
/// @param path The path to the file to export the adjacency matrix to (default: ./matrix.dsm)
162-
/// @param isAdj A boolean value indicating if the file contains the adjacency matrix or the distance matrix.
163-
/// @throws std::invalid_argument if the file is not found or invalid
164-
[[deprecated]] void exportMatrix(std::string path = "./matrix.dsm",
165-
bool isAdj = true);
166-
167141
template <typename T1, typename... Tn>
168142
requires is_node_v<std::remove_reference_t<T1>> &&
169143
(is_node_v<std::remove_reference_t<Tn>> && ...)

src/dsf/sources/RoadNetwork.cpp

Lines changed: 0 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -756,157 +756,6 @@ namespace dsf {
756756
}
757757
}
758758

759-
void RoadNetwork::importMatrix(const std::string& fileName,
760-
bool isAdj,
761-
double defaultSpeed) {
762-
// check the file extension
763-
std::string fileExt = fileName.substr(fileName.find_last_of(".") + 1);
764-
if (fileExt == "dsf") {
765-
std::ifstream file{fileName};
766-
if (!file.is_open()) {
767-
throw std::runtime_error("Error opening file \"" + fileName + "\" for reading.");
768-
}
769-
Size rows, cols;
770-
file >> rows >> cols;
771-
if (rows != cols) {
772-
throw std::invalid_argument("Adjacency matrix must be square");
773-
}
774-
Size n{rows};
775-
addNDefaultNodes<Intersection>(n);
776-
// each line has 2 elements
777-
while (!file.eof()) {
778-
Id index;
779-
double val;
780-
file >> index >> val;
781-
const auto srcId{static_cast<Id>(index / n)};
782-
const auto dstId{static_cast<Id>(index % n)};
783-
if (isAdj) {
784-
addStreet(Street(index, std::make_pair(srcId, dstId)));
785-
} else {
786-
addStreet(Street(index, std::make_pair(srcId, dstId), val));
787-
}
788-
edge(index)->setMaxSpeed(defaultSpeed);
789-
}
790-
} else {
791-
// default case: read the file as a matrix with the first two elements being the number of rows and columns and
792-
// the following elements being the matrix elements
793-
std::ifstream file{fileName};
794-
if (!file.is_open()) {
795-
throw std::runtime_error("Error opening file \"" + fileName + "\" for reading.");
796-
}
797-
Size rows, cols;
798-
file >> rows >> cols;
799-
if (rows != cols) {
800-
throw std::invalid_argument(
801-
"Adjacency matrix must be square. Rows: " + std::to_string(rows) +
802-
" Cols: " + std::to_string(cols));
803-
}
804-
Size n{rows};
805-
addNDefaultNodes<Intersection>(n);
806-
if (n * n > std::numeric_limits<Id>::max()) {
807-
throw std::invalid_argument(
808-
"Matrix size is too large for the current type of Id.");
809-
}
810-
Id index{0};
811-
while (!file.eof()) {
812-
double value;
813-
file >> value;
814-
if (value < 0) {
815-
throw std::invalid_argument(
816-
std::format("Element at index {} is negative ({}).", index, value));
817-
}
818-
if (value > 0) {
819-
const auto srcId{static_cast<Id>(index / n)};
820-
const auto dstId{static_cast<Id>(index % n)};
821-
if (isAdj) {
822-
addStreet(Street(index, std::make_pair(srcId, dstId)));
823-
} else {
824-
addStreet(Street(index, std::make_pair(srcId, dstId), value));
825-
}
826-
edge(index)->setMaxSpeed(defaultSpeed);
827-
}
828-
++index;
829-
}
830-
}
831-
this->m_updateMaxAgentCapacity();
832-
}
833-
834-
void RoadNetwork::importCoordinates(const std::string& fileName) {
835-
std::string fileExt = fileName.substr(fileName.find_last_of(".") + 1);
836-
if (fileExt == "dsf") {
837-
// first input number is the number of nodes
838-
std::ifstream file{fileName};
839-
if (!file.is_open()) {
840-
throw std::runtime_error("Error opening file \"" + fileName + "\" for reading.");
841-
}
842-
Size n;
843-
file >> n;
844-
if (n < this->nNodes()) {
845-
throw std::invalid_argument("Number of node coordinates in file is too small.");
846-
}
847-
double lat, lon;
848-
for (Size i{0}; i < n; ++i) {
849-
file >> lon >> lat;
850-
try {
851-
node(i)->setGeometry({lon, lat});
852-
} catch (...) {
853-
spdlog::warn(std::format("Node with id {} not found.", i));
854-
}
855-
}
856-
} else if (fileExt == "csv") {
857-
std::ifstream ifs{fileName};
858-
if (!ifs.is_open()) {
859-
throw std::runtime_error("Error opening file \"" + fileName + "\" for reading.");
860-
}
861-
// Check if the first line is nodeId;lat;lon
862-
std::string line;
863-
std::getline(ifs, line);
864-
if (line != "id;lat;lon;type") {
865-
throw std::invalid_argument("Invalid file format.");
866-
}
867-
double dLat, dLon;
868-
while (!ifs.eof()) {
869-
std::getline(ifs, line);
870-
if (line.empty()) {
871-
continue;
872-
}
873-
std::istringstream iss{line};
874-
std::string nodeId, lat, lon, type;
875-
std::getline(iss, nodeId, ';');
876-
std::getline(iss, lat, ';');
877-
std::getline(iss, lon, ';');
878-
std::getline(iss, type, '\n');
879-
dLon = lon == "Nan" ? 0. : std::stod(lon);
880-
dLat = lat == "Nan" ? 0. : std::stod(lat);
881-
auto const& nodes{this->nodes()};
882-
auto it = nodes.find(std::stoul(nodeId));
883-
if (it != nodes.cend()) {
884-
auto const& pNode{it->second};
885-
pNode->setGeometry(dsf::geometry::Point(dLon, dLat));
886-
if (type == "traffic_light" && !pNode->isTrafficLight()) {
887-
makeTrafficLight(it->first, 60);
888-
} else if (type == "roundabout" && !pNode->isRoundabout()) {
889-
makeRoundabout(it->first);
890-
}
891-
} else {
892-
spdlog::warn("Node with id {} not found. Skipping coordinates ({}, {}).",
893-
nodeId,
894-
dLat,
895-
dLon);
896-
}
897-
}
898-
} else {
899-
throw std::invalid_argument(
900-
std::format("File extension ({}) not supported", fileExt));
901-
}
902-
for (auto const& [_, pEdge] : edges()) {
903-
auto const& pSourceNode{node(pEdge->source())};
904-
auto const& pTargetNode{node(pEdge->target())};
905-
pEdge->setGeometry(
906-
geometry::PolyLine{*(pSourceNode->geometry()), *(pTargetNode->geometry())});
907-
}
908-
}
909-
910759
void RoadNetwork::importTrafficLights(const std::string& fileName) {
911760
std::ifstream file{fileName};
912761
if (!file.is_open()) {
@@ -951,24 +800,6 @@ namespace dsf {
951800
}
952801
}
953802

954-
void RoadNetwork::exportMatrix(std::string path, bool isAdj) {
955-
std::ofstream file{path};
956-
if (!file.is_open()) {
957-
throw std::runtime_error("Error opening file \"" + path + "\" for writing.");
958-
}
959-
auto const N{nNodes()};
960-
file << N << '\t' << N;
961-
if (isAdj) {
962-
std::for_each(m_edges.cbegin(), m_edges.cend(), [&N, &file](auto const& pair) {
963-
file << '\n' << pair.second->source() * N + pair.second->target() << '\t' << 1;
964-
});
965-
} else {
966-
std::for_each(m_edges.cbegin(), m_edges.cend(), [&N, &file](auto const& pair) {
967-
file << '\n' << pair.second->id() << '\t' << pair.second->length();
968-
});
969-
}
970-
}
971-
972803
TrafficLight& RoadNetwork::makeTrafficLight(Id const nodeId,
973804
Delay const cycleTime,
974805
Delay const counter) {

0 commit comments

Comments
 (0)