Skip to main content

GraphSpace

Struct GraphSpace 

Source
pub struct GraphSpace { /* private fields */ }
Expand description

A graph-based discrete space mirroring Julia Agents.jl GraphSpace.

Each node can hold an arbitrary number of agents. Agents move between nodes along edges. Distance is measured in graph hops.

Supports both directed and undirected graphs. Edges and vertices can be added/removed dynamically at runtime.

§Example: building floor graph

  [Floor1-RoomA] --edge-- [Floor1-Corridor] --edge-- [Floor1-RoomB]
                               |
                             (stairs)
                               |
  [Floor2-RoomA] --edge-- [Floor2-Corridor] --edge-- [Floor2-RoomB]

Each room/corridor/stairwell is a node. Edges connect adjacent spaces.

Implementations§

Source§

impl GraphSpace

Source

pub fn new(n: usize) -> Self

Create an undirected graph with n nodes and no edges.

Source

pub fn new_directed(n: usize, directed: bool) -> Self

Create a graph with n nodes. If directed is true, edges are one-way.

Source

pub fn num_vertices(&self) -> usize

Number of nodes in the graph.

Source

pub fn num_edges(&self) -> usize

Number of edges in the graph.

For undirected graphs, each edge is counted once (not twice).

Source

pub fn is_directed(&self) -> bool

Whether the graph is directed.

Source

pub fn add_vertex(&mut self) -> GraphPos

Add a new vertex and return its index.

Source

pub fn rem_vertex(&mut self, n: GraphPos) -> bool

Remove a vertex by swapping with the last vertex (matches Graphs.jl behavior). All agents at the removed vertex must be removed beforehand. Returns true if successful.

Source

pub fn add_edge(&mut self, a: GraphPos, b: GraphPos) -> bool

Add an edge from a to b. For undirected graphs, also adds b to a.

Source

pub fn rem_edge(&mut self, a: GraphPos, b: GraphPos) -> bool

Remove an edge from a to b.

Source

pub fn neighbors_out(&self, n: GraphPos) -> &[GraphPos]

Outgoing neighbors of a node (slice reference, no allocation).

Source

pub fn neighbors_in(&self, n: GraphPos) -> &[GraphPos]

Incoming neighbors of a node (slice reference, no allocation).

Source

pub fn neighbors_all(&self, n: GraphPos) -> Vec<GraphPos>

All neighbors (union of incoming and outgoing), deduplicated.

Source

pub fn neighbors(&self, n: GraphPos, kind: NeighborType) -> Vec<GraphPos>

Get neighbors according to a NeighborType selector.

Source

pub fn ids_in_position(&self, n: GraphPos) -> &[AgentId]

Agent IDs stored at a given node.

Source

pub fn positions(&self) -> Range<usize>

All valid node indices.

Source

pub fn nearby_positions( &self, pos: GraphPos, r: usize, kind: NeighborType, ) -> Vec<GraphPos>

Find all node indices reachable within r hops via BFS, excluding the origin.

Source

pub fn nearby_agent_ids( &self, pos: GraphPos, r: usize, kind: NeighborType, ) -> Vec<AgentId>

Find all agent IDs within r hops, including agents at the origin node.

Trait Implementations§

Source§

impl Clone for GraphSpace

Source§

fn clone(&self) -> GraphSpace

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for GraphSpace

Source§

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

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

impl<A> SpaceInteraction<A> for GraphSpace
where A: PositionedAgent<Position = GraphPos>,

Source§

type Error = GraphSpaceError

Error type for space operations.
Source§

fn random_position<R: RngCore>(&self, rng: &mut R) -> A::Position

Generate a random valid position within this space.
Source§

fn add_agent(&mut self, agent: &A) -> Result<(), Self::Error>

Register an agent with the space at its current position.
Source§

fn remove_agent(&mut self, agent: &A) -> Result<(), Self::Error>

Deregister an agent from the space.
Source§

fn nearby_ids(&self, position: &A::Position, radius: usize) -> Vec<AgentId>

Return all agent IDs within radius of position. Read more
Source§

impl Space for GraphSpace

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> Message for T
where T: Clone + Send + Sync + 'static,