BasicAgent

Struct BasicAgent 

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

A BasicAgent is an implementation of Agent.

Implementations§

Source§

impl BasicAgent

Source

pub fn new() -> Self

Create a new BasicAgent with a random Uuid

§Returns

The new BasicAgent with a Uuid generated using uuid::Uuid::new_v4.

§Examples
use belief_spread::BasicAgent;

let a = BasicAgent::new();
Source

pub fn new_with_uuid(u: Uuid) -> Self

Create a new BasicAgent with a specified Uuid

§Arguments
§Returns

The new BasicAgent with a specified Uuid.

§Examples
use belief_spread::BasicAgent;
use uuid::Uuid;

let u = Uuid::new_v4();
let a = BasicAgent::new_with_uuid(u);

Trait Implementations§

Source§

impl Agent for BasicAgent

Source§

fn get_activation(&self, time: SimTime, belief: &BeliefPtr) -> Option<f64>

Gets the activation of an Agent towards a BeliefPtr at a given SimTime.

This is always between -1 and +1.

§Arguments
§Returns

The activation, if found.

§Examples
use belief_spread::{BasicAgent, Agent, BasicBelief, BeliefPtr};

let mut a = BasicAgent::new();
let b = BasicBelief::new("b1".to_string());
let b_ptr: BeliefPtr = b.into();
a.set_activation(3, b_ptr.clone(), Some(0.1));
assert_eq!(a.get_activation(3, &b_ptr).unwrap(), 0.1);
Source§

fn get_activations(&self) -> &HashMap<SimTime, HashMap<BeliefPtr, f64>>

Gets the activations of an Agent towards all BeliefPtrs at all SimTimes.

This is always between -1 and +1.

BeliefPtrs are referenced by their Uuids.

§Return

A map from simulation time to a new map from BeliefPtr to the activation.

§Examples
use belief_spread::{BasicAgent, Agent, BasicBelief, BeliefPtr, UUIDd};

let mut a = BasicAgent::new();
let b = BasicBelief::new("b1".to_string());
let b_ptr: BeliefPtr = b.into();
a.set_activation(3, b_ptr.clone(), Some(0.1));
let activations = a.get_activations();
assert_eq!(activations.len(), 1);
assert_eq!(activations.get(&3).unwrap().len(), 1);
assert_eq!(*activations.get(&3).unwrap().get(&b_ptr).unwrap(), 0.1);
Source§

fn set_activation( &mut self, time: SimTime, belief: BeliefPtr, activation: Option<f64>, ) -> Result<(), OutOfRangeError>

Sets the activation of an Agent towards a BeliefPtr at a given SimTime.

If the activation is None, then the activation is deleted.

This is a value between -1 and +1.

§Arguments
  • time: The SimTime to update.
  • belief: The BeliefPtr to update.
  • activation: The new activation.
§Returns

A Result, Ok if nothing went wrong, or an Err with an OutOfRangeError, if the activation is not in the range [-1, +1].

§Examples
§Updating activation
use belief_spread::{BasicAgent, Agent, BasicBelief, BeliefPtr};

let mut a = BasicAgent::new();
let b = BasicBelief::new("b1".to_string());
let b_ptr: BeliefPtr = b.into();
a.set_activation(3, b_ptr.clone(), Some(0.1));
assert_eq!(a.get_activation(3, &b_ptr).unwrap(), 0.1);
a.set_activation(3, b_ptr.clone(), Some(-0.1));
assert_eq!(a.get_activation(3, &b_ptr).unwrap(), -0.1);
§Deleting activation
use belief_spread::{BasicAgent, Agent, BasicBelief, BeliefPtr};

let mut a = BasicAgent::new();
let b = BasicBelief::new("b1".to_string());
let b_ptr: BeliefPtr = b.into();
a.set_activation(3, b_ptr.clone(), Some(0.1));
assert_eq!(a.get_activation(3, &b_ptr).unwrap(), 0.1);
a.set_activation(3, b_ptr.clone(), None);
assert_eq!(a.get_activation(3, &b_ptr), None);
Source§

fn get_friends(&self) -> &HashMap<AgentPtr, f64>

Gets the friends of the Agent.

This gets the friends of the Agent (identified by their Uuid) with their weight of connection.

All weights are in the range [0, 1].

§Returns

The friends (identified by their Uuid) with their weight of connection.

§Examples
use belief_spread::{BasicAgent, Agent, AgentPtr, UUIDd};

