这是问题所在。有许多类型的乌龟、人、楼梯、电梯和自动扶梯。
我也有数据,数据是在不同的时间点追踪到的人的位置。我想把t列表、x列表和y列表按代理id变量放在不同的人下面。问题是,如果只有一只乌龟,那就是人,那么下面的代码就是工作。
globals [ num-agent t-list x-list y-list agent-id ]
to-report read-trace[ file ]
.....
end
to setup
let listInput read-trace "filename.csv"
let param item 0 listInput
set num-agent item 0 param
set listInput but-first listInput
create-turtles num-agent [
set t-list []
set x-list []
set y-list []
set size 2
set shape "person"
set speed random-float (1)
]
foreach listInput [
let t item 0 ?
set agent-id item 1 ? - 1
let locationid item 2 ?
let x item 3 ?
let y item 4 ?
ask turtles agent-id [
set t-list sentence t-list t
; scale x-y to fit in the world size (100 x 100)
set x-list sentence x-list x
set y-list sentence y-list y
]
]
end
但是,如果培育其他类型的代理人,我只想通过不同的代理人id将t-list、x-list和y-list放在海龟人下面,这是行不通的!
globals [ num-agent t-list x-list y-list agent-id ]
breed [ persons person ]
breed [ stairs stair ]
breed [ elevators elevator]
breed [ escalators escalator ]
breed [ nextlocations nextlocation ]
to-report read-trace[ file ]
.....
end
to setup
let listInput read-trace "filename.csv"
let param item 0 listInput
set num-agent item 0 param
set listInput but-first listInput
create-persons num-agent [
set t-list []
set x-list []
set y-list []
set size 2
set shape "person"
set speed random-float (1)
]
foreach listInput [
let t item 0 ?
set agent-id item 1 ? - 1
let locationid item 2 ?
let x item 3 ?
let y item 4 ?
ask persons agent-id [
set t-list sentence t-list t
; scale x-y to fit in the world size (100 x 100)
set x-list sentence x-list x
set y-list sentence y-list y
]
]
end
问题来了,问人代理id!有人能帮我吗?非常感谢!非常感谢!
可以通过数字访问代理,但一般来说,您不应该这样做。这里出现的问题有三个原因。
- 海龟是用一个id号创建的,这是它们的
who
号,在所有海龟中是唯一的(不仅仅是在一个品种中( - 当你问一只乌龟时,你必须使用单数(
turtle
不是turtles
,或者这里是person
不是persons
- 如果你这样问一只乌龟,你必须使用它的
who
号。您不能使用其他标识符(如agent-id
(,除非您确定它对应于其who
编号
因此,如果您复制并粘贴代码,ask persons agent-id
失败的原因有很多:您试图a.ask persons
,它是所有的人,b.通过单个数字agent-id
询问,它最多可以识别一只乌龟,c.使用agent-id
的值,它可能对应也可能不对应于person
的who
数字(取决于您是否在任何其他乌龟之前创建人(。原则上,您可以通过确保persons
都在任何其他海龟之前创建并更改为ask person agent-id
来解决所有这些问题。但是,操纵who
数字通常是不明智的。
假设你从来没有杀死或创造过更多的海龟,你可以引入一个全局sorted-persons
,并在创建你的人后立即将其设置到列表sort persons
中。如果你真的觉得必须在agent-id
之前访问它们,那么你可以ask item agent-id sorted-persons
做任何你想做的事情。