[][src]Struct net_ensembles::sw::SwEnsemble

pub struct SwEnsemble<T: Node, R: Rng> where
    T: Node,
    R: Rng
{ /* fields omitted */ }

Implements small-world graph ensemble

Sampling

Other

Minimal example

use net_ensembles::{SwEnsemble, EmptyNode};
use net_ensembles::traits::*; // I recommend always using this
use rand_pcg::Pcg64; //or whatever you want to use as rng
use net_ensembles::rand::SeedableRng; // I use this to seed my rng, but you can use whatever

let rng = Pcg64::seed_from_u64(12);

// now create small-world ensemble with 200 nodes
// and a rewiring probability of 0.3 for each edge
let sw_ensemble = SwEnsemble::<EmptyNode, Pcg64>::new(200, 0.3, rng);

Simple sampling example

use net_ensembles::{SwEnsemble, EmptyNode};
use net_ensembles::traits::*; // I recommend always using this
use rand_pcg::Pcg64; //or whatever you want to use as rng
use net_ensembles::rand::SeedableRng; // I use this to seed my rng, but you can use whatever
use std::fs::File;
use std::io::{BufWriter, Write};

let rng = Pcg64::seed_from_u64(122);

// now create small-world ensemble with 100 nodes
// and a rewiring probability of 0.3 for each edge
let mut sw_ensemble = SwEnsemble::<EmptyNode, Pcg64>::new(100, 0.3, rng);

// setup file for writing
let f = File::create("simple_sample_sw_example.dat")
    .expect("Unable to create file");
let mut f = BufWriter::new(f);
f.write_all(b"#diameter bi_connect_max average_degree\n")
    .unwrap();

// simple sample for 10 steps
sw_ensemble.simple_sample(10,
    |ensemble|
    {
        let diameter = ensemble.graph()
            .diameter()
            .unwrap();

        let bi_connect_max = ensemble.graph()
            .clone()
            .vertex_biconnected_components(false)[0];

        let average_degree = ensemble.graph()
            .average_degree();

        write!(f, "{} {} {}\n", diameter, bi_connect_max, average_degree)
            .unwrap();
    }
);

// or just collect this into a vector to print or do whatever
let vec = sw_ensemble.simple_sample_vec(10,
    |ensemble|
    {
        let diameter = ensemble.graph()
            .diameter()
            .unwrap();

        let transitivity = ensemble.graph()
            .transitivity();
        (diameter, transitivity)
    }
);
println!("{:?}", vec);

Save and load example

  • only works if feature "serde_support" is enabled
  • Note: "serde_support" is enabled by default
  • I need the #[cfg(feature = "serde_support")] to ensure the example does compile if you opt out of the default feature
  • you can do not have to use serde_json, look here for more info
use net_ensembles::traits::*; // I recommend always using this
use serde_json;
use rand_pcg::Pcg64;
use net_ensembles::{SwEnsemble, EmptyNode, rand::SeedableRng};
use std::fs::File;

let rng = Pcg64::seed_from_u64(95);
// create small-world ensemble
let sw_ensemble = SwEnsemble::<EmptyNode, Pcg64>::new(200, 0.3, rng);

#[cfg(feature = "serde_support")]
{
    // storing the ensemble in a file:

    let sw_file = File::create("store_SW.dat")
          .expect("Unable to create file");

    // or serde_json::to_writer(sw_file, &sw_ensemble);
    serde_json::to_writer_pretty(sw_file, &sw_ensemble);

    // loading ensemble from file:

    let mut read = File::open("store_SW.dat")
        .expect("Unable to open file");

    let sw: SwEnsemble::<EmptyNode, Pcg64> = serde_json::from_reader(read).unwrap();
}

Implementations

impl<T, R> SwEnsemble<T, R> where
    T: Node + SerdeStateConform,
    R: Rng
[src]

pub fn new(n: usize, r_prob: f64, rng: R) -> Self[src]

Initialize

  • create new SwEnsemble graph with n vertices
  • r_prob is probability of rewiring for each edge
  • rng is consumed and used as random number generator in the following
  • internally uses SwGraph<T>::new(n)

pub fn make_connected(&mut self)[src]

Experimental! Connect the connected components

  • resets edges, to connect the connected components
  • intended as starting point for a markov chain, if you require connected graphs
  • do not use this to independently (simple-) sample connected networks, as this will skew the statistics
  • This is still experimental, this member might change the internal functionallity resulting in different connected networks, without prior notice
  • This member might be removed in braking releases

pub fn draw_edge(&mut self) -> (usize, usize)[src]

  • draws random edge (i0, i1)
  • edge rooted at i0
  • uniform probability
  • result dependent on order of adjecency lists
  • mut because it uses the rng

pub fn sort_adj(&mut self)[src]

Sort adjecency lists

If you depend on the order of the adjecency lists, you can sort them

