Skip to content

Commit

Permalink
Merge pull request #16193 from s0nofab1t/afrotest
Browse files Browse the repository at this point in the history
Removed parameter 'partition' from ctor of AFRouter
  • Loading branch information
namdre authored Feb 18, 2025
2 parents 73ef042 + 9d0b45e commit 82cd94a
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 89 deletions.
22 changes: 12 additions & 10 deletions src/afrotest/AFROTest.cpp
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/****************************************************************************/
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
// Copyright (C) 2001-2023 German Aerospace Center (DLR) and others.
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
// Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0/
Expand All @@ -20,12 +20,12 @@
#include <config.h>
#include "AFROTest.h"

/// @brief The number of levels in the k-d tree partition
constexpr auto NUMBER_OF_LEVELS = 5; //or 4 or 8
/// @brief The maximum number of levels in the k-d tree partition
constexpr auto MAX_NUMBER_OF_LEVELS = 5; //or 4 or 8
/// @brief The number of start edges of the route queries
constexpr auto NUMBER_OF_START_EDGES = 32; // 32 -> 32^2 = 1024 test routes
constexpr auto MAX_NUMBER_OF_START_EDGES = 32; // 32 -> 32^2 = maximal 1024 test routes
/// @brief The number of end edges of the route queries
constexpr auto NUMBER_OF_END_EDGES = NUMBER_OF_START_EDGES;
constexpr auto MAX_NUMBER_OF_END_EDGES = MAX_NUMBER_OF_START_EDGES;

//#define ATST_DEBUG_LEVEL_0

