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