pops-core
0.9
PoPS (Pest or Pathogen Spread) Model Core C++ library
|
Network structure and algorithms. More...
#include <network_kernel.hpp>
Classes | |
class | ConstEitherWayIterator |
A const iterator which encapsulates either forward or reverse iterator. More... | |
class | SegmentView |
A const view of a Segment which can be iterated. More... | |
Public Types | |
using | NodeId = int |
Type for node IDs. More... | |
using | Statistics = std::map< std::string, int > |
Type for summary statistics. More... | |
Public Member Functions | |
Network (BBox< double > bbox, double ew_res, double ns_res, double speed) | |
Construct empty network. More... | |
std::pair< RasterIndex, RasterIndex > | xy_to_row_col (double x, double y) const |
Convert real world coordinates to row and column in the raster grid. More... | |
std::pair< RasterIndex, RasterIndex > | xy_to_row_col (const std::string &x, const std::string &y) const |
Coverts coordinates as xy_to_row_col(double, double) but converts from strings. More... | |
bool | out_of_bbox (double x, double y) const |
Test if coordinates XY are in the bounding box. More... | |
template<typename InputStream > | |
void | load (InputStream &node_stream, InputStream &segment_stream, bool allow_empty=false) |
Load nodes and segments from input streams. More... | |
const std::set< NodeId > & | get_nodes_at (RasterIndex row, RasterIndex col) const |
Get a list of nodes at a given cell. More... | |
template<typename Generator > | |
NodeId | get_random_node_at (RasterIndex row, RasterIndex col, Generator &generator) const |
Get a randomly selected node at a given cell. More... | |
bool | has_node_at (RasterIndex row, RasterIndex col) const |
Test if the network has a node at a given row and column. More... | |
std::pair< RasterIndex, RasterIndex > | get_node_row_col (NodeId node) const |
Get row and column for a node. More... | |
template<typename Generator > | |
std::tuple< int, int > | travel (RasterIndex row, RasterIndex col, double time, Generator &generator) const |
Travel in the network from given row and column for a given time. More... | |
std::vector< std::pair< NodeId, std::pair< RasterIndex, RasterIndex > > > | get_all_nodes () const |
Get all nodes as vector of all ids with their row and column. More... | |
Statistics | collect_statistics () const |
Collect statistics about the network. More... | |
template<typename OutputStream > | |
void | dump_yaml (OutputStream &stream) const |
Output network to a stream. More... | |
Static Public Member Functions | |
static Network | null_network () |
Create an empty network not meant for futher use. More... | |
Protected Types | |
using | NodeMatrix = std::map< NodeId, std::vector< NodeId > > |
Node connections (edges) More... | |
using | Segment = std::vector< std::pair< RasterIndex, RasterIndex > > |
Cells connecting two nodes (segment between nodes) More... | |
using | SegmentsByNodes = std::map< std::pair< NodeId, NodeId >, Segment > |
Segments by nodes (edges) More... | |
Protected Member Functions | |
template<typename InputStream > | |
void | load_nodes (InputStream &stream, std::set< NodeId > &node_ids) |
Read nodes from a stream. More... | |
template<typename InputStream > | |
void | load_segments (InputStream &stream, const std::set< NodeId > &node_ids) |
Read segments from a stream. More... | |
SegmentView | get_segment (NodeId start, NodeId end) const |
Get a segment between the two given nodes. More... | |
const std::vector< NodeId > & | nodes_connected_to (NodeId node) const |
Get nodes connected by an edge to a given node. More... | |
template<typename Generator > | |
NodeId | next_node (NodeId node, const std::set< NodeId > &ignore, Generator &generator) const |
Pick a next node from the given node. More... | |
Static Protected Member Functions | |
static NodeId | node_id_from_text (const std::string &text) |
Convert string to node ID. More... | |
template<typename Container , typename Generator > | |
static NodeId | pick_random_node (const Container &nodes, Generator &generator) |
Select a random node from a list of nodes. More... | |
Protected Attributes | |
BBox< double > | bbox_ |
Bounding box of the network grid in real world coordinates. More... | |
double | ew_res_ |
East-west resolution of the grid. More... | |
double | ns_res_ |
North-south resolution of the grid. More... | |
double | cell_travel_time_ |
Time to travel through one cell. More... | |
std::map< std::pair< RasterIndex, RasterIndex >, std::set< NodeId > > | nodes_by_row_col_ |
Node IDs stored by row and column (multiple nodes per cell) More... | |
NodeMatrix | node_matrix_ |
List of node neighbors by node ID (edges) More... | |
SegmentsByNodes | segments_by_nodes_ |
Lists of cells connecting nodes. More... | |
Network structure and algorithms.
The general workflow is contructing the object (with the constructor) and loading the data (with the load() function). Then the network is ready to be used for simulating trips over the network (with the travel() function).
When the travel() function is used from a kernel, user of the network directly calls only the setup functions.
The class exposes number of functions as public which are meant for testing or other special workflows.
RasterIndex template parameter is an integral type used for indexing in the grid, specifically used as a type for rows and columns.
Definition at line 51 of file network_kernel.hpp.
using pops::Network< RasterIndex >::NodeId = int |
Type for node IDs.
Definition at line 54 of file network_kernel.hpp.
|
protected |
Node connections (edges)
Definition at line 412 of file network_kernel.hpp.
|
protected |
Cells connecting two nodes (segment between nodes)
Definition at line 414 of file network_kernel.hpp.
|
protected |
Segments by nodes (edges)
Definition at line 416 of file network_kernel.hpp.
using pops::Network< RasterIndex >::Statistics = std::map<std::string, int> |
Type for summary statistics.
Definition at line 55 of file network_kernel.hpp.
|
inline |
Construct empty network.
bbox | Bounding box of the raster grid (in real world coordinates) |
ew_res | East-west resolution of the raster grid |
ns_res | North-south resolution of the raster grid |
speed | Speed of travel in the same units as coordinates |
Definition at line 65 of file network_kernel.hpp.
|
inline |
Collect statistics about the network.
Computes statistics such as number of nodes and segments.
Definition at line 305 of file network_kernel.hpp.
|
inline |
Output network to a stream.
Writes statistics and different views of the network as a YAML file.
Definition at line 345 of file network_kernel.hpp.
|
inline |
Get all nodes as vector of all ids with their row and column.
If there is more than one node at a given cell (row and column), each of these nodes is returned as a separate item in the list.
This translates the internal representation and returns a new object.
Definition at line 284 of file network_kernel.hpp.
|
inline |
Get row and column for a node.
node | Node id to get the coordinates for |
Definition at line 226 of file network_kernel.hpp.
|
inline |
Get a list of nodes at a given cell.
Returns a const reference to an internally stored set. If there are no nodes at a given cell, a reference to an empty set is returned.
row | Row in the raster grid |
col | Column in the raster grid |
Definition at line 166 of file network_kernel.hpp.
|
inline |
Get a randomly selected node at a given cell.
There can be more than one node at a given cell. This function selects one of these nodes randomly.
row | Row in the raster grid |
col | Column in the raster grid |
generator | Random number generator |
std::invalid_argument | if there is no node at a given cell |
Definition at line 190 of file network_kernel.hpp.
|
inlineprotected |
Get a segment between the two given nodes.
For a given pair of nodes, the function finds the connecting segment and returns a view of the segment oriented according to the order of the nodes.
start | Start node id |
end | End node id |
std::invalid_argument | if there is no segment connecting the nodes |
Definition at line 624 of file network_kernel.hpp.
|
inline |
Test if the network has a node at a given row and column.
row | Row |
col | Column |
Definition at line 211 of file network_kernel.hpp.
|
inline |
Load nodes and segments from input streams.
node_stream | Input stream with records for nodes |
segment_stream | Input stream with records for segments |
allow_empty | True if the loaded network can be empty (no nodes) |
The function can take any input stream which behaves like std::ifstream or std::istringstream. These are also the two expected streams to be used (for reading from a file and reading from a string, respectively).
A simple code for reading from files without any checking may look like this:
Definition at line 142 of file network_kernel.hpp.
|
inlineprotected |
Read nodes from a stream.
stream | Input stream with nodes and their coordinates | |
[out] | node_ids | Container to store node IDs in |
Definition at line 515 of file network_kernel.hpp.
|
inlineprotected |
Read segments from a stream.
stream | Input stream with segments |
node_ids | Container with node IDs to use |
Definition at line 551 of file network_kernel.hpp.
|
inlineprotected |
Pick a next node from the given node.
If there is more than one edge leading from the given node, a random node is picked. If there are no edges leading from the given node, the node is returned.
The random node is picked from candidate nodes which are nodes connected to a given node. The candidate nodes which are in the ignore list are excluded from the random selection. If all candiate nodes are in the ignore list, the ignore list is ignored and all candidate nodes are used.
The function always returns a node to go to even if it means going to an ignored node or returning the value of the node parameter.
Definition at line 664 of file network_kernel.hpp.
|
inlinestaticprotected |
Convert string to node ID.
text | String with node ID |
Definition at line 424 of file network_kernel.hpp.
|
inlineprotected |
Get nodes connected by an edge to a given node.
node | Node to get connections from |
Definition at line 643 of file network_kernel.hpp.
|
inlinestatic |
Create an empty network not meant for futher use.
This is useful when a network object is needed to contruct a kernel, but it will not be used in runtime.
Definition at line 80 of file network_kernel.hpp.
|
inline |
Test if coordinates XY are in the bounding box.
x | Real world coordinate X |
y | Real world coordinate Y |
Definition at line 114 of file network_kernel.hpp.
|
inlinestaticprotected |
Select a random node from a list of nodes.
Definition at line 697 of file network_kernel.hpp.
|
inline |
Travel in the network from given row and column for a given time.
All previously visited nodes are tracked and, if possible, excluded from further traveling.
Definition at line 245 of file network_kernel.hpp.
|
inline |
Coverts coordinates as xy_to_row_col(double, double) but converts from strings.
Definition at line 103 of file network_kernel.hpp.
|
inline |
Convert real world coordinates to row and column in the raster grid.
x | The X coordinate (east-west or column direction) |
y | The Y coordinate (north-south or row direction) |
Definition at line 92 of file network_kernel.hpp.
|
protected |
Bounding box of the network grid in real world coordinates.
Definition at line 707 of file network_kernel.hpp.
|
protected |
Time to travel through one cell.
Definition at line 710 of file network_kernel.hpp.
|
protected |
East-west resolution of the grid.
Definition at line 708 of file network_kernel.hpp.
|
protected |
List of node neighbors by node ID (edges)
Definition at line 713 of file network_kernel.hpp.
|
protected |
Node IDs stored by row and column (multiple nodes per cell)
Definition at line 712 of file network_kernel.hpp.
|
protected |
North-south resolution of the grid.
Definition at line 709 of file network_kernel.hpp.
|
protected |
Lists of cells connecting nodes.
Definition at line 714 of file network_kernel.hpp.