NeTrainSim 0.1.1 beta
The Open-Source Network Trains Simulator
 
Loading...
Searching...
No Matches
Network Class Reference

This class defined a network for trains. More...

#include <network.h>

Public Member Functions

 Network ()
 Default constructor.
 
 Network (const string &nodesFile, const string &linksFile, std::string netName="")
 Constructor for the Network class.
 
 Network (Vector< tuple< int, double, double, std::string, double, double > > nodesRecords, Vector< tuple< int, int, int, double, int, double, double, int, double, bool, std::string, std::string, double > > linksRecords, std::string netName="")
 Constructor for the Network class.
 
 Network (Vector< std::shared_ptr< NetNode > > theNodes, Vector< std::shared_ptr< NetLink > > theLinks, std::string netName="")
 Constructor for the Network class.
 
std::map< int, std::shared_ptr< NetNode > > defineNodes ()
 Define nodes.
 
Vector< int > getSimulatorTrainPath (Vector< int > userDefinedTrainPath)
 Gets simulator train path.
 
tuple< double, double, double, double, double > getNetworkStats ()
 Gets the network statistics.
 
std::shared_ptr< NetNodegetNodeByID (int &id)
 Gets node by identifier.
 
double getDistanceBetweenTwoNodes (std::shared_ptr< Train > train, std::shared_ptr< NetNode > node1, std::shared_ptr< NetNode > node2)
 Computes the distance between two nodes on a given train's path.
 
bool isConflictZone (std::shared_ptr< Train > train, std::shared_ptr< NetNode > node1, std::shared_ptr< NetNode > node2)
 Checks if there's a conflict zone between two nodes for the given train.
 
bool DistanceToEndOfAllLinkTrainsIsLarge (const std::shared_ptr< NetLink > link, const std::shared_ptr< Train > train)
 Checks if the distance to the end of all leading trains on a link is larger than a certain threshold (2 units in this case).
 
pair< double, double > getPositionbyTravelledDistance (std::shared_ptr< Train > train, double &travelledDistance)
 Computes the position of a train based on its travelled distance along a path.
 
std::pair< double, double > normalize (std::pair< double, double > vec)
 Normalize a 2D vector.
 
Vector< std::shared_ptr< NetSignal > > getSignalsByCurrentNodeList (const std::vector< std::shared_ptr< NetNode > > nodeList)
 Fetches the network signals associated with the list of given nodes.
 
std::shared_ptr< NetLinkgetLinkByStartandEndNodeID (const std::shared_ptr< Train > train, int startID, int endID, bool calcExact=true)
 Fetches the network link by start and end node identifiers.
 
Vector< std::shared_ptr< NetLink > > getLinksByStartandEndNode (std::shared_ptr< NetNode > startNode, std::shared_ptr< NetNode > endNode)
 Retrieves the network links between a specified start node and end node.
 
double getFullPathLength (std::shared_ptr< Train > train)
 Calculates the total length of the train's path.
 
std::shared_ptr< NetLinkgetLinkByStartNodeID (const std::shared_ptr< Train > train, int startNodeID)
 Retrieves a network link originating from a specified start node.
 
std::shared_ptr< NetLinkgetFirstTrainLink (const std::shared_ptr< Train > train)
 Retrieves the first link in a train's path.
 
Vector< double > generateCumLinksLengths (std::shared_ptr< Train > train)
 Generates a vector containing the cumulative lengths of the train's path.
 
std::shared_ptr< NetNodegetPreviousNodeByDistance (std::shared_ptr< Train > train, double travelledDistance, int &previousNodeID)
 Retrieves the previous node in the train's path given a travelled distance.
 
std::shared_ptr< NetLinkgetLinkFromDistance (std::shared_ptr< Train > train, double &travelledDistance, int &previousNodeID)
 Retrieves the link in the train's path that includes a specified travelled distance.
 
int getSimulatorNodeIDByUserID (int oldID)
 Translates a user-provided node identifier to a simulator-specific node identifier.
 
double getDistanceToSpecificNodeByTravelledDistance (std::shared_ptr< Train > train, double &travelledDistance, int &nodeID)
 Calculates the distance to a specific node given a travelled distance.
 
