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