| 1 | # Math::Evol.pm | ||||||||||||||||||||||||||||||||||||
| 2 | ######################################################################### | ||||||||||||||||||||||||||||||||||||
| 3 | # This Perl module is Copyright (c) 2002, Peter J Billam # | ||||||||||||||||||||||||||||||||||||
| 4 | # c/o P J B Computing, www.pjb.com.au # | ||||||||||||||||||||||||||||||||||||
| 5 | # # | ||||||||||||||||||||||||||||||||||||
| 6 | # This module is free software; you can redistribute it and/or # | ||||||||||||||||||||||||||||||||||||
| 7 | # modify it under the same terms as Perl itself. # | ||||||||||||||||||||||||||||||||||||
| 8 | ######################################################################### | ||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||
| 10 | package Math::Evol; | ||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | no strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 12 | 1 | $VERSION = '1.12'; | |||||||||||||||||||||||||||||||||||
| 13 | # gives a -w warning, but I'm afraid $VERSION .= ''; would confuse CPAN | ||||||||||||||||||||||||||||||||||||
| 14 | 1 | require Exporter; | |||||||||||||||||||||||||||||||||||
| 15 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||
| 16 | 1 | @EXPORT = qw(evol select_evol text_evol); | |||||||||||||||||||||||||||||||||||
| 17 | 1 | @EXPORT_OK = qw(arr2txt gaussn); | |||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||
| 19 | # various epsilons used in convergence testing ... | ||||||||||||||||||||||||||||||||||||
| 20 | 1 | $ea = 0.00000000000001; # absolute stepsize | |||||||||||||||||||||||||||||||||||
| 21 | 1 | $eb = 0.000000001; # relative stepsize | |||||||||||||||||||||||||||||||||||
| 22 | 1 | $ec = 0.0000000000000001; # absolute error | |||||||||||||||||||||||||||||||||||
| 23 | 1 | $ed = 0.00000000001; # relative error | |||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||
| 25 | sub new { | ||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | my $arg1 = shift; | ||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | my $class = ref($arg1) || $arg1; # can be used as class or instance method | ||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $arg1 || $arg1
MC/DC0%0/2
ref $arg1 || $arg1
ref $arg1
$arg1
Truth table0%0/3
A || B
A= ref $arg1B= $arg1
| |||||||||||||||||||||||||||||||||||||
| 28 | 0 | my $self = {}; # ref to an empty hash | |||||||||||||||||||||||||||||||||||
| 29 | 0 | bless $self, $class; | |||||||||||||||||||||||||||||||||||
| 30 | 0 | $self->_initialise(); | |||||||||||||||||||||||||||||||||||
| 31 | 0 | return $self; | |||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||
| 35 | 4 | ▶ | sub evol { my ( $xbref,$smref, $func_ref,$constrain_ref, $tm) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
evol: called CC=32 SCAR=37.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 36 | 4 | ▶ | if (ref $xbref ne 'ARRAY') { | ||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||
| 37 | 0 | die "Math::Evol::evol 1st arg must be an array ref\n"; | |||||||||||||||||||||||||||||||||||
| 38 | } elsif (ref $smref ne 'ARRAY') { | ||||||||||||||||||||||||||||||||||||
| 39 | 0 | die "Math::Evol::evol 2nd arg must be an array ref\n"; | |||||||||||||||||||||||||||||||||||
| 40 | } elsif (ref $func_ref ne 'CODE') { | ||||||||||||||||||||||||||||||||||||
| 41 | 0 | die "Math::Evol::evol 3rd arg must be a function ref\n"; | |||||||||||||||||||||||||||||||||||
| 42 | ▶ | } elsif ($constrain_ref && (ref $constrain_ref) ne 'CODE') { | |||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$constrain_ref and ref $constrain_ref ne "CODE"
MC/DC0%0/2
$constrain_ref and ref $constrain_ref ne "CODE"
$constrain_ref
ref $constrain_ref ne "CODE"
Truth table33%1/3
A and B
A= $constrain_refB= ref $constrain_ref ne "CODE"
| |||||||||||||||||||||||||||||||||||||
| 43 | 0 | die "Math::Evol::evol 4th arg must be a function ref\n"; | |||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||
| 45 | 4 | my @xb = @$xbref; | |||||||||||||||||||||||||||||||||||
| 46 | 4 | my @sm = @$smref; | |||||||||||||||||||||||||||||||||||
| 47 | 4 | ▶ | if (! $tm) { $tm = 10.0; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 48 | 4 | $tm = abs $tm; | |||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||
| 50 | 4 | my $debug = 0; | |||||||||||||||||||||||||||||||||||
| 51 | 4 | my $n = scalar @xb; # number of variables | |||||||||||||||||||||||||||||||||||
| 52 | 4 | my $nm1 = $n + $[ - 1; # last index | |||||||||||||||||||||||||||||||||||
| 53 | 4 | my ($usr_o, $sys_o) = times; | |||||||||||||||||||||||||||||||||||
| 54 | 4 | my ($fc, $rel_limit); # used to test convergence | |||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||
| 56 | # step-size adjustment stuff, courtesy Rechenberg ... | ||||||||||||||||||||||||||||||||||||
| 57 | 4 | my @l; foreach (1 .. 10) { push @l, $n*$_/5.0; } # 6 | |||||||||||||||||||||||||||||||||||
| 58 | 4 | my $le = $n+$n; my $lm = 0; my $lc = 0; | |||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||
| 60 | 4 | ▶ | if ($constrain_ref) { @xb = &$constrain_ref(@xb); } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 61 | 4 | $fb = &$func_ref(@xb); $fc = $fb; | |||||||||||||||||||||||||||||||||||
| 62 | 4 | while (1) { | |||||||||||||||||||||||||||||||||||
| 63 | 97984 | foreach $i ($[ .. $nm1) { $x[$i] = $xb[$i]+&gaussn($sm[$i]); } | |||||||||||||||||||||||||||||||||||
| 64 | 97984 | ▶ | if ($constrain_ref) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 65 | 97984 | @x = &$constrain_ref(@x); | |||||||||||||||||||||||||||||||||||
| 66 | 97984 | ▶ | warn 'new @x is '.&arr2txt(@x) if $debug; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||
| 68 | 97984 | $ff = &$func_ref (@x); # find new objective function | |||||||||||||||||||||||||||||||||||
| 69 | 97984 | ▶ | if ($ff <= $fb) { $le++; $fb=$ff; @xb=@x; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 70 | 97984 | ▶ | $lm++; if ($lm < $n) { next; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||
| 72 | # adjust the step sizes ... | ||||||||||||||||||||||||||||||||||||
| 73 | 24496 | my $k = 0.85 ** ($n+$n <=> $le-$l[$[]); | |||||||||||||||||||||||||||||||||||
| 74 | 24496 | foreach $i ($[ .. $nm1) { | |||||||||||||||||||||||||||||||||||
| 75 | 97984 | $sm[$i] *= $k; | |||||||||||||||||||||||||||||||||||
| 76 | 97984 | $rel_limit = abs ($eb * $xb[$i]); | |||||||||||||||||||||||||||||||||||
| 77 | 97984 | ▶ | if ($sm[$i] < $rel_limit) { $sm[$i] = $rel_limit; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 78 | 97984 | ▶ | if ($sm[$i] < $ea) { $sm[$i] = $ea; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||
| 80 | # could i_l%10 the index as in lua, rather than shift and push as here | ||||||||||||||||||||||||||||||||||||
| 81 | 24496 | shift @l; push (@l, $le); $lm = 0; | |||||||||||||||||||||||||||||||||||
| 82 | 24496 | ▶ | if ($debug) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 83 | 0 | warn "le=$le l[0]=$l[$[] n=$n k=$k\n"; | |||||||||||||||||||||||||||||||||||
| 84 | 0 | warn "new step sizes sm = ".&arr2txt(@sm); | |||||||||||||||||||||||||||||||||||
| 85 | 0 | warn "new l = ".&arr2txt(@l); | |||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||
| 88 | 24496 | ▶ | if (defined $ea) { $ea = abs $ea; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 89 | 24496 | ▶ | if (defined $eb) { $eb = abs $eb; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 90 | 24496 | ▶ | if (defined $ec) { $ec = abs $ec; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 91 | 24496 | ▶ | if (defined $ed) { $ed = abs $ed; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 92 | 24496 | ▶ | warn sprintf("fb=%g fc=%g ec=%g ed=%g\n",$fb,$fc,$ec,$ed) if $debug; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 93 | 24496 | $lc++; | |||||||||||||||||||||||||||||||||||
| 94 | 24496 | ▶ | if ($lc < 25) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 95 | 23517 | my ($usr_n, $sys_n) = times; | |||||||||||||||||||||||||||||||||||
| 96 | 23517 | ▶ | if (($usr_n+$sys_n - $usr_o-$sys_o) > $tm) { # out of time ? | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | warn "exceeded $tm seconds\n" if $debug; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 98 | 1 | return (\@xb, \@sm, $fb, 0); # return best current value | |||||||||||||||||||||||||||||||||||
| 99 | 23516 | } else { next; | |||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||
| 102 | 979 | ▶ | if ((defined $ec) && (($fc-$fb) <= $ec)) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $ec and $fc - $fb <= $ec
MC/DC100%2/2
defined $ec and $fc - $fb <= $ec
defined $ec
$fc - $fb <= $ec
Truth table100%3/3
A and B
A= defined $ecB= $fc - $fb <= $ec
| |||||||||||||||||||||||||||||||||||||
| 103 | 1 | ▶ | warn "converged absolutely\n" if $debug; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 104 | 1 | return (\@xb, \@sm, $fb, 1); # 29 | |||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||
| 106 | 978 | ▶ | if ((defined $ed) && (($fc-$fb)/$ed <= abs($fc))) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $ed and ($fc - $fb) / $ed <= abs $fc
MC/DC100%2/2
defined $ed and ($fc - $fb) / $ed <= abs $fc
defined $ed
($fc - $fb) / $ed <= abs $fc
Truth table100%3/3
A and B
A= defined $edB= ($fc - $fb) / $ed <= abs $fc
| |||||||||||||||||||||||||||||||||||||
| 107 | 2 | ▶ | warn "converged relativelty\n" if $debug; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 108 | 2 | return (\@xb, \@sm, $fb, 1); | |||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||
| 110 | 976 | $lc = 0; $fc = $fb; next; | |||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||
| 114 | 1 | ▶ | sub select_evol { my ($xbref,$smref,$func_ref,$constrain_ref,$nchoices) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
select_evol: called CC=21 SCAR=33.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 115 | 1 | ▶ | if (ref $xbref ne 'ARRAY') { | ||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||
| 116 | 0 | die "Math::Evol::select_evol 1st arg must be an array ref\n"; | |||||||||||||||||||||||||||||||||||
| 117 | } elsif (ref $smref ne 'ARRAY') { | ||||||||||||||||||||||||||||||||||||
| 118 | 0 | die "Math::Evol::select_evol 2nd arg must be an array ref\n"; | |||||||||||||||||||||||||||||||||||
| 119 | } elsif (ref $func_ref ne 'CODE') { | ||||||||||||||||||||||||||||||||||||
| 120 | 0 | die "Math::Evol::select_evol 3rd arg must be a function ref\n"; | |||||||||||||||||||||||||||||||||||
| 121 | ▶ | } elsif ($constrain_ref && (ref $constrain_ref) ne 'CODE') { | |||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$constrain_ref and ref $constrain_ref ne "CODE"
MC/DC0%0/2
$constrain_ref and ref $constrain_ref ne "CODE"
$constrain_ref
ref $constrain_ref ne "CODE"
Truth table33%1/3
A and B
A= $constrain_refB= ref $constrain_ref ne "CODE"
| |||||||||||||||||||||||||||||||||||||
| 122 | 0 | die "Math::Evol::select_evol 4th arg must be a function ref\n"; | |||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||
| 124 | 1 | my @xb = @$xbref; | |||||||||||||||||||||||||||||||||||
| 125 | 1 | my @sm = @$smref; | |||||||||||||||||||||||||||||||||||
| 126 | 1 | ▶ | if (! $nchoices) { $nchoices = 1; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||
| 128 | 1 | my $debug = 0; | |||||||||||||||||||||||||||||||||||
| 129 | 1 | my $n = scalar @xb; # number of variables | |||||||||||||||||||||||||||||||||||
| 130 | 1 | my $nm1 = $n + $[ - 1; # last index | |||||||||||||||||||||||||||||||||||
| 131 | 1 | my ($choice, $continue); | |||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||
| 133 | # step-size adjustment stuff, courtesy Rechenberg | ||||||||||||||||||||||||||||||||||||
| 134 | # modified from Schwefel's sliding flat window average to an EMA | ||||||||||||||||||||||||||||||||||||
| 135 | 1 | my $desired_success_rate = 1.0 - 0.8**$nchoices; | |||||||||||||||||||||||||||||||||||
| 136 | 1 | my $success_rate = $desired_success_rate; | |||||||||||||||||||||||||||||||||||
| 137 | # test every 5*$n binary choices equivalent - 10*$n is just too slow. | ||||||||||||||||||||||||||||||||||||
| 138 | 1 | my $lm = 0; | |||||||||||||||||||||||||||||||||||
| 139 | 1 | my $test_every = 5 * $n * (log 2) / (log ($nchoices+1)); | |||||||||||||||||||||||||||||||||||
| 140 | 1 | my $ema = exp (-1.0 / $test_every); | |||||||||||||||||||||||||||||||||||
| 141 | 1 | my $one_m_ema = 1.0 - $ema; | |||||||||||||||||||||||||||||||||||
| 142 | 1 | ▶ | warn sprintf | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 143 | ("n=$n nchoices=$nchoices success_rate=%g test_every=%g ema=%g\n", | ||||||||||||||||||||||||||||||||||||
| 144 | $success_rate, $test_every, $ema) if $debug; | ||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||
| 146 | 1 | ▶ | if ($constrain_ref) { @xb = &$constrain_ref(@xb); } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 147 | 1 | my @func_args; # array of refs to arrays | |||||||||||||||||||||||||||||||||||
| 148 | 1 | while () { | |||||||||||||||||||||||||||||||||||
| 149 | 1115 | my @x; #XXX? | |||||||||||||||||||||||||||||||||||
| 150 | 1115 | @func_args = ( \@xb ); | |||||||||||||||||||||||||||||||||||
| 151 | 1115 | foreach (1 .. $nchoices) { | |||||||||||||||||||||||||||||||||||
| 152 | 1115 | foreach $i ($[ .. $nm1) { | |||||||||||||||||||||||||||||||||||
| 153 | 4460 | $x[$i] = $xb[$i]+&gaussn($sm[$i]); | |||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||
| 155 | 1115 | ▶ | if ($constrain_ref) { @x = &$constrain_ref(@x); } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 156 | 1115 | push @func_args, [ @x ]; | |||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||
| 158 | 1115 | ($choice, $continue) = &$func_ref(@func_args); | |||||||||||||||||||||||||||||||||||
| 159 | 1115 | ▶ | if ($choice > $[) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 160 | 47 | @xb = @{$func_args[$choice]}; | |||||||||||||||||||||||||||||||||||
| 161 | 47 | $success_rate = $one_m_ema + $ema*$success_rate; | |||||||||||||||||||||||||||||||||||
| 162 | } else { | ||||||||||||||||||||||||||||||||||||
| 163 | 1068 | $success_rate = $ema*$success_rate; | |||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||
| 165 | 1115 | ▶ | warn "success_rate=$success_rate\n" if $debug; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 166 | 1115 | ▶ | if (!$continue) { return (\@xb, \@sm); } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 167 | 1114 | ▶ | $lm++; if ($lm < $test_every) { next; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||
| 169 | # adjust the step sizes ... | ||||||||||||||||||||||||||||||||||||
| 170 | 55 | my $k = 0.85 ** ($desired_success_rate <=> $success_rate); | |||||||||||||||||||||||||||||||||||
| 171 | 55 | ▶ | warn "success_rate=$success_rate k=$k\n" if $debug; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 172 | 55 | foreach $i ($[ .. $nm1) { | |||||||||||||||||||||||||||||||||||
| 173 | 220 | $sm[$i] *= $k; | |||||||||||||||||||||||||||||||||||
| 174 | 220 | $rel_limit = abs ($eb * $xb[$i]); | |||||||||||||||||||||||||||||||||||
| 175 | 220 | ▶ | if ($sm[$i] < $rel_limit) { $sm[$i] = $rel_limit; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 176 | 220 | ▶ | if ($sm[$i] < $ea) { $sm[$i] = $ea; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||
| 178 | 55 | ▶ | warn "new step sizes sm = ".&arr2txt(@sm) if $debug; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 179 | 55 | $lm = 0; | |||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | sub text_evol { my ($text, $nchoices); local ($func_ref); | ||||||||||||||||||||||||||||||||||
|
Subroutine
text_evol: not called CC=24 SCAR=64.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 184 | 0 | ($text, $func_ref, $nchoices) = @_; | |||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | return unless $text; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | if (ref $func_ref ne 'CODE') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 187 | 0 | die "Math::Evol::text_evol 2nd arg must be a function ref\n"; | |||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | if (! $nchoices) { $nchoices = 1; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||
| 191 | 0 | my $debug = 0; | |||||||||||||||||||||||||||||||||||
| 192 | 0 | local @text = split ("\n", $text); | |||||||||||||||||||||||||||||||||||
| 193 | 0 | my ($linenum,$m,@xb,@sm,@min,@max) = ($[,0); | |||||||||||||||||||||||||||||||||||
| 194 | 0 | local (@linenums, @firstbits, @middlebits, $lastbit); | |||||||||||||||||||||||||||||||||||
| 195 | 0 | my $n = $[ - 1; foreach (@text) { | |||||||||||||||||||||||||||||||||||
| 196 | 0 | ▶ | if (/^(.*?)(-?[\d.]+)(\D+)evol\s+step\s+([\d.]+)(.*)$/) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 197 | 0 | $n++; $linenums[$n] = $linenum; $firstbits[$n]=$1; | |||||||||||||||||||||||||||||||||||
| 198 | 0 | $xb[$n]=$2; $middlebits[$n]=$3; $sm[$n]=$4; $lastbit = $5; | |||||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | if ($lastbit =~ /min\s+([-\d.]+)/) { $min[$n] = $1; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | if ($lastbit =~ /max\s+([-\d.]+)/) { $max[$n] = $1; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||
| 202 | 0 | $linenum++; | |||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | warn "xb = ".&arr2txt(@xb) if $debug; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | warn "sm = ".&arr2txt(@sm) if $debug; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||
| 207 | # construct the constraint routine | ||||||||||||||||||||||||||||||||||||
| 208 | 0 | my $some_constraints = 0; | |||||||||||||||||||||||||||||||||||
| 209 | 0 | my @sub_constr = ("sub constrain {\n"); | |||||||||||||||||||||||||||||||||||
| 210 | 0 | ▶ | my $i = $[; while ($i <= $n) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 211 | 0 | ▶ | if (defined $min[$i] && defined $max[$i]) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/3
defined $min[$i] and defined $max[$i]
MC/DC0%0/2
defined $min[$i] and defined $max[$i]
defined $min[$i]
defined $max[$i]
Truth table0%0/3
A and B
A= defined $min[$i]B= defined $max[$i]
| |||||||||||||||||||||||||||||||||||||
| 212 | 0 | push @sub_constr,"\tif (\$_[$i]>$max[$i]) { \$_[$i]=$max[$i];\n"; | |||||||||||||||||||||||||||||||||||
| 213 | 0 | push @sub_constr,"\t} elsif (\$_[$i]<$min[$i]) { \$_[$i]=$min[$i];\n"; | |||||||||||||||||||||||||||||||||||
| 214 | 0 | push @sub_constr,"\t}\n"; | |||||||||||||||||||||||||||||||||||
| 215 | } elsif (defined $min[$i]) { | ||||||||||||||||||||||||||||||||||||
| 216 | 0 | push @sub_constr,"\tif (\$_[$i]<$min[$i]) { \$_[$i]=$min[$i]; }\n"; | |||||||||||||||||||||||||||||||||||
| 217 | } elsif (defined $max[$i]) { | ||||||||||||||||||||||||||||||||||||
| 218 | 0 | push @sub_constr,"\tif (\$_[$i]>$max[$i]) { \$_[$i]=$max[$i]; }\n"; | |||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||
| 220 | 0 | ▶ | if (defined $min[$i] || defined $max[$i]) { $some_constraints++; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $min[$i] or defined $max[$i]
MC/DC0%0/2
defined $min[$i] or defined $max[$i]
defined $min[$i]
defined $max[$i]
Truth table0%0/3
A or B
A= defined $min[$i]B= defined $max[$i]
| |||||||||||||||||||||||||||||||||||||
| 221 | 0 | $i++; | |||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||
| 223 | 0 | push @sub_constr, "\treturn \@_;\n}\n"; | |||||||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | warn join ('', @sub_constr)."\n" if $debug; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||
| 226 | sub choose_best { | ||||||||||||||||||||||||||||||||||||
| 227 | 0 | ▶ | my $xbref; my $linenum; @texts = (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
choose_best: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 228 | 0 | ▶ | while ($xbref = shift @_) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 229 | 0 | my @newtext = @text; my $i = $[; | |||||||||||||||||||||||||||||||||||
| 230 | 0 | foreach $linenum (@linenums) { | |||||||||||||||||||||||||||||||||||
| 231 | 0 | $newtext[$linenum] = $firstbits[$i] . sprintf ('%g', $$xbref[$i]) | |||||||||||||||||||||||||||||||||||
| 232 | . $middlebits[$i]; | ||||||||||||||||||||||||||||||||||||
| 233 | 0 | $i++; | |||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||
| 235 | 0 | push @texts, join ("\n", @newtext); | |||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||
| 237 | 0 | return &$func_ref(@texts); | |||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||
| 240 | 0 | my ($xbref, $smref); | |||||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | if ($some_constraints) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | eval join '', @sub_constr; if ($@) { die "text_evol: $@\n"; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 243 | 0 | ($xbref, $smref) = | |||||||||||||||||||||||||||||||||||
| 244 | &select_evol(\@xb, \@sm, \&choose_best, \&constrain, $nchoices); | ||||||||||||||||||||||||||||||||||||
| 245 | } else { | ||||||||||||||||||||||||||||||||||||
| 246 | 0 | ($xbref, $smref) = &select_evol(\@xb,\@sm,\&choose_best,0,$nchoices); | |||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||
| 249 | 0 | my @new_text = @text; $i = $[; | |||||||||||||||||||||||||||||||||||
| 250 | 0 | foreach $linenum (@linenums) { | |||||||||||||||||||||||||||||||||||
| 251 | 0 | $new_text[$linenum] = $firstbits[$i] . sprintf ('%g', $$xbref[$i]) | |||||||||||||||||||||||||||||||||||
| 252 | . $middlebits[$i] . ' evol step '. sprintf ('%g', $$smref[$i]); | ||||||||||||||||||||||||||||||||||||
| 253 | 0 | ▶ | if (defined $min[$i]) { $new_text[$linenum] .= " min $min[$i]"; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 254 | 0 | ▶ | if (defined $max[$i]) { $new_text[$linenum] .= " max $max[$i]"; } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 255 | 0 | $i++; | |||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||
| 257 | 0 | ▶ | warn join ("\n", @new_text)."\n" if $debug; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 258 | 0 | return join ("\n", @new_text)."\n"; | |||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||
| 261 | # --------------- infrastructure for evol ---------------- | ||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||
| 263 | sub arr2txt { # neat printing of arrays for debug use | ||||||||||||||||||||||||||||||||||||
| 264 | 0 | ▶ | my @txt; foreach (@_) { push @txt, sprintf('%g',$_); } | ||||||||||||||||||||||||||||||||||
|
Subroutine
arr2txt: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 265 | 0 | return join (' ',@txt)."\n"; | |||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||
| 267 | 1 | my $gaussn_a = rand; # reject 1st call to rand in case it's zero | |||||||||||||||||||||||||||||||||||
| 268 | 1 | my $gaussn_b; | |||||||||||||||||||||||||||||||||||
| 269 | my $gaussn_flag; | ||||||||||||||||||||||||||||||||||||
| 270 | 396396 | ▶ | sub gaussn { my $standdev = $_[$[]; | ||||||||||||||||||||||||||||||||||
|
Subroutine
gaussn: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 271 | # returns normal distribution around 0.0 by the Box-Muller rules | ||||||||||||||||||||||||||||||||||||
| 272 | 396396 | ▶ | if (! $gaussn_flag) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 273 | # $gaussn_a = sqrt(-2.0 * log(rand)); BUG #44777: Log of zero error | ||||||||||||||||||||||||||||||||||||
| 274 | 198198 | $gaussn_a = sqrt(-2.0 * log(rand(0.999)+0.001)); # 1.12 | |||||||||||||||||||||||||||||||||||
| 275 | 198198 | $gaussn_b = 6.28318531 * rand; | |||||||||||||||||||||||||||||||||||
| 276 | 198198 | $gaussn_flag = 1; | |||||||||||||||||||||||||||||||||||
| 277 | 198198 | return ($standdev * $gaussn_a * sin($gaussn_b)); | |||||||||||||||||||||||||||||||||||
| 278 | } else { | ||||||||||||||||||||||||||||||||||||
| 279 | 198198 | $gaussn_flag = 0; | |||||||||||||||||||||||||||||||||||
| 280 | 198198 | return ($standdev * $gaussn_a * cos($gaussn_b)); | |||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||
| 283 | 1 | 1; | |||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||
| 285 | __END__ | ||||||||||||||||||||||||||||||||||||