bool ccw (const std::pair< double, double > &A, const std::pair< double, double > &B, const std::pair< double, double > &C)
 Checks if three points make a counter-clockwise turn.
 
bool twoLinesIntersect (const std::pair< double, double > &A, const std::pair< double, double > &B, const std::pair< double, double > &C, const std::pair< double, double > &D)
 Checks if two lines intersect.
 
std::pair< Vector< std::shared_ptr< NetNode > >, double > shortestPathSearch (int startNodeID, int targetNodeID)
 Performs a shortest path search between two nodes.
 

Public Attributes

std::string networkName
 Holds the name of the network.
 
std::map< int, std::shared_ptr< NetNode > > nodes
 The nodes mapped by its simulator id.
 
Vector< std::shared_ptr< NetLink > > links
 The links.
 
Vector< std::shared_ptr< NetSignal > > networkSignals
 The signals.
 

Friends

ostream & operator<< (ostream &ostr, Network &stud)
 Stream insertion operator.
 

Detailed Description

This class defined a network for trains.

Author
Ahmed
Date
2/14/2023

Constructor & Destructor Documentation

◆ Network() [1/4]

Network::Network ( )
inline

Default constructor.

Initializes a new instance of the Network class.

Author
Ahmed Aredah
Date
2/14/2023

◆ Network() [2/4]

Network::Network ( const string &  nodesFile,
const string &  linksFile,
std::string  netName = "" 
)
inline

Constructor for the Network class.

Initializes a new instance of the Network class using node and link files.

Parameters
nodesFileA string representing the path to the nodes file.
linksFileA string representing the path to the links file.
netNameA string for the network name. If not provided, the network name will be generated from the linksFile name.
Author
Ahmed Aredah
Date
2/14/2023

◆ Network() [3/4]

Network::Network ( Vector< tuple< int, double, double, std::string, double, double > >  nodesRecords,
Vector< tuple< int, int, int, double, int, double, double, int, double, bool, std::string, std::string, double > >  linksRecords,
std::string  netName = "" 
)
inline

Constructor for the Network class.

Initializes a new instance of the Network class using pre-loaded nodes and links data.

Parameters
nodesRecordsA vector of tuples representing the nodes data.
linksRecordsA vector of tuples representing the links data.
netNameA string for the network name. If not provided, the network name will be "Unnamed Network".
Author
Ahmed Aredah
Date
2/14/2023

◆ Network() [4/4]

Network::Network ( Vector< std::shared_ptr< NetNode > >  theNodes,
Vector< std::shared_ptr< NetLink > >  theLinks,
std::string  netName = "" 
)
inline

Constructor for the Network class.

Initializes a new instance of the Network class using shared pointers to NetNode and NetLink.

Parameters
theNodesA vector of shared pointers to NetNode objects.
theLinksA vector of shared pointers to NetLink objects.
netNameA string for the network name. If not provided, the network name will be "Unnamed Network".
Author
Ahmed Aredah
Date
2/14/2023

Member Function Documentation

◆ ccw()

bool Network::ccw ( const std::pair< double, double > &  A,
const std::pair< double, double > &  B,
const std::pair< double, double > &  C 
)
inline

Checks if three points make a counter-clockwise turn.

Given three points A, B, C, it returns true if they are listed in counterclockwise order and false if they are listed in clockwise order or are colinear. The points are pairs of coordinates (x,y) in a 2D plane.

Parameters
AThe first point as a pair (x,y).
BThe second point as a pair (x,y).
CThe third point as a pair (x,y).
Returns
True if the points A, B, C are in counter-clockwise order, false otherwise.

◆ defineNodes()

std::map< int, std::shared_ptr< NetNode > > Network::defineNodes ( )
inline

Define nodes.

Author
Ahmed
Date
2/14/2023
Returns
A std::map<int,std::shared_ptr<NetNode>>

◆ DistanceToEndOfAllLinkTrainsIsLarge()

bool Network::DistanceToEndOfAllLinkTrainsIsLarge ( const std::shared_ptr< NetLink link,
const std::shared_ptr< Train train 
)
inline

Checks if the distance to the end of all leading trains on a link is larger than a certain threshold (2 units in this case).

