问题
After a short while I get a typical error message when running my netlogo model:
DISTANCE expected input to be an agent but got NOBODY instead. error while human 18 running DISTANCE
So far, I was not able to fix the mistake. Netlogo shows me the location in the source code where the mistake occurs:
let dist-nearest-resource distance nearest-resource
I believe to know that the message means that there are no green patches available to go to. I do know what else to code other than to say that the agents should move on and walk randomly around.
Here, down below is my minimal model to make you better understand. Does somebody know how to fix this?
breed [ humans human ]
humans-own [ energy ]
patches-own [ countdown ]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup
ca
create-humans(population)
[
set shape "person"
setxy random-xcor random-ycor
]
ask patches [
set pcolor one-of [green brown]
ifelse pcolor = green
[ set countdown 30 ]
[ set countdown random 30 ]
]
reset-ticks
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to go-people
ask humans [ orientation ]
end
to orientation
ifelse (energy < 4) [ ;if hungry
let nearest-resource min-one-of (patches with [pcolor = green] in-cone 3 360 ) [ distance myself ] ;check distance between you and nearest resource (3 fields 360 degrees view)
let dist-nearest-resource distance nearest-resource ;defines what is the shortest distance
if is-patch? nearest-resource [ ;if green patch exist at all
face nearest-resource fd distance nearest-resource ;face it and go directly to it
]
]
[ walk ] ;otherwise just walk randomly around
end
to walk
ask humans [
rt random-float 30 - random-float 30 ;randomly wandering around
if patch-at dx 0 = nobody [ ;humans get "bounced" away from the limits of the world
set heading (- heading) ]
if patch-at 0 dy = nobody [
set heading (180 - heading) ]
]
end
to sustainability ;countdown on brown patches: if 0 is reached, grow resources again after the time set by user
if pcolor = brown [
ifelse countdown <= 0
[ set pcolor green
set countdown regrowth-time ] ;exhausted fields need 30 ticks to recover
[ set countdown countdown - 1 ]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to go
if not any? turtles [ stop ] ;defensive programming
go-people
ask patches [ sustainability ]
set resources count patches with [pcolor = green]
tick
if count patches with [pcolor = green] = 0 [ stop ] ;model stops if food is no longer available
if count turtles = 0 [ stop ] ;model stops if all humans died
end
回答1:
In your code as it is, you never use the dist-nearest-resource
variable! Unless you planned to use dist-nearest-resource
for another purpose, you could just get rid of that whole line.
Furthermore, face nearest-resource fd distance nearest-resource
(which, by the way, doesn't crash because it's inside your if is-patch? nearest-resource
condition) could be replaced with a simple move-to nearest-resource
.
回答2:
You're exactly right about what's wrong. nearest-resource
is nobody
. Since it is being assigned with:
let nearest-resource min-one-of (patches with [pcolor = green] in-cone 3 360 ) [ distance myself ]
patches with [pcolor = green] in-cone 3 360
is empty.
It actually looks like you're trying to handle this already with if is-patch? nearest-resource
for the next line below the distance check. However, the distance check should be inside the body of the if as well, like so:
to orientation
ifelse (energy < 4) [ ;if hungry
let nearest-resource min-one-of (patches with [pcolor = green] in-cone 3 360 ) [ distance myself ] ;check distance between you and nearest resource (3 fields 360 degrees view)
if is-patch? nearest-resource [ ;if green patch exist at all
let dist-nearest-resource distance nearest-resource ;defines what is the shortest distance
face nearest-resource fd distance nearest-resource ;face it and go directly to it
]
]
[ walk ] ;otherwise just walk randomly around
end
However, with this code, the turtle won't do anything if it doesn't find a resource. We can rearrange things so that walk
is called when it doesn't find anything, like so:
to orientation
let nearest-resource min-one-of (patches with [pcolor = green] in-cone 3 360 ) [ distance myself ]
ifelse (energy < 4) and is-patch? nearest-resource [ ;if hungry and sees a resource
let dist-nearest-resource distance nearest-resource ;defines what is the shortest distance
face nearest-resource fd distance nearest-resource ;face it and go directly to it
]
[ walk ] ;otherwise just walk randomly around
end
By the way, in-cone 3 360
is equivalent to in-radius 3
.
来源:https://stackoverflow.com/questions/27986898/distance-expected-input-to-be-an-agent-in-netlogo