/usr/share/pyshared/pyevolve/Mutators.py is in python-pyevolve 0.6~rc1+svn398+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 | """
:mod:`Mutators` -- mutation methods module
=====================================================================
In this module we have the genetic operators of mutation for each chromosome representation.
"""
import Util
from random import randint as rand_randint, gauss as rand_gauss, uniform as rand_uniform
from random import choice as rand_choice
import Consts
import GTree
#############################
## 1D Binary String ##
#############################
def G1DBinaryStringMutatorSwap(genome, **args):
""" The 1D Binary String Swap Mutator """
if args["pmut"] <= 0.0: return 0
stringLength = len(genome)
mutations = args["pmut"] * (stringLength)
if mutations < 1.0:
mutations = 0
for it in xrange(stringLength):
if Util.randomFlipCoin(args["pmut"]):
Util.listSwapElement(genome, it, rand_randint(0, stringLength-1))
mutations+=1
else:
for it in xrange(int(round(mutations))):
Util.listSwapElement(genome, rand_randint(0, stringLength-1),
rand_randint(0, stringLength-1))
return int(mutations)
def G1DBinaryStringMutatorFlip(genome, **args):
""" The classical flip mutator for binary strings """
if args["pmut"] <= 0.0: return 0
stringLength = len(genome)
mutations = args["pmut"] * (stringLength)
if mutations < 1.0:
mutations = 0
for it in xrange(stringLength):
if Util.randomFlipCoin(args["pmut"]):
if genome[it] == 0: genome[it] = 1
else: genome[it] = 0
mutations+=1
else:
for it in xrange(int(round(mutations))):
which = rand_randint(0, stringLength-1)
if genome[which] == 0: genome[which] = 1
else: genome[which] = 0
return int(mutations)
####################
## 1D List ##
####################
def G1DListMutatorSwap(genome, **args):
""" The mutator of G1DList, Swap Mutator
.. note:: this mutator is :term:`Data Type Independent`
"""
if args["pmut"] <= 0.0: return 0
listSize = len(genome) - 1
mutations = args["pmut"] * (listSize+1)
if mutations < 1.0:
mutations = 0
for it in xrange(listSize+1):
if Util.randomFlipCoin(args["pmut"]):
Util.listSwapElement(genome, it, rand_randint(0, listSize))
mutations+=1
else:
for it in xrange(int(round(mutations))):
Util.listSwapElement(genome, rand_randint(0, listSize), rand_randint(0, listSize))
return int(mutations)
def G1DListMutatorSIM(genome, **args):
""" The mutator of G1DList, Simple Inversion Mutation
.. note:: this mutator is :term:`Data Type Independent`
"""
mutations = 0
if args["pmut"] <= 0.0: return 0
cuts = [rand_randint(0, len(genome)), rand_randint(0, len(genome))]
if cuts[0] > cuts[1]:
Util.listSwapElement(cuts, 0, 1)
if (cuts[1]-cuts[0]) <= 0:
cuts[1] = rand_randint(cuts[0], len(genome))
if Util.randomFlipCoin(args["pmut"]):
part = genome[cuts[0]:cuts[1]]
if len(part) == 0: return 0
part.reverse()
genome[cuts[0]:cuts[1]] = part
mutations += 1
return mutations
def G1DListMutatorIntegerRange(genome, **args):
""" Simple integer range mutator for G1DList
Accepts the *rangemin* and *rangemax* genome parameters, both optional.
"""
if args["pmut"] <= 0.0: return 0
listSize = len(genome)
mutations = args["pmut"] * listSize
if mutations < 1.0:
mutations = 0
for it in xrange(listSize):
if Util.randomFlipCoin(args["pmut"]):
genome[it] = rand_randint(genome.getParam("rangemin", Consts.CDefRangeMin),
genome.getParam("rangemax", Consts.CDefRangeMax))
mutations += 1
else:
for it in xrange(int(round(mutations))):
which_gene = rand_randint(0, listSize-1)
genome[which_gene] = rand_randint(genome.getParam("rangemin", Consts.CDefRangeMin),
genome.getParam("rangemax", Consts.CDefRangeMax))
return int(mutations)
def G1DListMutatorRealRange(genome, **args):
""" Simple real range mutator for G1DList
Accepts the *rangemin* and *rangemax* genome parameters, both optional.
"""
if args["pmut"] <= 0.0: return 0
listSize = len(genome)
mutations = args["pmut"] * (listSize)
if mutations < 1.0:
mutations = 0
for it in xrange(listSize):
if Util.randomFlipCoin(args["pmut"]):
genome[it] = rand_uniform(genome.getParam("rangemin", Consts.CDefRangeMin),
genome.getParam("rangemax", Consts.CDefRangeMax))
mutations += 1
else:
for it in xrange(int(round(mutations))):
which_gene = rand_randint(0, listSize-1)
genome[which_gene] = rand_uniform(genome.getParam("rangemin", Consts.CDefRangeMin),
genome.getParam("rangemax", Consts.CDefRangeMax))
return int(mutations)
def G1DListMutatorIntegerGaussian(genome, **args):
""" A gaussian mutator for G1DList of Integers
Accepts the *rangemin* and *rangemax* genome parameters, both optional. Also
accepts the parameter *gauss_mu* and the *gauss_sigma* which respectively
represents the mean and the std. dev. of the random distribution.
"""
if args["pmut"] <= 0.0: return 0
listSize = len(genome)
mutations = args["pmut"] * (listSize)
mu = genome.getParam("gauss_mu")
sigma = genome.getParam("gauss_sigma")
if mu is None:
mu = Consts.CDefG1DListMutIntMU
if sigma is None:
sigma = Consts.CDefG1DListMutIntSIGMA
if mutations < 1.0:
mutations = 0
for it in xrange(listSize):
if Util.randomFlipCoin(args["pmut"]):
final_value = genome[it] + int(rand_gauss(mu, sigma))
final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax))
final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin))
genome[it] = final_value
mutations += 1
else:
for it in xrange(int(round(mutations))):
which_gene = rand_randint(0, listSize-1)
final_value = genome[which_gene] + int(rand_gauss(mu, sigma))
final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax))
final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin))
genome[which_gene] = final_value
return int(mutations)
def G1DListMutatorRealGaussian(genome, **args):
""" The mutator of G1DList, Gaussian Mutator
Accepts the *rangemin* and *rangemax* genome parameters, both optional. Also
accepts the parameter *gauss_mu* and the *gauss_sigma* which respectively
represents the mean and the std. dev. of the random distribution.
"""
if args["pmut"] <= 0.0: return 0
listSize = len(genome)
mutations = args["pmut"] * (listSize)
mu = genome.getParam("gauss_mu")
sigma = genome.getParam("gauss_sigma")
if mu is None:
mu = Consts.CDefG1DListMutRealMU
if sigma is None:
sigma = Consts.CDefG1DListMutRealSIGMA
if mutations < 1.0:
mutations = 0
for it in xrange(listSize):
if Util.randomFlipCoin(args["pmut"]):
final_value = genome[it] + rand_gauss(mu, sigma)
final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax))
final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin))
genome[it] = final_value
mutations += 1
else:
for it in xrange(int(round(mutations))):
which_gene = rand_randint(0, listSize-1)
final_value = genome[which_gene] + rand_gauss(mu, sigma)
final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax))
final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin))
genome[which_gene] = final_value
return int(mutations)
def G1DListMutatorIntegerBinary(genome, **args):
""" The mutator of G1DList, the binary mutator
This mutator will random change the 0 and 1 elements of the 1D List.
"""
if args["pmut"] <= 0.0: return 0
listSize = len(genome)
mutations = args["pmut"] * (listSize)
if mutations < 1.0:
mutations = 0
for it in xrange(listSize):
if Util.randomFlipCoin(args["pmut"]):
if genome[it] == 0: genome[it] = 1
elif genome[it] == 1: genome[it] = 0
mutations += 1
else:
for it in xrange(int(round(mutations))):
which_gene = rand_randint(0, listSize-1)
if genome[which_gene] == 0: genome[which_gene] = 1
elif genome[which_gene] == 1: genome[which_gene] = 0
return int(mutations)
def G1DListMutatorAllele(genome, **args):
""" The mutator of G1DList, Allele Mutator
To use this mutator, you must specify the *allele* genome parameter with the
:class:`GAllele.GAlleles` instance.
"""
if args["pmut"] <= 0.0: return 0
listSize = len(genome) - 1
mutations = args["pmut"] * (listSize+1)
allele = genome.getParam("allele", None)
if allele is None:
Util.raiseException("to use the G1DListMutatorAllele, you must specify the 'allele' parameter", TypeError)
if mutations < 1.0:
mutations = 0
for it in xrange(listSize+1):
if Util.randomFlipCoin(args["pmut"]):
new_val = allele[it].getRandomAllele()
genome[it] = new_val
mutations+=1
else:
for it in xrange(int(round(mutations))):
which_gene = rand_randint(0, listSize)
new_val = allele[which_gene].getRandomAllele()
genome[which_gene] = new_val
return int(mutations)
####################
## 2D List ##
####################
def G2DListMutatorSwap(genome, **args):
""" The mutator of G1DList, Swap Mutator
.. note:: this mutator is :term:`Data Type Independent`
"""
if args["pmut"] <= 0.0: return 0
height, width = genome.getSize()
elements = height * width
mutations = args["pmut"] * elements
if mutations < 1.0:
mutations = 0
for i in xrange(height):
for j in xrange(width):
if Util.randomFlipCoin(args["pmut"]):
index_b = (rand_randint(0, height-1), rand_randint(0, width-1))
Util.list2DSwapElement(genome.genomeList, (i,j), index_b)
mutations+=1
else:
for it in xrange(int(round(mutations))):
index_a = (rand_randint(0, height-1), rand_randint(0, width-1))
index_b = (rand_randint(0, height-1), rand_randint(0, width-1))
Util.list2DSwapElement(genome.genomeList, index_a, index_b)
return int(mutations)
def G2DListMutatorIntegerRange(genome, **args):
""" Simple integer range mutator for G2DList
Accepts the *rangemin* and *rangemax* genome parameters, both optional.
"""
if args["pmut"] <= 0.0: return 0
height, width = genome.getSize()
elements = height * width
mutations = args["pmut"] * elements
range_min = genome.getParam("rangemin", Consts.CDefRangeMin)
range_max = genome.getParam("rangemax", Consts.CDefRangeMax)
if mutations < 1.0:
mutations = 0
for i in xrange(genome.getHeight()):
for j in xrange(genome.getWidth()):
if Util.randomFlipCoin(args["pmut"]):
random_int = rand_randint(range_min, range_max)
genome.setItem(i, j, random_int)
mutations += 1
else:
for it in xrange(int(round(mutations))):
which_x = rand_randint(0, genome.getWidth()-1)
which_y = rand_randint(0, genome.getHeight()-1)
random_int = rand_randint(range_min, range_max)
genome.setItem(which_y, which_x, random_int)
return int(mutations)
def G2DListMutatorIntegerGaussian(genome, **args):
""" A gaussian mutator for G2DList of Integers
Accepts the *rangemin* and *rangemax* genome parameters, both optional. Also
accepts the parameter *gauss_mu* and the *gauss_sigma* which respectively
represents the mean and the std. dev. of the random distribution.
"""
if args["pmut"] <= 0.0: return 0
height, width = genome.getSize()
elements = height * width
mutations = args["pmut"] * elements
mu = genome.getParam("gauss_mu")
sigma = genome.getParam("gauss_sigma")
if mu is None:
mu = Consts.CDefG2DListMutIntMU
if sigma is None:
sigma = Consts.CDefG2DListMutIntSIGMA
if mutations < 1.0:
mutations = 0
for i in xrange(genome.getHeight()):
for j in xrange(genome.getWidth()):
if Util.randomFlipCoin(args["pmut"]):
final_value = genome[i][j] + int(rand_gauss(mu, sigma))
final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax))
final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin))
genome.setItem(i, j, final_value)
mutations += 1
else:
for it in xrange(int(round(mutations))):
which_x = rand_randint(0, genome.getWidth()-1)
which_y = rand_randint(0, genome.getHeight()-1)
final_value = genome[which_y][which_x] + int(rand_gauss(mu, sigma))
final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax))
final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin))
genome.setItem(which_y, which_x, final_value)
return int(mutations)
def G2DListMutatorAllele(genome, **args):
""" The mutator of G2DList, Allele Mutator
To use this mutator, you must specify the *allele* genome parameter with the
:class:`GAllele.GAlleles` instance.
.. warning:: the :class:`GAllele.GAlleles` instance must have the homogeneous flag enabled
"""
if args["pmut"] <= 0.0: return 0
listSize = len(genome) - 1
mutations = args["pmut"] * (listSize+1)
allele = genome.getParam("allele", None)
if allele is None:
Util.raiseException("to use the G2DListMutatorAllele, you must specify the 'allele' parameter", TypeError)
if allele.homogeneous == False:
Util.raiseException("to use the G2DListMutatorAllele, the 'allele' must be homogeneous")
if mutations < 1.0:
mutations = 0
for i in xrange(genome.getHeight()):
for j in xrange(genome.getWidht()):
if Util.randomFlipCoin(args["pmut"]):
new_val = allele[0].getRandomAllele()
genome.setItem(i, j, new_val)
mutations+=1
else:
for it in xrange(int(round(mutations))):
which_x = rand_randint(0, genome.getWidth()-1)
which_y = rand_randint(0, genome.getHeight()-1)
new_val = allele[0].getRandomAllele()
genome.setItem(which_x, which_y, new_val)
return int(mutations)
def G2DListMutatorRealGaussian(genome, **args):
""" A gaussian mutator for G2DList of Real
Accepts the *rangemin* and *rangemax* genome parameters, both optional. Also
accepts the parameter *gauss_mu* and the *gauss_sigma* which respectively
represents the mean and the std. dev. of the random distribution.
"""
if args["pmut"] <= 0.0: return 0
height, width = genome.getSize()
elements = height * width
mutations = args["pmut"] * elements
mu = genome.getParam("gauss_mu")
sigma = genome.getParam("gauss_sigma")
if mu is None:
mu = Consts.CDefG2DListMutRealMU
if sigma is None:
sigma = Consts.CDefG2DListMutRealSIGMA
if mutations < 1.0:
mutations = 0
for i in xrange(genome.getHeight()):
for j in xrange(genome.getWidth()):
if Util.randomFlipCoin(args["pmut"]):
final_value = genome[i][j] + rand_gauss(mu, sigma)
final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax))
final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin))
genome.setItem(i, j, final_value)
mutations += 1
else:
for it in xrange(int(round(mutations))):
which_x = rand_randint(0, genome.getWidth()-1)
which_y = rand_randint(0, genome.getHeight()-1)
final_value = genome[which_y][which_x] + rand_gauss(mu, sigma)
final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax))
final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin))
genome.setItem(which_y, which_x, final_value)
return int(mutations)
#############################
## 2D Binary String ##
#############################
def G2DBinaryStringMutatorSwap(genome, **args):
""" The mutator of G2DBinaryString, Swap Mutator
.. versionadded:: 0.6
The *G2DBinaryStringMutatorSwap* function
"""
if args["pmut"] <= 0.0: return 0
height, width = genome.getSize()
elements = height * width
mutations = args["pmut"] * elements
if mutations < 1.0:
mutations = 0
for i in xrange(height):
for j in xrange(width):
if Util.randomFlipCoin(args["pmut"]):
index_b = (rand_randint(0, height-1), rand_randint(0, width-1))
Util.list2DSwapElement(genome.genomeString, (i,j), index_b)
mutations+=1
else:
for it in xrange(int(round(mutations))):
index_a = (rand_randint(0, height-1), rand_randint(0, width-1))
index_b = (rand_randint(0, height-1), rand_randint(0, width-1))
Util.list2DSwapElement(genome.genomeString, index_a, index_b)
return int(mutations)
def G2DBinaryStringMutatorFlip(genome, **args):
""" A flip mutator for G2DBinaryString
.. versionadded:: 0.6
The *G2DBinaryStringMutatorFlip* function
"""
if args["pmut"] <= 0.0: return 0
height, width = genome.getSize()
elements = height * width
mutations = args["pmut"] * elements
if mutations < 1.0:
mutations = 0
for i in xrange(genome.getHeight()):
for j in xrange(genome.getWidth()):
if Util.randomFlipCoin(args["pmut"]):
if genome[i][j] == 0: genome.setItem(i, j, 1)
else: genome.setItem(i, j, 0)
mutations += 1
else:
for it in xrange(int(round(mutations))):
which_x = rand_randint(0, genome.getWidth()-1)
which_y = rand_randint(0, genome.getHeight()-1)
if genome[i][j] == 0: genome.setItem(which_y, which_x, 1)
else: genome.setItem(which_y, which_x, 0)
return int(mutations)
#################
## Tree ##
#################
def GTreeMutatorSwap(genome, **args):
""" The mutator of GTree, Swap Mutator
.. versionadded:: 0.6
The *GTreeMutatorSwap* function
"""
if args["pmut"] <= 0.0: return 0
elements = len(genome)
mutations = args["pmut"] * elements
if mutations < 1.0:
mutations = 0
for i in xrange(len(genome)):
if Util.randomFlipCoin(args["pmut"]):
mutations += 1
nodeOne = genome.getRandomNode()
nodeTwo = genome.getRandomNode()
nodeOne.swapNodeData(nodeTwo)
else:
for it in xrange(int(round(mutations))):
nodeOne = genome.getRandomNode()
nodeTwo = genome.getRandomNode()
nodeOne.swapNodeData(nodeTwo)
return int(mutations)
def GTreeMutatorIntegerRange(genome, **args):
""" The mutator of GTree, Integer Range Mutator
Accepts the *rangemin* and *rangemax* genome parameters, both optional.
.. versionadded:: 0.6
The *GTreeMutatorIntegerRange* function
"""
if args["pmut"] <= 0.0: return 0
elements = len(genome)
mutations = args["pmut"] * elements
range_min = genome.getParam("rangemin", Consts.CDefRangeMin)
range_max = genome.getParam("rangemax", Consts.CDefRangeMax)
if mutations < 1.0:
mutations = 0
for i in xrange(len(genome)):
if Util.randomFlipCoin(args["pmut"]):
mutations += 1
rand_node = genome.getRandomNode()
random_int = rand_randint(range_min, range_max)
rand_node.setData(random_int)
else:
for it in xrange(int(round(mutations))):
rand_node = genome.getRandomNode()
random_int = rand_randint(range_min, range_max)
rand_node.setData(random_int)
return int(mutations)
def GTreeMutatorRealRange(genome, **args):
""" The mutator of GTree, Real Range Mutator
Accepts the *rangemin* and *rangemax* genome parameters, both optional.
.. versionadded:: 0.6
The *GTreeMutatorRealRange* function
"""
if args["pmut"] <= 0.0: return 0
elements = len(genome)
mutations = args["pmut"] * elements
range_min = genome.getParam("rangemin", Consts.CDefRangeMin)
range_max = genome.getParam("rangemax", Consts.CDefRangeMax)
if mutations < 1.0:
mutations = 0
for i in xrange(len(genome)):
if Util.randomFlipCoin(args["pmut"]):
mutations += 1
rand_node = genome.getRandomNode()
random_real = rand_uniform(range_min, range_max)
rand_node.setData(random_real)
else:
for it in xrange(int(round(mutations))):
rand_node = genome.getRandomNode()
random_real = rand_uniform(range_min, range_max)
rand_node.setData(random_real)
return int(mutations)
def GTreeMutatorIntegerGaussian(genome, **args):
""" A gaussian mutator for GTree of Integers
Accepts the *rangemin* and *rangemax* genome parameters, both optional. Also
accepts the parameter *gauss_mu* and the *gauss_sigma* which respectively
represents the mean and the std. dev. of the random distribution.
"""
if args["pmut"] <= 0.0: return 0
elements = len(genome)
mutations = args["pmut"] * elements
mu = genome.getParam("gauss_mu", Consts.CDefG1DListMutIntMU)
sigma = genome.getParam("gauss_sigma", Consts.CDefG1DListMutIntSIGMA)
if mutations < 1.0:
mutations = 0
for i in xrange(len(genome)):
if Util.randomFlipCoin(args["pmut"]):
mutations += 1
rand_node = genome.getRandomNode()
final_value = rand_node.getData() + int(rand_gauss(mu, sigma))
final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax))
final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin))
rand_node.setData(final_value)
else:
for it in xrange(int(round(mutations))):
rand_node = genome.getRandomNode()
final_value = rand_node.getData() + int(rand_gauss(mu, sigma))
final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax))
final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin))
rand_node.setData(final_value)
return int(mutations)
def GTreeMutatorRealGaussian(genome, **args):
""" A gaussian mutator for GTree of Real numbers
Accepts the *rangemin* and *rangemax* genome parameters, both optional. Also
accepts the parameter *gauss_mu* and the *gauss_sigma* which respectively
represents the mean and the std. dev. of the random distribution.
"""
if args["pmut"] <= 0.0: return 0
elements = len(genome)
mutations = args["pmut"] * elements
mu = genome.getParam("gauss_mu", Consts.CDefG1DListMutRealMU)
sigma = genome.getParam("gauss_sigma", Consts.CDefG1DListMutRealSIGMA)
if mutations < 1.0:
mutations = 0
for i in xrange(len(genome)):
if Util.randomFlipCoin(args["pmut"]):
mutations += 1
rand_node = genome.getRandomNode()
final_value = rand_node.getData() + rand_gauss(mu, sigma)
final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax))
final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin))
rand_node.setData(final_value)
else:
for it in xrange(int(round(mutations))):
rand_node = genome.getRandomNode()
final_value = rand_node.getData() + rand_gauss(mu, sigma)
final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax))
final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin))
rand_node.setData(final_value)
return int(mutations)
###################
## Tree GP ##
###################
def GTreeGPMutatorOperation(genome, **args):
""" The mutator of GTreeGP, Operation Mutator
.. versionadded:: 0.6
The *GTreeGPMutatorOperation* function
"""
if args["pmut"] <= 0.0: return 0
elements = len(genome)
mutations = args["pmut"] * elements
ga_engine = args["ga_engine"]
gp_terminals = ga_engine.getParam("gp_terminals")
assert gp_terminals is not None
gp_function_set = ga_engine.getParam("gp_function_set")
assert gp_function_set is not None
if mutations < 1.0:
mutations = 0
for i in xrange(len(genome)):
if Util.randomFlipCoin(args["pmut"]):
mutations += 1
rand_node = genome.getRandomNode()
assert rand_node is not None
if rand_node.getType() == Consts.nodeType["TERMINAL"]:
term_operator = rand_choice(gp_terminals)
else:
op_len = gp_function_set[rand_node.getData()]
fun_candidates = []
for o, l in gp_function_set.items():
if l==op_len:
fun_candidates.append(o)
if len(fun_candidates) <= 0:
continue
term_operator = rand_choice(fun_candidates)
rand_node.setData(term_operator)
else:
for it in xrange(int(round(mutations))):
rand_node = genome.getRandomNode()
assert rand_node is not None
if rand_node.getType() == Consts.nodeType["TERMINAL"]:
term_operator = rand_choice(gp_terminals)
else:
op_len = gp_function_set[rand_node.getData()]
fun_candidates = []
for o, l in gp_function_set.items():
if l==op_len:
fun_candidates.append(o)
if len(fun_candidates) <= 0:
continue
term_operator = rand_choice(fun_candidates)
rand_node.setData(term_operator)
return int(mutations)
def GTreeGPMutatorSubtree(genome, **args):
""" The mutator of GTreeGP, Subtree Mutator
This mutator will recreate random subtree of the tree using the grow algorithm.
.. versionadded:: 0.6
The *GTreeGPMutatorSubtree* function
"""
if args["pmut"] <= 0.0: return 0
ga_engine = args["ga_engine"]
max_depth = genome.getParam("max_depth", None)
mutations = 0
if max_depth is None:
Util.raiseException("You must specify the max_depth genome parameter !", ValueError)
if max_depth < 0:
Util.raiseException("The max_depth must be >= 1, if you want to use GTreeGPMutatorSubtree crossover !", ValueError)
branch_list = genome.nodes_branch
elements = len(branch_list)
for i in xrange(elements):
node = branch_list[i]
assert node is not None
if Util.randomFlipCoin(args["pmut"]):
depth = genome.getNodeDepth(node)
mutations += 1
root_subtree = GTree.buildGTreeGPGrow(ga_engine, 0, max_depth-depth)
node_parent = node.getParent()
if node_parent is None:
genome.setRoot(root_subtree)
genome.processNodes()
return mutations
else:
root_subtree.setParent(node_parent)
node_parent.replaceChild(node, root_subtree)
genome.processNodes()
return int(mutations)
|