Why is this list index out of range?

I am currently working on a school assignment from the AI department where I have to make a Genetic Algorithm. It is meant to solve the 8 Queens problem using a list called board, where each index is a column and the value in that index is the row. The algorithm itself works fine, but every time I try to run it, it crashes after the while loop reaches populationSize - 2 (in this case 18). The error that I get is about line:

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

and the error is:

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

I am trying to understand what is going wrong, but I do not see why it would go out of range. Here is the code that I have so far:

Function Reproduce

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 Mutate

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

Genetic Algorithm

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

All the print-statements were added to see which parts did execute and which did not execute. The code crashes as soon as the l reaches 18 here, which of course it should not. All help would be appreciated!

1 answer

  • answered 2020-05-22 12:46 sabik

    I think population only has 19 items, not 20; all the populations are initialized using range(populationSize - 1) which only has 19 numbers (from 0 to 18).

    You can check this by also printing out len(population)