本文整理汇总了Python中pyevolve.Util.listSwapElement方法的典型用法代码示例。如果您正苦于以下问题:Python Util.listSwapElement方法的具体用法?Python Util.listSwapElement怎么用?Python Util.listSwapElement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyevolve.Util
的用法示例。
在下文中一共展示了Util.listSwapElement方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: G1DBinaryStringXTwoPoint
# 需要导入模块: from pyevolve import Util [as 别名]
# 或者: from pyevolve.Util import listSwapElement [as 别名]
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)
示例2: G1DListMutatorSIM
# 需要导入模块: from pyevolve import Util [as 别名]
# 或者: from pyevolve.Util import listSwapElement [as 别名]
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
示例3: G1DBinaryStringSetXTwoPoint
# 需要导入模块: from pyevolve import Util [as 别名]
# 或者: from pyevolve.Util import listSwapElement [as 别名]
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)
示例4: G1DBinaryStringMutatorSwap
# 需要导入模块: from pyevolve import Util [as 别名]
# 或者: from pyevolve.Util import listSwapElement [as 别名]
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)
示例5: G1DListMutatorSwap
# 需要导入模块: from pyevolve import Util [as 别名]
# 或者: from pyevolve.Util import listSwapElement [as 别名]
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)
示例6: crossover
# 需要导入模块: from pyevolve import Util [as 别名]
# 或者: from pyevolve.Util import listSwapElement [as 别名]
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)
示例7: test_listSwapElement
# 需要导入模块: from pyevolve import Util [as 别名]
# 或者: from pyevolve.Util import listSwapElement [as 别名]
def test_listSwapElement(self):
_list = [1, 2, 3]
Util.listSwapElement(_list, 0, 1)
self.assertEqual(_list, [2, 1, 3])