Performance

  • internally uses pattern-defeating quicksort as long as that is the standard
  • sorts an adjecency list with length d in worst-case: O(d log(d))
  • is called for each adjecency list, i.e., self.vertex_count() times

pub fn r_prob(&self) -> f64[src]

  • returns rewiring probability

pub fn set_r_prob(&mut self, r_prob: f64)[src]

  • set new value for rewiring probability

Note

  • will only set the value, which will be used from now on
  • if you also want to create a new sample, call randomize afterwards

pub fn contained_iter_neighbors_mut(
    &mut self,
    index: usize
) -> NContainedIterMut<T, SwContainer<T>>
[src]

  • retuns GenericGraph::contained_iter_neighbors_mut
  • otherwise you would not have access to this function, since no mut access to the graph is allowed

Trait Implementations

impl<T, R> AsRef<GenericGraph<T, SwContainer<T>>> for SwEnsemble<T, R> where
    T: Node,
    R: Rng
[src]

impl<T, R> Borrow<GenericGraph<T, SwContainer<T>>> for SwEnsemble<T, R> where
    T: Node,
    R: Rng
[src]

impl<T: Clone + Node, R: Clone + Rng> Clone for SwEnsemble<T, R> where
    T: Node,
    R: Rng
[src]

impl<T: Debug + Node, R: Debug + Rng> Debug for SwEnsemble<T, R> where
    T: Node,
    R: Rng
[src]

impl<'de, T: Node, R: Rng> Deserialize<'de> for SwEnsemble<T, R> where
    T: Node,
    R: Rng,
    T: Deserialize<'de>,
    R: Deserialize<'de>, 
[src]

impl<T, R> GraphIteratorsMut<T, GenericGraph<T, SwContainer<T>>, SwContainer<T>> for SwEnsemble<T, R> where
    T: Node + SerdeStateConform,
    R: Rng
[src]

impl<T, R> HasRng<R> for SwEnsemble<T, R> where
    T: Node + SerdeStateConform,
    R: Rng
[src]

fn rng(&mut self) -> &mut R[src]

Access RNG

If, for some reason, you want access to the internal random number generator: Here you go

fn swap_rng(&mut self, rng: R) -> R[src]

Swap random number generator

  • returns old internal rng

impl<T, R> MarkovChain<SwChangeState, SwChangeState> for SwEnsemble<T, R> where
    T: Node + SerdeStateConform,
    R: Rng
[src]

fn m_step(&mut self) -> SwChangeState[src]

Markov step

  • use this to perform a markov step
  • keep in mind, that it is not unlikely for a step to do Nothing as it works by drawing an edge and then reseting it with r_prob, else the edge is rewired
  • result SwChangeState can be used to undo the step with self.undo_step(result)
  • result should never be InvalidAdjecency or GError if used on a valid graph

fn undo_step(&mut self, step: SwChangeState) -> SwChangeState[src]

Undo a markov step

  • rewires edge to old state
  • Note: cannot undo InvalidAdjecency or GError, will just return InvalidAdjecency or GError respectively
  • returns result of rewire

Important:

Restored graph is the same as before the random step except the order of nodes in the adjacency list might be shuffled!

fn undo_step_quiet(&mut self, step: SwChangeState)[src]

Undo a Monte Carlo step

  • rewires edge to old state
  • panics if you try to undo InvalidAdjecency or GError
  • panics if rewire result (SwChangeState) is invalid (i.e. !result.is_valid())

Important:

Restored graph is the same as before the random step except the order of nodes in the adjacency list might be shuffled!

impl<T: Node, R: Rng> Serialize for SwEnsemble<T, R> where
    T: Node,
    R: Rng,
    T: Serialize,
    R: Serialize
[src]

impl<T, R> SimpleSample for SwEnsemble<T, R> where
    T: Node + SerdeStateConform,
    R: Rng
[src]

fn randomize(&mut self)[src]

Randomizes the edges according to small-world model

  • this is used by SwEnsemble::new to create the initial topology
  • you can use this for sampling the ensemble
  • runs in O(vertices)

impl<T, R> WithGraph<T, GenericGraph<T, SwContainer<T>>> for SwEnsemble<T, R> where
    T: Node + SerdeStateConform,
    R: Rng
[src]

Auto Trait Implementations

impl<T, R> RefUnwindSafe for SwEnsemble<T, R> where
    R: RefUnwindSafe,
    T: RefUnwindSafe

impl<T, R> Send for SwEnsemble<T, R> where
    R: Send,
    T: Send

impl<T, R> Sync for SwEnsemble<T, R> where
    R: Sync,
    T: Sync

impl<T, R> Unpin for SwEnsemble<T, R> where
    R: Unpin,
    T: Unpin

impl<T, R> UnwindSafe for SwEnsemble<T, R> where
    R: UnwindSafe,
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<S, Res, A> Metropolis<S, Res> for A where
    A: MarkovChain<S, Res>, 
[src]

impl<T> SerdeStateConform for T where
    T: Serialize
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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