如何在 NetLogo 6.2 中选择补丁变量的第二大值?
How to choose the second highest value of a patch variable in NetLogo 6.2?
我在执行以下代码时遇到了困难:我有一段代码,圆锥内的海龟选择一个资源 > 30 的补丁。当时,乌龟是资源最高的补丁值一只乌龟没动。所以我使用“其他”命令放置了这行代码。然而,现在发生的情况是,如果乌龟所在的补丁具有最高的资源值,它会选择另一个资源> 30 的补丁。问题是,例如,乌龟一侧的补丁具有资源值= 51 和另一个值为 31,她选择 31。我想实现的是:如果乌龟所在的补丁是资源价值最高的补丁(并且乌龟已经收集了这个资源)她会选择另一个资源价值第二高的邻居补丁。我尝试使用 max-one-of 但出现错误:“MAX-ONE-OF 需要 2 个输入,一个代理集和一个数字块。
有没有人有什么想法,我该如何解决?
提前致谢
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
就像新程序员经常遇到的情况一样,您太过拘泥于特定的思维模式。所以当代码应该始终反映您正在尝试做的事情时,您正在使问题变得非常技术化并且代码臃肿。你想要的很简单,所以代码应该很简单。尝试缩小并考虑其他选项。
如果我理解正确,海龟应该选择资源丰富的补丁来利用 them/gather 它们的资源。但是他们不应该两次选择相同的补丁。
有意义的可能解决方案:
-乌龟利用补丁后,资源应该低于30。这样它就不会成为候选者。如果不低于30,反正搬家似乎也没什么意义。
-使用补丁拥有的变量“exploited”,在海龟移动到那里后设置为“true”,在海龟离开后设置为“false”。那么你可以使用with [ resource-value > 30 & exploited = false ]
代替当前的with
检查。
我在执行以下代码时遇到了困难:我有一段代码,圆锥内的海龟选择一个资源 > 30 的补丁。当时,乌龟是资源最高的补丁值一只乌龟没动。所以我使用“其他”命令放置了这行代码。然而,现在发生的情况是,如果乌龟所在的补丁具有最高的资源值,它会选择另一个资源> 30 的补丁。问题是,例如,乌龟一侧的补丁具有资源值= 51 和另一个值为 31,她选择 31。我想实现的是:如果乌龟所在的补丁是资源价值最高的补丁(并且乌龟已经收集了这个资源)她会选择另一个资源价值第二高的邻居补丁。我尝试使用 max-one-of 但出现错误:“MAX-ONE-OF 需要 2 个输入,一个代理集和一个数字块。
有没有人有什么想法,我该如何解决?
提前致谢
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
就像新程序员经常遇到的情况一样,您太过拘泥于特定的思维模式。所以当代码应该始终反映您正在尝试做的事情时,您正在使问题变得非常技术化并且代码臃肿。你想要的很简单,所以代码应该很简单。尝试缩小并考虑其他选项。
如果我理解正确,海龟应该选择资源丰富的补丁来利用 them/gather 它们的资源。但是他们不应该两次选择相同的补丁。
有意义的可能解决方案:
-乌龟利用补丁后,资源应该低于30。这样它就不会成为候选者。如果不低于30,反正搬家似乎也没什么意义。
-使用补丁拥有的变量“exploited”,在海龟移动到那里后设置为“true”,在海龟离开后设置为“false”。那么你可以使用with [ resource-value > 30 & exploited = false ]
代替当前的with
检查。