我目前正在AI部门从事学校作业,在那里我必须制定遗传算法。它旨在使用称为板的列表来解决8 Queens问题,其中每个索引是一列,而该索引中的值是行。该算法本身运行良好,但是每次我尝试运行该算法时,while循环达到种群大小-2(在本例中为18)后就会崩溃。我得到的错误是关于行:
child = reproduce(population[l], population[l + 1], nqueens)
,错误是:
回溯(最近通话最近): 在第370行的文件“ nqueens1.py”中 main()
文件“ nqueens1.py”,主行,第363行 Genetic_algorithm(board)
genetic_algorithm中的文件“ nqueens1.py”,第291行 孩子=繁殖(人口[l],人口[l + 1],nqueens)
IndexError:列表索引超出范围
我试图了解问题出在哪里,但是我不明白为什么它会超出范围。这是我到目前为止的代码:
功能复制
def reproduce(boardX, boardY, nqueens):
boardChild = [-1] * nqueens
n = random.randint(0, (nqueens - 1)) #percentage of how much of one parent is reproduced and how much of the other parent
d = 0
for d in range(n): # the first n part of parent x
boardChild[d] = boardX[d]
s = d + 1
for s in range(nqueens) : # the last n-(len(board)-1) part of parent y
boardChild[s] = boardY[s]
return boardChild
功能突变
def mutate(child):
print('mutate')
boardMutated = [-1] * len(child)
newColumn = random.randint(0, len(child)-1)
newRow = random.randint(0, len(child)-1)
boardMutated[newColumn] = newRow
return boardMutated
遗传算法
def genetic_algorithm(board):
optimum = (len(board) - 1) * len(board) / 2
print('optimum:' + str(optimum))
nqueens = len(board)
populationSize = 20
population = []
for i in range(populationSize -1): #initializes first pooulation
population.append([])
for _ in range(nqueens):
population[i].append(random.randint(0, nqueens-1))
population[i].append(evaluate_state(population[i]))
if evaluate_state(population[i]) == optimum:
print("solved puzzle! 0")
print_board(population[i])
return
t = 0
while t != 1000:
population.sort(key=lambda x: x[nqueens -1 ]) #sorts the population from highest population size
for i in range(populationSize -1):
del population[i][-1]
newPopulation = [ [] for _ in range(populationSize -1) ]
newPopulation[0] = reproduce(population[1], population[0], nqueens) #
chance = random.randint(0, 100)
if chance < 5: # small chance that the child gets mutated
newPopulation[0] = mutate(newPopulation[0])
if evaluate_state(newPopulation[0]) == optimum:
print('if evaluate_state(child) == optimum:')
print("Solved puzzle! 1")
print_board(newPopulation[0])
return
if evaluate_state(newPopulation[0]) == optimum:
print("Solved puzzle! 2")
print('if evaluate_state(newPopulation[1]) == optimum:')
print_board(newPopulation[1])
return
l = 0
while l != (populationSize -1):
print(str(l))
child = reproduce(population[l], population[l + 1], nqueens) # reproduces the new generation
print_board(child)
if evaluate_state(child) == optimum:
print('if evaluate_state(child) == optimum:')
print("Solved puzzle! 3")
print_board(child)
return
chance = random.randint(0, 100)
if chance < 5: # small chance that the child gets mutated
child = mutate(child)
if evaluate_state(child) == optimum:
print('if evaluate_state(child) == optimum:')
print("Solved puzzle! 4")
print_board(child)
return
newPopulation[l] = child
l += 1
t += 1
添加了所有打印语句,以查看哪些部分已执行,哪些未执行。一旦l在此处达到18,代码就会崩溃,这当然不应该崩溃。所有帮助将不胜感激!
答案 0 :(得分:2)
我认为population
只有19项,而不是20;所有种群均使用range(populationSize - 1)
初始化,该变量只有19个数字(从0到18)。
您还可以通过打印出len(population)