| 1 | ##-*- Mode: CPerl -*- | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | ##====================================================================== | ||||||||||||||||||||||||||||
| 4 | ## Header Administrivia | ||||||||||||||||||||||||||||
| 5 | ##====================================================================== | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | our $VERSION = '0.08'; | ||||||||||||||||||||||||||||
| 8 | pp_setversion($VERSION); | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 11 | ## bad hacks | ||||||||||||||||||||||||||||
| 12 | require PDL::Bad; | ||||||||||||||||||||||||||||
| 13 | sub ISBAD { return $PDL::Bad::Status ? ("\$ISBAD(".join(',',@_).")") : '0 /* ISBAD(): no bad support available */'; } | ||||||||||||||||||||||||||||
| 14 | sub ISBADVAR { return $PDL::Bad::Status ? ("\$ISBADVAR(".join(',',@_).")") : '0 /* ISBADVAR(): no bad support available */'; } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | sub ISGOOD { return $PDL::Bad::Status ? ("\$ISGOOD(".join(',',@_).")") : '1 /* ISGOOD(): no bad support available */'; } | ||||||||||||||||||||||||||||
| 17 | sub ISGOODVAR { return $PDL::Bad::Status ? ("\$ISGOODVAR(".join(',',@_).")") : '1 /* ISGOODVAR(): no bad support available */'; } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 20 | ## index-type hacks | ||||||||||||||||||||||||||||
| 21 | require PDL::Core; | ||||||||||||||||||||||||||||
| 22 | my ($INDX); | ||||||||||||||||||||||||||||
| 23 | if (defined(&PDL::indx)) { | ||||||||||||||||||||||||||||
| 24 | #print STDERR "PDL >= v2.007 detected : using built-in PDL_Indx type\n"; | ||||||||||||||||||||||||||||
| 25 | $INDX = 'indx'; | ||||||||||||||||||||||||||||
| 26 | pp_addhdr("/* No PDL_Indx typedef needed for PDL >= v2.007 */\n"); | ||||||||||||||||||||||||||||
| 27 | pp_addpm("*ga_indx = \&PDL::indx;\n"); | ||||||||||||||||||||||||||||
| 28 | } else { | ||||||||||||||||||||||||||||
| 29 | #print STDERR "PDL < v2.007 detected : using hacked PDL_Indx type\n"; | ||||||||||||||||||||||||||||
| 30 | $INDX = 'int'; | ||||||||||||||||||||||||||||
| 31 | $pindx = 'long'; | ||||||||||||||||||||||||||||
| 32 | pp_addhdr("/* PDL_Indx typedef hack for PDL < v2.007 */\n" | ||||||||||||||||||||||||||||
| 33 | ."typedef int PDL_Indx;\n" | ||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | pp_addpm("*ga_indx = \&PDL::long;\n"); | ||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 39 | ## pm additions | ||||||||||||||||||||||||||||
| 40 | pp_addpm({At=>'Top'},<<'EOPM'); | ||||||||||||||||||||||||||||
| 41 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =pod | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head1 NAME | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | PDL::GA - Genetic algorithm utilities for PDLs | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | use PDL; | ||||||||||||||||||||||||||||
| 52 | use PDL::GA; | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | ##------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 55 | ## TODO... | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =cut | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | EOPM | ||||||||||||||||||||||||||||
| 60 | ## /pm additions | ||||||||||||||||||||||||||||
| 61 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 64 | ## Exports: None | ||||||||||||||||||||||||||||
| 65 | pp_export_nothing(); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 68 | ## Includes / defines | ||||||||||||||||||||||||||||
| 69 | require Config; ##-- perl configuration, for drand01 macro | ||||||||||||||||||||||||||||
| 70 | pp_addhdr(<<"EOH"); | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | /*-- Macro to be used to generate normalized random numbers --*/ | ||||||||||||||||||||||||||||
| 73 | #define GA_RAND01() $Config::Config{drand01} | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | EOH | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | ##====================================================================== | ||||||||||||||||||||||||||||
| 78 | ## C Utilities | ||||||||||||||||||||||||||||
| 79 | ##====================================================================== | ||||||||||||||||||||||||||||
| 80 | pp_addhdr(<<'EOH'); | ||||||||||||||||||||||||||||
| 81 | /*-- C header stuff goes here --*/ | ||||||||||||||||||||||||||||
| 82 | #include <stdlib.h> | ||||||||||||||||||||||||||||
| 83 | EOH | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | ##====================================================================== | ||||||||||||||||||||||||||||
| 86 | ## PDL::PP Wrappers | ||||||||||||||||||||||||||||
| 87 | ##====================================================================== | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | ##====================================================================== | ||||||||||||||||||||||||||||
| 90 | ## Basic Utilities | ||||||||||||||||||||||||||||
| 91 | #pp_addpm(<<'EOPM'); | ||||||||||||||||||||||||||||
| 92 | #=pod | ||||||||||||||||||||||||||||
| 93 | # | ||||||||||||||||||||||||||||
| 94 | #=head1 Basic Utilities | ||||||||||||||||||||||||||||
| 95 | # | ||||||||||||||||||||||||||||
| 96 | #=cut | ||||||||||||||||||||||||||||
| 97 | #EOPM | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | ##====================================================================== | ||||||||||||||||||||||||||||
| 100 | ## Weighted Selection | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | pp_addpm(<<'EOPM'); | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =pod | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head1 Weighted Selection | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =cut | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | EOPM | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 114 | ## Roulette-wheel selection | ||||||||||||||||||||||||||||
| 115 | pp_add_exported('','roulette'); | ||||||||||||||||||||||||||||
| 116 | pp_addpm(<<'EOPM'); | ||||||||||||||||||||||||||||
| 117 | =pod | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head2 roulette | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =for sig | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | Signature: (weightmap(M); %options) | ||||||||||||||||||||||||||||
| 124 | Options: | ||||||||||||||||||||||||||||
| 125 | n => $n | ||||||||||||||||||||||||||||
| 126 | to => [o]selindices($n) | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | Stochastic (roulette-wheel) selection of $n objects from | ||||||||||||||||||||||||||||
| 129 | $M objects, governed by the likelihood distribution $weightmap(), allowing repetitions. | ||||||||||||||||||||||||||||
| 130 | Calls PDL::Primitive::vsearch(). | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub roulette { | ||||||||||||||||||||||||||
|
Subroutine
roulette: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 0 | my ($wmap,%opts) = @_; | |||||||||||||||||||||||||||
| 136 | 0 | my ($seli); | |||||||||||||||||||||||||||
| 137 | 0 | ▶ | if (defined($opts{to})) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 138 | 0 | $seli = $opts{to}; | |||||||||||||||||||||||||||
| 139 | } elsif (defined($opts{n})) { | ||||||||||||||||||||||||||||
| 140 | 0 | ▶ | $seli = zeroes(ga_indx(), (($wmap->dims)[1..($wmap->ndims-1)]), $opts{n}) if (!defined($seli)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | $seli->resize((($wmap->dims)[1..($wmap->ndims-1)]), $opts{n}) | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if ($seli->ndims != $wmap->ndims || $seli->dim(-1) != $opts{n}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$seli->ndims != $wmap->ndims or $seli->dim(-1) != $opts{"n"}
MC/DC0%0/2
$seli->ndims != $wmap->ndims or $seli->dim(-1) != $opts{"n"}
$seli->ndims != $wmap->ndims
$seli->dim(-1) != $opts{"n"}
Truth table0%0/3
A or B
A= $seli->ndims != $wmap->ndimsB= $seli->dim(-1) != $opts{"n"}
| |||||||||||||||||||||||||||||
| 143 | } else { | ||||||||||||||||||||||||||||
| 144 | 0 | $seli = zeroes(ga_indx(),1); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 0 | my $wsum = $wmap->sumover->slice(',*1'); | |||||||||||||||||||||||||||
| 147 | 0 | my $selw = PDL->random($seli->dims); | |||||||||||||||||||||||||||
| 148 | 0 | $selw *= $wsum; | |||||||||||||||||||||||||||
| 149 | 0 | $selw->vsearch($wmap->cumusumover, $seli); | |||||||||||||||||||||||||||
| 150 | 0 | return $seli; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | EOPM | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 157 | ## Roulette-wheel selection (no repetition) | ||||||||||||||||||||||||||||
| 158 | pp_add_exported('','roulette_nr'); | ||||||||||||||||||||||||||||
| 159 | pp_addpm(<<'EOPM'); | ||||||||||||||||||||||||||||
| 160 | =pod | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head2 roulette_nr | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =for sig | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Signature: (weightmap(M); %options) | ||||||||||||||||||||||||||||
| 167 | Options: | ||||||||||||||||||||||||||||
| 168 | n => $n | ||||||||||||||||||||||||||||
| 169 | to => [o]selindices($n) | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Stochastic (roulette-wheel) selection of $n objects from | ||||||||||||||||||||||||||||
| 172 | $M objects, governed by the likelihood distribution $weightmap(), without repetitions. | ||||||||||||||||||||||||||||
| 173 | Wrapper for cumuweighselect_nr. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =cut | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | sub roulette_nr { | ||||||||||||||||||||||||||
|
Subroutine
roulette_nr: not called CC=6 SCAR=36.2
Pod
documented
| |||||||||||||||||||||||||||||
| 178 | 1 | my ($wmap,%opts) = @_; | |||||||||||||||||||||||||||
| 179 | 0 | my ($seli); | |||||||||||||||||||||||||||
| 180 | 0 | ▶ | if (defined($opts{to})) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 181 | 0 | $seli = $opts{to}; | |||||||||||||||||||||||||||
| 182 | } elsif (defined($opts{n})) { | ||||||||||||||||||||||||||||
| 183 | 0 | ▶ | $seli = zeroes(ga_indx(), (($wmap->dims)[1..($wmap->ndims-1)]), $opts{n}) if (!defined($seli)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | $seli->resize((($wmap->dims)[1..($wmap->ndims-1)]), $opts{n}) | ||||||||||||||||||||||||||||
| 185 | 0 | ▶ | if ($seli->ndims != $wmap->ndims || $seli->dim(-1) != $opts{n}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$seli->ndims != $wmap->ndims or $seli->dim(-1) != $opts{"n"}
MC/DC0%0/2
$seli->ndims != $wmap->ndims or $seli->dim(-1) != $opts{"n"}
$seli->ndims != $wmap->ndims
$seli->dim(-1) != $opts{"n"}
Truth table0%0/3
A or B
A= $seli->ndims != $wmap->ndimsB= $seli->dim(-1) != $opts{"n"}
| |||||||||||||||||||||||||||||
| 186 | } else { | ||||||||||||||||||||||||||||
| 187 | 0 | $seli = zeroes(ga_indx(),1); | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | 0 | my $wsum = $wmap->sumover->slice(',*1'); | |||||||||||||||||||||||||||
| 190 | 0 | my $selw = PDL->random($seli->dims); | |||||||||||||||||||||||||||
| 191 | 0 | $selw *= $wsum; | |||||||||||||||||||||||||||
| 192 | 0 | return cumuweightselect_nr($wmap->cumusumover, $selw, $seli); | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | EOPM | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 199 | ## weightselect() : weighted selection | ||||||||||||||||||||||||||||
| 200 | pp_add_exported('','weightselect'); | ||||||||||||||||||||||||||||
| 201 | pp_addpm(<<'EOPM'); | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =pod | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head2 weightselect | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =for sig | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | Signature: (weightmap(M); selweights(S); [o]selindices(S)) | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | Stochastically select $S objects from a pool $M objects, allowing repetitions. | ||||||||||||||||||||||||||||
| 212 | Likelihood selecting an object $i is given by $weightmap($i). Target | ||||||||||||||||||||||||||||
| 213 | selection likelihoods are passed as $selweights(), which should have | ||||||||||||||||||||||||||||
| 214 | values in the range [0,sum($weightmap)\(. Selected targets are | ||||||||||||||||||||||||||||
| 215 | returned as indices in the range [0,$M\( in the PDL $selindices(). | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | See also: | ||||||||||||||||||||||||||||
| 218 | roulette(), | ||||||||||||||||||||||||||||
| 219 | cumuweightselect(), | ||||||||||||||||||||||||||||
| 220 | roulette_nr(), | ||||||||||||||||||||||||||||
| 221 | weightselect_nr(), | ||||||||||||||||||||||||||||
| 222 | cumuweightselect_nr(), | ||||||||||||||||||||||||||||
| 223 | PDL::Primitive::vsearch(), | ||||||||||||||||||||||||||||
| 224 | PDL::Ufunc::cumusumover(). | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =cut | ||||||||||||||||||||||||||||
| 227 | 0 | ▶ | |||||||||||||||||||||||||||
|
Subroutine
weightselect: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 228 | sub weightselect { | ||||||||||||||||||||||||||||
| 229 | #my ($wmap,$selw,$seli) = @_; | ||||||||||||||||||||||||||||
| 230 | return | ||||||||||||||||||||||||||||
| 231 | 0 | #$selw->vsearch($wmap->cumusumover, @_); | |||||||||||||||||||||||||||
| 232 | $_[1]->vsearch($_[0]->cumusumover, @_[2..$#_]); | ||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | EOPM | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 239 | ## weightselect_nr() : weighted selection, no repetition | ||||||||||||||||||||||||||||
| 240 | pp_add_exported('','weightselect_nr'); | ||||||||||||||||||||||||||||
| 241 | pp_addpm(<<'EOPM'); | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =pod | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =head2 weightselect_nr | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =for sig | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | Signature: (weightmap(M); selweights(S); [o]selindices(S)) | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | Like weightselect() without repetition. | ||||||||||||||||||||||||||||
| 252 | Wraps cumuweightselect_nr(). | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | =cut | ||||||||||||||||||||||||||||
| 255 | 0 | ▶ | |||||||||||||||||||||||||||
|
Subroutine
weightselect_nr: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 256 | sub weightselect_nr { | ||||||||||||||||||||||||||||
| 257 | #my ($wmap,$selw,$seli) = @_; | ||||||||||||||||||||||||||||
| 258 | return | ||||||||||||||||||||||||||||
| 259 | 0 | #cumuweightselect_nr($wmap->cumusumover,$selw,$seli); | |||||||||||||||||||||||||||
| 260 | cumuweightselect_nr($_[0]->cumusumover, @_[1..$#_]); | ||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | EOPM | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 267 | ## cumuweightselect() : weighted selection (cumulative) | ||||||||||||||||||||||||||||
| 268 | pp_add_exported('','cumuweightselect'); | ||||||||||||||||||||||||||||
| 269 | pp_addpm(<<'EOPM'); | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | =pod | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | =head2 cumuweightselect | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =for sig | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | Signature: (cumuweightmap(M); selweights(S); indx [o]selindices(S)) | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | Stochastically select $S objects from a pool $M objects, allowing repetitions. | ||||||||||||||||||||||||||||
| 280 | Cumulative likelihood selecting an object $i is given by $cumweightmap($i). Target | ||||||||||||||||||||||||||||
| 281 | selection likelihoods are passed as $selweights(), which should have | ||||||||||||||||||||||||||||
| 282 | values in the range [0,$cumuweightmap[-1]\(. Selected targets are | ||||||||||||||||||||||||||||
| 283 | returned as indices in the range [0,$M\( in the PDL $selindices(). | ||||||||||||||||||||||||||||
| 284 | Really just a wrapper for PDL::Primitive::vsearch(). | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | See also: | ||||||||||||||||||||||||||||
| 287 | roulette(), | ||||||||||||||||||||||||||||
| 288 | weightselect(), | ||||||||||||||||||||||||||||
| 289 | roulette_nr(), | ||||||||||||||||||||||||||||
| 290 | weightselect_nr(), | ||||||||||||||||||||||||||||
| 291 | cumuweightselect_nr(), | ||||||||||||||||||||||||||||
| 292 | PDL::Primitive::vsearch(), | ||||||||||||||||||||||||||||
| 293 | PDL::Ufunc::cumusumover(). | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | =cut | ||||||||||||||||||||||||||||
| 296 | 0 | ▶ | |||||||||||||||||||||||||||
|
Subroutine
cumuweightselect: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 297 | sub cumuweightselect { | ||||||||||||||||||||||||||||
| 298 | #my ($cwmap,$selw,$seli) = splice(@_,0,2); | ||||||||||||||||||||||||||||
| 299 | return | ||||||||||||||||||||||||||||
| 300 | 0 | #$selw->vsearch($cwmap, @_); | |||||||||||||||||||||||||||
| 301 | $_[1]->vsearch($_[0], @_[2..$#_]); | ||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | EOPM | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 308 | ## cumuweightselect_nr() : weighted selection (cumulative), no repeats | ||||||||||||||||||||||||||||
| 309 | pp_add_exported('','cumuweightselect_nr'); | ||||||||||||||||||||||||||||
| 310 | pp_addpm(<<'EOPM'); | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | =pod | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | =head2 cumuweightselect_nr | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | =for sig | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | Signature: (cumuweightmap(M); selweights(S); indx [o]selindices(S); indx [t]trynext(M); byte [t]ignore(M)) | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | Stochastically select $S objects from a pool $M objects, without repetitions. | ||||||||||||||||||||||||||||
| 321 | Really just a wrapper for PDL::Primitive::vesarch() and ga_make_unique(). | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | =cut | ||||||||||||||||||||||||||||
| 324 | 0 | ▶ | |||||||||||||||||||||||||||
|
Subroutine
cumuweightselect_nr: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 325 | 0 | sub cumuweightselect_nr { | |||||||||||||||||||||||||||
| 326 | 0 | ▶ | my ($cwmap,$selw,$seli,$try,$ignore) = @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | 0 | $seli = zeroes(ga_indx(),$selw->dims) if (!defined($seli)); | |||||||||||||||||||||||||||
| 328 | 0 | ▶ | $selw->vsearch($cwmap, $seli); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 329 | 0 | ▶ | $try = 1+PDL->sequence(ga_indx(),$cwmap->dim(0)) if (!defined($try)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | 0 | $seli->inplace->ga_make_unique($try, (defined($ignore) ? $ignore : qw())); | |||||||||||||||||||||||||||
| 331 | return $seli; | ||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | EOPM | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 337 | ## ga_make_unique() : make an integer-set unique | ||||||||||||||||||||||||||||
| 338 | pp_add_exported('','ga_make_unique'); | ||||||||||||||||||||||||||||
| 339 | pp_def | ||||||||||||||||||||||||||||
| 340 | ('ga_make_unique', | ||||||||||||||||||||||||||||
| 341 | Pars => "$INDX selected(S); int trynext(M); $INDX \[o]unique_selected(S); byte [t]ignore(M)", | ||||||||||||||||||||||||||||
| 342 | Inplace=>['selected','unique_selected'], | ||||||||||||||||||||||||||||
| 343 | HandleBad=>1, | ||||||||||||||||||||||||||||
| 344 | Code => | ||||||||||||||||||||||||||||
| 345 | ' | ||||||||||||||||||||||||||||
| 346 | PDL_Indx s_max = $SIZE(S)-1; | ||||||||||||||||||||||||||||
| 347 | PDL_Indx m_max = $SIZE(M)-1; | ||||||||||||||||||||||||||||
| 348 | PDL_Indx i_items; | ||||||||||||||||||||||||||||
| 349 | PDL_Indx item, ntried; | ||||||||||||||||||||||||||||
| 350 | /*-- sanity check --*/ | ||||||||||||||||||||||||||||
| 351 | if ($SIZE(S) > $SIZE(M)) { | ||||||||||||||||||||||||||||
| 352 | barf("PDL::GA::ga_make_unique(): too many selections requested (S > M)"); | ||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | threadloop %{ | ||||||||||||||||||||||||||||
| 355 | loop (M) %{ $ignore() = 0; %} /*-- initially, all choices are open --*/ | ||||||||||||||||||||||||||||
| 356 | loop (S) %{ | ||||||||||||||||||||||||||||
| 357 | PDL_Indx item0 = $selected(); | ||||||||||||||||||||||||||||
| 358 | if ( '.ISGOODVAR('item0,selected').' ) { | ||||||||||||||||||||||||||||
| 359 | for (ntried=0,item=item0; $ignore(M=>item) && ntried < $SIZE(M); ntried++) { | ||||||||||||||||||||||||||||
| 360 | item = $trynext(M=>item); | ||||||||||||||||||||||||||||
| 361 | if ( '.ISBADVAR('item,trynext').' ) { break; } | ||||||||||||||||||||||||||||
| 362 | item %= $SIZE(M); | ||||||||||||||||||||||||||||
| 363 | if (item < 0) item += $SIZE(M); | ||||||||||||||||||||||||||||
| 364 | if (item==item0) break; /*-- loop detected --*/ | ||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||
| 366 | if ( '.ISGOODVAR(item,selected).' ) { | ||||||||||||||||||||||||||||
| 367 | $ignore(M=>item) = 1; | ||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||
| 369 | } else { item = item0; } | ||||||||||||||||||||||||||||
| 370 | $unique_selected() = item; | ||||||||||||||||||||||||||||
| 371 | %} | ||||||||||||||||||||||||||||
| 372 | %} | ||||||||||||||||||||||||||||
| 373 | ', | ||||||||||||||||||||||||||||
| 374 | Doc=> | ||||||||||||||||||||||||||||
| 375 | q( | ||||||||||||||||||||||||||||
| 376 | Remove repetitions from a vector of selected items $selected() while retaining vector length. | ||||||||||||||||||||||||||||
| 377 | $selected() should have values in the range [0..($M-1)], and it must be the case | ||||||||||||||||||||||||||||
| 378 | that $S <= $M. | ||||||||||||||||||||||||||||
| 379 | The vector $trynext() is used to (iteratively) map a non-unique item to the "next-best" item, | ||||||||||||||||||||||||||||
| 380 | and are implicitly interpreted modulo $M. | ||||||||||||||||||||||||||||
| 381 | The temporary $ignore is used to record which items have already appeared. | ||||||||||||||||||||||||||||
| 382 | May be run in-place on $selected(). | ||||||||||||||||||||||||||||
| 383 | Generally, $trynext() should be something like 1+sequence($M). | ||||||||||||||||||||||||||||
| 384 | ), | ||||||||||||||||||||||||||||
| 385 | ); | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | ##====================================================================== | ||||||||||||||||||||||||||||
| 389 | ## bits <-> integer type conversions | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | pp_addpm(<<'EOPM'); | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | =pod | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | =head1 Gene Encoding and Decoding | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | =cut | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | EOPM | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 402 | ## int->bits: tobits (safe) | ||||||||||||||||||||||||||||
| 403 | pp_add_exported('','tobits'); | ||||||||||||||||||||||||||||
| 404 | pp_addpm(<<'EOPM'); | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | =pod | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | =head2 tobits | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | =for sig | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | Signature: (ints(); [o]bits(B)) | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | Extract individual bits from integer type pdls. | ||||||||||||||||||||||||||||
| 415 | Output pdl will be created with appropriate dimensions if unspecified. | ||||||||||||||||||||||||||||
| 416 | Serious waste of memory, since PDL does not have a 'bit' type. | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | =cut | ||||||||||||||||||||||||||||
| 419 | 0 | ▶ | |||||||||||||||||||||||||||
|
Subroutine
tobits: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 420 | 0 | sub tobits { | |||||||||||||||||||||||||||
| 421 | 0 | ▶ | my ($ints,$bits) = @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 422 | 0 | $bits = zeroes($ints->type,8*PDL::howbig($ints->type),$ints->dims) if (!defined($bits)); | |||||||||||||||||||||||||||
| 423 | 0 | _tobits($ints,$bits); | |||||||||||||||||||||||||||
| 424 | return $bits; | ||||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | EOPM | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 430 | ## int->bits: _tobits (requires output pdl) | ||||||||||||||||||||||||||||
| 431 | require PDL::Types; | ||||||||||||||||||||||||||||
| 432 | my @pdlpp_integer_typechars = grep {/^(?:[BSULQN]|LL|US)$/} map {$_->{ppsym}} values %PDL::Types::typehash; | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | pp_add_exported('','_tobits'); | ||||||||||||||||||||||||||||
| 435 | pp_def | ||||||||||||||||||||||||||||
| 436 | ('_tobits', | ||||||||||||||||||||||||||||
| 437 | Pars => 'a(); [o]bits(B)', | ||||||||||||||||||||||||||||
| 438 | GenericTypes => \@pdlpp_integer_typechars, | ||||||||||||||||||||||||||||
| 439 | Code=> | ||||||||||||||||||||||||||||
| 440 | (' | ||||||||||||||||||||||||||||
| 441 | int i; | ||||||||||||||||||||||||||||
| 442 | int nbits = 8*sizeof($GENERIC(a)); | ||||||||||||||||||||||||||||
| 443 | if (nbits > $SIZE(B)) nbits = $SIZE(B); | ||||||||||||||||||||||||||||
| 444 | threadloop %{ | ||||||||||||||||||||||||||||
| 445 | for (i=0; i < nbits; i++) { | ||||||||||||||||||||||||||||
| 446 | $bits(B=>i) = $a() & (1<<i); | ||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||
| 448 | %} | ||||||||||||||||||||||||||||
| 449 | '), | ||||||||||||||||||||||||||||
| 450 | Doc=> | ||||||||||||||||||||||||||||
| 451 | ('(Low-level method) | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | Extract individual bits from integer type pdls. | ||||||||||||||||||||||||||||
| 454 | Output pdl $bits() must be specified! | ||||||||||||||||||||||||||||
| 455 | '), | ||||||||||||||||||||||||||||
| 456 | ); | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 459 | ## bits->int: frombits | ||||||||||||||||||||||||||||
| 460 | pp_add_exported('','frombits'); | ||||||||||||||||||||||||||||
| 461 | pp_def | ||||||||||||||||||||||||||||
| 462 | ('frombits', | ||||||||||||||||||||||||||||
| 463 | Pars => 'bits(B); [o]a()', | ||||||||||||||||||||||||||||
| 464 | GenericTypes => \@pdlpp_integer_typechars, | ||||||||||||||||||||||||||||
| 465 | Code=> | ||||||||||||||||||||||||||||
| 466 | (' | ||||||||||||||||||||||||||||
| 467 | int i; | ||||||||||||||||||||||||||||
| 468 | int nbits = 8*sizeof($GENERIC(a)); | ||||||||||||||||||||||||||||
| 469 | $GENERIC(a) val; | ||||||||||||||||||||||||||||
| 470 | if (nbits > $SIZE(B)) nbits = $SIZE(B); | ||||||||||||||||||||||||||||
| 471 | threadloop %{ | ||||||||||||||||||||||||||||
| 472 | val = 0; | ||||||||||||||||||||||||||||
| 473 | for (i=0; i < nbits; i++) { | ||||||||||||||||||||||||||||
| 474 | if ($bits(B=>i)) { val |= (1<<i); } | ||||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||
| 476 | $a() = val; | ||||||||||||||||||||||||||||
| 477 | %} | ||||||||||||||||||||||||||||
| 478 | '), | ||||||||||||||||||||||||||||
| 479 | Doc=> | ||||||||||||||||||||||||||||
| 480 | ('Compress expanded bit-pdls to integer types. | ||||||||||||||||||||||||||||
| 481 | '), | ||||||||||||||||||||||||||||
| 482 | ); | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | ##====================================================================== | ||||||||||||||||||||||||||||
| 487 | ## Mutation | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | pp_addpm(<<'EOPM'); | ||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | =pod | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | =head1 Mutation | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | =cut | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | EOPM | ||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 500 | ## mutate_bool() : logical (boolean) mutation | ||||||||||||||||||||||||||||
| 501 | pp_add_exported('','mutate_bool'); | ||||||||||||||||||||||||||||
| 502 | pp_def | ||||||||||||||||||||||||||||
| 503 | ('mutate_bool', | ||||||||||||||||||||||||||||
| 504 | Pars => 'genes(G); float+ rate(G); [o]mutated(G)', | ||||||||||||||||||||||||||||
| 505 | Inplace=>['genes'], | ||||||||||||||||||||||||||||
| 506 | Code => | ||||||||||||||||||||||||||||
| 507 | (' | ||||||||||||||||||||||||||||
| 508 | loop(G) %{ | ||||||||||||||||||||||||||||
| 509 | if (GA_RAND01() < $rate()) { | ||||||||||||||||||||||||||||
| 510 | $mutated() = !$genes(); | ||||||||||||||||||||||||||||
| 511 | } else { | ||||||||||||||||||||||||||||
| 512 | $mutated() = $genes(); | ||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||
| 514 | %} | ||||||||||||||||||||||||||||
| 515 | '), | ||||||||||||||||||||||||||||
| 516 | Doc => | ||||||||||||||||||||||||||||
| 517 | ('Mutate binary-valued (boolean) genes.'), | ||||||||||||||||||||||||||||
| 518 | ); | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 522 | ## mutate_range() : mutation of interval-data genes | ||||||||||||||||||||||||||||
| 523 | pp_def | ||||||||||||||||||||||||||||
| 524 | ('mutate_range', | ||||||||||||||||||||||||||||
| 525 | Pars => 'genes(G); float+ rate(G); min(G); max(G); [o]mutated(G)', | ||||||||||||||||||||||||||||
| 526 | Inplace=>['genes'], | ||||||||||||||||||||||||||||
| 527 | Code => | ||||||||||||||||||||||||||||
| 528 | (' | ||||||||||||||||||||||||||||
| 529 | loop(G) %{ | ||||||||||||||||||||||||||||
| 530 | if (GA_RAND01() < $rate()) { | ||||||||||||||||||||||||||||
| 531 | $mutated() = $min() + GA_RAND01()*($max()-$min()); | ||||||||||||||||||||||||||||
| 532 | } else { | ||||||||||||||||||||||||||||
| 533 | $mutated() = $genes(); | ||||||||||||||||||||||||||||
| 534 | } | ||||||||||||||||||||||||||||
| 535 | %} | ||||||||||||||||||||||||||||
| 536 | '), | ||||||||||||||||||||||||||||
| 537 | Doc => | ||||||||||||||||||||||||||||
| 538 | ('Mutate genes in the range [$min,$max\(.'), | ||||||||||||||||||||||||||||
| 539 | ); | ||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 542 | ## mutate_addrange() : additive mutation of scalar genes | ||||||||||||||||||||||||||||
| 543 | pp_def | ||||||||||||||||||||||||||||
| 544 | ('mutate_addrange', | ||||||||||||||||||||||||||||
| 545 | Pars => 'genes(G); float+ rate(G); min(G); max(G); [o]mutated(G)', | ||||||||||||||||||||||||||||
| 546 | Inplace=>['genes'], | ||||||||||||||||||||||||||||
| 547 | Code => | ||||||||||||||||||||||||||||
| 548 | (' | ||||||||||||||||||||||||||||
| 549 | loop(G) %{ | ||||||||||||||||||||||||||||
| 550 | if (GA_RAND01() < $rate()) { | ||||||||||||||||||||||||||||
| 551 | $mutated() = $genes() + $min() + GA_RAND01()*($max()-$min()); | ||||||||||||||||||||||||||||
| 552 | } else { | ||||||||||||||||||||||||||||
| 553 | $mutated() = $genes(); | ||||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||
| 555 | %} | ||||||||||||||||||||||||||||
| 556 | '), | ||||||||||||||||||||||||||||
| 557 | Doc => | ||||||||||||||||||||||||||||
| 558 | ('Mutate genes by adding values in the range [$min,$max\(.'), | ||||||||||||||||||||||||||||
| 559 | ); | ||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||
| 563 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 564 | ## mutate_bits | ||||||||||||||||||||||||||||
| 565 | pp_add_exported('','mutate_bits'); | ||||||||||||||||||||||||||||
| 566 | pp_addpm(<<'EOPM'); | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | =pod | ||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | =head2 mutate_bits | ||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | =for sig | ||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | Signature: (genes(G); rate(); [o]mutated(G)) | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | Mutate traditional bit-string genes. | ||||||||||||||||||||||||||||
| 577 | Calls mutate_bool(), tobits(), frombits(). | ||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||
| 579 | =cut | ||||||||||||||||||||||||||||
| 580 | 0 | ▶ | |||||||||||||||||||||||||||
|
Subroutine
mutate_bits: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 581 | sub mutate_bits { | ||||||||||||||||||||||||||||
| 582 | #my ($pop,$rate,$dst) = @_; | ||||||||||||||||||||||||||||
| 583 | 0 | #return $pop->tobits->inplace->mutate_bool($rate)->frombits(defined($dst) ? $dst : qw()); | |||||||||||||||||||||||||||
| 584 | return $_[0]->tobits->inplace->mutate_bool($_[1])->frombits(@_[2..$#_]); | ||||||||||||||||||||||||||||
| 585 | } | ||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | EOPM | ||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 590 | ## _mutate_bits() : bitwise mutation (integer types only!) | ||||||||||||||||||||||||||||
| 591 | pp_def | ||||||||||||||||||||||||||||
| 592 | ('_mutate_bits', | ||||||||||||||||||||||||||||
| 593 | Pars => 'genes(G); float+ rate(G); [o]mutated(G)', | ||||||||||||||||||||||||||||
| 594 | GenericTypes => \@pdlpp_integer_typechars, | ||||||||||||||||||||||||||||
| 595 | Inplace=>['genes'], | ||||||||||||||||||||||||||||
| 596 | Code => | ||||||||||||||||||||||||||||
| 597 | (' | ||||||||||||||||||||||||||||
| 598 | loop(G) %{ | ||||||||||||||||||||||||||||
| 599 | $GENERIC(genes) gene = $genes(); | ||||||||||||||||||||||||||||
| 600 | $GENERIC(rate) mrate = $rate(); | ||||||||||||||||||||||||||||
| 601 | int i; | ||||||||||||||||||||||||||||
| 602 | for (i=0; i < 8*sizeof($GENERIC(genes)); i++) { | ||||||||||||||||||||||||||||
| 603 | int mask = 1<<i; | ||||||||||||||||||||||||||||
| 604 | if (GA_RAND01() < mrate) { | ||||||||||||||||||||||||||||
| 605 | if (gene & mask) { gene &= ~mask; } | ||||||||||||||||||||||||||||
| 606 | else { gene |= mask; } | ||||||||||||||||||||||||||||
| 607 | } | ||||||||||||||||||||||||||||
| 608 | } | ||||||||||||||||||||||||||||
| 609 | $mutated() = gene; | ||||||||||||||||||||||||||||
| 610 | %} | ||||||||||||||||||||||||||||
| 611 | '), | ||||||||||||||||||||||||||||
| 612 | Doc => | ||||||||||||||||||||||||||||
| 613 | ('(Low-level method) | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | Mutate traditional bit-string genes. | ||||||||||||||||||||||||||||
| 616 | This should be equivalent to mutate_bits(), but appears to involve | ||||||||||||||||||||||||||||
| 617 | less overhead (faster for many calls). | ||||||||||||||||||||||||||||
| 618 | '), | ||||||||||||||||||||||||||||
| 619 | ); | ||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||
| 623 | ##====================================================================== | ||||||||||||||||||||||||||||
| 624 | ## Crossover: low-level | ||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||
| 626 | pp_addpm(<<'EOPM'); | ||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | =pod | ||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | =head1 Crossover | ||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||
| 632 | =cut | ||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | EOPM | ||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||
| 637 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 638 | ## _xover1() : single-point crossover | ||||||||||||||||||||||||||||
| 639 | pp_def | ||||||||||||||||||||||||||||
| 640 | ('_xover1', | ||||||||||||||||||||||||||||
| 641 | Pars => "mom(G); dad(G); $INDX xpoint(); [o]kid(G)", | ||||||||||||||||||||||||||||
| 642 | Inplace => ['mom'], | ||||||||||||||||||||||||||||
| 643 | Code => | ||||||||||||||||||||||||||||
| 644 | (' | ||||||||||||||||||||||||||||
| 645 | PDL_Indx xat = $xpoint(); | ||||||||||||||||||||||||||||
| 646 | loop(G) %{ | ||||||||||||||||||||||||||||
| 647 | if (G < xat) { | ||||||||||||||||||||||||||||
| 648 | $kid() = $mom(); | ||||||||||||||||||||||||||||
| 649 | } else { | ||||||||||||||||||||||||||||
| 650 | $kid() = $dad(); | ||||||||||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||||||||
| 652 | %} | ||||||||||||||||||||||||||||
| 653 | '), | ||||||||||||||||||||||||||||
| 654 | Doc => | ||||||||||||||||||||||||||||
| 655 | ('(Low-level method) | ||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||
| 657 | Single-point crossover. | ||||||||||||||||||||||||||||
| 658 | $kid() is computed by single-point crossover of $mom() (initial subsequence) | ||||||||||||||||||||||||||||
| 659 | and $dad() (final subsequence). For symmetric crossover (two offspring per crossing), | ||||||||||||||||||||||||||||
| 660 | call this method twice: | ||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||
| 662 | $kid1 = _xover1($mom, $dad, $points); | ||||||||||||||||||||||||||||
| 663 | $kid2 = _xover1($dad, $mom, $points); | ||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||
| 665 | '), | ||||||||||||||||||||||||||||
| 666 | ); | ||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||
| 670 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 671 | ## _xover2() : dual-point crossover | ||||||||||||||||||||||||||||
| 672 | pp_def | ||||||||||||||||||||||||||||
| 673 | ('_xover2', | ||||||||||||||||||||||||||||
| 674 | Pars => "mom(G); dad(G); $INDX xstart(); int xend(); [o]kid(G)", | ||||||||||||||||||||||||||||
| 675 | Inplace => ['mom'], | ||||||||||||||||||||||||||||
| 676 | Code => | ||||||||||||||||||||||||||||
| 677 | (' | ||||||||||||||||||||||||||||
| 678 | PDL_Indx xstartat = $xstart(); | ||||||||||||||||||||||||||||
| 679 | PDL_Indx xendat = $xend(); | ||||||||||||||||||||||||||||
| 680 | loop(G) %{ | ||||||||||||||||||||||||||||
| 681 | if (G < xstartat || G >= xendat) { | ||||||||||||||||||||||||||||
| 682 | $kid() = $mom(); | ||||||||||||||||||||||||||||
| 683 | } else { | ||||||||||||||||||||||||||||
| 684 | $kid() = $dad(); | ||||||||||||||||||||||||||||
| 685 | } | ||||||||||||||||||||||||||||
| 686 | %} | ||||||||||||||||||||||||||||
| 687 | '), | ||||||||||||||||||||||||||||
| 688 | Doc => | ||||||||||||||||||||||||||||
| 689 | ('(Low-level method) | ||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||
| 691 | Dual-point crossover. | ||||||||||||||||||||||||||||
| 692 | $kid() is computed by dual-point crossover of $mom() (initial and final subsequences) | ||||||||||||||||||||||||||||
| 693 | and $dad() (internal subsequence). For symmetric crossover (two offspring per crossing), | ||||||||||||||||||||||||||||
| 694 | call this method twice: | ||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||
| 696 | $kid1 = _xover2($mom, $dad, $points1, $points2); | ||||||||||||||||||||||||||||
| 697 | $kid2 = _xover2($dad, $mom, $points1, $points2); | ||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||
| 699 | '), | ||||||||||||||||||||||||||||
| 700 | ); | ||||||||||||||||||||||||||||
| 701 | |||||||||||||||||||||||||||||
| 702 | ##====================================================================== | ||||||||||||||||||||||||||||
| 703 | ## Crossover: high-level | ||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||
| 705 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 706 | ## xover1 | ||||||||||||||||||||||||||||
| 707 | pp_add_exported('','xover1'); | ||||||||||||||||||||||||||||
| 708 | pp_addpm(<<'EOPM'); | ||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||
| 710 | =pod | ||||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||
| 712 | =head2 xover1 | ||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||
| 714 | =for sig | ||||||||||||||||||||||||||||
| 715 | |||||||||||||||||||||||||||||
| 716 | Signature: (mom(G); dad(G); float+ rate(); [o]kid(G)) | ||||||||||||||||||||||||||||
| 717 | |||||||||||||||||||||||||||||
| 718 | Random single-point crossover. | ||||||||||||||||||||||||||||
| 719 | Calls _xover1(). | ||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||
| 721 | =cut | ||||||||||||||||||||||||||||
| 722 | 0 | ▶ | |||||||||||||||||||||||||||
|
Subroutine
xover1: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 723 | 0 | sub xover1 { | |||||||||||||||||||||||||||
| 724 | 0 | my ($mom, $dad, $rate, $kid) = @_; | |||||||||||||||||||||||||||
| 725 | 0 | ▶ | my $xwhich = (PDL->random($mom->dim(1)) < $rate)->which; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 726 | 0 | ▶ | if ($xwhich->isempty) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 727 | return ($mom->is_inplace | ||||||||||||||||||||||||||||
| 728 | ? $mom | ||||||||||||||||||||||||||||
| 729 | : (defined($kid) | ||||||||||||||||||||||||||||
| 730 | ? ($kid .= $mom) | ||||||||||||||||||||||||||||
| 731 | : ($kid = pdl($mom)))); | ||||||||||||||||||||||||||||
| 732 | 0 | } | |||||||||||||||||||||||||||
| 733 | 0 | my $xpoint = PDL->zeroes(ga_indx(),$mom->dim(1)) + $mom->dim(0); | |||||||||||||||||||||||||||
| 734 | 0 | ▶ | $xpoint->index($xwhich) .= PDL->random($xwhich->nelem)*($mom->dim(0)-1)+1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 735 | return _xover1($mom,$dad, $xpoint, (defined($kid) ? $kid : qw())); | ||||||||||||||||||||||||||||
| 736 | } | ||||||||||||||||||||||||||||
| 737 | |||||||||||||||||||||||||||||
| 738 | EOPM | ||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||
| 741 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 742 | ## xover2 | ||||||||||||||||||||||||||||
| 743 | pp_add_exported('','xover2'); | ||||||||||||||||||||||||||||
| 744 | pp_addpm(<<'EOPM'); | ||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||
| 746 | =pod | ||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||
| 748 | =head2 xover2 | ||||||||||||||||||||||||||||
| 749 | |||||||||||||||||||||||||||||
| 750 | =for sig | ||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||
| 752 | Signature: (mom(G); dad(G); float+ rate(); [o]kid(G)) | ||||||||||||||||||||||||||||
| 753 | |||||||||||||||||||||||||||||
| 754 | Random dial-point crossover. | ||||||||||||||||||||||||||||
| 755 | Calls _xover2(). | ||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||
| 757 | =cut | ||||||||||||||||||||||||||||
| 758 | 0 | ▶ | |||||||||||||||||||||||||||
|
Subroutine
xover2: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 759 | 0 | sub xover2 { | |||||||||||||||||||||||||||
| 760 | 0 | my ($mom, $dad, $rate, $kid) = @_; | |||||||||||||||||||||||||||
| 761 | 0 | ▶ | my $xwhich = (PDL->random($mom->dim(1)) < $rate)->which; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 762 | 0 | ▶ | if ($xwhich->isempty) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 763 | return ($mom->is_inplace | ||||||||||||||||||||||||||||
| 764 | ? $mom | ||||||||||||||||||||||||||||
| 765 | : (defined($kid) | ||||||||||||||||||||||||||||
| 766 | ? ($kid .= $mom) | ||||||||||||||||||||||||||||
| 767 | : ($kid = pdl($mom)))); | ||||||||||||||||||||||||||||
| 768 | 0 | } | |||||||||||||||||||||||||||
| 769 | 0 | my $xpoint1 = PDL->zeroes(ga_indx(),$mom->dim(1)) + $mom->dim(0); | |||||||||||||||||||||||||||
| 770 | 0 | $xpoint1->index($xwhich) .= PDL->random($xwhich->nelem)*($mom->dim(0)-1)+1; | |||||||||||||||||||||||||||
| 771 | 0 | my $xpoint2 = pdl($xpoint1); | |||||||||||||||||||||||||||
| 772 | 0 | ▶ | $xpoint2->index($xwhich) += 1+PDL->random($xwhich->nelem)*($mom->dim(0)-$xpoint1->index($xwhich)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 773 | return _xover2($mom,$dad, $xpoint1, $xpoint2, (defined($kid) ? $kid : qw())); | ||||||||||||||||||||||||||||
| 774 | } | ||||||||||||||||||||||||||||
| 775 | |||||||||||||||||||||||||||||
| 776 | EOPM | ||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||
| 779 | ##====================================================================== | ||||||||||||||||||||||||||||
| 780 | ## Footer Administrivia | ||||||||||||||||||||||||||||
| 781 | ##====================================================================== | ||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||
| 783 | ##------------------------------------------------------ | ||||||||||||||||||||||||||||
| 784 | ## pm additions | ||||||||||||||||||||||||||||
| 785 | pp_addpm(<<'EOPM'); | ||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||
| 787 | ##--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 788 | =pod | ||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||
| 790 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||
| 792 | Perl by Larry Wall. | ||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||
| 794 | PDL by Karl Glazebrook, Tuomas J. Lukka, Christian Soeller, and others. | ||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||
| 796 | =cut | ||||||||||||||||||||||||||||
| 797 | |||||||||||||||||||||||||||||
| 798 | ##---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 799 | =pod | ||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||
| 801 | =head1 KNOWN BUGS | ||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||
| 803 | Probably many. | ||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||
| 805 | =cut | ||||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||
| 808 | ##--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 809 | =pod | ||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||
| 811 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||
| 813 | Bryan Jurish E<lt>moocow@cpan.org<gt> | ||||||||||||||||||||||||||||
| 814 | |||||||||||||||||||||||||||||
| 815 | =head2 Copyright Policy | ||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||
| 817 | Copyright (C) 2006-2007, Bryan Jurish. All rights reserved. | ||||||||||||||||||||||||||||
| 818 | |||||||||||||||||||||||||||||
| 819 | This package is free software, and entirely without warranty. | ||||||||||||||||||||||||||||
| 820 | You may redistribute it and/or modify it under the same terms | ||||||||||||||||||||||||||||
| 821 | as Perl itself. | ||||||||||||||||||||||||||||
| 822 | |||||||||||||||||||||||||||||
| 823 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 824 | |||||||||||||||||||||||||||||
| 825 | perl(1), PDL(3perl). | ||||||||||||||||||||||||||||
| 826 | |||||||||||||||||||||||||||||
| 827 | =cut | ||||||||||||||||||||||||||||
| 828 | |||||||||||||||||||||||||||||
| 829 | EOPM | ||||||||||||||||||||||||||||
| 830 | |||||||||||||||||||||||||||||
| 831 | |||||||||||||||||||||||||||||
| 832 | # Always make sure that you finish your PP declarations with | ||||||||||||||||||||||||||||
| 833 | # pp_done | ||||||||||||||||||||||||||||
| 834 | pp_done(); | ||||||||||||||||||||||||||||
| 835 | ##---------------------------------------------------------------------- | ||||||||||||||||||||||||||||