let mut a1 = BasicAgent::new();
let a2 = BasicAgent::new();
let a2_ptr: AgentPtr = a2.into();
a1.set_friend_weight(a2_ptr.clone(), Some(0.1)).unwrap();
let friends = a1.get_friends();
assert_eq!(friends.len(), 1);
assert_eq!(*friends.get(&a2_ptr).unwrap(), 0.1);
Source§

fn get_friend_weight(&self, friend: &AgentPtr) -> Option<f64>

Gets the weight of a friend of the Agent.

The weight will be in the range [0, 1];

If they are not friends, returns None.

§Arguments
  • friend: The friend.
§Returns

The weight, or None if they are not friends.

§Examples
use belief_spread::{BasicAgent, Agent, UUIDd, AgentPtr};

let mut a1 = BasicAgent::new();
let a2 = BasicAgent::new();
let a2_ptr: AgentPtr = a2.into();
a1.set_friend_weight(a2_ptr.clone(), Some(0.1)).unwrap();
assert_eq!(a1.get_friend_weight(&a2_ptr).unwrap(), 0.1)
Source§

fn set_friend_weight( &mut self, friend: AgentPtr, weight: Option<f64>, ) -> Result<(), OutOfRangeError>

Set the weight of a friend of the Agent.

If they are not friends, this adds another Agent as a friend with a supplied weight.

weight must be in the range [0, 1].

If friend already exists, the weight is overwritten.

If the weight is None, the friend is removed if they were friends.

§Arguments
  • friend: The friend.
  • weight: The weight
§Returns

A Result, Ok if nothing went wrong, or an Err with an OutOfRangeError, if the weight is not in the range [0, +1].

§Examples
use belief_spread::{BasicAgent, Agent, UUIDd, AgentPtr};

let mut a1 = BasicAgent::new();
let a2 = BasicAgent::new();
let a2_ptr: AgentPtr = a2.into();
a1.set_friend_weight(a2_ptr.clone(), Some(0.1)).unwrap();
assert_eq!(a1.get_friend_weight(&a2_ptr).unwrap(), 0.1)
Source§

fn get_action(&self, time: SimTime) -> Option<&BehaviourPtr>

Gets the BehaviourPtr the Agent performed at a given SimTime.

Returns the BehaviourPtr.

§Arguments
§Returns

The BehaviourPtr, if one was performed at time.

§Examples
use belief_spread::{BasicAgent, Agent, BasicBehaviour, BehaviourPtr, UUIDd};

let mut a1 = BasicAgent::new();
let b = BasicBehaviour::new("b1".to_string());
let b_ptr: BehaviourPtr = b.into();

a1.set_action(2, Some(b_ptr.clone()));
assert_eq!(a1.get_action(2).unwrap(), &b_ptr);
Source§

fn get_actions(&self) -> &HashMap<SimTime, BehaviourPtr>

Gets all of the BehaviourPtrs that the Agent has performed.

§Returns

A HashMap from SimTime to BehaviourPtr.

§Examples
use belief_spread::{BasicAgent, Agent, BasicBehaviour, BehaviourPtr, UUIDd};

let mut a1 = BasicAgent::new();
let b = BasicBehaviour::new("b1".to_string());
let b_ptr: BehaviourPtr = b.into();

a1.set_action(2, Some(b_ptr.clone()));
let actions = a1.get_actions();
assert_eq!(actions.len(), 1);
assert_eq!(actions.get(&2).unwrap(), &b_ptr);
Source§

fn set_action(&mut self, time: SimTime, behaviour: Option<BehaviourPtr>)

Sets the BehaviourPtr the Agent performed at a given time.

If None, it unsets the BehaviourPtr.

§Arguments
§Examples
use belief_spread::{BasicAgent, Agent, BasicBehaviour, BehaviourPtr, UUIDd};

let mut a1 = BasicAgent::new();
let b = BasicBehaviour::new("b1".to_string());
let b_ptr: BehaviourPtr = b.into();

a1.set_action(2, Some(b_ptr.clone()));
assert_eq!(a1.get_action(2).unwrap(), &b_ptr);
Source§

fn get_delta(&self, belief: &BeliefPtr) -> Option<f64>

Gets the delta for a given BeliefPtr.

This is the value that the activation for the BeliefPtr changed by (multiplicatively) at every time step.

This is a strictly positive value (i.e., > 0).

§Arguments
§Returns

The delta for the BeliefPtr and this Agent, if found.

§Examples
use belief_spread::{BasicAgent, Agent, BasicBelief, BeliefPtr, UUIDd};

let mut a = BasicAgent::new();
let b = BasicBelief::new("b1".to_string());
let b_ptr: BeliefPtr = b.into();
a.set_delta(b_ptr.clone(), Some(0.1)).unwrap();
assert_eq!(a.get_delta(&b_ptr).unwrap(), 0.1);
Source§