Parameters
linkA shared_ptr to a NetLink object.
trainA shared_ptr to a Train object.
Returns
true if the distance to all trains is larger than 2 units, false otherwise.
Author
Ahmed
Date
2/14/2023

◆ generateCumLinksLengths()

Vector< double > Network::generateCumLinksLengths ( std::shared_ptr< Train train)
inline

Generates a vector containing the cumulative lengths of the train's path.

The function iterates over the train's path and for each node, calculates the cumulative distance from the start node to this node. The result is stored in a vector which is then returned.

Parameters
trainA shared pointer to the train object.
Returns
A vector of doubles containing the cumulative lengths of the links in the train's path.

◆ getDistanceBetweenTwoNodes()

double Network::getDistanceBetweenTwoNodes ( std::shared_ptr< Train train,
std::shared_ptr< NetNode node1,
std::shared_ptr< NetNode node2 
)
inline

Computes the distance between two nodes on a given train's path.

Parameters
trainA shared_ptr to a Train object representing the train for which to compute the distance.
node1A shared_ptr to the first NetNode object.
node2A shared_ptr to the second NetNode object.
Returns
A double representing the distance between node1 and node2 along the train's path.
Author
Ahmed
Date
2/14/2023

◆ getDistanceToSpecificNodeByTravelledDistance()

double Network::getDistanceToSpecificNodeByTravelledDistance ( std::shared_ptr< Train train,
double &  travelledDistance,
int &  nodeID 
)
inline

Calculates the distance to a specific node given a travelled distance.

This function retrieves the cumulative distance to the specified node and subtracts the travelled distance. The result is the remaining distance to the specified node.

Parameters
trainThe train object.
travelledDistanceThe distance that has been travelled.
nodeIDThe identifier of the node.
Returns
The distance to the specified node from the current location.

◆ getFirstTrainLink()

std::shared_ptr< NetLink > Network::getFirstTrainLink ( const std::shared_ptr< Train train)
inline

Retrieves the first link in a train's path.

This function retrieves the first node from the train's path and finds the link from that node to the next node in the path.

Parameters
trainThe train whose first link is to be retrieved.
Returns
A shared pointer to the first NetLink object in the train's path.
Exceptions
std::runtime_errorIf the train's path is empty.

◆ getFullPathLength()

double Network::getFullPathLength ( std::shared_ptr< Train train)
inline

Calculates the total length of the train's path.

This function iterates over the train's path, adding the length of each link between nodes to compute the total path length.

Parameters
trainThe train whose path length will be calculated.
Returns
The total length of the train's path.
Exceptions
std::out_of_rangeIf trainPath contains a node not connected to the previous node.

◆ getLinkByStartandEndNodeID()

std::shared_ptr< NetLink > Network::getLinkByStartandEndNodeID ( const std::shared_ptr< Train train,
int  startID,
int  endID,
bool  calcExact = true 
)
inline

Fetches the network link by start and end node identifiers.

Checks all the links between the two nodes and returns the one that has the train, or the link with the least cost if calcExact is false. If calcExact is true, it will try to find a link where the train can travel without colliding with other trains.

Author
Ahmed
Date
2/14/2023
Parameters
trainA shared pointer to the Train object for which the link is to be found.
startIDThe start node identifier.
endIDThe end node identifier.
calcExact(Optional) Flag to calculate exact link, default is true.
Returns
A shared pointer to the NetLink object representing the link between startID and endID.

◆ getLinkByStartNodeID()

std::shared_ptr< NetLink > Network::getLinkByStartNodeID ( const std::shared_ptr< Train train,
int  startNodeID 
)
inline

Retrieves a network link originating from a specified start node.

This function finds the next node in the train's path after the specified start node, then retrieves the link from the start node to the next node.

Parameters
trainThe train that is being processed.
startNodeIDThe identifier of the start node.
Returns
A shared pointer to the NetLink object from the start node to the next node.
Exceptions
std::out_of_rangeIf startNodeID is not in trainPath or it is the last node in the path.

◆ getLinkFromDistance()

std::shared_ptr< NetLink > Network::getLinkFromDistance ( std::shared_ptr< Train train,
double &  travelledDistance,
int &  previousNodeID 
)
inline

Retrieves the link in the train's path that includes a specified travelled distance.

This function iterates over the train's path until it finds the node whose cumulative distance is greater than the travelled distance. The function then retrieves the link from the previous node to this one.

Parameters
trainThe train object.
travelledDistanceThe distance that has been travelled.
previousNodeIDAn input/output parameter that will store the ID of the previous node.
Returns
A shared pointer to the NetLink object that includes the travelled distance.

◆ getLinksByStartandEndNode()

Vector< std::shared_ptr< NetLink > > Network::getLinksByStartandEndNode ( std::shared_ptr< NetNode startNode,
std::shared_ptr< NetNode endNode 
)
inline

Retrieves the network links between a specified start node and end node.

This function will search the linked nodes of the start node and return all links that point to the provided end node.

Parameters
startNodeThe starting node of the links.
endNodeThe end node of the links.
Returns
A vector of shared pointers to the NetLink objects.
Exceptions
std::out_of_rangeIf the endNode is not a neighbor of the startNode.

◆ getNetworkStats()

tuple< double, double, double, double, double > Network::getNetworkStats ( )
inline

Gets the network statistics.

Returns
A tuple with the percentage of catenary links, the cumulative consumed energy of the catenary, the cumulative regenerated energy of the catenary, the total length of the links and the total length of links with catenary.
Author
Ahmed Aredah
Date
2/14/2023

◆ getNodeByID()

std::shared_ptr< NetNode > Network::getNodeByID ( int &  id)
inline

Gets node by identifier.

Author
Ahmed
Date
2/14/2023
Exceptions
std::runtime_errorRaised when a runtime error condition occurs.
Parameters
[in,out]idThe identifier.
Returns
The node by identifier.

◆ getPositionbyTravelledDistance()

pair< double, double > Network::getPositionbyTravelledDistance ( std::shared_ptr< Train train,
double &  travelledDistance 
)
inline

Computes the position of a train based on its travelled distance along a path.

This function calculates the position of a train on its path given the travelled distance. The path is represented as a sequence of links, each with its own length. The function iterates over these links to find the one on which the train is currently located, and then calculates the train's position on this link.

If the travelled distance is 0 or less, the function returns the coordinates of the first node on the path. If the travelled distance is equal to or greater than the total path length, it returns the coordinates of the last node. Otherwise, the function determines on which link the train is located and calculates the precise position.

Parameters
trainA shared_ptr to the Train object for which to compute the position. This object contains the train's path, the total path length, and the cumulative lengths of the individual links.
travelledDistanceThe distance the train has travelled along its path. Must be non-negative and not exceed the total path length.
Returns
A std::pair<double, double> representing the train's position. The first element is the x-coordinate, and the second element is the y-coordinate. If the function is unable to determine the position (for example, if the travelled distance exceeds the total path length), it returns the coordinates (0.0, 0.0).
Author
Ahmed Aredah
Date
2/14/2023

◆ getPreviousNodeByDistance()

std::shared_ptr< NetNode > Network::getPreviousNodeByDistance ( std::shared_ptr< Train train,
double  travelledDistance,
int &  previousNodeID 
)
inline

Retrieves the previous node in the train's path given a travelled distance.

This function iterates over the train's path until it finds the node whose cumulative distance is greater than the travelled distance. The function then retrieves the node prior to this one.

Parameters
trainThe train object.
travelledDistanceThe distance that has been travelled.
previousNodeIDAn input/output parameter that will store the ID of the previous node.
Returns
A shared pointer to the previous NetNode object in the train's path.

◆ getSignalsByCurrentNodeList()

Vector< std::shared_ptr< NetSignal > > Network::getSignalsByCurrentNodeList ( const std::vector< std::shared_ptr< NetNode > >  nodeList)
inline

Fetches the network signals associated with the list of given nodes.

Iterates over each node and checks the node's signals. If a signal's current node matches the current iterating node, the signal is added to a list that is returned after the completion of the function.

Author
Ahmed
Date
2/14/2023
Parameters
nodeListReference to a vector of shared pointers to NetNode objects.
Returns
Vector of shared pointers to NetSignal objects associated with the nodes in the nodeList.

