Python DEAP库,使用随机词作为个体 [英] Python DEAP library, using random words as individuals

查看:171
本文介绍了Python DEAP库,使用随机词作为个体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更好地处理DEAP.我想制作一种遗传算法,将单词作为个体作为总体,并通过检查这些单词与给定最大单词"的距离(读取:拼写)来最大化此算法.到目前为止,这是我按照文档中的示例进行的操作

I'm trying to get a better handle on DEAP. I want to make a genetic algorithm that has words as individuals as a population and it maximizes this by checking how far in distance(read: spelling) these words are from a given "maximal word". This is what I have done so far following examples in the documentation

import random
from randomwordgenerator import randomwordgenerator

from deap import base
from deap import creator
from deap import tools

creator.create("FitnessMax", base.Fitness, weights=("hot",))
creator.create("Individual", str, fitness=creator.FitnessMax)

toolbox = base.Toolbox()
toolbox.register("attr_str", randomwordgenerator.generate_random_words)
toolbox.register("individual", tools.initRepeat, creator.Individual,
                 toolbox.attr_str, n=1)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

ind = toolbox.individual()
print(ind)

让我感到困惑的是当我打印(ind)时得到的输出

where I get confused is when I print(ind) I get this output

<generator object initRepeat.<locals>.<genexpr> at 0x10407d888>

但是当我将代码更改为示例代码时(如下所示)

When I change the code to the example code however(seen below)

import random

from deap import base
from deap import creator
from deap import tools

IND_SIZE = 5

creator.create("FitnessMin", base.Fitness, weights=(-1.0, -1.0))
creator.create("Individual", list, fitness=creator.FitnessMin)

toolbox = base.Toolbox()
toolbox.register("attr_float", random.random)
toolbox.register("individual", tools.initRepeat, creator.Individual,
                 toolbox.attr_float, n=IND_SIZE)

ind1 = toolbox.individual()
print(ind1)

这是输出

[0.6047278872004169, 0.8976450330325899, 0.9795210255969901, 0.5752663675034192, 0.8511975930513275]

我真的很困惑为什么我的示例不仅仅打印字符串,任何人都可以对此有所了解吗?不幸的是,他们没有将字符串作为个人使用的示例,因此我试图自己调试它,但遇到了麻烦.感谢您的帮助

I'm really confused as to why my example doesn't just print a string, can anyone glean some insight into this? Unfortunately they don't have examples of using strings as individuals so I'm trying to debug it on my own but am having a tough time. Any help is appreciated

推荐答案

经过大量辩论,以下是有关为何不起作用的解释. Individual是使用基类str声明的,这对于您的意图和用途而言是很好的.但是,当您在工具箱中注册individual方法时,然后使用tools.initRepeat它将提供一个生成器作为容器的参数.对于基于list类的个人,这会很好,因为然后会评估生成器并将其存储在列表中.但是,由于您的是基于str的,因此python执行的隐式转换仅返回字符串",但不会通过生成器的内容来填充您的个人.由于DEAP提供的工具似乎不适合您的问题,因此建议您编写自己的脚本来生成种群和/或个体.以下应该可以解决您的限制:

After much debate, here is the explanation as to why it will not work. The Individual is declared using the base class str, which is fine for your intent and purposes. However, when you register the individual method in the toolbox, you then use tools.initRepeat which provides a generator as an argument to the container. For individuals based on the list class this would be fine since the generator is then evaluated and stored within the list. However, since yours is based on str, the implicit conversion performed by python simply returns the string "", but does not go through the content of the generator to populate your individual. Since it seems that the tools provided by DEAP are not suited for your problem, I would suggest you write you own script for generating a population and/or individual. The following should answer your constraints:

from randomwordgenerator.randomwordgenerator import generate_random_words

from deap import base
from deap import creator
from deap import tools

POP_SIZE = 10

creator.create("FitnessMax", base.Fitness, weights=(1,))
creator.create("Individual", str, fitness=creator.FitnessMax)

toolbox = base.Toolbox()
toolbox.register("attr_str", generate_random_words, n=1)

ind = creator.Individual(toolbox.attr_str())
pop = [creator.Individual(toolbox.attr_str()) for _ in range(POP_SIZE)]

print(ind)
print(pop)

这将生成一个随机词list的总体,然后可以对其进行求值.

This will generate a population as a list of random words, which you can then evaluate.

这篇关于Python DEAP库,使用随机词作为个体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