netlogo realistic predator behaviour - netlogo

I am working with Netlogo to create a realistic predator prey-model and am stumped at the distance command.
ifelse any? sheep in-radius senserange [
let prey min-one-of sheep in-radius senserange [distance myself]
**ifelse [distance prey] < speedrange**
[move-to prey
let addenergy [energy] of prey
ask prey [die]
set energy energy + (addenergy * energy-gain-from-sheep)
rt 180
fd speedrange
]
[set heading towards prey
fd speedrange
]
]
[wiggle
fd 1]
The code in bold is the problem: I want the wolves not to immediately go to the prey but move towards it in an increment of its speedrange, unless of course it is within that range, then run to it and grab it ;).
I get ERROR: "Expected an agent or number or string here, rather than a list or block"
I think it is with the distance command, I've also tried something like this
ifelse prey in-radius speedrange [distance myself]
Thsi version results in ERROR: "IFELSE expected this input to be a T/F but got a turtle agentset or patch agentset instead

try ifelse distance prey < speedrange instead of ifelse [distance prey] < speedrange - you don't need the reporter/command brackets here

Related

How to set myheading equal to leader's heading in Netlogo

what i'm trying to do is:
Pseudo code
to flock
check flockmates
if find any leader inside the flockmates
change myheading to leader's heading
else
follow flocking rule [separate, allign, cohesion]
end
Below is code that I use.
turtles-own
[ flockmates
nearest-neighbor
leader?
leader
]
to setup
__clear-all-and-reset-ticks
ask n-of population patches with [ pcolor = blue]
[sprout 1
[set color white
set size 0.6
set leader? false]
]
choose-leaders
end
to choose-leaders
ask n-of ((percent_of_leader / 100) * population ) turtles
[set leader? true
set color black
set size 0.6
set leader self
]
end
to go
ask turtles [flock]
end
to flock
find-flockmates
let nearby-leaders turtles with [leader? ]
ifelse any? nearby-leaders
[ set heading [heading] of nearby-leaders]
[ find-nearest-neighbor
ifelse distance nearest-neighbor < minimum-separation
[separate]
[ if any? flockmates
[align
cohere ]]]
end
to find-flockmates ;; turtle procedure
set flockmates other turtles in-cone vision vision-angle
end
to find-nearest-neighbor ;; turtle procedure
set nearest-neighbor min-one-of flockmates [distance myself]
end
however when I run the code, this error message pop-up can't set turtles variable HEADING to non-number [147]. and it point to this code [set heading [heading] of nearby-leaders]. what did i do wrong here? really appreciate if someone can help.
Because nearby-leaders in your code is a turtle-set, you should use one-of:
set heading [heading] of one-of nearby-leaders

How to make simulation run faster in netlogo instead of using Slider bar near to view update

Is there any code to make simulation run faster in netlogo, instead of using slider bar near to setting? What my code need to do is to simulate the crowd behavior, it's work fine if the number of turtles around 100,however when I increase the number up to 300-800 turtles, simulation take very long to finish. Each tick also take very long to count from 0 to 1 and next until all turtles die. one thing that I suspect cause slow simulation is when ask turtles to evacuate. without evacuate rule, everything went smoothly even set a maximum number of turtles. is there other way to write evacuate rule, so that it can run faster? thanks.
to go
ask turtles [wander fd 0.01]
if emergency? = true [move]
if all? turtles [ pcolor = red ] ;stops simuation
[stop]
tick
end
to wander
[ do..something]
end
to move
set time-to-evacuate time-to-evacuate + 1
ask turtles [avoid-obstacles fd 0.1]
ask turtles [follow-leader fd 0.1]
ask turtles [flock fd 0.1]
ask turtles with [pcolor != red] [evacuate fd 0.1]
ask turtles with [pcolor = red][die]
end
to evacuate
ask turtles with [color = black ]
[let beings-seen patches in-cone 10 135 with [pcolor = red]
if any? beings-seen
[ let target one-of beings-seen
face target]]
ask turtles with [color = white ]
[let beings-seen patches in-cone 5 135 with [pcolor = red]
if any? beings-seen
[ let target one-of beings-seen
face target]]
end
to avoid-obstacles
[do something]
end
to follow-leader
[do something]
end
to flock
[do something]
end
In your move procedure you have:
ask turtles with [pcolor != red] [ evacuate ... ]
And then in evacuate you have:
ask turtles with [color = black] [ ... ]
evacuate is already being run by all of the non-red turtles, so you've got every non-red turtle asking every black turtle to do something at every time tick.
I don't think you intended that.
I have to guess a bit at your intent, but I think if in evacuate you replace the ask with an if:
if color = black [ ... ]
that's probably closer to what you meant.