Expand All @@ -49,14 +49,15 @@ AFROTest::test(const ROVehicle* const vehicle, bool unbuildIsWarning, typename S
long long int kDPartitionBuildStart = 0;
long long int kDPartitionBuildTime = 0;
kDPartitionBuildStart = SysUtils::getCurrentMillis();
KDTreePartition<ROEdge, RONode, ROVehicle>* partition = new KDTreePartition<ROEdge, RONode, ROVehicle>(NUMBER_OF_LEVELS, edges, havePermissions, haveRestrictions);
KDTreePartition<ROEdge, RONode, ROVehicle>* partition = new KDTreePartition<ROEdge, RONode, ROVehicle>(MAX_NUMBER_OF_LEVELS, edges, havePermissions, haveRestrictions);
partition->init(vehicle);
kDPartitionBuildTime = (SysUtils::getCurrentMillis() - kDPartitionBuildStart);
std::cout << "Time spent for k-d partition build: " << elapsedMs2string(kDPartitionBuildTime) << std::endl;
std::cout << "Forward k-d tree partition ready." << std::endl;
AFRouter<ROEdge, RONode, ROVehicle>* arcFlagRouter = new AFRouter<ROEdge, RONode, ROVehicle>(edges,
partition, unbuildIsWarning, operation, flippedOperation, weightPeriod, lookup, flippedLookup, havePermissions,
unbuildIsWarning, operation, flippedOperation, weightPeriod, lookup, flippedLookup, havePermissions,
haveRestrictions);
std::cout << "AFRouter instantiated." << std::endl;
CHRouter<ROEdge, ROVehicle>* cHRouter = new CHRouter<ROEdge, ROVehicle>(
edges, unbuildIsWarning, operation, /*SVC_IGNORING*/vehicle->getVClass(), weightPeriod, havePermissions, haveRestrictions);
AStarRouter<ROEdge, ROVehicle>* aStar = new AStarRouter<ROEdge, ROVehicle>(edges, unbuildIsWarning, operation, lookup,
Expand Down Expand Up @@ -190,7 +191,7 @@ AFROTest::testRoutes(const Cell* cell1, const Cell* cell2, const ROVehicle* cons
}
std::unordered_set<const ROEdge*> someCell1InsideEdges;
auto iter = cell1InsideEdges->begin();
std::advance(iter, MIN2(cell1InsideEdges->size(), static_cast<size_t>(NUMBER_OF_START_EDGES)));
std::advance(iter, MIN2(cell1InsideEdges->size(), static_cast<size_t>(MAX_NUMBER_OF_START_EDGES)));
someCell1InsideEdges.insert(cell1InsideEdges->begin(), iter);
delete cell1InsideEdges;
std::unordered_set<const ROEdge*>* cell2InsideEdges = cell2->edgeSet(vehicle);
Expand All @@ -204,7 +205,7 @@ AFROTest::testRoutes(const Cell* cell1, const Cell* cell2, const ROVehicle* cons
}
std::unordered_set<const ROEdge*> someCell2InsideEdges;
auto iter2 = cell2InsideEdges->begin();
std::advance(iter2, MIN2(cell2InsideEdges->size(), static_cast<size_t>(NUMBER_OF_END_EDGES)));
std::advance(iter2, MIN2(cell2InsideEdges->size(), static_cast<size_t>(MAX_NUMBER_OF_END_EDGES)));
someCell2InsideEdges.insert(cell2InsideEdges->begin(), iter2);
delete cell2InsideEdges;

Expand Down Expand Up @@ -409,4 +410,5 @@ AFROTest::testQuery(const ROEdge* edge1, const ROEdge* edge2, const ROVehicle* c
}
}


/****************************************************************************/
8 changes: 2 additions & 6 deletions src/duarouter/duarouter_main.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
/// @author Daniel Krajzewicz
/// @author Jakob Erdmann
/// @author Michael Behrisch
/// @author Ruediger Ebendt
/// @date Thu, 06 Jun 2002
///
// Main for DUAROUTER
Expand Down Expand Up @@ -150,15 +151,10 @@ computeRoutes(RONet& net, ROLoader& loader, OptionsCont& oc) {
ROEdge::getAllEdges(), oc.getBool("ignore-errors"), ttFunction,
begin, end, weightPeriod, net.hasPermissions(), oc.getInt("routing-threads"));
} else if (routingAlgorithm == "arcflag") {
/// @brief The number of levels in the k-d tree partition
constexpr auto NUMBER_OF_LEVELS = 5; //or 4 or 8
ROVehicle defaultVehicle(SUMOVehicleParameter(), nullptr, net.getVehicleTypeSecure(DEFAULT_VTYPE_ID), &net);
KDTreePartition<ROEdge, RONode, ROVehicle>* partition = new KDTreePartition<ROEdge, RONode, ROVehicle>(NUMBER_OF_LEVELS, ROEdge::getAllEdges(), net.hasPermissions(), oc.isSet("restriction-params"));
partition->init(&defaultVehicle);
auto ttFunction = gWeightsRandomFactor > 1 ? &ROEdge::getTravelTimeStaticRandomized : &ROEdge::getTravelTimeStatic;
auto reversedTtFunction = gWeightsRandomFactor > 1 ? &FlippedEdge<ROEdge, RONode, ROVehicle>::getTravelTimeStaticRandomized : &FlippedEdge<ROEdge, RONode, ROVehicle>::getTravelTimeStatic;
router = new AFRouter<ROEdge, RONode, ROVehicle>(ROEdge::getAllEdges(),
partition, oc.getBool("ignore-errors"), ttFunction, reversedTtFunction, (oc.isSet("weight-files") ? string2time(oc.getString("weight-period")) : SUMOTime_MAX),
oc.getBool("ignore-errors"), ttFunction, reversedTtFunction, (oc.isSet("weight-files") ? string2time(oc.getString("weight-period")) : SUMOTime_MAX),
nullptr, nullptr, net.hasPermissions(), oc.isSet("restriction-params"));
} else {
throw ProcessError(TLF("Unknown routing Algorithm '%'!", routingAlgorithm));
Expand Down
25 changes: 18 additions & 7 deletions src/utils/router/AFBuild.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ class AFBuild {
/** @brief Constructor
* @param[in] flippedEdges The flipped (aka reversed / backward) edges
* @param[in] flippedPartition The k-d tree partition of the backward graph with flipped edges
* @param[in] numberOfLevels The number of levels
* @param[in] unbuildIsWarning The flag indicating whether network unbuilds should issue warnings or errors
* @param[in] flippedOperation The operation for a backward graph with flipped edges
* @param[in] flippedLookup The lookup table for a backward graph with flipped edges
Expand All @@ -102,14 +101,14 @@ class AFBuild {
AFBuild(
const std::vector<FlippedEdge<E, N, V>*>& flippedEdges,
const KDTreePartition<FlippedEdge<E, N, V>, FlippedNode<E, N, V>, V>* const flippedPartition,
int numberOfLevels, bool unbuildIsWarning,
bool unbuildIsWarning,
typename SUMOAbstractRouter<FlippedEdge<E, N, V>, V>::Operation flippedOperation,
const std::shared_ptr<const FlippedLookupTable> flippedLookup = nullptr,
const bool havePermissions = false, const bool haveRestrictions = false,
const std::vector<FlippedEdge<E, N, V>*>* toProhibit = nullptr) :
myFlippedEdges(flippedEdges),
myFlippedPartition(flippedPartition),
myNumberOfLevels(numberOfLevels),
myNumberOfLevels(-1),
myErrorMsgHandler(unbuildIsWarning ? MsgHandler::getWarningInstance() : MsgHandler::getErrorInstance()),
myFlippedOperation(flippedOperation),
myFlippedLookupTable(flippedLookup),
Expand Down Expand Up @@ -144,7 +143,7 @@ class AFBuild {
* @param[in] vehicle The vehicle
* @param[in] flagInfos The arc flag informations
*/
void init(SUMOTime time, const V* const vehicle, std::vector<FlagInfo*>& flagInfos);
void init(SUMOTime msTime, const V* const vehicle, std::vector<FlagInfo*>& flagInfos);
/** @brief Set the flipped partition
* param[in] flippedPartition The flipped partition
*/
Expand All @@ -156,13 +155,19 @@ class AFBuild {
* @return The SHARC level number corresponding to the given partition level number
*/
int partitionLevel2SHARCLevel(int partitionLevel) {
if (myNumberOfLevels < 0) {
throw std::runtime_error("The (actual) number of levels is uninitialized, call AFBuild::init() before.");
}
return AFRouter<E, N, V>::partitionLevel2SHARCLevel(partitionLevel, myNumberOfLevels);
}
/** @brief Converts a SHARC level number to a partition level number
* @param[in] sHARCLevel The SHARC level
* @return The partition level number corresponding to the given SHARC level number
*/
int sHARCLevel2PartitionLevel(int sHARCLevel) {
if (myNumberOfLevels < 0) {
throw std::runtime_error("The (actual) number of levels is uninitialized, call AFBuild::init() before.");
}
return AFRouter<E, N, V>::sHARCLevel2PartitionLevel(sHARCLevel, myNumberOfLevels);
}

Expand Down Expand Up @@ -199,7 +204,7 @@ class AFBuild {
/// @brief The partition for the backward graph with flipped edges
const KDTreePartition<FlippedEdge<E, N, V>, FlippedNode<E, N, V>, V>* myFlippedPartition;
/// @brief The number of levels
const int myNumberOfLevels;
int myNumberOfLevels;
/// @brief The handler for routing errors
MsgHandler* const myErrorMsgHandler;
/// @brief The object's operation to perform on a backward graph with flipped edges
Expand Down Expand Up @@ -255,6 +260,11 @@ void AFBuild<E, N, V>::init(SUMOTime msTime, const V* const vehicle, std::vector
}
}
int sHARCLevel;
assert(myFlippedPartition != nullptr);
myNumberOfLevels = myFlippedPartition->getNumberOfLevels();
if (myNumberOfLevels < 0) {
throw std::runtime_error("The (actual) number of levels is uninitialized, call myFlippedPartition->init() before.");
}
for (sHARCLevel = 0; sHARCLevel < myNumberOfLevels - 1; sHARCLevel++) {
#ifdef AFBU_DEBUG_LEVEL_0
std::cout << "Starting computation of flags of level " << sHARCLevel << " (levels run from 0 to "
Expand Down Expand Up @@ -401,7 +411,7 @@ void AFBuild<E, N, V>::computeArcFlags(SUMOTime msTime, const int sHARCLevel, co
#endif
// initialization of arc flag vectors
initBoundaryEdges(boundaryEdges);
#ifdef AFBU_DEBUG_LEVEL_1
#ifdef AFBU_DEBUG_LEVEL_0
long long int startTime = SysUtils::getCurrentMillis();
#endif
std::map<const FlippedEdge<E, N, V>*, std::vector<const FlippedEdge<E, N, V>*>> incomingEdgesOfOutgoingBoundaryEdges;
Expand All @@ -412,7 +422,8 @@ void AFBuild<E, N, V>::computeArcFlags(SUMOTime msTime, const int sHARCLevel, co
int index = 0; // boundary node index
for (const FlippedNode<E, N, V>* boundaryNode : boundaryNodes) {
myNode2EdgeRouter->reset(vehicle);
if (myNode2EdgeRouter->computeNode2Edges(boundaryNode, boundaryEdges, vehicle, msTime)) {
// last param = true -> mute output of node2edge router
if (myNode2EdgeRouter->computeNode2Edges(boundaryNode, boundaryEdges, vehicle, msTime, true)) {
#ifdef AFBU_DEBUG_LEVEL_2
std::cout << "Node-to-edge router succeeded." << std::endl;
#endif
Expand Down
40 changes: 26 additions & 14 deletions src/utils/router/AFBuilder.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class AFBuilder {
typedef AbstractLookupTable<FlippedEdge<E, N, V>, V> FlippedLookupTable;

/** @brief Constructor
* @param[in] numberOfLevels The number of levels
* @param[in] maxNumberOfLevels The number of levels
* @param[in] edges The container with all edges of the network
* @param[in] unbuildIsWarning The flag indicating whether network unbuilds should issue warnings or errors
* @param[in] flippedOperation The operation for a backward graph with flipped edges
Expand All @@ -63,15 +63,17 @@ class AFBuilder {
* @param[in] haveRestrictions The flag indicating whether edges have restrictions which must be respected
* @param[in] toProhibit The list of explicitly prohibited edges
*/
AFBuilder(int numberOfLevels, const std::vector<E*>& edges, bool unbuildIsWarning,
AFBuilder(int maxNumberOfLevels, const std::vector<E*>& edges, bool unbuildIsWarning,
typename SUMOAbstractRouter<FlippedEdge<E, N, V>, V>::Operation flippedOperation,
const std::shared_ptr<const FlippedLookupTable> flippedLookup = nullptr,
const bool havePermissions = false, const bool haveRestrictions = false,
const std::vector<FlippedEdge<E, N, V>*>* toProhibit = nullptr) :
myEdges(edges),
myNumberOfLevels(numberOfLevels),
myNumberOfArcFlags(2 * (myNumberOfLevels - 1)),
#ifdef AFBL_DEBUG_LEVEL_0
myMaxNumberOfLevels(maxNumberOfLevels),
#ifdef AFBL_DEBUG_LEVEL_2
myNumberOfArcFlags(-1),
#endif
#ifdef AFBL_DEBUG_LEVEL_2
myArcFlagsFileName("arcflags.csv"),
#endif
myAmClean(true) {
Expand All @@ -91,12 +93,12 @@ class AFBuilder {
#ifdef AFBL_DEBUG_LEVEL_0
std::cout << "Flipped edges / nodes are ready." << std::endl;
#endif
myFlippedPartition = new KDTreePartition<FlippedEdge<E, N, V>, FlippedNode<E, N, V>, V>(myNumberOfLevels,
myFlippedPartition = new KDTreePartition<FlippedEdge<E, N, V>, FlippedNode<E, N, V>, V>(myMaxNumberOfLevels,
myFlippedEdges, havePermissions, haveRestrictions);
#ifdef AFBL_DEBUG_LEVEL_0
std::cout << "Instantiating arc flag build..." << std::endl;
#endif
myArcFlagBuild = new AFBuild<E, N, V>(myFlippedEdges, myFlippedPartition, numberOfLevels, unbuildIsWarning,
myArcFlagBuild = new AFBuild<E, N, V>(myFlippedEdges, myFlippedPartition, unbuildIsWarning,
flippedOperation, flippedLookup, havePermissions, haveRestrictions, toProhibit);

#ifdef AFBL_DEBUG_LEVEL_0
Expand Down Expand Up @@ -128,11 +130,14 @@ class AFBuilder {
* @return The partition level number
*/
int sHARCLevel2PartitionLevel(int sHARCLevel) {
if (myNumberOfLevels < 0) {
throw std::runtime_error("The (actual) number of levels is uninitialized, call AFBuilder::build() before.");
}
return AFRouter<E, N, V>::sHARCLevel2PartitionLevel(sHARCLevel, myNumberOfLevels);
}

protected:
#ifdef AFBL_DEBUG_LEVEL_0
#ifdef AFBL_DEBUG_LEVEL_2
/** @brief Loads already precomputed arc flags from a CSV file (for testing purposes)
* @param[in] fileName The name of the CSV file
*/
Expand Down Expand Up @@ -160,11 +165,13 @@ class AFBuilder {
std::vector<FlagInfo*> myFlagInfos;
/// @brief The arc flag build
AFBuild<E, N, V>* myArcFlagBuild;
/// @brief The number of levels of the k-d tree partition of the network
/// @brief The maximum number of levels of the k-d tree partition of the network
int myMaxNumberOfLevels;
/// @brief The (actual) number of levels of the initialized k-d tree partition of the network
int myNumberOfLevels;
#ifdef AFBL_DEBUG_LEVEL_2
/// @brief The number of arc flags per each edge
int myNumberOfArcFlags;
#ifdef AFBL_DEBUG_LEVEL_0
/// @brief The name of the arc flags file.
// @note This is a CSV file for convenience/testing purposes
const std::string myArcFlagsFileName;
Expand Down Expand Up @@ -201,12 +208,16 @@ std::vector<typename AFInfo<E>::FlagInfo*>& AFBuilder<E, N, V>::build(SUMOTime m
assert(myFlippedPartition);
if (myFlippedPartition->isClean()) {
myFlippedPartition->init(vehicle);
myNumberOfLevels = myFlippedPartition->getNumberOfLevels();
#ifdef AFBL_DEBUG_LEVEL_2
myNumberOfArcFlags = 2 * (myNumberOfLevels - 1);
#endif
myArcFlagBuild->setFlippedPartition(myFlippedPartition);
} else {
myFlippedPartition->reset(vehicle);
}
assert(myArcFlagBuild);
#ifdef AFBL_DEBUG_LEVEL_0
#ifdef AFBL_DEBUG_LEVEL_2
bool fileExists = this->fileExists(myArcFlagsFileName);
if (fileExists && myAmClean) {
std::cout << "Loading arc flags from file " << myArcFlagsFileName << std::endl;
Expand All @@ -215,13 +226,13 @@ std::vector<typename AFInfo<E>::FlagInfo*>& AFBuilder<E, N, V>::build(SUMOTime m
} else {
#endif
myArcFlagBuild->init(msTime, vehicle, myFlagInfos);
#ifdef AFBL_DEBUG_LEVEL_0
#ifdef AFBL_DEBUG_LEVEL_2
}
#endif
delete myFlippedPartition;
myFlippedPartition = nullptr;

#ifdef AFBL_DEBUG_LEVEL_0
#ifdef AFBL_DEBUG_LEVEL_2
if (!fileExists) {
std::cout << "Saving arc flags..." << std::endl;
// save flag vectors in a CSV file (one column, flag vectors in the order of edges)
Expand All @@ -233,9 +244,10 @@ std::vector<typename AFInfo<E>::FlagInfo*>& AFBuilder<E, N, V>::build(SUMOTime m
return myFlagInfos;
}

#ifdef AFBL_DEBUG_LEVEL_0
#ifdef AFBL_DEBUG_LEVEL_2
template<class E, class N, class V>
void AFBuilder<E, N, V>::saveFlagsToCsv(const std::string fileName) {
assert(myNumberOfArcFlags > 0);
std::ofstream csvFile(fileName);
for (FlagInfo* flagInfo : myFlagInfos) {
if ((flagInfo->arcFlags).empty()) {
Expand Down
Loading

0 comments on commit 82cd94a

Please sign in to comment.