我可以用变量修改 NetLogo 命令吗?
Can I modify NetLogo commands with variables?
从例子开始:
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
并且 冗余 通过使用不同的品种名称重复相同的过程我想做一些像 this(put 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
请注意,我将应由新创建的海龟执行的命令放在匿名过程中(使用 NetLogo 6.0.1 语法)然后 运行 that在传递给 run
的字符串中。您也可以将所有内容都放在一个大字符串中,但这样您就会失去编译器检查、语法突出显示、速度等。
但是,无论如何,不要做任何这些。使用 Jen 的方法。
从例子开始:
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
并且 冗余 通过使用不同的品种名称重复相同的过程我想做一些像 this(put 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
请注意,我将应由新创建的海龟执行的命令放在匿名过程中(使用 NetLogo 6.0.1 语法)然后 运行 that在传递给 run
的字符串中。您也可以将所有内容都放在一个大字符串中,但这样您就会失去编译器检查、语法突出显示、速度等。
但是,无论如何,不要做任何这些。使用 Jen 的方法。