fn get_deltas(&self) -> &HashMap<BeliefPtr, f64>

Gets all the deltas for the Agent.

This is the value that the activation for the BeliefPtr changed by (multiplicatively) at every time step.

This is a strictly positive value (i.e., > 0).

§Returns

A map from BeliefPtr to delta.

§Examples
use belief_spread::{BasicAgent, Agent, BasicBelief, BeliefPtr, UUIDd};

let mut a = BasicAgent::new();
let b = BasicBelief::new("b1".to_string());
let b_ptr: BeliefPtr = b.into();
a.set_delta(b_ptr.clone(), Some(0.1)).unwrap();
let deltas = a.get_deltas();
assert_eq!(deltas.len(), 1);
assert_eq!(*deltas.get(&b_ptr).unwrap(), 0.1);
Source§

fn set_delta( &mut self, belief: BeliefPtr, delta: Option<f64>, ) -> Result<(), OutOfRangeError>

Sets the delta for a given BeliefPtr.

This is the value that the activation for the BeliefPtr changed by (multiplicatively) at every time step.

This is a strictly positive value (i.e., > 0).

If delta is None, then this function removes the delta.

§Arguments
  • belief: The BeliefPtr.
  • delta: The new delta.
§Returns

A Result, Ok if nothing is wrong, or an Err with a OutOfRangeError, if the delta is not strictly positive.

§Examples
use belief_spread::{BasicAgent, Agent, BasicBelief, BeliefPtr, UUIDd};

let mut a = BasicAgent::new();
let b = BasicBelief::new("b1".to_string());
let b_ptr: BeliefPtr = b.into();
a.set_delta(b_ptr.clone(), Some(0.1)).unwrap();
assert_eq!(a.get_delta(&b_ptr).unwrap(), 0.1);
Source§

fn weighted_relationship( &self, t: SimTime, b1: &BeliefPtr, b2: &BeliefPtr, ) -> Option<f64>

Gets the weighted relationship between BeliefPtrs b1 and b2.

This is the compatibility for holding b2, given that the Agent already holds b1.

This is equal to the activation of b1 (Agent::get_activation), multiplied by the relationship between b1 and b2 (crate::Belief::get_relationship).

Returns None if either activation of b1 at time t is None, or the relationship between b1 and b2 is None.

§Arguments
§Returns

The weighted relationship.

§Examples
use belief_spread::{Agent, BasicAgent, BeliefPtr, BasicBelief};

let mut a = BasicAgent::new();
let b1 = BasicBelief::new("b1".to_string());
let b1_ptr: BeliefPtr = b1.into();
let b2 = BasicBelief::new("b2".to_string());
let b2_ptr: BeliefPtr = b2.into();
b1_ptr.borrow_mut().set_relationship(b2_ptr.clone(), Some(0.5));
a.set_activation(2, b1_ptr.clone(), Some(0.5));

assert_eq!(a.weighted_relationship(2, &b1_ptr, &b2_ptr).unwrap(), 0.25);
Source§

fn contextualise(&self, t: SimTime, b: &BeliefPtr, beliefs: &[BeliefPtr]) -> f64

Gets the context for holding the BeliefPtr b.

This is the compatibility for holding b, given all the beliefs the agent holds.

