« blib/lib/SNA/Network/Generator/ConfigurationModel.pm
↑ Summary
blib/lib/SNA/Network/Algorithm/Connectivity.pm »
| 1 | package SNA::Network::Generator::MCMC; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 15 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 15 | our @EXPORT = qw(shuffle); | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 15 | ▶ | use List::MoreUtils qw(uniq none); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | SNA::Network::Generator::MCMC - Generate random networks by a series of edge swaps according to the Markov Chain Monte Carlo principle | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | use SNA::Network; | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | my $net = SNA::Network->new_from_pajek_net($filename); | ||||||||||||||||||||||||||||||||||||||||||
| 23 | $net->shuffle; | ||||||||||||||||||||||||||||||||||||||||||
| 24 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 25 | for (1.100) { | ||||||||||||||||||||||||||||||||||||||||||
| 26 | say $net->shuffle->identify_weakly_connected_components; | ||||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | The following methods are added to L<SNA::Network>. | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | =head2 shuffle | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | Generates a random networks by a series of edge swaps on an existing network according to the Markov Chain Monte Carlo principle. This means that the initial network will be changed and have a totally new, random edge structure. | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | This method will exactly preserve all in- and outdegrees, and is guaranteed to sample uniformly at random from all possible simple graph configurations. | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | You may optionally pass the number of steps to perform. By default, the currently best known number is used, namely the number of edges in the network multiplied by its logarithm. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | Returns the network reference again, in order to enable method chaining. | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | sub shuffle { | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | ▶ | my ($self, $steps) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
shuffle: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | $steps ||= int (int $self->edges * log int $self->edges); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$steps ||= int int $self->edges * log(int $self->edges)
MC/DC0%0/2
$steps ||= int int $self->edges * log(int $self->edges)
$steps
int int $self->edges * log(int $self->edges)
Truth table33%1/3
A ||= B
A= $stepsB= int int $self->edges * log(int $self->edges)
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | for (1 .. $steps) { | |||||||||||||||||||||||||||||||||||||||||
| 53 | 10538 | _swap_random_edges($self); | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | sub _swap_random_edges { | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 10538 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_swap_random_edges: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 10538 | my $index_one = int rand int $self->edges; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 10538 | my $index_two = int rand int $self->edges; | |||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 10538 | my $edge_one = $self->{edges}->[$index_one]; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 10538 | my $edge_two = $self->{edges}->[$index_two]; | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 10538 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
&uniq($edge_one->source, $edge_one->target, $edge_two->source, $edge_two->target) == 4 and &none(sub {
$_ == $edge_two->target;
}
, $edge_one->source->outgoing_nodes) and &none(sub {
$_ == $edge_one->target;
}
, $edge_two->source->outgoing_nodes)
&uniq($edge_one->source, $edge_one->target, $edge_two->source, $edge_two->target) == 4 and &none(sub {
$_ == $edge_two->target;
}
, $edge_one->source->outgoing_nodes)
MC/DC100%3/3
&uniq($edge_one->source, $edge_one->target, $edge_two->source, $edge_two->target) == 4 and &none(sub {
$_ == $edge_two->target;
}
, $edge_one->source->outgoing_nodes) and &none(sub {
$_ == $edge_one->target;
}
, $edge_two->source->outgoing_nodes)
&uniq($edge_one->source, $edge_one->target, $edge_two->source, $edge_two->target) == 4
&none(sub {
$_ == $edge_two->target;
}
, $edge_one->source->outgoing_nodes)
&none(sub {
$_ == $edge_one->target;
}
, $edge_two->source->outgoing_nodes)
Truth table100%4/4
A and B and C
A= &uniq($edge_one->source, $edge_one->target, $edge_two->source, $edge_two->target) == 4B= &none(sub {
$_ == $edge_two->target;
}
, $edge_one->source->outgoing_nodes)C= &none(sub {
$_ == $edge_one->target;
}
, $edge_two->source->outgoing_nodes)
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | uniq($edge_one->source, $edge_one->target, $edge_two->source, $edge_two->target) == 4 | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 188629 | ▶ | and none { $_ == $edge_two->target } $edge_one->source->outgoing_nodes | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 158627 | ▶ | and none { $_ == $edge_one->target } $edge_two->source->outgoing_nodes | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 74 | # swap target nodes | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 6930 | my $edge_one_target = $edge_one->target; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 6930 | $edge_one->{target} = $edge_two->target; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 6930 | $edge_two->{target} = $edge_one_target; | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | Darko Obradovic, C<< <dobradovic at gmx.de> >> | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | Please report any bugs or feature requests to C<bug-sna-network-node at rt.cpan.org>, or through | ||||||||||||||||||||||||||||||||||||||||||
| 89 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SNA-Network>. I will be notified, and then you'll | ||||||||||||||||||||||||||||||||||||||||||
| 90 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | perldoc SNA::Network | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=SNA-Network> | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | L<http://annocpan.org/dist/SNA-Network> | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | =item * CPAN Ratings | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | L<http://cpanratings.perl.org/d/SNA-Network> | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | =item * Search CPAN | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | L<http://search.cpan.org/dist/SNA-Network> | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | Copyright 2009 Darko Obradovic, all rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 133 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 15 | 1; # End of SNA::Network::Generator::MCMC | |||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||