Reputation: 43
I have this code from book Agent-based and individual based-modeling by Railsback chapter 16 but it doesn't work and I don't know why. I am new in NetLogo, the software shows "Expected keyword" in line 22. (create-packs)
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
Thanks
Upvotes: 3
Views: 1945
Reputation: 17678
you need to start a procedure with the word to
but you are not allowed to call a procedure create-packs because that's actually a NetLogo command. I don't have the book to hand, but I suspect this is the setup procedure. Add the procedure name in the previous line, probably this:
to setup
create-packs initial-num-packs
you also need to end each procedure with the word end
, which is also missing. Should look like this further down:
end
to go
In the future, do the syntax checking (green tick) at the end of every block of code rather than typing the whole page. That way you have some idea of where the error is and you can compare what you have typed with what's in the book.
Upvotes: 4