我正在积极尝试重新编程Traffic 2 lanes的示例模型,但通过我自己的添加,我添加了一个看起来像是底部有人的foothpath,但当我运行代码时,它有时会在同一个补丁上添加所需的4个人。我该如何解决这个问题?
to make-people
create-people 4 [setup-turtles]
end
to setup-turtles ;;Turtle Procedure
set shape "person"
let y-coordinates (list -8 -7 -6 -5)
let remove-index random length y-coordinates
set ycor item remove-index y-coordinates
set y-coordinates remove-item remove-index y-coordinates
set xcor 19
end
代码的其余部分与社会科学下Netlogo中名为Traffic 2 Lanes的样本模型相同,不同的是不同的人群。
问题是每个人都在为自己的创建再次定义y-coordinates
列表。该列表不会从一个人创建到下一个人,因此一个人从列表中删除其中一个项目不会对下一个人创建时重新定义的列表产生任何影响。最简单的方法是将y-coordinates
定义为global
变量,这样每个人都可以在同一个列表中工作。因此,当一个人删除一个坐标时,下一个人将得到该缩短的列表。尝试
breed [people person]
globals [y-coordinates]
to make-people
set y-coordinates (list -8 -7 -6 -5)
create-people 4 [setup-turtles]
end
to setup-turtles ;;Turtle Procedure
set shape "person"
let remove-index random length y-coordinates
set ycor item remove-index y-coordinates
set y-coordinates remove-item remove-index y-coordinates
set xcor 19
show y-coordinates
end
show语句将向您展示y-coordinates
列表确实被每只新海龟缩短了。