def G1DBinaryStringXTwoPoint(genome, **args): """ The 1D Binary String crossover, Two Point .. warning:: You can't use this crossover method for binary strings with length of 1. """ sister = None brother = None gMom = args["mom"] gDad = args["dad"] if len(gMom) == 1: utils.raiseException( "The Binary String have one element, can't use the Two Point Crossover method !", TypeError) cuts = [prng.randint(1, len(gMom)), prng.randint(1, len(gMom))] if cuts[0] > cuts[1]: utils.listSwapElement(cuts, 0, 1) if args["count"] >= 1: sister = gMom.clone() sister.resetStats() sister[cuts[0]:cuts[1]] = gDad[cuts[0]:cuts[1]] if args["count"] == 2: brother = gDad.clone() brother.resetStats() brother[cuts[0]:cuts[1]] = gMom[cuts[0]:cuts[1]] return (sister, brother)
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 = [prng.randint(0, len(genome) + 1), prng.randint(0, len(genome) + 1)] # HERE IT SHOULD BE INCLUSIVE if cuts[0] > cuts[1]: utils.listSwapElement(cuts, 0, 1) if (cuts[1] - cuts[0]) <= 0: cuts[1] = prng.randint(cuts[0], len(genome) + 1) # HERE IT SHOULD BE INCLUSIVE if utils.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 G1DBinaryStringXTwoPoint(genome, **args): """ The 1D Binary String crossover, Two Point .. warning:: You can't use this crossover method for binary strings with length of 1. """ sister = None brother = None gMom = args["mom"] gDad = args["dad"] if len(gMom) == 1: utils.raiseException("The Binary String have one element, can't use the Two Point Crossover method !", TypeError) cuts = [prng.randint(1, len(gMom)), prng.randint(1, len(gMom))] if cuts[0] > cuts[1]: utils.listSwapElement(cuts, 0, 1) if args["count"] >= 1: sister = gMom.clone() sister.resetStats() sister[cuts[0]:cuts[1]] = gDad[cuts[0]:cuts[1]] if args["count"] == 2: brother = gDad.clone() brother.resetStats() brother[cuts[0]:cuts[1]] = gMom[cuts[0]:cuts[1]] return (sister, brother)
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 = [ prng.randint(0, len(genome) + 1), prng.randint(0, len(genome) + 1) ] # HERE IT SHOULD BE INCLUSIVE if cuts[0] > cuts[1]: utils.listSwapElement(cuts, 0, 1) if (cuts[1] - cuts[0]) <= 0: cuts[1] = prng.randint(cuts[0], len(genome) + 1) # HERE IT SHOULD BE INCLUSIVE if utils.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 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) mutations = args["pmut"] * listSize if mutations < 1.0: mutations = 0 for it in xrange(listSize): if utils.randomFlipCoin(args["pmut"]): utils.listSwapElement(genome, it, prng.randint(0, listSize)) mutations += 1 else: for it in xrange(int(round(mutations))): utils.listSwapElement(genome, prng.randint(0, listSize), prng.randint(0, listSize)) return int(mutations)
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 utils.randomFlipCoin(args["pmut"]): utils.listSwapElement(genome, it, prng.randint(0, stringLength)) mutations += 1 else: for it in xrange(int(round(mutations))): utils.listSwapElement(genome, prng.randint(0, stringLength), prng.randint(0, stringLength)) return int(mutations)