pub struct Sampler { /* private fields */ }
Expand description
A sampler for regular bipartite graph.
This is the main struct of this crate.
The builder
method is used to build a sampler
and the sample_with
method is used
to sample a random Graph
.
A sampler is specified by 4 parameters: - variable’s degree: the same for all variables, - constraint’s degree: the same for all contraints, - number of variables, - number of contraints.
To make sure that the graphs are regular, the number of variables times their degree need to equal the number of contraints times their degree. If this is not satified, the builder will panic during construction.
Example
This can be used to generate a random graph with 5 variables of degree 3 and 3 constraints of degree 5.
use rand::thread_rng;
let sampler = Sampler::builder()
.number_of_variables(5)
.variable_degree(3)
.number_of_constraints(3)
.constraint_degree(5)
.build()
.unwrap();
let graph = sampler.sample_with(&mut thread_rng());
assert_eq!(graph.number_of_variables(), 5);
for variable in graph.variables() {
assert_eq!(variable.degree(), 3);
}
assert_eq!(graph.number_of_constraints(), 3);
for constraint in graph.constraints() {
assert_eq!(constraint.degree(), 5);
}
However, this will return an error since the parameters do not define a regular graph.
let sampler = Sampler::builder()
.number_of_variables(5)
.variable_degree(3)
.number_of_constraints(3)
.constraint_degree(3)
.build();
assert!(sampler.is_err());
Reproductibility
If you use the same random number generator, the graphs should be the same.
use rand::SeedableRng;
use rand::rngs::SmallRng; // require the small_rng feature
let sampler = Sampler::builder()
.number_of_variables(5)
.variable_degree(3)
.number_of_constraints(3)
.constraint_degree(5)
.build()
.unwrap();
// Create two rngs with the same seed.
let mut rng = SmallRng::seed_from_u64(123);
let mut other_rng = SmallRng::seed_from_u64(123);
let graph = sampler.sample_with(&mut rng);
let other_graph = sampler.sample_with(&mut other_rng);
assert_eq!(graph, other_graph);
Implementations
sourceimpl Sampler
impl Sampler
sourcepub fn sample_with<R: Rng>(&self, rng: &mut R) -> Graph
pub fn sample_with<R: Rng>(&self, rng: &mut R) -> Graph
Samples a random graph with the sampler parameters.
sourcepub fn number_of_variables(&self) -> usize
pub fn number_of_variables(&self) -> usize
Returns the number of variables in the graphs that will be generated by the sampler.
sourcepub fn number_of_constraints(&self) -> usize
pub fn number_of_constraints(&self) -> usize
Returns the number of constraints in the graphs that will be generated by the sampler.
sourcepub fn number_of_edges(&self) -> usize
pub fn number_of_edges(&self) -> usize
Returns the number of edges in the graphs that will be generated by the sampler.
sourcepub fn variable_degree(&self) -> usize
pub fn variable_degree(&self) -> usize
Returns the variable’s degree in the graphs that will be generated by the sampler.
sourcepub fn constraint_degree(&self) -> usize
pub fn constraint_degree(&self) -> usize
Returns the contraint’s degree in the graphs that will be generated by the sampler.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Sampler
impl Send for Sampler
impl Sync for Sampler
impl Unpin for Sampler
impl UnwindSafe for Sampler
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more