本文整理汇总了Python中FunctionSlot.FunctionSlot.set方法的典型用法代码示例。如果您正苦于以下问题:Python FunctionSlot.set方法的具体用法?Python FunctionSlot.set怎么用?Python FunctionSlot.set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FunctionSlot.FunctionSlot
的用法示例。
在下文中一共展示了FunctionSlot.set方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: xrange
# 需要导入模块: from FunctionSlot import FunctionSlot [as 别名]
# 或者: from FunctionSlot.FunctionSlot import set [as 别名]
class GPopulation:
""" GPopulation Class - The container for the population
**Examples**
Get the population from the :class:`GSimpleGA.GSimpleGA` (GA Engine) instance
>>> pop = ga_engine.getPopulation()
Get the best fitness individual
>>> bestIndividual = pop.bestFitness()
Get the best raw individual
>>> bestIndividual = pop.bestRaw()
Get the statistics from the :class:`Statistics.Statistics` instance
>>> stats = pop.getStatistics()
>>> print stats["rawMax"]
10.4
Iterate, get/set individuals
>>> for ind in pop:
>>> print ind
(...)
>>> for i in xrange(len(pop)):
>>> print pop[i]
(...)
>>> pop[10] = newGenome
>>> pop[10].fitness
12.5
:param genome: the :term:`Sample genome`, or a GPopulation object, when cloning.
"""
def __init__(self, genome):
""" The GPopulation Class creator """
if isinstance(genome, GPopulation):
#Cloning a population?
self.oneSelfGenome = genome.oneSelfGenome
self.internalPop = []
self.internalPopRaw = []
self.popSize = genome.popSize
self.sortType = genome.sortType
self.sorted = False
self.minimax = genome.minimax
self.scaleMethod = genome.scaleMethod
self.allSlots = [self.scaleMethod]
self.internalParams = genome.internalParams
self.multiProcessing = genome.multiProcessing
self.statted = False
self.stats = Statistics()
self.proc_pool = genome.proc_pool
return
logging.debug("New population instance, %s class genomes.", genome.__class__.__name__)
self.oneSelfGenome = genome
self.internalPop = []
self.internalPopRaw = []
self.popSize = 0
self.proc_pool = None
self.sortType = Consts.CDefPopSortType
self.sorted = False
self.minimax = Consts.CDefPopMinimax
self.scaleMethod = FunctionSlot("Scale Method")
self.scaleMethod.set(Consts.CDefPopScale)
self.allSlots = [self.scaleMethod]
self.internalParams = {}
self.multiProcessing = (False, False)
# Statistics
self.statted = False
self.stats = Statistics()
#---------------------------------------------------------------------------------
def setMultiProcessing(self, flag=True, full_copy=False, number_of_processes=None):
""" Sets the flag to enable/disable the use of python multiprocessing module.
Use this option when you have more than one core on your CPU and when your
evaluation function is very slow.
The parameter "full_copy" defines where the individual data should be copied back
after the evaluation or not. This parameter is useful when you change the
individual in the evaluation function.
:param flag: True (default) or False
:param full_copy: True or False (default)
:param number_of_processes: None = use the default, or specify the number
.. warning:: Use this option only when your evaluation function is slow, se you
will get a good tradeoff between the process communication speed and the
parallel evaluation.
"""
#Save the parameters
old_settings = self.multiProcessing
self.multiProcessing = (flag, full_copy, number_of_processes)
#Re-initialize if anything changed.
if (old_settings != self.multiProcessing):
#.........这里部分代码省略.........
示例2: your_func
# 需要导入模块: from FunctionSlot import FunctionSlot [as 别名]
# 或者: from FunctionSlot.FunctionSlot import set [as 别名]
class GSimpleGA:
""" GA Engine Class - The Genetic Algorithm Core
Example:
>>> ga = GSimpleGA.GSimpleGA(genome)
>>> ga.selector.set(Selectors.GRouletteWheel)
>>> ga.setGenerations(120)
>>> ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria)
:param genome: the :term:`Sample Genome`
:param interactiveMode: this flag enables the Interactive Mode, the default is True
:param seed: the random seed value
.. note:: if you use the same random seed, all the runs of algorithm will be the same
"""
selector = None
""" This is the function slot for the selection method
if you want to change the default selector, you must do this: ::
ga_engine.selector.set(Selectors.GRouletteWheel) """
stepCallback = None
""" This is the :term:`step callback function` slot,
if you want to set the function, you must do this: ::
def your_func(ga_engine):
# Here you have access to the GA Engine
return False
ga_engine.stepCallback.set(your_func)
now *"your_func"* will be called every generation.
When this function returns True, the GA Engine will stop the evolution and show
a warning, if is False, the evolution continues.
"""
terminationCriteria = None
""" This is the termination criteria slot, if you want to set one
termination criteria, you must do this: ::
ga_engine.terminationCriteria.set(GSimpleGA.ConvergenceCriteria)
Now, when you run your GA, it will stop when the population converges.
There are those termination criteria functions: :func:`GSimpleGA.RawScoreCriteria`, :func:`GSimpleGA.ConvergenceCriteria`, :func:`GSimpleGA.RawStatsCriteria`, :func:`GSimpleGA.FitnessStatsCriteria`
But you can create your own termination function, this function receives
one parameter which is the GA Engine, follows an example: ::
def ConvergenceCriteria(ga_engine):
pop = ga_engine.getPopulation()
return pop[0] == pop[len(pop)-1]
When this function returns True, the GA Engine will stop the evolution and show
a warning, if is False, the evolution continues, this function is called every
generation.
"""
def __init__(self, genome, seed=None, interactiveMode=True):
""" Initializator of GSimpleGA """
if seed: random.seed(seed)
if type(interactiveMode) != BooleanType:
Util.raiseException("Interactive Mode option must be True or False", TypeError)
if not isinstance(genome, GenomeBase):
Util.raiseException("The genome must be a GenomeBase subclass", TypeError)
self.internalPop = GPopulation(genome)
self.nGenerations = Consts.CDefGAGenerations
self.pMutation = Consts.CDefGAMutationRate
self.pCrossover = Consts.CDefGACrossoverRate
self.nElitismReplacement = Consts.CDefGAElitismReplacement
self.setPopulationSize(Consts.CDefGAPopulationSize)
self.minimax = Consts.minimaxType["maximize"]
self.elitism = True
# Adapters
self.dbAdapter = None
self.migrationAdapter = None
self.time_init = None
self.interactiveMode = interactiveMode
self.interactiveGen = -1
self.GPMode = False
self.selector = FunctionSlot("Selector")
self.stepCallback = FunctionSlot("Generation Step Callback")
self.terminationCriteria = FunctionSlot("Termination Criteria")
self.selector.set(Consts.CDefGASelector)
self.allSlots = [ self.selector, self.stepCallback, self.terminationCriteria ]
self.internalParams = {}
self.currentGeneration = 0
# GP Testing
for classes in Consts.CDefGPGenomes:
#.........这里部分代码省略.........
示例3: xrange
# 需要导入模块: from FunctionSlot import FunctionSlot [as 别名]
# 或者: from FunctionSlot.FunctionSlot import set [as 别名]
class GPopulation:
""" GPopulation Class - The container for the population
**Examples**
Get the population from the :class:`GSimpleGA.GSimpleGA` (GA Engine) instance
>>> pop = ga_engine.getPopulation()
Get the best fitness individual
>>> bestIndividual = pop.bestFitness()
Get the best raw individual
>>> bestIndividual = pop.bestRaw()
Get the statistics from the :class:`Statistics.Statistics` instance
>>> stats = pop.getStatistics()
>>> print stats["rawMax"]
10.4
Iterate, get/set individuals
>>> for ind in pop:
>>> print ind
(...)
>>> for i in xrange(len(pop)):
>>> print pop[i]
(...)
>>> pop[10] = newGenome
>>> pop[10].fitness
12.5
:param genome: the :term:`Sample genome`
"""
def __init__(self, genome):
""" The GPopulation Class creator """
logging.debug("New population instance, %s class genomes.", genome.__class__.__name__)
self.oneSelfGenome = genome
self.internalPop = []
self.popSize = 0
self.sortType = Consts.CDefPopSortType
self.sorted = False
self.minimax = Consts.CDefPopMinimax
self.scaleMethod = FunctionSlot("Scale Method")
self.scaleMethod.set(Consts.CDefPopScale)
self.allSlots = [self.scaleMethod]
# Statistics
self.statted = False
self.stats = Statistics()
def setMinimax(minimax):
""" Sets the population minimax
Example:
>>> pop.setMinimax(Consts.minimaxType["maximize"])
:param minimax: the minimax type
"""
self.minimax = minimax
def __repr__(self):
""" Returns the string representation of the population """
ret = "- GPopulation\n"
ret += "\tPopulation Size:\t %d\n" % (self.popSize,)
ret += "\tSort Type:\t\t %s\n" % (Consts.sortType.keys()[Consts.sortType.values().index(self.sortType)].capitalize(),)
ret += "\tMinimax Type:\t\t %s\n" % (Consts.minimaxType.keys()[Consts.minimaxType.values().index(self.minimax)].capitalize(),)
for slot in self.allSlots:
ret+= "\t" + slot.__repr__()
ret+="\n"
ret+= self.stats.__repr__()
return ret
def __len__(self):
""" Return the length of population """
return len(self.internalPop)
def __getitem__(self, key):
""" Returns the specified individual from population """
return self.internalPop[key]
def __iter__(self):
""" Returns the iterator of the population """
return iter(self.internalPop)
def __setitem__(self, key, value):
""" Set an individual of population """
self.internalPop[key] = value
self.__clear_flags()
def __clear_flags(self):
self.sorted = False
self.statted = False
def getStatistics(self):
""" Return a Statistics class for statistics
#.........这里部分代码省略.........