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 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 googleQueryMutator(genome, **args): """ The mutator of GoogleQueryChromosome """ print "Mutating genome", genome if args["pmut"] <= 0.0: return 0 # height, width = genome.getParam('seqlength') height = 2 width = genome.getParam('seqlength') 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 = (random.randint(0, height - 1), random.randint(0, width - 1)) list2DSwapElement(genome.genomeList, (i, j), index_b) mutations += 1 else: for it in xrange(int(round(mutations))): index_a = (random.randint(0, height - 1), random.randint(0, width - 1)) index_b = (random.randint(0, height - 1), random.randint(0, width - 1)) Util.list2DSwapElement(genome.genomeList, index_a, index_b) return int(mutations)
def test_list2DSwapElement(self): _list = [[1, 2, 3], [4, 5, 6]] Util.list2DSwapElement(_list, (0, 1), (1, 1)) self.assertEqual(_list, [[1, 5, 3], [4, 2, 6]])