我在做以下代码时遇到了困难:我有一段代码,里面的海龟选择一个资源> 30的补丁。什么时候,那块乌龟是乌龟不动的价值最高的那块。所以我使用"other“命令放入了这行代码。然而,现在发生的情况是,如果海龟所在的补丁具有最高的资源价值,它会选择资源> 30的另一个补丁。问题是,例如,海龟一侧的一个补丁的资源值= 51,另一个补丁的值为31,而她选择31。我想要实现的是:如果海龟所在的补丁是具有最高资源价值的补丁(并且海龟已经收集了该资源),她将选择具有第二高资源价值的另一个相邻补丁。我尝试使用max-one-of,但得到了一个错误:"MAX-ONE-OF expected 2 input,a agentset and a number block。
有没有人有什么想法,我该怎么解决?
提前感谢
to go
ask turtles
[
let availablePatch patches in-cone 5 90 with [ resource-value > 30 ]
ask patch-here [ set availablePatch other availablePatch ] ;; remove the patch it is in, because if the patch it is in is the one with the highest value within your range of vision, the turtle does not move
; ask patch-here [ set availablePatch other max-one-of [ availablePatch ] ]
let neighAvailable count availablePatch
ifelse neighAvailable = 0
[
move-around
]
[
let target max-one-of availablePatch [ resource-value ]
face target move-to target
set step-count step-count + 1
]
]
end
to move-around
right random 360
let step-length 2
forward step-length
end发布于 2021-10-21 11:26:35
就像新程序员经常遇到的情况一样,你太沉迷于特定的思维模式了。所以你让问题变得非常技术性,代码变得臃肿,而代码应该总是反映你正在尝试做的事情。你想要的是简单的,所以代码应该是简单的。试着缩小并考虑其他选项。
如果我理解正确的话,海龟应该选择高资源补丁来利用它们/收集它们的资源。但是他们不应该两次选择相同的补丁。
有意义的可能解决方案:
-after一个乌龟已经开发了一个补丁,资源应该在30以下。这样它就不会成为候选人了。如果它不低于30,那么搬家似乎也没有多大意义。
利用补丁自己的变量“-use”,在海龟移动到那里后设置为"true“,在海龟离开后设置为"false”。然后,您可以使用with [ resource-value > 30 & exploited = false ]代替当前的with检查。
https://stackoverflow.com/questions/69655226
复制相似问题