How to avoid turtles revisiting the patch on which they were last time?

Turtles stay on patches for 60 ticks, and then move to another target patch. How to avoid turtles revisiting the patch on which they were last time? Thanks
Hi Seth and Frank,
Thank you very much for your reply. I am sorry I did not describe the questions in detail.
Turtles will not visit the patch that they were on the last tick, and will move to another nearest patch instead in next tick. The following codes mean they find the nearest patch, and move on to it.
What I would want to do is the turtle will find the nearest patch again in the next tick. They will move to other alternative that is nearest to themselves, if the nearest patch is still the same one that they were on the last tick. Thanks
let closest-leaf min-one-of (patches in-radius 1 with [pcolor = lime]) [distance myself]
face closest-leaf
fd distance closest-leaf
A good way is to have a turtles-own variable of patches visited that can be maintained (remember to initialize it to the empty list when you create the turtle).
turtles-own [ patches-visited ]
to setup
...
ask turtles [ set patches-visited [] ]
...
end
to move
let potential-targets filter [ not member? ? patches-visited ] target-patches
let target-patch one-of potential-targets
if target-patch != NOBODY [
set patches-visited fput target-patch patches-visited
; move to target patch
]
end
Add a get-target reporter.
to-report get-target ;;turtle proc
let %close patches in-radius 1 with [pcolor = lime and self != [patch-here] of myself]
ifelse (any? %close) [
report min-one-of %close [distance myself]
] [
report nobody
]
end
Now you can easily change your mind about how to pick a target. You can then move to the target (if one exists).
to move ;; turtle proc
let %target get-target
ifelse (%target = nobody) [
;handle this case
] [
face %target move-to %target
]
end

Streamline agent behavior in NetLogo

I'm trying to model the avoidance of animal agents from human agents in NetLogo. First, I asked a single predator to avoid people using two behaviors, "wary" and "scared". This worked nicely. But then I asked the prey animals (168 individuals right now but potentially many more) to do the same and the model has slowed down to a snail's pace. As I'm pretty new to NetLogo I'm sure that there is a more efficient way to code this behavior. Any suggestions on how to streamline this process? I'm sure there is a better way to do it. Thanks!
to avoid-people ;; test if people too close to predator and prey and animals moves away if is.
ask predator [
ifelse ticks mod 24 >= 5 and ticks mod 24 < 18 [ ;makes sure the animals respond to people during the daytime
humans-near
ifelse any? wary
[ fd 0 ]
[ ]
humans-too-near
if any? scared
[run-away]
] [set wary 0 set scared 0]]
ask preys [
ifelse ticks mod 24 >= 5 and ticks mod 24 < 18 [
humans-near
ifelse any? wary
[ fd 0 ]
[ ]
humans-too-near
if any? scared
[run-away]
] [set wary 0 set scared 0]]
end
;;Humans-near and humans-too-near are functions
;;Alert-distance and flight initiation distance are sliders for the predator but are set values for prey
to humans-near ;;adds all humans in alert-distance radius of animal to an agent subset for that agent.
ask predator [
set wary humans in-radius alert-distance]
ask preys [
set wary humans in-radius 10]
end
to humans-too-near ;;adds all humans in flight-initiation-distance radius of animal to an agent subset for that agent.
ask predator [
set scared humans in-radius flight-initiation-distance]
ask preys [
set scared humans in-radius 5]
end
to run-away ;;Make animal avoid the human closest to it.
set nearest-human min-one-of scared [distance myself]
turn-away ([heading] of nearest-human) max-separate-turn
end
;;this keeps the animals inside the tropical forest and away from human settlement.
;;Max-separate-turn is a slider dictating the angle that the predator runs away from the human
to turn-away [new-heading max-turn]
turn-at-most (subtract-headings heading new-heading) max-turn
ifelse [habitat = typeTrop] of patch-ahead run-distance
[fd run-distance] [turn-away ([heading] of nearest-human) max-separate-turn]
end
to turn-at-most [turn max-turn]
ifelse abs turn > max-turn
[ ifelse turn > 0
[ rt max-turn ]
[ lt max-turn ] ]
[ rt turn ]
end
I did not understand your code, but this is one way to do what you want, I am not sure how agents should behave if they are scared or they move with wary, but you can change these easily :
Breed [predators predator]
Breed [Humans Human]
Breed [Preys Prey]
turtles-own [
wary
scared
]
to setup
Clear-all
Create-humans 5 [Set color orange set shape "person" move-to patch random 30 random 30]
Create-Preys 5[Set color white Set shape "Sheep" move-to patch random 30 random 30]
Create-predators 5 [set color red Set shape "wolf" move-to patch random 30 random 30]
ask turtles
[set Wary false
Set Scared False
]
reset-ticks
end
to go
ask turtles
[rt random 5
fd 0.3]
avoid-people
tick
end
to avoid-people
ifelse is-day?
[
ask predators
[ if humans-near?
[
set wary true
if humans-too-near? [Set Scared true]
set label (word wary "," Scared )
]
]
Ask Preys
[ if humans-near?
[
set wary true
if humans-too-near? [Set Scared true]
set label (word wary "," Scared )
]
]
]
[; what they should do when its night time
]
end
to-report humans-too-near?
report any? humans in-radius 2
end
to-report humans-near?
report any? humans in-radius 5
end
to-report is-day?
report (ticks mod 24 >= 5 and ticks mod 24 < 18)
end
*Update:
Your problem was in having 2 ask inside each other , I am glad your model now runs faster.

