【问题标题】:Why does my code keep making a string with one or two letters instead of three?为什么我的代码总是用一两个字母而不是三个字母组成一个字符串?
【发布时间】:2019-08-31 08:09:54
【问题描述】:

我正在学习如何使用遗传算法。我发现这个(相对地,清楚地)简单的练习让我了解了如何做的基础知识(https://blog.sicara.com/getting-started-genetic-algorithms-python-tutorial-81ffa1dd72f9)。

练习的目标是破解函数中给出的“密码”。然后它会做它的整个算法。首先,它使一组随机字符串成为“密码”的长度。

def generateOrganism(length):
  possible_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
  i = 0 
  result = ""
  while i < length:
    i += 1
    character = random.choice(possible_chars)
    result += character
  return result


def generatePopulation(sizePopulation, password):
  population = []
  print('Starting Algorithm')
  i = 0
  while i < sizePopulation:
    population.append(generateOrganism(len(password)))
    i += 1
  return population

然后检查每个单词的适应度(由单词与密码的接近程度决定),如下所示:

def fitness (password, test_word):

    if (len(test_word) != len(password)):
        print("taille incompatible")
        return
    else:
        score = 0
        i = 0
        while (i < len(password)):
            if (password[i] == test_word[i]):
                score+=1
            i+=1
        return score * 100 / len(password)


这就是所谓的 computePerfPopulation 函数,它制作一个单词及其适应度的字典。

def computePerfPopulation(population, password):
  populationPerf = {}
  for individual in population:
    populationPerf[individual] = fitness(password, individual)
    if fitness(password, individual) == 100:
      print("EUREKA, WE HAVE CRACKED THE PASSWORD. IT'S '", individual, "'")
      return 'Done'
  print(populationPerf)
  return sorted(populationPerf.items(), key = operator.itemgetter(1), reverse = True)

然后将该字典传递给 selectFromPopulation 函数,该函数选择最适合的词和一些随机词用于“繁殖”。

def selectFromPopulation(populationSorted, best_sample, lucky_few):
  nextGen = []
  for i in range(best_sample):
    nextGen.append(populationSorted[i][0])
  for i in range(lucky_few):
    nextGen.append(random.choice(populationSorted)[0])
  random.shuffle(nextGen)
  return nextGen

然后用下面的函数来孕育单词。 这就是问题所在。

def createChildren(breeders, num_of_children):
  nextPopulation = []
  for i in range(0, len(breeders) // 2):
    for j in range(0, num_of_children):
      nextPopulation.append(createChild(breeders[i], breeders[len(breeders) -1 -i]))
  print(nextPopulation)
  print(len(nextPopulation))
  return nextPopulation



def createChild(individual1, individual2):
  child = ""
  for i in range(len(individual1)):
    if (int(100) * random.random()) < 50:
      child += individual1[i]

    else:
      print(i)
      print(individual2)
      child += individual2[i]
  return child

然后一些随机词可能会被下面的函数变异,但这并不完全重要。然后整个事情循环,直到收到密码

def mutatePopulation(population, chance_mutate): # population is a list

  for i in range(len(population)):
    if int(random.random() * 100) < chance_mutate:
      population[i] = mutateWord(population[i])
  return population


def mutateWord(word):
  possible_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'

  index_mods = int(random.random() * len(word))
  if index_mods == 0:
    word = random.choice(possible_chars) + word[1:]
    print(word)

  else:
    word = random.choice(possible_chars) + word[index_mods+1:]

  return word

有时整个项目会按预期运行,并且会找到“密码”。但偶尔会出现以下错误:

Traceback (most recent call last):
  File "main.py", line 146 in <module>
    project(100, 'lol' 10, 10, 5, 5)
  File "main.py", line 137 in projcet
    remakePopulation = createChildren(newBreeders, num_of_child)
  File "main.py", line 33 in createChildren
    nextPopulation.append(createChild(breeders[i], breeders[len(breeders) - 1 - 1]))
  File "main.py", line 49, in createChild
    child += individual2[i]
IndexError: string index out of range

当我调查这个问题时,我开始打印由 createChildren 函数生成的列表,(我将在下面给出整个项目代码)并注意到偶尔(在第二个循环或以上循环中,从不在第一个)一些单词会是一两个字符。我怀疑这是因为,当我再次循环它时,我将新的人口插入到 computePerfPopulation函数和新的人口与原来的人口不一样,扔掉索引? (我希望这是有道理的)

我不知道是什么导致了问题,如果有人能告诉我发生了什么,我将不胜感激。 (我知道这很啰嗦,但请多多包涵)另外,如果您有任何关于使这段代码更好的建议,并且如果您能给我一个遗传算法实现的好资源,我将非常感激。

(如果您将所有这些代码放在此处并运行它,经过几次尝试它应该会向您显示错误等) 这是完整的项目代码:

import random
import operator


def mutatePopulation(population, chance_mutate): # population is a list

  for i in range(len(population)):
    if int(random.random() * 100) < chance_mutate:
      population[i] = mutateWord(population[i])
  return population


def mutateWord(word):
  possible_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'

  index_mods = int(random.random() * len(word))
  if index_mods == 0:
    word = random.choice(possible_chars) + word[1:]
    print(word)

  else:
    word = random.choice(possible_chars) + word[index_mods+1:]

  return word




def createChildren(breeders, num_of_children):
  nextPopulation = []
  for i in range(0, len(breeders) // 2):
    for j in range(0, num_of_children):
      nextPopulation.append(createChild(breeders[i], breeders[len(breeders) -1 -i]))
  print(nextPopulation)
  print(len(nextPopulation))
  return nextPopulation



def createChild(individual1, individual2):
  child = ""
  for i in range(len(individual1)):
    if (int(100) * random.random()) < 50:
      child += individual1[i]

    else:
      print(i)
      print(individual2)
      child += individual2[i]
  return child




def selectFromPopulation(populationSorted, best_sample, lucky_few):
  nextGen = []
  for i in range(best_sample):
    nextGen.append(populationSorted[i][0])
  for i in range(lucky_few):
    nextGen.append(random.choice(populationSorted)[0])
  random.shuffle(nextGen)
  return nextGen



def computePerfPopulation(population, password):
  populationPerf = {}
  for individual in population:
    populationPerf[individual] = fitness(password, individual)
    if fitness(password, individual) == 100:
      print("EUREKA, WE HAVE CRACKED THE PASSWORD. IT'S '", individual, "'")
      return 'Done'
  print(populationPerf)
  return sorted(populationPerf.items(), key = operator.itemgetter(1), reverse = True)



def generateOrganism(length):
  possible_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
  i = 0 
  result = ""
  while i < length:
    i += 1
    character = random.choice(possible_chars)
    result += character
  return result


def generatePopulation(sizePopulation, password):
  population = []
  print('Starting Algorithm')
  i = 0
  while i < sizePopulation:
    population.append(generateOrganism(len(password)))
    i += 1
  return population



def fitness(password, test_word): # fitness function of the algorithm

  if len(test_word) != len(password):
    badFit = 0.0
    return badFit

  else:
    score = 0
    i = 0
    while i < len(password):
      if password[i] == test_word[i]:
        score += 1

      i += 1
  if test_word == password:
    print("SUCCESS")

  fit = (score * 100) / len(password)

  return fit



def project(population_size, password, best_sample, lucky_few, num_of_child, chance_of_mutation):
  password = str(password)
  population = generatePopulation(population_size, password)
  populationSorted = computePerfPopulation(population, password)
  #print(computePerfPopulation(population, password))
  breeders = selectFromPopulation(populationSorted, best_sample, lucky_few)
  nextPopulation = createChildren(breeders, num_of_child)
  nextGeneration = mutatePopulation(nextPopulation, chance_of_mutation)
  while True:
    i = 1
    newPopulationSorted = computePerfPopulation(nextGeneration, password)
    if newPopulationSorted == 'Done':
      break
    newBreeders = selectFromPopulation(newPopulationSorted, best_sample, lucky_few)
    remakePopulation = createChildren(newBreeders, num_of_child)
    nextGeneration = mutatePopulation(remakePopulation, chance_of_mutation)
    print(nextGeneration)
    print(len(nextGeneration))
    input('Press enter to continue')
    i += 1

【问题讨论】:

  • 代码中有几个地方循环遍历一个变量并使用计数器获取另一个变量中的索引。每当您这样做时,您一定会遇到索引错误。 createChild 内部的 for 循环和其中的 else 语句就是一个例子。
  • @Rashid'Lee'Ibrahim 你知道怎么解决吗?我什么都想不出来。这可能就是我的问题发生的原因。

标签: python-3.x genetic-algorithm


【解决方案1】:

在部分

def mutateWord(word):
    possible_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'

可能是单个文本而不是数组?

喜欢:

def mutateWord(word):
    possible_chars = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0]

这是一个镜头!

【讨论】:

  • 不,那没用。感谢您的尝试:)
  • 您记得将代码中使用字符串值的部分更改为数组吗?
  • 我改成possible_chars = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
  • 这样说:if index_mods == 0: word = random.choice(possible_chars) + word[1:] print(word)
  • 您必须更改整个代码,而不仅仅是将部分文本更改为数组。
猜你喜欢
  • 2022-11-04
  • 2016-11-01
  • 1970-01-01
  • 2019-11-17
  • 2022-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
相关资源
最近更新 更多