I’m writing a genetic algorithm in python about the optimal series of moves for a virtual organism that will get it the most randomly-placed food in a 2D-grid. It does not have intelligence; it just moves in a pattern ie circle or square. My code for creating the 2D array for the environment that the organisms reside in is this:
grid = ([])
for i in range(5):
grid[i]=0
for j in range(5):
grid[i][j]=0
(board[4][5] means 4,5 in x, y; and the value of board[4][5] is 0 or 1, depending on
whether or not the space is occupied. Right now the program is really just assigning
a zero-value to each space, indicating no individual is there)
It just says “list assignment index out of range.” How can i fix this? By the way, does anyone know of a better way to create the 2D environment for the organisms?
right now your array is only one element and you’re indexing outside of the array. Try this
in place of your grid. This will now give you a 5 by 5 grid and now you can index grid[3][4].