turtles overshooting their target and getting stuck in infinite movement loop

I am modeling tax evasion. My model has stationary traders (one breed of turtles) and customers (another breed).
The traders go towards the cheapest trader in their vicinity.
As long as my model has less than 10 traders, I have not experienced this problem. But as I ramp it up to over 20, one of the traders seem to, by chance, be at a set of coordinates that make every customer get stuck in an infinite movement loop where they move forward by 1, but overshoot the target, turn around, and overshoot again etc.
I can get less problems by decreasing forward movement to say 0.001 instead of 1, but the problem will still occur eventually.
Is there a quick fix to this problem? I can imagine a solution where an ifelse makes them jump directly into the traders coordinates when within range 1 or something, but is there an easier way?
I have tried implementing moving towards nearest trader given distance - as I suggested above, but now the customers get stuck in conga lines in groups in random locations without a trader
Here is the code regarding movement:
to find_food
ifelse ( num-traders-close < 2 )
[nearest_food]
[choose-cheapest]
end
to nearest_food
let nearest-food min-one-of (traders )[distance myself]
let cf-dist distance min-one-of traders [distance myself]
ifelse closest-trader > 1
[face nearest-food
fd 1]
[face nearest-food
fd cf-dist]
end
to choose-cheapest
let cheapest-food min-one-of traders [price]
let cf-dist distance min-one-of traders [distance myself]
ifelse closest-trader > 1
[face cheapest-food
fd 1]
[face cheapest-food
fd cf-dist ]
end
In the code in your other question , I have tried to follow your own code, but you can do this as well :
instead of finding one traders , you always check one-of close traders to that customer, and cheapest food and nearest food are customer property.
Breed [Customers Customer]
Breed [Traders trader]
Customers-own
[cheapest-food nearest-food traders-close]
traders-own [price]
to setup
random-seed 234523432
clear-all
Create-traders 10 [move-to one-of patches set price random 100 set shape "house" set color white ]
create-Customers 50 [move-to one-of patches set shape "person" ]
reset-ticks
end
to go
ask customers
[
set-customers
find_food
]
tick
end
to set-customers
rt random 100
fd 1
set traders-close traders with [distance myself < 5]
set nearest-food min-one-of (traders-close )[distance myself]
set cheapest-food min-one-of traders-close [price]
end
to find_food
ifelse ( count traders-close < 2 )
[ifelse nearest-food != nobody
[Move-to nearest-food ]
[Move-to min-one-of Traders [Distance myself]]
]
[ ifelse cheapest-food != nobody
[Move-to cheapest-food]
[Move-to min-one-of Traders with [Distance myself < 5][price]]
]
end