Skip to main content

RoadGraph

Struct RoadGraph 

Source
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

Source

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).

Source

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.

Source

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.

Source

pub fn node_count(&self) -> u32

Number of nodes; the node ids are 1..=node_count().

Source

pub fn arc_count(&self) -> usize

Number of arcs.

Source

pub fn coordinates(&self, node: u32) -> Option<[i32; 2]>

The coordinates of node as [x, y], or None if no coordinates were loaded for it.

Trait Implementations§

Source§

impl Clone for RoadGraph

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RoadGraph

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for RoadGraph

Source§

impl Graph for RoadGraph

Source§

type State = u32

A search state. Ord keys the per-state bookkeeping (a BTreeMap, since the crate is no_std).
Source§

type Weight = [u32; 2]

The cost vector of one edge, for example [u32; 2].
Source§

fn successors(&self, state: &u32, out: &mut Vec<(u32, [u32; 2])>)

Appends every outgoing edge of state to out as (successor, weight). Read more
Source§

impl PartialEq for RoadGraph

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl ReverseGraph for RoadGraph

Source§

fn predecessors(&self, state: &u32, out: &mut Vec<(u32, [u32; 2])>)

Appends every incoming edge of state to out as (predecessor, weight), where weight is the cost of the edge from predecessor to state. Read more
Source§

impl StructuralPartialEq for RoadGraph

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.