pub struct RoadGraph { /* private fields */ }Expand description
A road network with [distance, time] arc costs, loaded from DIMACS
files.
States are the DIMACS node ids, 1..=node_count(). Arcs are stored in
compressed sparse rows in both directions, so Graph::successors and
ReverseGraph::predecessors are a slice copy each.
§Examples
use multiobjective_search::{RoadGraph, boa_star, ZeroHeuristic};
let distance = "c tiny\np sp 3 3\na 1 2 10\na 2 3 10\na 1 3 25\n";
let time = "p sp 3 3\na 1 2 5\na 2 3 5\na 1 3 6\n";
let graph = RoadGraph::from_dimacs(distance, time)?;
assert_eq!(graph.node_count(), 3);
let out = boa_star(&graph, ZeroHeuristic, 1, |s| *s == 3);
let costs: Vec<_> = out.solutions.iter().map(|s| s.cost).collect();
assert_eq!(costs, [[20, 10], [25, 6]]);Implementations§
Source§impl RoadGraph
impl RoadGraph
Sourcepub fn from_dimacs(distance: &str, time: &str) -> Result<Self, DimacsError>
pub fn from_dimacs(distance: &str, time: &str) -> Result<Self, DimacsError>
Parses a distance .gr file and a time .gr file with the same
arcs in the same order.
§Errors
Returns a DimacsError for a malformed or inconsistent file:
a missing or second problem line, a malformed line, a node id out of
range, an arc count that differs from the problem line, or two files
that differ in node count (DimacsErrorKind::NodeCount, line 0),
in arc count (DimacsErrorKind::ArcCount, line 0) or in an arc’s
endpoints (DimacsErrorKind::ArcMismatch, at the time
file’s line).
Sourcepub fn from_arcs(nodes: u32, arcs: &[(u32, u32, [u32; 2])]) -> Self
pub fn from_arcs(nodes: u32, arcs: &[(u32, u32, [u32; 2])]) -> Self
Builds a graph over nodes 1..=nodes from (from, to, [distance, time]) arcs.
§Panics
Panics if an arc endpoint is outside 1..=nodes.
Sourcepub fn with_coordinates(self, co: &str) -> Result<Self, DimacsError>
pub fn with_coordinates(self, co: &str) -> Result<Self, DimacsError>
Adds the node coordinates from a .co file.
§Errors
Returns a DimacsError for a missing or second problem line, a
malformed line, a node count that differs from the graph’s, a node id
out of range or listed twice.
Sourcepub fn node_count(&self) -> u32
pub fn node_count(&self) -> u32
Number of nodes; the node ids are 1..=node_count().