This is the average of Agent::weighted_relationship for every BeliefPtr (as given in `beliefs).

§Arguments
§Returns

The context.

§Examples
use belief_spread::{Agent, BasicAgent, BeliefPtr, BasicBelief, UUIDd};

let mut a = BasicAgent::new();
let b1 = BasicBelief::new("b1".to_string());
let b1_ptr: BeliefPtr = b1.into();
let b2 = BasicBelief::new("b2".to_string());
let b2_ptr: BeliefPtr = b2.into();

a.set_activation(2, b1_ptr.clone(), Some(1.0)).unwrap();
a.set_activation(2, b2_ptr.clone(), Some(1.0)).unwrap();

b1_ptr.borrow_mut().set_relationship(
    b1_ptr.clone(),
    Some(0.5),
)
.unwrap();
b1_ptr.borrow_mut().set_relationship(b2_ptr.clone(), Some(-0.75)).unwrap();

let mut beliefs: Vec<BeliefPtr> = Vec::new();
beliefs.push(b1_ptr.clone());
beliefs.push(b2_ptr.clone());

assert_eq!(
    a.contextualise(2, &b1_ptr, &beliefs),
    -0.125
);
Source§

fn pressure(&self, time: SimTime, belief: &BeliefPtr) -> f64

Gets the pressure the Agent feels to adopt a BeliefPtr given the actions of their friends.

This does not take into account the beliefs that the Agent already holds.

§Arguments
§Returns

The pressure

§Examples
use belief_spread::{BasicAgent, Agent, BasicBehaviour, BehaviourPtr, BasicBelief, BeliefPtr};
use std::collections::HashMap;

let mut agent = BasicAgent::new();
let mut f1 = BasicAgent::new();
let mut f2 = BasicAgent::new();
let b1 = BasicBehaviour::new("b1".to_string());
let b1_ptr: BehaviourPtr = b1.into();
let b2 = BasicBehaviour::new("b2".to_string());
let b2_ptr: BehaviourPtr = b2.into();

f1.set_action(2, Some(b1_ptr.clone()));
f2.set_action(2, Some(b2_ptr.clone()));

let mut belief = BasicBelief::new("b1".to_string());
let belief_ptr: BeliefPtr = belief.into();
belief_ptr.borrow_mut().set_perception(b1_ptr.clone(), Some(0.2)).unwrap();
belief_ptr.borrow_mut().set_perception(b2_ptr.clone(), Some(0.3)).unwrap();

agent.set_friend_weight(f1.into(), Some(0.5));
agent.set_friend_weight(f2.into(), Some(1.0));

assert_eq!(agent.pressure(2, &belief_ptr), 0.2);
Source§

fn activation_change( &self, time: SimTime, belief: &BeliefPtr, beliefs: &[BeliefPtr], ) -> f64

Gets the change in activation for the Agent as a result of the BehaviourPtrs observed.

This takes into account the beliefs that the agent already holds

§Arguments
§Return
  • The change in activation
§Examples
use belief_spread::{BasicAgent, Agent, BasicBehaviour, BehaviourPtr, BasicBelief, BeliefPtr};
use float_cmp::approx_eq;

let mut agent = BasicAgent::new();
let mut f1 = BasicAgent::new();
let mut f2 = BasicAgent::new();
let b1 = BasicBehaviour::new("b1".to_string());
let b1_ptr: BehaviourPtr = b1.into();
let b2 = BasicBehaviour::new("b2".to_string());
let b2_ptr: BehaviourPtr = b2.into();

f1.set_action(2, Some(b1_ptr.clone()));
f2.set_action(2, Some(b2_ptr.clone()));

let belief = BasicBelief::new("b1".to_string());
let belief_ptr: BeliefPtr = belief.into();
belief_ptr.borrow_mut().set_perception(b1_ptr.clone(), Some(0.2)).unwrap();
belief_ptr.borrow_mut().set_perception(b2_ptr.clone(), Some(0.3)).unwrap();

agent.set_friend_weight(f1.into(), Some(0.5));
agent.set_friend_weight(f2.into(), Some(1.0));
// Pressure is 0.2

let belief2 = BasicBelief::new("b2".to_string());
let belief2_ptr: BeliefPtr = belief2.into();
let mut beliefs = Vec::<BeliefPtr>::new();
beliefs.push(belief_ptr.clone());
beliefs.push(belief2_ptr.clone());

agent.set_activation(2, belief_ptr.clone(), Some(1.0)).unwrap();
agent.set_activation(2, belief2_ptr.clone(), Some(1.0)).unwrap();
belief_ptr.borrow_mut().set_relationship(belief_ptr.clone(), Some(0.5)).unwrap();
belief_ptr.borrow_mut().set_relationship(belief2_ptr.clone(), Some(-0.75)).unwrap();
// Contextualise is -0.125

assert!(approx_eq!(
    f64,
    agent.activation_change(2, &belief_ptr, &beliefs),
    0.0875,
    ulps = 2
))
Source§

impl From<BasicAgent> for AgentPtr

Source§

fn from(a: BasicAgent) -> Self

Convert from a BasicAgent into a AgentPtr.

This consumes the BasicAgent.

§Arguments
§Returns

The AgentPtr.

§Examples
use belief_spread::{BasicAgent, AgentPtr};

let a = BasicAgent::new();
let a_ptr = AgentPtr::from(a);
use belief_spread::{BasicAgent, AgentPtr};

let a = BasicAgent::new();
let a_ptr: AgentPtr = a.into();
Source§

impl UUIDd for BasicAgent

Source§

fn uuid(&self) -> &Uuid

Get the UUID of the BasicAgent.

Source§

fn set_uuid(&mut self, u: Uuid)

Set the UUID of the BasicAgent.

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> 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, 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