我想在将物品添加到海龟邻居列表之前更改其价值。我在舱口中设置项目属性的原始值。然后,将其添加到我正在考虑的乌龟列表中。我现在想做的是更新原始值,添加少量(也许是随机浮动0.1),以使邻居列表具有属性+随机浮动0.1。与我要更改的内容相关的代码部分是:
to action
if breed = breed1 [
hatch-item 1 [
hide-turtle
set attribute random-float 1
set this-item self
ask myself [
set my-list fput this-item my-list
]
]
ask link-neighbors with [breed = breed1] [
set attribute (attribute + random-float 0.1)
set my-list fput this-item my-list
]
...
]
特别是这些代码行:
ask link-neighbors with [breed = breed1] [
set attribute (attribute + random-float 0.1)
set my-list fput this-item my-list
]
我在这里要做的是更改此项目的值,更新其原始值,然后再将其添加到乌龟的邻居列表中。我试图考虑:
set attribute (attribute + random-float 0.1)
为了更新属性,但是在列表中此项目的属性的值再次被初始化,所以我有0 + random-float 0.01。
[能否请您告诉我如何从原始属性值更新(不创建新值; 例如,如果我的属性原始值= 0.5且随机浮点数1等于0.2,我应该有一个新值等于0.7),然后将其作为此项目添加到邻居列表中?
更新:
这是当前输出的示例:
(turtle 2) (item 16) with attribute 0.147
neigh: 0 (item 16) with attribute 0 with random-float 0.2
但是我想拥有的是:
(turtle 2) (item 16) with attribute 0.147
neigh: 0 (item 16) with attribute 0.347 with random-float 0.2
希望您能帮助我。
谢谢
您如何测试值,您拥有的代码将更改名为attribute的变量的值。因此,如果您认为它没有被更改,也许您是在向错误的乌龟询问属性值?您可以通过在之前和之后执行打印语句来查看此内容:
to action
if breed = breed1 [
hatch-item 1 [
hide-turtle
type "Old vlue: " print attribute
set attribute random-float 1
type "New value: " print attribute
set this-item self
ask myself [ set my-list fput this-item my-list ]
]
ask link-neighbors with [breed = breed1] [
set my-list fput this-item my-list
]
...
]