本文整理汇总了Python中FunctionSlot.FunctionSlot.isEmpty方法的典型用法代码示例。如果您正苦于以下问题:Python FunctionSlot.isEmpty方法的具体用法?Python FunctionSlot.isEmpty怎么用?Python FunctionSlot.isEmpty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FunctionSlot.FunctionSlot
的用法示例。
在下文中一共展示了FunctionSlot.isEmpty方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MPIMigrator
# 需要导入模块: from FunctionSlot import FunctionSlot [as 别名]
# 或者: from FunctionSlot.FunctionSlot import isEmpty [as 别名]
#.........这里部分代码省略.........
self.nMigrationRate = generations
def getMigrationRate(self):
""" Return the the generation frequency supposed to migrate
and receive individuals
:rtype: the number of generations
"""
return self.nMigrationRate
def setGAEngine(self, ga_engine):
""" Sets the GA Engine handler """
self.GAEngine = ga_engine
def start(self):
""" Initializes the migration scheme """
pass
def stop(self):
""" Stops the migration engine """
pass
def getGroupName(self):
""" Gets the group name
.. note:: all islands of evolution which are supposed to exchange
individuals, must have the same group name.
"""
return self.groupName
def setGroupName(self, name):
""" Sets the group name
:param name: the group name
.. note:: all islands of evolution which are supposed to exchange
individuals, must have the same group name.
"""
self.groupName = name
def select(self):
""" Pickes an individual from population using specific selection method
:rtype: an individual object
"""
if self.selector.isEmpty():
return self.GAEngine.select(popID=self.GAEngine.currentGeneration)
else:
for it in self.selector.applyFunctions(self.GAEngine.internalPop,
popID=self.GAEngine.currentGeneration):
return it
def selectPool(self, num_individuals):
""" Select num_individuals number of individuals and return a pool
:param num_individuals: the number of individuals to select
:rtype: list with individuals
"""
pool = [self.select() for _ in xrange(num_individuals)]
return pool
def gather_bests(self):
'''
Collect all the best individuals from the various populations. The
result is stored in process 0
'''
best_guy = self.best_selector(self.GAEngine.internalPop,
popID=self.GAEngine.currentGeneration)
self.all_stars = self.comm.gather(sendobj = best_guy, root = 0)
def exchange(self):
""" This is the main method, is where the individuals
are exchanged """
if not self.isReady():
return
pool_to_send = self.selectPool(self.getNumIndividuals())
pool_received = self.comm.sendrecv(sendobj=pool_to_send,
dest=self.dest,
sendtag=0,
recvobj=None,
source=self.source,
recvtag=0)
population = self.GAEngine.getPopulation()
pool = pool_received
for i in xrange(self.getNumReplacement()):
if len(pool) <= 0:
break
choice = rand_choice(pool)
pool.remove(choice)
# replace the worst
population[len(population)-1-i] = choice
self.gather_bests()
示例2: __init__
# 需要导入模块: from FunctionSlot import FunctionSlot [as 别名]
# 或者: from FunctionSlot.FunctionSlot import isEmpty [as 别名]
#.........这里部分代码省略.........
def setNumReplacement(self, num_individuals):
""" Return the number of individuals that will be
replaced in the migration process
:param num_individuals: the number of individuals to be replaced
"""
self.nReplacement = num_individuals
def getNumIndividuals(self):
""" Return the number of individuals that will migrate
:rtype: the number of individuals to be replaced
"""
return self.nIndividuals
def setNumIndividuals(self, num_individuals):
""" Set the number of individuals that will migrate
:param num_individuals: the number of individuals
"""
self.nIndividuals = num_individuals
def setMigrationRate(self, generations):
""" Sets the generation frequency supposed to migrate
and receive individuals.
:param generations: the number of generations
"""
self.nMigrationRate = generations
def getMigrationRate(self):
""" Return the the generation frequency supposed to migrate
and receive individuals
:rtype: the number of generations
"""
return self.nMigrationRate
def setGAEngine(self, ga_engine):
""" Sets the GA Engine handler """
self.GAEngine = ga_engine
def start(self):
""" Initializes the migration scheme """
pass
def stop(self):
""" Stops the migration engine """
pass
def getGroupName(self):
""" Gets the group name
.. note:: all islands of evolution which are supposed to exchange
individuals, must have the same group name.
"""
return self.groupName
def setGroupName(self, name):
""" Sets the group name
:param name: the group name
.. note:: all islands of evolution which are supposed to exchange
individuals, must have the same group name.
"""
self.groupName = name
def setMyself(self, host, port):
""" Which interface you will use to send/receive data
:param host: your hostname
:param port: your port
"""
self.myself = (host, port)
def select(self):
""" Pickes an individual from population using specific selection method
:rtype: an individual object
"""
if self.selector.isEmpty():
return self.GAEngine.select(popID=self.GAEngine.currentGeneration)
else:
for it in self.selector.applyFunctions(self.GAEngine.internalPop, popID=self.GAEngine.currentGeneration):
return it
def selectPool(self, num_individuals):
""" Select num_individuals number of individuals and return a pool
:param num_individuals: the number of individuals to select
:rtype: list with individuals
"""
pool = [self.select() for i in xrange(num_individuals)]
return pool
def exchange(self):
""" Exchange individuals """
pass
示例3: your_func
# 需要导入模块: from FunctionSlot import FunctionSlot [as 别名]
# 或者: from FunctionSlot.FunctionSlot import isEmpty [as 别名]
#.........这里部分代码省略.........
self.internalPop.create(minimax=self.minimax)
self.internalPop.initialize(ga_engine=self)
logging.debug("The GA Engine was initialized !")
def getPopulation(self):
""" Return the internal population of GA Engine
:rtype: the population (:class:`GPopulation.GPopulation`)
"""
return self.internalPop
def getStatistics(self):
""" Gets the Statistics class instance of current generation
:rtype: the statistics instance (:class:`Statistics.Statistics`)
"""
return self.internalPop.getStatistics()
def step(self):
""" Just do one step in evolution, one generation """
genomeMom = None
genomeDad = None
newPop = GPopulation(self.internalPop)
logging.debug("Population was cloned.")
size_iterate = len(self.internalPop)
# Odd population size
if size_iterate % 2 != 0: size_iterate -= 1
crossover_empty = self.select(popID=self.currentGeneration).crossover.isEmpty()
for i in xrange(0, size_iterate, 2):
genomeMom = self.select(popID=self.currentGeneration)
genomeDad = self.select(popID=self.currentGeneration)
if not crossover_empty and self.pCrossover >= 1.0:
for it in genomeMom.crossover.applyFunctions(mom=genomeMom, dad=genomeDad, count=2):
(sister, brother) = it
else:
if not crossover_empty and Util.randomFlipCoin(self.pCrossover):
for it in genomeMom.crossover.applyFunctions(mom=genomeMom, dad=genomeDad, count=2):
(sister, brother) = it
else:
sister = genomeMom.clone()
brother = genomeDad.clone()
sister.mutate(pmut=self.pMutation, ga_engine=self)
brother.mutate(pmut=self.pMutation, ga_engine=self)
newPop.internalPop.append(sister)
newPop.internalPop.append(brother)
if len(self.internalPop) % 2 != 0:
genomeMom = self.select(popID=self.currentGeneration)
genomeDad = self.select(popID=self.currentGeneration)
if Util.randomFlipCoin(self.pCrossover):
for it in genomeMom.crossover.applyFunctions(mom=genomeMom, dad=genomeDad, count=1):
(sister, brother) = it
else:
sister = random.choice([genomeMom, genomeDad])
sister = sister.clone()
示例4: SimplePSO
# 需要导入模块: from FunctionSlot import FunctionSlot [as 别名]
# 或者: from FunctionSlot.FunctionSlot import isEmpty [as 别名]
#.........这里部分代码省略.........
return (self.currentStep == self.timeSteps)
def execute(self, freq_stats=0):
""" Do all the steps until the termination criteria or time Steps achieved,
accepts the freq_stats (default is 0) to dump statistics at n-step
Example:
>>> pso_engine.evolve(freq_stats=10)
(...)
:param freq_stats: if greater than 0, the statistics will be
printed every freq_stats step.
"""
#Start time
self.time_init = time()
#Creates a new report if reportAdapter is not None.
if self.reportAdapter: self.reportAdapter.open()
#Initialize the PSO Engine
self.initialize() #Already evaluates all particles
print "Starting loop over evolutionary algorithm."
try:
while not self.constructSolution():
stopFlagCallback = False
stopFlagTerminationCriteria = False
if not self.stepCallback.isEmpty():
for it in self.stepCallback.applyFunctions(self):
stopFlagCallback = it
if not self.terminationCriteria.isEmpty():
for it in self.terminationCriteria.applyFunctions(self):
stopFlagTerminationCriteria = it
if freq_stats != 0:
if (self.currentStep % freq_stats == 0) or (self.currentStep == 1):
self.printStats()
if self.reportAdapter:
if self.currentStep % self.reportAdapter.statsGenFreq == 0:
self.dumpStatsReport()
if stopFlagTerminationCriteria:
print '\n\tExecution stopped by Termination Criteria function !\n'
break
if stopFlagCallback:
print '\n\tExecution stopped by Step Callback function!\n'
break
if self.interactiveMode:
if sys_platform[:3] == "win":
if msvcrt.kbhit():
if ord(msvcrt.getch()) == Consts.CDefESCKey:
print "Loading modules for Interactive mode...",
import pypso.Interaction
print "done!\n"
interact_banner = "## PyPSO v.%s - Interactive Mode ##\nPress CTRL-Z to quit interactive mode." % (pypso.__version__,)