AI-Termites
view release on metacpan or search on metacpan
--- #YAML:1.0
name: AI-Termites
version: 0.02
abstract: Artificial termites simulation
author:
- Salvador Fandino <sfandino@yahoo.com>
license: unknown
distribution_type: module
configure_requires:
ExtUtils::MakeMaker: 0
build_requires:
ExtUtils::MakeMaker: 0
requires:
Math::nSphere: 0.01
Math::Vector::Real: 0.06
Math::Vector::Real::kdTree: 0.03
Math::Vector::Real::MultiNormalMixture: 0.01
Math::Vector::Real::Random: 0.02
no_index:
directory:
- t
- inc
generated_by: ExtUtils::MakeMaker version 6.56
To install this module type the following:
perl Makefile.PL
make
make test
make install
DEPENDENCIES
This module requires these other modules and libraries:
blah blah blah
COPYRIGHT AND LICENCE
Put the correct copyright and licence information here.
Copyright (C) 2011 by Salvador Fandino
This library is free software; you can redistribute it and/or modify
lib/AI/Termites.pm view on Meta::CPAN
my $termites = AI::Termites::VicinusOcurro->new(dim => 2,
n_wood => 1000,
n_termites => 100);
$termites->iterate for 0..10000;
=head1 DESCRIPTION
This module simulates a termites world based on the ideas described on
the book "Adventures in Modeling" by Vanessa Stevens Colella, Eric
Klopfer and Mitchel Resnick
(L<http://education.mit.edu/starlogo/adventures/>).
In this version, termites can move in a n-dimensional boxed space, and
are not limited to integer coordinates.
Also, the way they decide when to pick or leave wood are customizable,
allowing to investigate how changing the rules affects the emergent
behaviors.
The module implements several termite subspecies (subclasses):
=over 4
=item LoginquitasPostulo
This termites subspecie measures the distance to the nearest piece of
wood.
=item NemusNidor
This termite smells the wood.
=item PeractioBaro
This termite is pretty simple and all that can see is if there is or
not some piece of wood in the vecinity.
lib/AI/Termites/LoginquitasPostulo.pm view on Meta::CPAN
my $self = shift;
my @ixs = grep !$self->{wood}[$_]{taken}, 0..$#{$self->{wood}};
$self->{kdtree_ixs} = \@ixs;
$self->{kdtree} = Math::Vector::Real::kdTree->new(map $_->{pos}, @{$self->{wood}}[@ixs]);
}
sub termite_take_wood_p {
my ($self, $termite) = @_;
my $pos = $termite->{pos};
my $near = $self->{near};
my $wood_ix = $self->{kdtree}->find_nearest_neighbor($pos, $near);
if (defined $wood_ix) {
my ($next_ix, $d) = $self->{kdtree}->find_nearest_neighbor($pos, $near, $wood_ix);
if (not defined $next_ix or rand($near) < $d) {
return $self->{kdtree_ixs}[$wood_ix];
}
}
undef
}
sub termite_leave_wood_p {
my ($self, $termite) = @_;
my $pos = $termite->{pos};
my $near = $self->{near};
my ($wood_ix, $d) = $self->{kdtree}->find_nearest_neighbor($pos, $near);
if (defined $wood_ix and rand($near) > $d) {
return 1;
}
undef;
}
1;
lib/AI/Termites/NemusNidor.pm view on Meta::CPAN
my $mnm = Math::Vector::Real::MultiNormalMixture->new(mu => [map $_->{pos}, @{$self->{wood}}[@ixs]],
sigma => $sigma);
$self->{mnm} = $mnm;
$self->{mnm_max} = $mnm->max_density_estimation;
}
sub termite_take_wood_p {
my ($self, $termite) = @_;
my $pos = $termite->{pos};
my $near = $self->{near};
my $wood_ix = $self->{kdtree}->find_nearest_neighbor($pos, $near);
if (defined $wood_ix) {
my @near = $self->{kdtree}->find_in_ball($pos, $near * 3, $wood_ix);
my $density = $self->{mnm}->density_portion($pos, @near);
my $max = $self->{mnm_max};
$self->{mnm_max} = $max = $density if $density > $max;
# printf "take -> max: %6g, density: %6g. ratio: %02.7f\n", $max, $density, $density/$max * 100;
return $self->{kdtree_ixs}[$wood_ix] if $density < rand($max);
}
undef
}
lib/AI/Termites/PeractioBaro.pm view on Meta::CPAN
my @ixs = grep !$self->{wood}[$_]{taken}, 0..$#{$self->{wood}};
# say '@ixs: ', scalar @ixs;
$self->{kdtree_ixs} = \@ixs;
$self->{kdtree} = Math::Vector::Real::kdTree->new(map $_->{pos}, @{$self->{wood}}[@ixs]);
}
sub termite_take_wood_p {
my ($self, $termite) = @_;
my $pos = $termite->{pos};
my $near = $self->{near};
my $wood_ix = $self->{kdtree}->find_nearest_neighbor($pos, $near);
if (defined $wood_ix) {
# say "one near $pos, $near";
my $second = $self->{kdtree}->find_nearest_neighbor($pos, $near, $wood_ix);
return $self->{kdtree_ixs}[$wood_ix] unless defined $second;
# say "two near $wood_ix - $second";
}
undef
}
sub termite_leave_wood_p {
my ($self, $termite) = @_;
my $pos = $termite->{pos};
my $near = $self->{near};
return defined $self->{kdtree}->find_nearest_neighbor($pos, $near);
undef;
}
1;
lib/AI/Termites/VicinusOccurro.pm view on Meta::CPAN
$self->{alpha} = $nlog2/(nsphere_volumen($self->{dim} - 1, $self->{near}) * $self->{wood_density});
}
sub termite_take_wood_p {
my ($self, $termite) = @_;
my $pos = $termite->{pos};
my $near = $self->{near};
my $wood_ix = $self->{kdtree}->find_nearest_neighbor($pos, $near);
if (defined $wood_ix) {
my $count = $self->{kdtree}->find_in_ball($pos, $near, $wood_ix);
if (exp($self->{alpha} * $count) > rand) {
return $self->{kdtree_ixs}[$wood_ix];
}
}
undef
}
sub termite_leave_wood_p {
samples/termites.pl view on Meta::CPAN
my $wood = 200;
my $near;
my $one_of = 5;
my $width = 1024;
my $dim = 2;
my $taken = 0;
my $output = "output";
my $truecolor = 0;
my $top = 0;
my $result = GetOptions( "world-size=s" => \$world,
"specie=s" => \$specie,
"termites=i" => \$termites,
"wood=i" => \$wood,
"near=s" => \$near,
"one-of=i" => \$one_of,
"width=i" => \$width,
"dim=i" => \$dim,
"taken" => \$taken,
"output=s" => \$output,
"truecolor" => \$truecolor,
samples/termites.pl view on Meta::CPAN
=item --near SIZE
The size of the boll that a termite will consider as its
neighborhood. Defaults to 1/50 of the world size.
Every specie uses this parameter in a different way.
=item --taken
Represent on the drawings the pieces of wood that are actually being
moved by some termite.
=item --output FILENAME
Prefix used for the file names of the generated PNGs. Defaults to
C<output>.
=item --one-of N
Save to file one of every N frames. Defaults to 5.
samples/termites.pl view on Meta::CPAN
=head1 MAKING MOVIES
In order to convert a set of PNGs into an animation, ffmpeg can be
used as follows:
ffmpeg -i output-%05d.png video.mpg
=head1 SEE ALSO
The idea about artificial termites comes from the book "Adventures in
Modeling" by Vanessa Stevens Colella, Eric Klopfer and Mitchel Resnick
(L<http://education.mit.edu/starlogo/adventures/>).
An online Artificial Termites simulation can be found here:
L<http://www.permutationcity.co.uk/alife/termites.html>.
The origin of this module lies on the following PerlMonks post:
L<http://perlmonks.org/?node_id=908684>.
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2011 by Salvador FandiE<ntilde>o,
( run in 0.633 second using v1.01-cache-2.11-cpan-49f99fa48dc )