Netlogo while循环只有一次(Netlogo while loop only one time)

编程入门 行业动态 更新时间:2024-10-22 17:35:32
Netlogo while循环只有一次(Netlogo while loop only one time)

我在Netlogo中遇到一个小问题,我几天前才开始工作,我试着和两个冒险家一起制作一个迷宫,然后让他们从两个不同的起始位置到最后一个位置。 我的代码的所有开头工作正常,画出我的迷宫,但是当我想让冒险者去的时候,只有其中一个朝着正确的方向前进并找到了出口,而第二个甚至没有沿着要求的方向前进(东)。 我认为问题出在我的GO程序中,但我无法找到解决方案......这是我的代码,我在Netlogo 5.2上工作

to createaventurier create-aventuriers pointsdepart [set shape "person" set color pink set size 1 move-to one-of patches with [pcolor = green] ask patch-here [set pcolor blue] set beta ycor ] show count aventuriers end

这个程序完成它应该做的事情。

to go set i 0 createaventurier while [i < pointsdepart] [show count aventuriers ask one-of aventuriers [set heading 90 execute] set i i + 1 ] show count pas end

并且在这里,程序返回没有冒险者(没有代理或代理集),而观察者返回我有两个(当我想要两个冒险者)。 我在代码的开头编写了它们,并且在其他程序中使用了很多while循环,这非常有效。 我对这个软件并不放心,我只是想找一个简单的解释,(我的英语也不是很好)。 如果你需要我程序的其他部分,我可以发布它,但我不认为它们是必需的。 如果您需要更多信息,我也可以发布,但我希望我已经足够清楚了。

我提前谢谢你。

I have a small problem in Netlogo, which I began to work on only a few days ago, I try to make a maze with two adventurers, and I make them go from two different starting locations, to one final location. All the beginning of my code works fine, to draw my maze, but when I want to make the adventurers go, only one of them goes in the right direction and find the exit, and the second doesn't even go in the asked direction (East). I think the problem is in my GO procedure, but I can't reach to find a solution... Here is my code, I work on Netlogo 5.2

to createaventurier create-aventuriers pointsdepart [set shape "person" set color pink set size 1 move-to one-of patches with [pcolor = green] ask patch-here [set pcolor blue] set beta ycor ] show count aventuriers end

Here the program does what it's supposed to do.

to go set i 0 createaventurier while [i < pointsdepart] [show count aventuriers ask one-of aventuriers [set heading 90 execute] set i i + 1 ] show count pas end

And it's here that the program return that there are no adventurers (no agents or agentsets) while the observer returns me that there are two of them (when I want two adventurers). I Breed-ed them at the beginning of the code, and I used a lot of while loops in other procedures, which worked perfectly. I'm not at ease with the software, I'm just looking for a simple explanation, (I'm not so good in english too). If you need some other parts of my program I can post it, but I don't think they'll be needed. If you need more informations I can also post it, but I hope I have been clear enough.

I thank you in advance.

最满意答案

这是代码的简化版本。 我已经将冒险者变成了海龟,所以我不需要breeds并为你的变量pointsdepart硬编码2号。 它的工作正常,因为总有2只乌龟。

to setup clear-all ask n-of 20 patches [set pcolor green] reset-ticks end to make-agents create-turtles 2 [ set shape "person" set color pink set size 1 move-to one-of patches with [pcolor = green] ask patch-here [ set pcolor blue] ] show count turtles end to go let i 0 make-agents while [ i < 2 ] [ show count turtles ask one-of turtles [ set heading 90 forward 1 ] set i i + 1 ] show count turtles end

这表明问题出在你的execute函数中(我用forward 1代替)。

运行我的代码将证明一个逻辑问题。 您正在循环(在此示例中为两次)并在每个循环中运行ask one-of 。 one-of选择一只随机的乌龟,所以你可以让它们各自运行你的执行代码一次,或者你可能每次都选择相同的乌龟。 您很可能希望代码看起来更像这样:

to go make-agents ; note - should really be in setup, not go ask turtles [ set heading 90 forward 1 ] show count turtles end

此外,您通常会在go程序结束时使用tick命令来提前计时,然后再次运行go程序,以便乌龟继续移动等等。这就是为什么我评论创建冒险者的呼吁应该真的是在setup过程中,否则每次时钟前进时都会创建另外2个冒险者。

设置程序适用于模拟开始时需要的所有内容(例如创建冒险者,设置迷宫,为冒险者提供初始资源)。 go程序用于正在模拟的实际过程(例如,移动,从环境中获取资源,耗尽能量)。

Here is a simplified version of your code. I have changed adventurers into turtles so I didn't need breeds and hard-coded the number 2 for your variable pointsdepart. It works fine, in the sense that there are always 2 turtles.

to setup clear-all ask n-of 20 patches [set pcolor green] reset-ticks end to make-agents create-turtles 2 [ set shape "person" set color pink set size 1 move-to one-of patches with [pcolor = green] ask patch-here [ set pcolor blue] ] show count turtles end to go let i 0 make-agents while [ i < 2 ] [ show count turtles ask one-of turtles [ set heading 90 forward 1 ] set i i + 1 ] show count turtles end

This suggests that the problem is in your execute function (which I replaced with forward 1).

Running my code will demonstrate a logical problem. You are looping through (twice in this example) and running ask one-of in each loop. one-of selects a random turtle, so you might get them each to run your execute code once, or you might have the same turtle chosen each time. It is very likely you want code that looks more like this:

to go make-agents ; note - should really be in setup, not go ask turtles [ set heading 90 forward 1 ] show count turtles end

Also, you would generally have a tick command at the end of the go procedure to advance the clock and then the go procedure is run again so the turtles continue to move etc. This is why I have commented that the call to create the adventurers should really be in the setup procedure, otherwise another 2 adventurers will be created each time the clock advances.

The setup procedure is for everything that needs to be in place for the beginning of the simulation (eg creating adventurers, setting up your maze, giving initial resources to adventurers). The go procedure is for the actual process that is being simulated (eg moving, obtaining resources from the environment, depleting energy).

更多推荐

本文发布于:2023-08-05 20:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1439275.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Netlogo   loop   time

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!