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: Util.raiseException( "The Binary String have one element, can't use the Two Point Crossover method !", TypeError) cuts = [rand_randint(1, len(gMom) - 1), rand_randint(1, len(gMom) - 1)] if cuts[0] > cuts[1]: Util.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 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: Util.raiseException("The Binary String have one element, can't use the Two Point Crossover method !", TypeError) cuts = [rand_randint(1, len(gMom)-1), rand_randint(1, len(gMom)-1)] if cuts[0] > cuts[1]: Util.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 = [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 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 mutate_subset(subset): mutations = 0 for idx in xrange(len(subset)): if Util.randomFlipCoin(args["pmut"]): Util.listSwapElement( genome, subset[idx], subset[rand_randint(0, len(subset) - 1)]) mutations += 1 return mutations
def mutate_genome(genome, **args): """ Mutations that respect gender preference, by mutating only within gender/gender preference groups. """ global m_gp_assigned_hackers global f_gp_assigned_hackers global np_assigned_hackers def indices_of_subset(subset): return [i for i, x in enumerate(genome) if hackers[x] in subset] def mutate_subset(subset): mutations = 0 for idx in xrange(len(subset)): if Util.randomFlipCoin(args["pmut"]): Util.listSwapElement( genome, subset[idx], subset[rand_randint(0, len(subset) - 1)]) mutations += 1 return mutations if args["pmut"] <= 0.0: return 0 listSize = len(genome) mutations = args["pmut"] * listSize # 1. Find subsets to do mutations in. m_gp = indices_of_subset(m_gp_assigned_hackers) f_gp = indices_of_subset(f_gp_assigned_hackers) np_ = indices_of_subset(np_assigned_hackers) # 2. Run random mutations on each subset. if mutations < 1.0: mutations = mutate_subset(m_gp) + mutate_subset( f_gp) + mutate_subset(np_) else: for _ in xrange(int(round(mutations))): a = [m_gp, f_gp, np_] len_a = np.array(map(len, a)) subset = choice(a, p=(len_a / norm(len_a, ord=1))) Util.listSwapElement(genome, randint(0, len(subset) - 1), randint(0, len(subset) - 1)) return int(mutations)
def G1DBinaryStringSetXTwoPoint(genome, **args): """The 1D Binary String Set crossover, Two Point""" sister = None brother = None gMom = args["mom"] gDad = args["dad"] # the mother will always have the smallest bitstring if len(args["mom"]) > len(args["dad"]): gMom = args["dad"] #change roles gDad = args["mom"] if len(gMom) == 1: Util.raiseException( "The Binary String have one element, can't use the Two Point Crossover method !", TypeError) #Generating random crossover points over the mother parent cutsMom = [rand_randint(1, len(gMom) - 1), rand_randint(1, len(gMom) - 1)] if cutsMom[0] > cutsMom[1]: Util.listSwapElement(cutsMom, 0, 1) # Computing the distance from the cuts to the nearest rule to the left cutsMomDistance = [ gMom.distanceFromCutToClosestRuleByLeft(cutsMom[0]), gMom.distanceFromCutToClosestRuleByLeft(cutsMom[1]) ] # Computing factible crossover points for the dad parent factibleDadCuts = gDad.getCutPointsFromDistances(cutsMomDistance[0], cutsMomDistance[1]) #picking one random cut pair for the parent from the factible cuts cutsDad = factibleDadCuts[rand_randint(0, len(factibleDadCuts) - 1)] # genome crossover if args["count"] >= 1: sister = gMom.clone() sister.resetStats() sister.substitute(cutsMom[0], cutsMom[1], gDad[cutsDad[0]:cutsDad[1]]) if args["count"] == 2: brother = gDad.clone() brother.resetStats() brother.substitute(cutsDad[0], cutsDad[1], gMom[cutsMom[0]:cutsMom[1]]) return (sister, brother)
def G1DBinaryStringSetXTwoPoint(genome, **args): """The 1D Binary String Set crossover, Two Point""" sister = None brother = None gMom = args["mom"] gDad = args["dad"] # the mother will always have the smallest bitstring if len(args["mom"]) > len(args["dad"]): gMom = args["dad"] #change roles gDad = args["mom"] if len(gMom) == 1: Util.raiseException("The Binary String have one element, can't use the Two Point Crossover method !", TypeError) #Generating random crossover points over the mother parent cutsMom = [rand_randint(1, len(gMom)-1), rand_randint(1, len(gMom)-1)] if cutsMom[0] > cutsMom[1]: Util.listSwapElement(cutsMom, 0, 1) # Computing the distance from the cuts to the nearest rule to the left cutsMomDistance = [gMom.distanceFromCutToClosestRuleByLeft(cutsMom[0]), gMom.distanceFromCutToClosestRuleByLeft(cutsMom[1])] # Computing factible crossover points for the dad parent factibleDadCuts = gDad.getCutPointsFromDistances(cutsMomDistance[0],cutsMomDistance[1]) #picking one random cut pair for the parent from the factible cuts cutsDad = factibleDadCuts[rand_randint(0,len(factibleDadCuts)-1)] # genome crossover if args["count"] >= 1: sister = gMom.clone() sister.resetStats() sister.substitute(cutsMom[0],cutsMom[1],gDad[cutsDad[0]:cutsDad[1]]) if args["count"] == 2: brother = gDad.clone() brother.resetStats() brother.substitute(cutsDad[0],cutsDad[1], gMom[cutsMom[0]:cutsMom[1]]) return (sister, brother)
def G1DBinaryStringMutatorSwap(genome, **args): """ The 1D Binary String Swap Mutator """ if args["pmut"] <= 0.0: return 0 stringLength = len(genome) #mutations = args["pmut"] * (stringLength) mutations = 0.5 * (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 G1DBinaryStringMutatorSwap(genome, **args): """ The 1D Binary String Swap Mutator """ if args["pmut"] <= 0.0: return 0 stringLength = len(genome) #mutations = args["pmut"] * (stringLength) mutations = 0.5 * (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 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 Util.randomFlipCoin(args["pmut"]): Util.listSwapElement(genome, it, rand_randint(0, listSize - 1)) mutations += 1 else: for it in xrange(int(round(mutations))): Util.listSwapElement(genome, rand_randint(0, listSize - 1), rand_randint(0, listSize - 1)) return int(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 Util.randomFlipCoin(args["pmut"]): Util.listSwapElement(genome, it, rand_randint(0, listSize - 1)) mutations += 1 else: for it in xrange(int(round(mutations))): Util.listSwapElement(genome, rand_randint(0, listSize - 1), rand_randint(0, listSize - 1)) return int(mutations)
def crossover(genome, **args): sister = None brother = None gMom = args["mom"] gDad = args["dad"] points = [ random.randrange(0, (len(gMom)) / RULESIZE), random.randrange(0, (len(gMom)) / RULESIZE) ] if points[0] > points[1]: Util.listSwapElement(points, 0, 1) remainder = [random.randrange(0, RULESIZE), random.randrange(0, RULESIZE)] if remainder[0] > remainder[1]: Util.listSwapElement(remainder, 0, 1) points2 = [ random.randrange(0, len(gDad) / RULESIZE), random.randrange(0, len(gDad) / RULESIZE) ] if points2[0] > points2[1]: Util.listSwapElement(points2, 0, 1) cut1 = points[0] * RULESIZE + remainder[0] cut2 = points[1] * RULESIZE + remainder[1] cut3 = points2[0] * RULESIZE + remainder[0] cut4 = points2[1] * RULESIZE + remainder[1] if args["count"] >= 1: sister = gMom.clone() sister.resetStats() sister[cut1:cut2] = gDad[cut3:cut4] sister.listSize = len(sister.genomeList) if args["count"] == 2: brother = gDad.clone() brother.resetStats() brother[cut3:cut4] = gMom[cut1:cut2] brother.listSize = len(brother.genomeList) return (sister, brother)
def crossover(genome, **args): sister = None brother = None gMom = args["mom"] gDad = args["dad"] points = [random.randrange(0, (len(gMom))/ RULESIZE) , random.randrange(0, (len(gMom))/RULESIZE)] if points[0] > points[1]: Util.listSwapElement(points, 0, 1) remainder = [random.randrange(0, RULESIZE), random.randrange(0, RULESIZE)] if remainder[0] > remainder[1]: Util.listSwapElement(remainder, 0, 1) points2 = [random.randrange(0, len(gDad) / RULESIZE), random.randrange(0, len(gDad)/RULESIZE)] if points2[0] > points2[1]: Util.listSwapElement(points2, 0, 1) cut1 = points[0]*RULESIZE+remainder[0] cut2 = points[1]*RULESIZE+remainder[1] cut3 = points2[0]*RULESIZE+remainder[0] cut4 = points2[1]*RULESIZE+remainder[1] if args["count"] >= 1: sister = gMom.clone() sister.resetStats() sister[cut1:cut2] = gDad[cut3:cut4] sister.listSize = len(sister.genomeList) if args["count"] == 2: brother = gDad.clone() brother.resetStats() brother[cut3:cut4] = gMom[cut1:cut2] brother.listSize = len(brother.genomeList) return (sister, brother)
def test_listSwapElement(self): _list = [1, 2, 3] Util.listSwapElement(_list, 0, 1) self.assertEqual(_list, [2, 1, 3])