我可以通过变量修改NetLogo命令



示例

开始

to make-new-car [freq x y head ] if (random-float 100 < freq) and not any? turtles-on patch x y [ create-cars 1 [ setxy x y set heading head set color one-of base-colors ] ] end

但是,我想拥有更多的汽车品种 - 不仅是一种汽车品种。我也想保持简单,并觉得做 this (第一个功能与上面的功能相同(:

to make-new-car [freq x y head ] if (random-float 100 < freq) and not any? turtles-on patch x y [ create-cars 1 [ setxy x y set heading head set color one-of base-colors ] ] end

to make-new-carSE [freq x y head ] if (random-float 100 < freq) and not any? turtles-on patch x y [ create-carsSE 1 [ setxy x y set heading head set color one-of base-colors ] ] end

和be 冗余通过重复相同的过程,只需用不同的品种名称即可完成 this 之类的事情(将breed-name用作论证,并将其与Create一起使用 - 命令(:

to make-new-car [freq x y head breed-name] if (random-float 100 < freq) and not any? turtles-on patch x y [ create-breed-name 1 [ setxy x y set heading head set color one-of base-colors ] ] end

但是,Netlogo抱怨 create-breed-name尚未定义。有什么想法吗?

最简单的方法是做create-turtles,然后是set breed breed-name。这是一个例子。

breed [ testers tester ]
to make-turtles [ breed-name ]
  create-turtles 1 [ set breed breed-name ]
end
to setup
  make-turtles testers
end

构造合适的字符串后,您也可以使用run做点事,但是我认为上面更简单。

与Jen的答案一起去。到目前为止,这是完成您需要的最直接方法。

只是为了它,这是使用run进行的一种方法:

to make-new-car [freq x y head breed-name]
  let commands [ ->
    setxy x y
    set heading head
    set color one-of base-colors
  ]
  if (random-float 100 < freq) and not any? turtles-on patch x y [
    run (word "create-" breed-name " 1 [ run commands ]")
  ]
end

请注意,我将新创建的Turtle应该在匿名过程中执行的命令(使用NetLogo 6.0.1语法(,然后在字符串传递给run的字符串中运行。您也可以将所有内容都放在一个大字符串中,但是然后您会放松编译器检查,语法突出显示,速度等。

但是,无论如何,不要这样做。使用Jen的方法。

最新更新