本文整理汇总了Python中GPopulation.GPopulation.create方法的典型用法代码示例。如果您正苦于以下问题:Python GPopulation.create方法的具体用法?Python GPopulation.create怎么用?Python GPopulation.create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GPopulation.GPopulation
的用法示例。
在下文中一共展示了GPopulation.create方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: your_func
# 需要导入模块: from GPopulation import GPopulation [as 别名]
# 或者: from GPopulation.GPopulation import create [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:
#.........这里部分代码省略.........