netlogo 2中的动态龟创作[contd ..] [英] Dynamic turtle creation in netlogo 2 [contd..]

查看:186
本文介绍了netlogo 2中的动态龟创作[contd ..]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在界面选项卡中,我有一个滑块,其值范围在2& 10.根据用户使用此滑块定义的值,应该创建许多数量的海龟。

In the interface tab i have a slider whose value ranges between 2 & 10. Depending on the value defined by the user using this slider, that many number of turtles should be created.

我尝试使用多个if语句,但是有一个问题后面的步骤。

I tried using multiple if statements but there is a problem in the succeeding steps.

if (slider-value = 2) [create2] 
if (slider-value = 3) [create3] 
if (slider-value = 4) [create4] 
if (slider-value = 5) [create5]

在使用上述条件创建海龟后,我必须为每个单独的乌龟分配额外的规则,如坐标位置,如何移动等等,我尝试再次使用多个if语句。但是似乎不起作用。

After creating the turtles using the above if conditions, i have to assign additional rules to each individual turtle like co-ordinate position, rules for how they should move etc. and i tried again using multiple if statements. But it doesn't seem to work.

例如,创建的子查询如下

for example the sub-query for create is as follows

to create2
  create-challengers 2
  ask turtle 0 [set color blue set label-color blue set size 2 
                set xcor party1-left-right ]
  ask turtle 1 [set color red set label-color red set size 2 
                set xcor party2-left-right ]

  ask turtles [ update-rule set old-mysize 0 set shape "default"]

end

用于创建3只海龟: / p>

for creating 3 turtles:

to create3
  create-challengers 3
  ask turtle 0 [set color blue set label-color blue set size 2 
                set xcor party1-left-right ]
  ask turtle 1 [set color red set label-color red set size 2 
                set xcor party2-left-right ]
  ask turtle 2 [set color green set label-color green set size 2 
               set xcor party3-left-right ]

  ask turtles [ update-rule set old-mysize 0 set shape "default"]

end

so等等。

主要问题是即使程序工作,无论用户定义了多少只海龟,所有的10个创建,但只有用户定义海龟的数量移动,即如果用户已经弄脏3,那么当我运行程序时,10只龟就被创建,但只有3个移动。

The main problem is even though program works irrespective of how many turtles the user has defined, all the 10 gets created but only user defined number of turtles move, i.e. if the user has assined 3 then when i run the program 10 turtles are created but only 3 move.

有没有办法绕过在其他编程语言中,可以简单地使用if-else语句。

Is there a way to get around like in other programming languages where one can simply use an if-else statement.

有人可以建议一种方式,真的很感激帮助。

Can someone suggest a way, would really appreciate the help.

提前感谢!

海龟创建后,我会为他们分配一些规则:

After the turtles are created i assign certain rules for them to move:

to update-rule  
  if (slider-values = 2) [update2]
  if (slider-values = 3) [update3]
  if (slider-values = 4) [update4]
  if (slider-values = 5) [update5]
end

一次a获得我为update2,update3创建多个子规则,这些子规则基本上是针对每个乌龟,具体取决于用户定义的数量:

And once again i create multiple sub-rule for update2, update3 which are basically for the each turtle depending on how many the user has defined:

如果有2只海龟: p>

If there are 2 turtles:

to update2 
  ask turtle 0 [set my-rule party1-rule] 
  ask turtle 1 [set my-rule party2-rule]
end

在3龟的情况下:

to update3
  ask turtle 0 [set my-rule party1-rule] 
  ask turtle 1 [set my-rule party2-rule]
  ask turtle 2 [set my-rule party3-rule] 
end

以下是运动规则

to adapt
  if (my-rule = "hunter") [hunt]
  ;;NB stickers do nothing
  if (my-rule = "aggregator") [aggregate]

end


to hunt                 
  ifelse (mysize > old-mysize) [jump 1] [set heading heading + 90 + random-float 180 jump 1]
  set old-mysize mysize 
end

to run-general
  create2 create3 create4 create5 create-voters update-support
  ask challengers [adapt] update-support

end

to go
  run-general

  tick

end


推荐答案

如Seth所指出的那样,由于海龟被创建,问题可能在代码的 move 部分。就像一般意见一样,您的代码创建3只海龟与创建2只海龟相同,除了额外的乌龟。如果这一般是正确的,如果您使用以下格式,您的代码将会更容易:

As noted by Seth, the problem is likely to be in the move part of the code since the turtles are being created. Just as general comments, it looks like your code for creating 3 turtles is identical to that for creating 2 turtles except for the additional turtle. If this is generally true, your code would be much easier if you used a format like:

to setup-challengers
  create-challengers 1 [set color blue set label-color blue set size 2 
                set xcor party1-left-right ]
  create-challengers 1 [set color red set label-color red set size 2 
                set xcor party2-left-right ]
  if slider-value >= 3
    [ create-challengers 1 [ set color green set label-color green set size 2
                           set xcor party3-left-right ] ]
  if slider-value >= 4
    [ create-challengers 1 [ <whatever you want number 4 to look like> ] ]

  ask challengers [<do stuff>]
end

这样你只需要为每个乌龟的代码写一次,如果你改变主意的颜色或东西,这更容易改变。

This way you only need the code for each turtle to be written once so if you change your mind about colours or something, it is much easier to change.

这篇关于netlogo 2中的动态龟创作[contd ..]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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