我也一直不舒服DEAP的利用全球范围内的,而且我觉得我有为您提供替代解决方案。
可以在每个循环迭代中导入每个模块的不同版本,从而避免对全局范围的依赖。
this_random = importlib.import_module("random")
this_creator = importlib.import_module("deap.creator")
this_algorithms = importlib.import_module("deap.algorithms")
this_base = importlib.import_module("deap.base")
this_tools = importlib.import_module("deap.tools")
据我所知,这似乎与多处理有关。
作为一个例子,下面是DEAP的onemax_mp.py版本,它避免了将全部DEAP文件放在全局范围内。我在__main__
中包含了一个循环,它改变了每次迭代的权重。 (第一次使用的次数最大,第二次最少。)一切正常,多处理。
#!/usr/bin/env python2.7
# This file is part of DEAP.
#
# DEAP is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# DEAP is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with DEAP. If not, see <http://www.gnu.org/licenses/>.
import array
import multiprocessing
import sys
if sys.version_info < (2, 7):
print("mpga_onemax example requires Python >= 2.7.")
exit(1)
import numpy
import importlib
def evalOneMax(individual):
return sum(individual),
def do_onemax_mp(weights, random_seed=None):
""" Run the onemax problem with the given weights and random seed. """
# create local copies of each module
this_random = importlib.import_module("random")
this_creator = importlib.import_module("deap.creator")
this_algorithms = importlib.import_module("deap.algorithms")
this_base = importlib.import_module("deap.base")
this_tools = importlib.import_module("deap.tools")
# hoisted from global scope
this_creator.create("FitnessMax", this_base.Fitness, weights=weights)
this_creator.create("Individual", array.array, typecode='b',
fitness=this_creator.FitnessMax)
this_toolbox = this_base.Toolbox()
this_toolbox.register("attr_bool", this_random.randint, 0, 1)
this_toolbox.register("individual", this_tools.initRepeat,
this_creator.Individual, this_toolbox.attr_bool, 100)
this_toolbox.register("population", this_tools.initRepeat, list,
this_toolbox.individual)
this_toolbox.register("evaluate", evalOneMax)
this_toolbox.register("mate", this_tools.cxTwoPoint)
this_toolbox.register("mutate", this_tools.mutFlipBit, indpb=0.05)
this_toolbox.register("select", this_tools.selTournament, tournsize=3)
# hoisted from __main__
this_random.seed(random_seed)
pool = multiprocessing.Pool(processes=4)
this_toolbox.register("map", pool.map)
pop = this_toolbox.population(n=300)
hof = this_tools.HallOfFame(1)
this_stats = this_tools.Statistics(lambda ind: ind.fitness.values)
this_stats.register("avg", numpy.mean)
this_stats.register("std", numpy.std)
this_stats.register("min", numpy.min)
this_stats.register("max", numpy.max)
this_algorithms.eaSimple(pop, this_toolbox, cxpb=0.5, mutpb=0.2, ngen=40,
stats=this_stats, halloffame=hof)
pool.close()
if __name__ == "__main__":
for tgt_weights in ((1.0,), (-1.0,)):
do_onemax_mp(tgt_weights)
感谢您的快速结果。这是我的结果:TypeError:不能实例化抽象的 deap。creator.Fitness'>与抽象属性权重。创作者需要有明确的权重。 如果我移动“init”函数中的所有创建者的东西(并添加一个相应的全局工具箱),那么我得到的错误如AttributeError:'NoneType'对象没有属性'装饰器'等。工具箱类有几个在此工具箱= Toolbox()声明之后的“init”函数内部的“注册”和“装饰”方法(为了简洁起见,此处省略)。 –
hobscrk777
2014-10-20 16:42:54
@ user3325401好的,我刚刚下载了'deap',并得到了这个例子的工作(见上面的编辑)。尽管没有看到您的实际代码,但我不知道它是否适用于您。我们的想法是只设置依赖'init'内的'weights'的值,并在父进程中执行其他所有操作。 – dano 2014-10-20 16:48:22
再次感谢。但第二点,我认为问题在于工具箱类注册了GA流程中使用的一些功能。例如,在全局范围内,我定义了这样的东西:“toolbox.register(”population“,tools.initRepeat,list,toolbox.individual)”。然后在主函数中执行所有GA函数,我实际从函数“toolbox.population(n = numIndividuals)”中抽取“ ”如果我将toolbox.register语句移动到init函数中,那么错误我得到的是“AttributeError:'工具箱'对象没有属性'人口'”。 – hobscrk777 2014-10-20 16:59:18