Termites/NemusNidor.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 78 CC 6 SCAR 21.7
CC
6
Cov
0%
termite_take_wood_p
20.0
termite_leave_wood_p
12.0
before_termites_action
2.0
CRAP 8.8
1 package AI::Termites::NemusNidor;
2  
3 use 5.010;
4 use strict;
5 use warnings;
6  
7 use Math::Vector::Real;
8 use Math::Vector::Real::kdTree;
9 use Math::Vector::Real::MultiNormalMixture;
10  
11 use parent 'AI::Termites';
12  
13 sub before_termites_action {
14 my $self = shift;
15 my @ixs = grep !$self->{wood}[$_]{taken}, 0..$#{$self->{wood}};
16 $self->{kdtree_ixs} = \@ixs;
17 $self->{kdtree} = Math::Vector::Real::kdTree->new(map $_->{pos}, @{$self->{wood}}[@ixs]);
18 my $sigma = $self->{near}**(-2) * log(2);
19 # print "sigma: $sigma\n";
20 my $mnm = Math::Vector::Real::MultiNormalMixture->new(mu => [map $_->{pos}, @{$self->{wood}}[@ixs]],
21 sigma => $sigma);
22 $self->{mnm} = $mnm;
23 $self->{mnm_max} = $mnm->max_density_estimation;
24 }
25  
26 sub termite_take_wood_p {
27 my ($self, $termite) = @_;
28 my $pos = $termite->{pos};
29 my $near = $self->{near};
30 my $wood_ix = $self->{kdtree}->find_nearest_neighbor($pos, $near);
31 if (defined $wood_ix) {
32 my @near = $self->{kdtree}->find_in_ball($pos, $near * 3, $wood_ix);
33 my $density = $self->{mnm}->density_portion($pos, @near);
34 my $max = $self->{mnm_max};
35 $self->{mnm_max} = $max = $density if $density > $max;
36 # printf "take -> max: %6g, density: %6g. ratio: %02.7f\n", $max, $density, $density/$max * 100;
37 return $self->{kdtree_ixs}[$wood_ix] if $density < rand($max);
38 }
39 undef
40 }
41  
42 sub termite_leave_wood_p {
43 my ($self, $termite) = @_;
44 my $pos = $termite->{pos};
45 my $near = $self->{near};
46 my @near = $self->{kdtree}->find_in_ball($pos, $near * 3);
47 my $density = $self->{mnm}->density_portion($pos, @near);
48 my $max = $self->{mnm_max};
49 $self->{mnm_max} = $max = $density if $density > $max;
50 # printf "leave -> max: %6g, density: %6g. ratio: %02.7f\n", $max, $density, $density/$max * 100;
51 return 1 if $density > rand($max);
52 undef;
53 }
54  
55 1;