本文整理汇总了Python中deap.base.Toolbox方法的典型用法代码示例。如果您正苦于以下问题:Python base.Toolbox方法的具体用法?Python base.Toolbox怎么用?Python base.Toolbox使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类deap.base
的用法示例。
在下文中一共展示了base.Toolbox方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _setup_toolbox
# 需要导入模块: from deap import base [as 别名]
# 或者: from deap.base import Toolbox [as 别名]
def _setup_toolbox(self):
with warnings.catch_warnings():
warnings.simplefilter('ignore')
creator.create('FitnessMulti', base.Fitness, weights=(-1.0, 1.0))
creator.create('Individual', gp.PrimitiveTree, fitness=creator.FitnessMulti, statistics=dict)
self._toolbox = base.Toolbox()
self._toolbox.register('expr', self._gen_grow_safe, pset=self._pset, min_=self._min, max_=self._max)
self._toolbox.register('individual', tools.initIterate, creator.Individual, self._toolbox.expr)
self._toolbox.register('population', tools.initRepeat, list, self._toolbox.individual)
self._toolbox.register('compile', self._compile_to_sklearn)
self._toolbox.register('select', tools.selNSGA2)
self._toolbox.register('mate', self._mate_operator)
if self.tree_structure:
self._toolbox.register('expr_mut', self._gen_grow_safe, min_=self._min, max_=self._max + 1)
else:
self._toolbox.register('expr_mut', self._gen_grow_safe, min_=self._min, max_=self._max)
self._toolbox.register('mutate', self._random_mutation_operator)
示例2: _set_toolbox
# 需要导入模块: from deap import base [as 别名]
# 或者: from deap.base import Toolbox [as 别名]
def _set_toolbox(self, value):
"""setter of toolbox"""
try: # Check the type
check_var("toolbox", value, "dict")
except CheckTypeError:
check_var("toolbox", value, "deap.base.Toolbox")
# property can be set from a list to handle loads
if (
type(value) == dict
): # Load type from saved dict {"type":type(value),"str": str(value),"serialized": serialized(value)]
self._toolbox = loads(value["serialized"].encode("ISO-8859-2"))
else:
self._toolbox = value
# DEAP toolbox
# Type : deap.base.Toolbox
示例3: getLocaleEvolutionToolbox
# 需要导入模块: from deap import base [as 别名]
# 或者: from deap.base import Toolbox [as 别名]
def getLocaleEvolutionToolbox(World, locale):
toolbox = base.Toolbox()
toolbox.register("ImmigrateHoF", immigrateHoF, locale.HallOfFame)
toolbox.register("ImmigrateRandom", immigrateRandom, World.tools.population)
toolbox.register("filterThreshold", filterAwayThreshold, locale)
toolbox.register("filterTrades", filterAwayTradeCounts, locale)
toolbox.register("filterExposure", filterAwayRoundtripDuration, locale)
toolbox.register('ageZero', promoterz.supplement.age.ageZero)
toolbox.register(
'populationAges',
promoterz.supplement.age.populationAges,
World.conf.generation.ageBoundaries,
)
toolbox.register(
'populationPD',
promoterz.supplement.phenotypicDivergence.populationPhenotypicDivergence,
World.tools.constructPhenotype,
)
toolbox.register('evaluatePopulation', evaluatePopulation)
return toolbox
示例4: getGlobalToolbox
# 需要导入模块: from deap import base [as 别名]
# 或者: from deap.base import Toolbox [as 别名]
def getGlobalToolbox(representationModule):
# GLOBAL FUNCTION TO GET GLOBAL TBX UNDER DEVELOPMENT;
toolbox = base.Toolbox()
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create(
"Individual",
list,
fitness=creator.FitnessMax,
PromoterMap=None,
Strategy=genconf.Strategy,
)
toolbox.register("mate", representationModule.crossover)
toolbox.register("mutate", representationModule.mutate)
PromoterMap = initPromoterMap(Attributes)
toolbox.register("newind", initInd, creator.Individual, PromoterMap)
toolbox.register("population", tools.initRepeat, list, toolbox.newind)
toolbox.register("constructPhenotype", representationModule.constructPhenotype)
return toolbox
示例5: main
# 需要导入模块: from deap import base [as 别名]
# 或者: from deap.base import Toolbox [as 别名]
def main():
N, LAMBDA = 30, 1000
MU = int(LAMBDA/4)
strategy = EMNA(centroid=[5.0]*N, sigma=5.0, mu=MU, lambda_=LAMBDA)
toolbox = base.Toolbox()
toolbox.register("evaluate", benchmarks.sphere)
toolbox.register("generate", strategy.generate, creator.Individual)
toolbox.register("update", strategy.update)
# Numpy equality function (operators.eq) between two arrays returns the
# equality element wise, which raises an exception in the if similar()
# check of the hall of fame. Using a different equality function like
# numpy.array_equal or numpy.allclose solve this issue.
hof = tools.HallOfFame(1, similar=numpy.array_equal)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", numpy.mean)
stats.register("std", numpy.std)
stats.register("min", numpy.min)
stats.register("max", numpy.max)
algorithms.eaGenerateUpdate(toolbox, ngen=150, stats=stats, halloffame=hof)
return hof[0].fitness.values[0]
示例6: init_deap_functions
# 需要导入模块: from deap import base [as 别名]
# 或者: from deap.base import Toolbox [as 别名]
def init_deap_functions(self):
creator.create("Fitness", base.Fitness, weights=self.weights)
creator.create("Individual", list, fitness=creator.Fitness)
self.toolbox = base.Toolbox()
self.toolbox.register("individual", tools.initIterate, creator.Individual, self.generate_ind)
self.toolbox.register("population", tools.initRepeat, list, self.toolbox.individual)
self.toolbox.register("evaluate", self.fit_func)
if self.penalty != None:
self.toolbox.decorate("evaluate", tools.DeltaPenality(self.feasible, self.inf_val))
示例7: create_toolbox
# 需要导入模块: from deap import base [as 别名]
# 或者: from deap.base import Toolbox [as 别名]
def create_toolbox(self):
"""OptiGenAlgNsga2Deap method to create DEAP toolbox
Parameters
----------
self : OptiGenAlgNsga2Deap
Returns
-------
self : OptiGenAlgNsga2Deap
OptiGenAlgNsga2Deap with toolbox created
"""
# Create toolbox
self.toolbox = base.Toolbox()
# Create Fitness and individual
creator.create(
"FitnessMin", base.Fitness, weights=[-1 for _ in self.problem.design_var]
)
creator.create("Individual", list, typecode="d", fitness=creator.FitnessMin)
self.toolbox.register("creator", creator.Individual)
# Register individual and population
self.toolbox.register(
"individual",
create_indiv,
self.toolbox.creator,
self.problem.output,
self.problem.design_var,
)
self.toolbox.register("population", tools.initRepeat, list, self.toolbox.individual)
示例8: geneticAlgorithm
# 需要导入模块: from deap import base [as 别名]
# 或者: from deap.base import Toolbox [as 别名]
def geneticAlgorithm(X, y, n_population, n_generation):
"""
Deap global variables
Initialize variables to use eaSimple
"""
# create individual
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
# create toolbox
toolbox = base.Toolbox()
toolbox.register("attr_bool", random.randint, 0, 1)
toolbox.register("individual", tools.initRepeat,
creator.Individual, toolbox.attr_bool, len(X.columns))
toolbox.register("population", tools.initRepeat, list,
toolbox.individual)
toolbox.register("evaluate", getFitness, X=X, y=y)
toolbox.register("mate", tools.cxOnePoint)
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)
# initialize parameters
pop = toolbox.population(n=n_population)
hof = tools.HallOfFame(n_population * n_generation)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", np.mean)
stats.register("min", np.min)
stats.register("max", np.max)
# genetic algorithm
pop, log = algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2,
ngen=n_generation, stats=stats, halloffame=hof,
verbose=True)
# return hall of fame
return hof
示例9: get_prediction_toolbox
# 需要导入模块: from deap import base [as 别名]
# 或者: from deap.base import Toolbox [as 别名]
def get_prediction_toolbox(self, features, pset):
toolbox = base.Toolbox()
toolbox.register("best_individuals", afpo.find_pareto_front)
toolbox.register("predict",
fast_evaluate.fast_numpy_evaluate,
context=pset.context,
predictors=features,
get_node_semantics=sp.get_node_semantics)
return toolbox
示例10: get_scoring_toolbox
# 需要导入模块: from deap import base [as 别名]
# 或者: from deap.base import Toolbox [as 别名]
def get_scoring_toolbox(self, features, response, pset):
toolbox = base.Toolbox()
toolbox.register("validate_func", partial(self.error_function, response=response))
toolbox.register("score",
fast_evaluate.fast_numpy_evaluate,
get_node_semantics=sp.get_node_semantics,
context=pset.context,
predictors=features,
error_function=toolbox.validate_func)
return toolbox
示例11: get_prediction_toolbox
# 需要导入模块: from deap import base [as 别名]
# 或者: from deap.base import Toolbox [as 别名]
def get_prediction_toolbox(self, features, pset):
toolbox = base.Toolbox()
toolbox.register("best_individuals", self.get_best_individuals)
toolbox.register("predict",
fast_evaluate.fast_numpy_evaluate,
context=pset.context,
predictors=features,
get_node_semantics=sp.get_node_semantics)
return toolbox
示例12: register
# 需要导入模块: from deap import base [as 别名]
# 或者: from deap.base import Toolbox [as 别名]
def register(self, alias, function, *args, **kargs):
"""
Register a *function* in the toolbox under the name *alias*. You
may provide default arguments that will be passed automatically when
calling the registered function. Fixed arguments can then be overriden
at function call time.
:param alias: The name the operator will take in the toolbox. If the
alias already exists it will overwrite the the operator
already present.
:param function: The function to which the alias refers.
:param args: one or more positional arguments to pass to the registered function, optional
:param kargs: one or more keyword arguments to pass to the registered function, optional
.. hint::
Under the hood lies the partial function binding. Check :func:`functools.partial` for details.
.. note::
If an operator needs its probability specified, like mutation and crossover operators, it can be done by
inserting the probability into the :attr:`pbs` dictionary with the same alias. Alternatively, it can be
given with a the special keyword argument `pb` in this method ::
tb = Toolbox()
tb.register('mut_uniform', mutate_uniform, ind_pb=0.02)
tb.pbs['mut_uniform'] = 0.1
or equivalently ::
tb = Toolbox()
tb.register('mut_uniform', mutate_uniform, ind_pb=0.02, pb=0.1)
As a result, the special keyword argument `pb` is always excluded from binding into *function*.
"""
pb = None
if 'pb' in kargs:
pb = kargs['pb']
del kargs['pb']
super().register(alias, function, *args, **kargs)
if pb is not None:
self.pbs[alias] = pb
示例13: create_toolbox
# 需要导入模块: from deap import base [as 别名]
# 或者: from deap.base import Toolbox [as 别名]
def create_toolbox(num_bits):
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
# Initialize the toolbox
toolbox = base.Toolbox()
# Generate attributes
toolbox.register("attr_bool", random.randint, 0, 1)
# Initialize structures
toolbox.register("individual", tools.initRepeat, creator.Individual,
toolbox.attr_bool, num_bits)
# Define the population to be a list of individuals
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
# Register the evaluation operator
toolbox.register("evaluate", eval_func)
# Register the crossover operator
toolbox.register("mate", tools.cxTwoPoint)
# Register a mutation operator
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
# Operator for selecting individuals for breeding
toolbox.register("select", tools.selTournament, tournsize=3)
return toolbox
示例14: create_toolbox
# 需要导入模块: from deap import base [as 别名]
# 或者: from deap.base import Toolbox [as 别名]
def create_toolbox():
pset = gp.PrimitiveSet("MAIN", 1)
pset.addPrimitive(operator.add, 2)
pset.addPrimitive(operator.sub, 2)
pset.addPrimitive(operator.mul, 2)
pset.addPrimitive(division_operator, 2)
pset.addPrimitive(operator.neg, 1)
pset.addPrimitive(math.cos, 1)
pset.addPrimitive(math.sin, 1)
pset.addEphemeralConstant("rand101", lambda: random.randint(-1,1))
pset.renameArguments(ARG0='x')
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMin)
toolbox = base.Toolbox()
toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=1, max_=2)
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("compile", gp.compile, pset=pset)
toolbox.register("evaluate", eval_func, points=[x/10. for x in range(-10,10)])
toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("mate", gp.cxOnePoint)
toolbox.register("expr_mut", gp.genFull, min_=0, max_=2)
toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset)
toolbox.decorate("mate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17))
toolbox.decorate("mutate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17))
return toolbox
示例15: create_toolbox
# 需要导入模块: from deap import base [as 别名]
# 或者: from deap.base import Toolbox [as 别名]
def create_toolbox():
global robot, pset
pset = gp.PrimitiveSet("MAIN", 0)
pset.addPrimitive(robot.if_target_ahead, 2)
pset.addPrimitive(Prog().prog2, 2)
pset.addPrimitive(Prog().prog3, 3)
pset.addTerminal(robot.move_forward)
pset.addTerminal(robot.turn_left)
pset.addTerminal(robot.turn_right)
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
# Attribute generator
toolbox.register("expr_init", gp.genFull, pset=pset, min_=1, max_=2)
# Structure initializers
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr_init)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", eval_func)
toolbox.register("select", tools.selTournament, tournsize=7)
toolbox.register("mate", gp.cxOnePoint)
toolbox.register("expr_mut", gp.genFull, min_=0, max_=2)
toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset)
return toolbox