我有这本代码来自Railsback第16章基于代理和基于个人的建模,但它不起作用,我不知道为什么。我是NetLogo的新手,软件在第22行显示“预期关键字”。(创建包)
breed [dogs dog]
breed [packs pack]
breed [disperser-groups disperser-group]
dogs-own
[
age
sex
status
my-pack
my-disperser-group
]
packs-own [pack-members]
disperser-groups-own
[
sex
group-members
]
create-packs initial-num-packs
[
; set a location and shape just for display
setxy random-xcor random-ycor
set shape "box"
; create the pack’s dogs
let num-dogs random-poisson initial-mean-pack-size
hatch-dogs num-dogs
[
; first, set display variables
set heading random 360
fd 1
; now assign dog state variables
ifelse random-bernoulli 0.5
[set sex "male"]
[set sex "female"]
set age random 7
set-my-status ; a dog procedure that sets
; social status from age
set my-pack myself ; set dog’s pack to the one
; creating it
] ; end of hatch-dogs
; Initialize the pack’s agentset that contains its dogs
set pack-members dogs with [my-pack = myself]
; show count pack-members ; Test output – off for now
; now select the alpha dogs
update-pack-alphas ; a pack procedure to give the
; pack 2 alphas
] ; end of create-packs
to go
tick
if ticks > years-to-simulate [stop]
; First, age and status updates
ask dogs
[
set age age + 1
set-my-status
]
ask packs [update-pack-alphas]
; Second, reproduction
ask packs [reproduce]
; Third, dispersal
ask packs [disperse]
; Fourth, mortality
ask dogs [do-mortality]
; Fifth, mortality of collectives
ask packs [do-pack-mortality]
ask disperser-groups [if count group-members = 0 [die]]
; Sixth, pack formation
ask disperser-groups [do-pack-formation]
; Finally, produce output
update-output
end
to disperse ; a pack procedure
; First, identify the subordinates and stop if none
let my-subordinates pack-members with
[status = "subordinate"]
if not any? my-subordinates [stop]
; Now check females
if count my-subordinates with [sex = "female"] = 1
[
if random-bernoulli 0.5
[
create-disperser-group-from
my-subordinates with [sex = "female"]
]
]
if count my-subordinates with [sex = "female"] > 1
[
create-disperser-group-from
my-subordinates with [sex = "female"]
]
; And check males
if count my-subordinates with [sex = "male"] = 1
[
if random-bernoulli 0.5
[
create-disperser-group-from
my-subordinates with [sex = "male"]
]
]
if count my-subordinates with [sex = "male"] > 1
[
create-disperser-group-from
my-subordinates with [sex = "male"]
]
end ; to disperse
to create-disperser-group-from [some-dogs]
; a pack procedure
; "some-dogs" is an agentset of the dispersers
; First, create a disperser group and put the dogs in it
hatch-disperser-groups 1
[
; Set disperser group variables
set group-members some-dogs
set sex [sex] of one-of some-dogs
; Display the group
set shape "car"
set heading random 360
fd 2
; Now the disperser group sets the variables of the
; dispersing dogs
ask some-dogs
[
set my-disperser-group myself
set status "disperser"
set color green
; and display them in a line from the disperser group
move-to my-disperser-group
set heading [heading] of my-disperser-group
fd 1 + random-float 2
] ; end of ask some-dogs
] ; end of hatch-disperser-groups
; Finally, remove the dispersers from their former pack
let dogs-former-pack [my-pack] of one-of some-dogs
ask dogs-former-pack
[set pack-members pack-members with
[status != "disperser"]]
end ; to create-disperser-group-from
由于
答案 0 :(得分:3)
您需要使用单词to
启动一个过程,但不允许调用过程create-packs,因为它实际上是一个NetLogo命令。我手头没有这本书,但我怀疑这是设置程序。在上一行中添加过程名称,可能是:
to setup
create-packs initial-num-packs
您还需要使用单词end
结束每个程序,这也是缺失的。应该看起来更进一步:
end
to go
将来,在每个代码块的末尾进行语法检查(绿色勾选),而不是键入整个页面。这样你就可以知道错误的位置,并且可以将你输入的内容与书中的内容进行比较。