NetLogo:模型卡住,没有错误消息 [英] NetLogo: Model gets stuck w no error message

查看:92
本文介绍了NetLogo:模型卡住,没有错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着让一群海龟(Movers)穿过一道门,避开白色的墙.不知何故,模型在几次运行后冻结了.Go 按钮保持黑色,蓝色圆圈永远转动.没有给出错误味精.它必须陷入移动驱动器"中的某些计算中.功能,但我无法确定原因.

I try to make a bunch of turtles (Movers) to go trough a gate and avoid the wall which is white. Somehow the model freezes after a few runs. Go button stays black and blue circle turns for ever. No error MSG given. It must get stuck in some calculation within the "move-movers" function but I can't determine why.

我添加了我的代码的简化版本,但仍然会导致崩溃.复制&粘贴运行.禁用世界包装.包括num-movers"的滑块变量.

I added a simplified version of my code which still produces the crash. Copy & paste to run. Disable world wrap. Include a slider for "num-movers" Variable.

breed [ movers mover ]
movers-own [ steps ] ; Steps will be used to determine if an agent has moved.

to setup
clear-all
reset-ticks
ask patches [ set pcolor green ]
basic-pattern
end

to basic-pattern ; sets up gate and wall
let wallXCor 16 ; sets a white line to determine the inside & outside of the gate
repeat 33 [
ask patch wallXCor 0 [ set pcolor white ]
set wallXCor wallXCor - 1 
]
ask patches with [ pycor > 0 ] [ set pcolor lime ] ; sets the outside of the gate to another color (lime)
; changes color of the center to lime to create a passable opening
ask patch 0 0 [ set pcolor lime ]
ask patch 1 0 [ set pcolor lime ]
ask patch -1 0 [ set pcolor lime ]
end

to distribute-agents ; Distributes the Movers outside the gate based on the patch color lime. The number needs to be set via slider "num-movers"
repeat num-movers [
ask one-of patches with [ pcolor = lime and pycor > 2 and any? turtles-here = false ] [
sprout-movers 1 [ set color red set shape "circle" facexy 0 -12 ] set num-movers num-movers- 1 ]
] end

to go
move-movers
tick
end

to move-movers ; reset the steps variable and facing
ask movers [ set steps steps + 1 ]
ask movers [ facexy 0 -3 ]
; following lines checks if next patch to be steped upon is "legal".
while [ any? movers with [ steps > 0 ] ] [     
ask movers with [ steps > 0 ] [
ifelse is-patch? patch-ahead 1
and not any? turtles-on patch-ahead 1
and [ not member? pcolor [ white brown ] ] of patch-ahead 1
[
fd 1
 set steps steps - 1
] [ dirchange ] 
]
]
end

to dirchange ;If not able to move to next patch change direction to allow a step backwards.
if ( pxcor <= 0 and ycor >= 0 ) [ facexy 1 3 ] ;fd 1 set steps steps - 1]
if ( pxcor >= 0 and ycor >= 0 ) [ facexy -1 3 ] ;fd 1 set steps steps - 1]
end

推荐答案

您没有收到错误消息,因为没有实际错误.代码只是卡在你的 while 循环中.

You're not getting an error message, because there is no actual error. The code just gets stuck in your while loop.

您是否要在 dirchange 中注释掉 fd 1 set steps steps - 1 ?我的猜测是,您有一堆海龟面对相同的补丁(1,3 或 -1, 3)并被卡住,因为它们都不能移动,因为另一只海龟在它们面前.而且因为你只会在他们实际移动时减去他们的步数,所以他们中的一些永远不会达到 0 步.

Did you mean to comment out the fd 1 set steps steps - 1 in your dirchange? My guess is that you have a bunch of turtles that face the same patch (either 1,3 or -1, 3) and get stuck because none of them can move because another turtle is in front of them. And because you only subtract from their steps if they actually move, some of them never get to 0 steps.

While 通常是一个不好的原语,尤其是当你的移动代码中有这么多条件时,很难知道是什么导致你的 while 循环没有结束.是因为你的海龟面对一堵墙,还是因为它们在世界的边界,或者因为其他人挡住了它们的去路?您只是不知道,而且由于代码陷入循环,您的模型视图不会更新,因此您无法看到发生了什么.

While is in general a bad primitive to use for this reason, especially when you have this many conditionals in your move code, making it hard to know what is causing your while loop to not end. is it because your turtles are facing a wall, or because they are at the boundary of the world, or because someone else is blocking their path? You just don't know, and because the code is stuck in a loop, your model view doesn't update so you can't see what is going on.

如果你坚持保持while,我至少会加一个保护措施:写一个海龟报告器来检查你的海龟是否能够移动并破坏你的while 如果他们不能,或者给他们有限次数的尝试移动,而不是要求他们实际移动.

If you insist on keeping the while, I would at least put in a safeguard: write a turtle reporter that checks if your turtles are able to move and break your while if they can't, or give them a finite number of attempts at moving, rather requiring them to have actually moved.

这篇关于NetLogo:模型卡住,没有错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