◆ getSimulatorNodeIDByUserID()

int Network::getSimulatorNodeIDByUserID ( int  oldID)
inline

Translates a user-provided node identifier to a simulator-specific node identifier.

This function iterates over the network nodes until it finds a node with a user ID that matches the provided oldID. The function then returns the simulator-specific ID of this node.

Parameters
oldIDThe user-provided identifier.
Returns
The simulator-specific identifier for the node.
Exceptions
std::runtime_errorIf a node with the provided user ID cannot be found.

◆ getSimulatorTrainPath()

Vector< int > Network::getSimulatorTrainPath ( Vector< int >  userDefinedTrainPath)
inline

Gets simulator train path.

Author
Ahmed
Date
2/14/2023
Parameters
userDefinedTrainPathFull path list of the user defined train file.
Returns
The simulator train path.

◆ isConflictZone()

bool Network::isConflictZone ( std::shared_ptr< Train train,
std::shared_ptr< NetNode node1,
std::shared_ptr< NetNode node2 
)
inline

Checks if there's a conflict zone between two nodes for the given train.

A conflict zone exists when there's only one link between nodes.

Parameters
trainA shared_ptr to a Train object.
node1A shared_ptr to the first NetNode object.
node2A shared_ptr to the second NetNode object.
Returns
true if a conflict zone exists, false otherwise.
Author
Ahmed
Date
2/14/2023

◆ normalize()

std::pair< double, double > Network::normalize ( std::pair< double, double >  vec)
inline

Normalize a 2D vector.

This function takes a 2D vector represented as a std::pair of doubles and normalizes it. Normalizing a vector adjusts its length to be 1, but keeps its direction the same. If the input vector has length 0, the function will return the same vector.

Parameters
vecThe vector to be normalized, represented as a std::pair of doubles. The first element of the pair represents the x component of the vector, and the second element represents the y component.
Returns
A std::pair representing the normalized vector. If the input vector had length 0, the same vector is returned.
Author
Ahmed
Date
6/8/2023

◆ shortestPathSearch()

std::pair< Vector< std::shared_ptr< NetNode > >, double > Network::shortestPathSearch ( int  startNodeID,
int  targetNodeID 
)
inline

Performs a shortest path search between two nodes.

This function implements a Dijkstra-like shortest path algorithm. It takes the start node and target node IDs, then calculates the shortest path between them by traversing the connected nodes and minimizing the overall path length. It returns a pair, where the first element is a vector of nodes representing the shortest path, and the second element is the total length of this path.

Parameters
startNodeIDThe identifier for the start node.
targetNodeIDThe identifier for the target node.
Returns
A pair consisting of a vector of node pointers forming the shortest path (in order from start to target), and a double representing the total length of the path.

◆ twoLinesIntersect()

bool Network::twoLinesIntersect ( const std::pair< double, double > &  A,
const std::pair< double, double > &  B,
const std::pair< double, double > &  C,
const std::pair< double, double > &  D 
)
inline

Checks if two lines intersect.

This function takes four points A, B, C, D representing two lines (AB and CD). It determines whether these lines intersect by checking if the two points of one line straddle the other line.

Parameters
AThe first point of the first line as a pair (x,y).
BThe second point of the first line as a pair (x,y).
CThe first point of the second line as a pair (x,y).
DThe second point of the second line as a pair (x,y).
Returns
True if the lines AB and CD intersect, false otherwise.

Friends And Related Symbol Documentation

◆ operator<<

ostream & operator<< ( ostream &  ostr,
Network stud 
)
friend

Stream insertion operator.

Author
Ahmed
Date
2/14/2023
Parameters
[in,out]ostrThe ostr.
[in,out]studThe stud.
Returns
The shifted result.

Member Data Documentation

◆ links

Vector<std::shared_ptr<NetLink> > Network::links

The links.

◆ networkName

std::string Network::networkName

Holds the name of the network.

◆ networkSignals

Vector<std::shared_ptr<NetSignal> > Network::networkSignals

The signals.

◆ nodes

std::map<int, std::shared_ptr<NetNode> > Network::nodes

The nodes mapped by its simulator id.