簡體   English   中英

為什么此列表索引超出范圍?

[英]Why is this list index out of range?

我目前正在 AI 部門完成一項學校作業,我必須在其中制作遺傳算法。 它旨在使用名為 board 的列表解決 8 Queens 問題,其中每個索引是一列,該索引中的值是行。 該算法本身運行良好,但每次我嘗試運行它時,它都會在 while 循環達到 populationSize - 2(在本例中為 18)后崩潰。 我得到的錯誤是關於行:

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

錯誤是:

Traceback(最近一次調用最后一次):文件“nqueens1.py”,第 370 行,在 main()

文件“nqueens1.py”,第 363 行,在主基因算法(板)中

文件“nqueens1.py”,第 291 行,在genetic_algorithm child=reproduce(population[l], population[l + 1], nqueens)

IndexError:列表索引超出范圍

我試圖了解出了什么問題,但我不明白為什么 go 會超出范圍。 這是我到目前為止的代碼:

Function 轉載

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

Function 變異

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)來檢查這一點

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM