如何为我的遗传算法创建环境?
我正在用 python 编写一个关于虚拟生物体的最佳一系列移动的遗传算法,这将使它在 2D 网格中获得最随机放置的食物。它没有智慧;它只是以某种模式移动,即圆形或方形。我为生物体所处环境创建二维数组的代码是这样的:
grid = ([])
for i in range(5):
grid[i]=0
for j in range(5):
grid[i][j]=0
(board[4][5] 表示 x, y 中的 4,5;board[4][5] 的值为 0 或 1 , 根据 空间是否被占用。现在程序实际上只是分配 每个空间的值为零,表示那里没有个人)
它只是说“列表分配索引超出范围”。我该如何解决这个问题?顺便问一下,有人知道为生物体创建 2D 环境的更好方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
现在你的数组只有一个元素,并且你正在数组之外进行索引。尝试用这个
代替你的网格。现在将为您提供 5 x 5 网格,现在您可以索引网格[3][4]。
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].
它可能需要对您的适应度函数进行大量评估,因此有效的实施可能对您非常有益。 Numpy 提供开箱即用的多维数组。
它会给你一个用零填充的 5x5 数组。 Numpy 还提供了一些不错的功能,例如计算值的出现次数,这比纯 Python 实现要快得多。
It's likely to require a lot of evaluations of your fitness function, so an efficient implementation might be very beneficial for you. Numpy offers multi-dimensional arrays out of the box.
it will give you a 5x5 array filled with zeros. Numpy offers also nice things like counting occurrences of a value, which will be way faster than a pure Python implementation.