gpt4 book ai didi

python - 为什么此列表索引超出范围?

转载 作者:行者123 更新时间:2023-12-04 09:54:24 25 4
gpt4 key购买 nike

我目前正在处理 AI 部门的一项学校作业,我必须在那里制定遗传算法。它旨在使用称为 board 的列表解决 8 Queens 问题,其中每个索引是一列,该索引中的值是行。该算法本身运行良好,但每次我尝试运行它时,它都会在 while 循环达到populationSize - 2(在本例中为18)后崩溃。我得到的错误是关于行:

child = reproduce(population[l], population[l + 1], nqueens)

错误是:

Traceback (most recent call last): File "nqueens1.py", line 370, in main()

File "nqueens1.py", line 363, in main genetic_algorithm(board)

File "nqueens1.py", line 291, in genetic_algorithm child = reproduce(population[l], population[l + 1], nqueens)

IndexError: list index out of range



我试图了解出了什么问题,但我不明白为什么它会超出范围。这是我到目前为止的代码:

功能再现
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,代码就会崩溃,这当然不应该。所有帮助将不胜感激!

最佳答案

我想 population只有 19 个项目,而不是 20 个;所有种群都使用 range(populationSize - 1) 初始化它只有 19 个数字(从 0 到 18)。

您也可以通过打印 len(population) 来检查这一点。

关于python - 为什么此列表索引超出范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61955145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com