实施基本的捕食者-被捕食者模拟
我正在尝试实现捕食者-猎物模拟,但遇到了问题。 捕食者寻找附近的猎物并吃掉它。如果附近没有猎物,它们会移动到随机的空单元格中。
基本上,我遇到麻烦的部分是当我进步了“一代”时。
假设我有一个 3x3 的网格,每个单元格编号从 0 到 8。
如果我在 0 和 1 中有 2 个捕食者,则检查第一个捕食者 0,它会移动到单元格 3 或 4
例如,如果它转到单元 3,那么它会继续检查 predator 1。这似乎是正确的 但它有点“优先”索引值较低的生物体。我尝试过使用 2 个数组,但这似乎也不起作用,因为它会检查生物体存在但不存在的位置。 ._.
有人知道如何“公平”和“正确”地做到这一点吗?
I am trying to implement a predator-prey simulation, but I am running into a problem.
A predator searches for nearby prey, and eats it. If there are no near by prey, they move to a random vacant cell.
Basically the part I am having trouble with is when I advanced a "generation."
Say I have a grid that is 3x3, with each cell numbered from 0 to 8.
If I have 2 predators in 0 and 1, first predator 0 is checked, it moves to either cell 3 or 4
For example, if it goes to cell 3, then it goes on to check predator 1. This may seem correct
but it kind of "gives priority" to the organisms with lower index values.. I've tried using 2 arrays, but that doesn't seem to work either as it would check places where organisms are but aren't. ._.
Anyone have an idea of how to do this "fairly" and "correctly?"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我最近用 Java 做了一个类似的任务。从顶行到底部处理捕食者不仅会给较低指数带来“不公平的优势”,而且还会在猎物和捕食者的运动中产生模式。
我通过以随机顺序选择行和列来克服这个问题。这样,每个捕食者/猎物都有相同的机会在一代的早期阶段被处理。
随机化的一种方法是创建一个
(行,列)
对的链接列表。然后打乱链表。在每一代,选择一个随机索引开始并继续处理。I recently did a similar task in Java. Processing the predators starting from the top row to bottom not only gives "unfair advantage" to lower indices but also creates patterns in the movement of the both preys and predators.
I overcame this problem by choosing both row and columns in random ordered fashion. This way, every predator/prey has the same chance of being processed at early stages of a generation.
A way to randomize would be creating a linked list of
(row,column)
pairs. Then shuffle the linked list. At each generation, choose a random index to start from and keep processing.更多的是作为评论,如果你的猎物如此密集,这是一个常见问题,我怀疑你没有长寿的“种群”。另外,作为评论随机更新你的掠夺者。也就是说,不要逐步浏览位置数组,而是获取捕食者列表并将它们随机化,然后一一更新。我认为有必要,但不知道是否足够。
More as a comment then anything else if your prey are so dense that this is a common problem I suspect you don't have a "population" that will live long. Also as a comment update your predators randomly. That is, instead of stepping through your array of locations take your list of predators and randomize them and then update them one by one. I think is necessary but I don't know if it is sufficient.
这个问题可以通过一种称为双缓冲的技术来解决,该技术也用于计算机图形学(为了防止当前正在绘制的图像干扰当前正在屏幕上显示的图像)。使用两个数组。第一个数组保存当前状态,您根据第一个数组做出有关移动的所有决策,但您在另一个数组中执行移动。然后,你交换他们的角色。
编辑:看来我没有足够彻底地阅读你的问题。双缓冲和随机化可能都是需要的,具体取决于您的规则的复杂程度(但如果除了您所描述的规则之外没有其他规则,则随机化应该足够了)。不过,它们解决了两个不同的问题:
This problem is solved with a technique called double buffering, which is also used in computer graphics (in order to prevent the image currently being drawn from disturbing the image currently being displayed on the screen). Use two arrays. The first one holds the current state, and you make all decisions about movement based on the first array, but you perform the movement in the other array. Then, you swap their roles.
Edit: Looks like I didn't read your question thoroughly enough. Double buffering and randomization might both be needed, depending on how complex your rules are (but if there are no rules other than the ones you've described, randomization should suffice). They solve two distinct problems, though:
某种循环法怎么样?将你的掠食者放入循环链表中,并保留指向当前“第一个”节点的指针。然后,每一代将第一个指针前进到列表中的下一个位置。您可以轻松地将新的掠夺者插入到循环列表的前面或后面。
How about some sort of round robin method. Put your predators in a circular linked list and keep a pointer to the node that's currently "first". Then, advance that first pointer to the next place in the list each generation. You could insert new predators either at the front or the back of your circular list with ease.