本文整理汇总了Python中deap.tools.mutPolynomialBounded方法的典型用法代码示例。如果您正苦于以下问题:Python tools.mutPolynomialBounded方法的具体用法?Python tools.mutPolynomialBounded怎么用?Python tools.mutPolynomialBounded使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类deap.tools
的用法示例。
在下文中一共展示了tools.mutPolynomialBounded方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: nsga2
# 需要导入模块: from deap import tools [as 别名]
# 或者: from deap.tools import mutPolynomialBounded [as 别名]
def nsga2(self, fit_func, fit_func_args=[], init_pop=None, cxpb=0.95, mutpb=0.95):
# init
random.seed(self.seed)
np.random.seed(self.seed)
self.fit_func = fit_func
self.fit_func_args = fit_func_args
self.cxpb = cxpb # cross probability
self.mutpb = mutpb # mutation probability
self.init_deap_functions()
self.toolbox.register("mate", tools.cxSimulatedBinaryBounded, low=self.bounds_min, up=self.bounds_max,
eta=1.0) # crossover = mate
self.toolbox.register("mutate", tools.mutPolynomialBounded, eta=10.0, low=self.bounds_min, up=self.bounds_max,
indpb=self.mutpb)
# self.toolbox.register("preselect", tools.selTournamentDCD)
self.toolbox.register("preselect", tools.selRandom)
self.toolbox.register("select", tools.selNSGA2)
if self.log_print: print("Number of used CPU: %i" % MPI_SIZE)
# optimization
result = self.optimize(init_pop)
if self.log_print: print("End of (successful) evolution")
result_nd = self.get_nondominated_inds(result)
if self.log_file:
fh1 = open(self.log_file, 'a')
fh1.write("\n-------------------------- End of (successful) evolution --------------------------\n")
fh1.write("\nNon dominated individuals\n")
for ind in result_nd:
fh1.write("ind --> fit_func: " + str(ind) + ' --> ' + str(ind.fitness.values) + '\n')
fh1.close()
return result_nd
示例2: mutate
# 需要导入模块: from deap import tools [as 别名]
# 或者: from deap.tools import mutPolynomialBounded [as 别名]
def mutate(self, individual, probability):
if self.method == 'Polynomial':
return tools.mutPolynomialBounded(individual, eta=20.0, low=0.0, up=1.0, indpb=1 / len(individual))[0]
elif self.method == 'Shuffle':
return tools.mutShuffleIndexes(individual, probability)[0]
示例3: mutate
# 需要导入模块: from deap import tools [as 别名]
# 或者: from deap.tools import mutPolynomialBounded [as 别名]
def mutate(self, indiv):
""" Mutate the individual design variables with different strategies according to the variables types :
- interval : Polynomial Bounded mutation or user defined
- set : Resampling the variable according to its initialization function
- pyleecan : Resampling the variable according to its initialization function
Parameters
----------
solver : Solver
solver to perform the genetic algorithm with DEAP
indiv : individual (e.g. OptiGenAlgIndivDeap)
individual to mutate
Returns
-------
is_mutation : boolean
True if at least one mutation occured
"""
is_mutation = False
for k, design_var_name in enumerate(indiv.design_var_name_list):
if self.p_mutate < random.random(): # Perform mutation
is_mutation = True
if self.mutator == None:
if (
indiv.design_var[design_var_name].type_var == "interval"
): # Interval variable
# Using polynomial bounded mutation as in Deb and al., "A Fast and Elitist Multiobjective Genetic Algorithm: NSGA-II"
if isinstance(indiv[k], Iterable):
indiv[k] = mutPolynomialBounded(
indiv[k], 20, *indiv.design_var[design_var_name].space, 1
)[0]
else:
# Function takes list in argument and returns list
indiv[k] = mutPolynomialBounded(
[indiv[k]], 20, *indiv.design_var[design_var_name].space, 1
)[0][0]
else:
indiv[k] = indiv.design_var[design_var_name].function(
indiv.design_var[design_var_name].space
)
else: # User defined mutator
if (
indiv.design_var[design_var_name].type_var == "interval"
): # Interval variable
indiv[k] = self.mutator(indiv[k])
else:
# TODO Allow to redefine mutators for pyleecan types or set
indiv[k] = indiv.design_var[design_var_name].function(
indiv.design_var[design_var_name].space
)
return is_mutation
示例4: test_nsga2
# 需要导入模块: from deap import tools [as 别名]
# 或者: from deap.tools import mutPolynomialBounded [as 别名]
def test_nsga2():
NDIM = 5
BOUND_LOW, BOUND_UP = 0.0, 1.0
MU = 16
NGEN = 100
toolbox = base.Toolbox()
toolbox.register("attr_float", random.uniform, BOUND_LOW, BOUND_UP)
toolbox.register("individual", tools.initRepeat, creator.__dict__[INDCLSNAME], toolbox.attr_float, NDIM)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", benchmarks.zdt1)
toolbox.register("mate", tools.cxSimulatedBinaryBounded, low=BOUND_LOW, up=BOUND_UP, eta=20.0)
toolbox.register("mutate", tools.mutPolynomialBounded, low=BOUND_LOW, up=BOUND_UP, eta=20.0, indpb=1.0/NDIM)
toolbox.register("select", tools.selNSGA2)
pop = toolbox.population(n=MU)
fitnesses = toolbox.map(toolbox.evaluate, pop)
for ind, fit in zip(pop, fitnesses):
ind.fitness.values = fit
pop = toolbox.select(pop, len(pop))
for gen in range(1, NGEN):
offspring = tools.selTournamentDCD(pop, len(pop))
offspring = [toolbox.clone(ind) for ind in offspring]
for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
if random.random() <= 0.9:
toolbox.mate(ind1, ind2)
toolbox.mutate(ind1)
toolbox.mutate(ind2)
del ind1.fitness.values, ind2.fitness.values
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
pop = toolbox.select(pop + offspring, MU)
hv = hypervolume(pop, [11.0, 11.0])
# hv = 120.777 # Optimal value
assert hv > HV_THRESHOLD, "Hypervolume is lower than expected %f < %f" % (hv, HV_THRESHOLD)
for ind in pop:
assert not (any(numpy.asarray(ind) < BOUND_LOW) or any(numpy.asarray(ind) > BOUND_UP))
示例5: test_nsga3
# 需要导入模块: from deap import tools [as 别名]
# 或者: from deap.tools import mutPolynomialBounded [as 别名]
def test_nsga3():
NDIM = 5
BOUND_LOW, BOUND_UP = 0.0, 1.0
MU = 16
NGEN = 100
ref_points = tools.uniform_reference_points(2, p=12)
toolbox = base.Toolbox()
toolbox.register("attr_float", random.uniform, BOUND_LOW, BOUND_UP)
toolbox.register("individual", tools.initRepeat, creator.__dict__[INDCLSNAME], toolbox.attr_float, NDIM)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", benchmarks.zdt1)
toolbox.register("mate", tools.cxSimulatedBinaryBounded, low=BOUND_LOW, up=BOUND_UP, eta=20.0)
toolbox.register("mutate", tools.mutPolynomialBounded, low=BOUND_LOW, up=BOUND_UP, eta=20.0, indpb=1.0/NDIM)
toolbox.register("select", tools.selNSGA3, ref_points=ref_points)
pop = toolbox.population(n=MU)
fitnesses = toolbox.map(toolbox.evaluate, pop)
for ind, fit in zip(pop, fitnesses):
ind.fitness.values = fit
pop = toolbox.select(pop, len(pop))
# Begin the generational process
for gen in range(1, NGEN):
offspring = algorithms.varAnd(pop, toolbox, 1.0, 1.0)
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
# Select the next generation population
pop = toolbox.select(pop + offspring, MU)
hv = hypervolume(pop, [11.0, 11.0])
# hv = 120.777 # Optimal value
assert hv > HV_THRESHOLD, "Hypervolume is lower than expected %f < %f" % (hv, HV_THRESHOLD)
for ind in pop:
assert not (any(numpy.asarray(ind) < BOUND_LOW) or any(numpy.asarray(ind) > BOUND_UP))