Netlogo Flocking model turtles are bored - netlogo

I'm new to programming in Netlogo and I need to adapt the code so that turtles get bored after 50 ticks of flying in formation and make a right turn of 90 degrees. I modified the code while looking at some other examples but I get a very strong hunch it doesn't do what it's supposed to do. Can you please help me or give me a hint? Here's the code in question:
turtles-own [
flockmates ;; agentset of nearby turtles
nearest-neighbor ;; closest one of our flockmates
flock-tick
]
to setup
clear-all
create-turtles population
[ set color yellow - 2 + random 7 ;; random shades look nice
set size 1.5 ;; easier to see
setxy random-xcor random-ycor
set flockmates no-turtles
set flock-tick 0 ]
reset-ticks
end
to go
ask turtles [ flock ]
;; the following line is used to make the turtles
;; animate more smoothly.
repeat 5 [ ask turtles [ fd 1 ] display ]
;; for greater efficiency, at the expense of smooth
;; animation, substitute the following line instead:
;; ask turtles [ fd 1 ]
tick
end
to flock ;; turtle procedure
find-flockmates
if any? flockmates
[ find-nearest-neighbor
ifelse distance nearest-neighbor < minimum-separation
[ separate ]
[ align
cohere ] ]
end
to find-flockmates ;; turtle procedure
set flockmates other turtles in-cone vision 90
end
to find-nearest-neighbor ;; turtle procedure
set nearest-neighbor min-one-of flockmates [distance myself]
end
to flock-timer
if any? flockmates [
set flock-tick 50
while [ flock-tick > 0 ] [
set flock-tick ( flock-tick - 1 ) ]
if flock-tick = 0
[ set heading ( heading - 90 ) ] ]
end
The rest of the code is still the same as the the model in the library.
I must admit I'm not really sure of what I'm doin and the northwestern website is also not very helpfull to me (probably my own fault).
Thanks!

If we add comments to your code, we can see the logic problem.
to flock-timer
if any? flockmates [
;the next line sets `flock-tick` to 50
set flock-tick 50
;the next line sets `flock-tick` to 0
while [flock-tick > 0] [set flock-tick (flock-tick - 1)]
;so the next condition will always be satisfied
if (flock-tick = 0) [
;and so the heading will always be changed
set heading (heading - 90)
]
]
end
You might break things up to make it easier to follow the logic.
to flock-timer ;turtle proc
update-flock-tick
if (flock-tick = 0) [get-bored]
end
to update-flock-ticks ;turtle proc
;you can add additional logic here if you wish
if (flock-tick > 0) [
set flock-tick (flock-tick - 1)
]
end
to get-bored ;turtle proc
set heading (heading + one-of [-90, 90])
end
This is not a complete solution, but it should get you started.

Related

How to group turtles together depending on their colour and minimum distance

I am writing a model in NetLogo where I start with a 5 turtles populations in colours (blue, green, yellow, red, white) with random starting co-ordinates. When I run the model, I want when the white turtle meets the green or yellow or red turtle, they become joined with it and move together as a group. And when the white turtle meets the blue turtle, it separates from it and both continue moving randomly. I saw the example model for bird flocking in the model library and I have been trying to modify it for my model (see code attached).
In the 'flocking example' from the models' library, turtles are set to align, cohere and separate when too close to each other (depending on the distance for the nearest-neighbour and minimum-separation). This works well and I was able to use it to tell turtles to flock, keeping them intact as a group as they continue to move. My question is how to get only the (green, yellow, red) turtles to flock when they meet the white turtles and leaving the blue turtles moving randomly.
turtles-own [
flockmates ;; agentset of nearby turtles
nearest-neighbor ;; closest one of our flockmates
]
;Setting up the 5 turtles here
to setup
clear-all
create-turtles pop1
[ set color blue ; all pop
set size 1.5 ;; easier to see
setxy random-xcor random-ycor
set shape "person"]
create-turtles pop2
[ set color green ; stage1
set size 1.5 ;; easier to see
setxy random-xcor random-ycor
set shape "person"]
create-turtles pop3
[ set color yellow ; stage2
set size 1.5 ;; easier to see
setxy random-xcor random-ycor
set shape "person"]
create-turtles pop4
[ set color red ;stage3
set size 1.5 ;; easier to see
setxy random-xcor random-ycor
set shape "person"]
create-turtles pop5
[ set color white ; chv
set size 1.5 ;; easier to see
setxy random-xcor random-ycor
set shape "person"]
reset-ticks
end
to go
ask turtles [ flock ]
;ask turtles [ meet]
;; the following line is used to make the turtles ;; animate more smoothly.
repeat 5 [ ask turtles [ fd 0.2 ] ] ;this seems much faster
;; for greater efficiency, at the expense of smooth
;; animation, substitute the following line instead:
;ask turtles [ fd 1 ] ;than this
tick
end
;How do I make to get only the (green, yellow, red) turtles to flock when
;they meet the white turtles. Leaving the blue turtles moving randomly. How can do that?
; find-samecolor
; if any? flock
; ifelse separate
;end
;If green, yellow, red meets white turtle, they move together; nearest distance
to flock ;; turtle procedure
find-flockmates
if any? flockmates
[ find-nearest-neighbor
ifelse
distance nearest-neighbor < minimum-separation
[ align ]
[ cohere ]
;[separate]
]
end
;If blue meets white turtle, they move together; nearest distance
to find-flockmates ;; turtle procedure
set flockmates other turtles in-radius vision
end
to find-nearest-neighbor ;; turtle procedure
set nearest-neighbor min-one-of flockmates [distance myself]
end
;;; SEPARATE
;to separate ;; turtle procedure
; turn-away ([heading] of nearest-neighbor) max-separate-turn
;end
;;; ALIGN
to align ;; turtle procedure
turn-towards average-flockmate-heading max-align-turn
end
to-report average-flockmate-heading ;; turtle procedure
;; We can't just average the heading variables here.
;; For example, the average of 1 and 359 should be 0,
;; not 180. So we have to use trigonometry.
let x-component sum [dx] of flockmates
let y-component sum [dy] of flockmates
ifelse x-component = 0 and y-component = 0
[ report heading ]
[ report atan x-component y-component ]
end
;;; COHERE
to cohere ;; turtle procedure
turn-towards average-heading-towards-flockmates max-cohere-turn
end
to-report average-heading-towards-flockmates ;; turtle procedure
;; "towards myself" gives us the heading from the other turtle
;; to me, but we want the heading from me to the other turtle,
;; so we add 180
let x-component mean [sin (towards myself + 180)] of flockmates
let y-component mean [cos (towards myself + 180)] of flockmates
ifelse x-component = 0 and y-component = 0
[ report heading ]
[ report atan x-component y-component ]
end
;;; HELPER PROCEDURES
to turn-towards [new-heading max-turn] ;; turtle procedure
turn-at-most (subtract-headings new-heading heading) max-turn
end
;to turn-away [new-heading max-turn] ;; turtle procedure
; turn-at-most (subtract-headings heading new-heading) max-turn
;end
;; turn right by "turn" degrees (or left if "turn" is negative),
;; but never turn more than "max-turn" degrees
to turn-at-most [turn max-turn] ;; turtle procedure
ifelse abs turn > max-turn
[ ifelse turn > 0
[ rt max-turn ]
[ lt max-turn ] ]
[ rt turn ]
end
Sorry about the delay, for some reason I didn't see this question when you first asked it. The key procedure in this code is:
to find-flockmates ;; turtle procedure
set flockmates other turtles in-radius vision
end
What this does is says that any turtle that the source turtle can see is considered a flockmate. What you want to do is say that blue turtles cannot be flockmates (that is, they don't create flocks themselves and are not considered flockmates by other coloured turtles). Try this:
to find-flockmates ;; turtle procedure
if color != blue
[ set flockmates other turtles with [color != blue] in-radius vision
]
end
The first line makes it so that blue turtle don't find flockmates. The second line excludes blue turtles from the flocks of others.
-----UPDATE----
You actually have this procedure within a turtle procedure about flocking. This procedure sets the variable 'flockmates' to the set of turtles found but, if the turtle is blue, there are no flockmates and it doesn't return an empty turtleset. The best way to fix it is to use a to-report procedure, so all turtles will have an appropriate data type (a turtle set) for their variable.
to-report find-flockmates ;; turtle procedure
ifelse color = blue
[ report no-turtles ] ; this is an empty turtle-set
[ report other turtles with [color != blue] in-radius vision ]
end
Then, to use this procedure, you need to change:
to flock ;; turtle procedure
find-flockmates
if any? flockmates
to this:
to flock ;; turtle procedure
set flockmates find-flockmates
if any? flockmates
You can make two different types of turtles-own with their own rules. I did a multi agents works and everyone has their owns rules and with deafferents turtles-own group everything works like a charm.
ant-own[ velocita-ant metabolismo-ant sf ;;scorta ants massimo_sf] killer own[velocita-killer metabolismo-killer sk ;;scorta killers massimo_sk]

NetLogo - how do I get all patches the turtle facing?

How do I get a patch set that contains all patches that the turtle is facing?
I know patch-ahead report the patch with a specific distance. But what if I want to get all patches in this direction instead of the single one with specific distance?
What you can do is hatch a turtle and move it forward until it reaches the edge of the world, adding all the patches it crosses.
Here's a visible version to see the approach:
to testme
clear-all
create-turtles 1 [setxy random-xcor random-ycor]
ask one-of turtles
[ set pcolor red
hatch 1
[ while [can-move? 1]
[ forward 1
set pcolor red
]
die
]
]
end
To actually do the patchset version, you need to start with the current patch and add the patches as the hatched turtle moves over them. Try this for a procedure version and a demonstration of how it can be used:
turtles-own [ my-path ]
to testme
clear-all
create-turtles 1 [setxy random-xcor random-ycor]
ask one-of turtles
[ set my-path get-patches-forward self
print my-path
]
end
to-report get-patches-forward [ #me ] ; turtle procedure
let front-patches patch-here
hatch 1
[ while [can-move? 1]
[ forward 1
set front-patches (patch-set front-patches patch-here)
]
die
]
report front-patches
end
This will return the wrong answer if the world is wrapped because the hatched turtle can keep on going indefinitely. Instead, you would need to check its coordinates rather than relying on the can-move? primitive.

Netlogo , How to display number of ticks

Hi I am writing simulation which uses ticks to represent time in simulation environment . Is it possible to display tick counter in monitor GUI? Also I have tried to input the code but it is not working.
My sample code:
if [agents count = 0]
show "count ticks"
This should show tick exactly value when agent unit value is zero. For example if agent = 0 at tick 200 it should display 200 on monitor even the whole simulation run on 500 ticks.
whole code is
patches-own[
nest?
nest-scent
food
food-source-number
chemical
]
breed [agents agent] ;agent is ant
breed [antiagents a-antiagent] ;antiagent spider
antiagents-own[energy]
agents-own[venom]
;;;;;;;;;;;;;;;;;;;;;;;;;; Setup ;;;;;;;;;;;;;;;;;;;
to setup
clear-all
set-default-shape agents "ant"
create-agents initial-ants ;; create the ants, then initialize their variables
[
set color orange
set size 2 ;; easier to see
set venom (ant-venom-strength)
]
set-default-shape antiagents "spider"
create-antiagents initial-spiders ;; create the spider, then initialize their variables
[
set color yellow
set size 3 ;; easier to see
set energy (spider-health)
setxy random-xcor random-ycor
]
setup-patch
display-labels
reset-ticks
end
;;;;; nest food patch;;;
to setup-patch
ask patches
[ setup-nest
setup-food
recolor-patch ]
end
;;;;; setup nest
to setup-nest ;; patch procedure
;; set nest? variable to true inside the nest, false elsewhere
set nest? (distancexy 0 0) < 5
;; spread a nest-scent over the whole world -- stronger near the nest
set nest-scent 200 - distancexy 0 0
end
to setup-food ;; patch procedure
;; setup food source one on the right
if (distancexy (0.6 * max-pxcor) 0) < 5
[ set food-source-number 1 ]
;; setup food source two on the lower-left
if (distancexy (-0.6 * max-pxcor) (-0.6 * max-pycor)) < 5
[ set food-source-number 2 ]
;; setup food source three on the upper-left
if (distancexy (-0.8 * max-pxcor) (0.8 * max-pycor)) < 5
[ set food-source-number 3 ]
;; set "food" at sources to either 1 or 2, randomly
if food-source-number > 0
[ set food one-of [1 2] ]
end
to recolor-patch ;; patch procedure
;; give color to nest and food sources
ifelse nest?
[ set pcolor violet ]
[ ifelse food > 0
[ if food-source-number = 1 [ set pcolor cyan ]
if food-source-number = 2 [ set pcolor sky ]
if food-source-number = 3 [ set pcolor blue ]
if food-source-number = 4 [ set pcolor brown ] ]
;; scale color to show chemical concentration
[ set pcolor scale-color green chemical 0.1 5 ] ]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;; GO ;;;;;;;;;;;;;;;;;;;;;;
to go ;; forever button
;;;;;;;;;;;;Spider ;;;;;;;;
ask antiagents [
setup-spider-movemonet ; option for user to select spider movement
;move-spider
catch-ant
spider-death
]
;;;;;;;;;; Ant ;;;;;;;;;;;;
ask agents
[ if who >= ticks [ stop ] ;; delay initial departure
ifelse color = orange
[ look-for-food ] ;; not carrying food? look for it
[ return-to-nest ] ;; carrying food? take it back to nest
wiggle
fd 1 ]
diffuse chemical (diffusion-rate / 100)
ask patches
[ set chemical chemical * (100 - evaporation-rate) / 100 ;; slowly evaporate chemical
recolor-patch ]
tick
display-labels
;;;;;;;;;;; Stop function ;;;;;;;;;;
if count agents = 0 [stop]
if count patches with [pcolor = blue] = 0 [stop]
end
;;;;;;;;;;;;;;;;;;;;;;;;;; function in go ;;;;;;;;;;;;;;;
to look-for-food ;; turtle procedure
if food > 0
[ set color green + 1 ;; pick up food
set food food - 1 ;; and reduce the food source
rt 180 ;; and turn around
stop ]
;; go in the direction where the chemical smell is strongest
if (chemical >= 0.05) and (chemical < 2)
[ uphill-chemical ]
end
;;;;;;;;;;; ant function ;;;;;;;;;;
;; sniff left and right, and go where the strongest smell is
to uphill-chemical ;; turtle procedure
let scent-ahead chemical-scent-at-angle 0
let scent-right chemical-scent-at-angle 45
let scent-left chemical-scent-at-angle -45
if (scent-right > scent-ahead) or (scent-left > scent-ahead)
[ ifelse scent-right > scent-left
[ rt 45 ]
[ lt 45 ] ]
end
to return-to-nest
ifelse nest?
[ ;; drop food and head out again
set color orange
rt 180 ]
[ set chemical chemical + 60 ;; drop some chemical
uphill-nest-scent ] ;; head toward the greatest value of nest-scent
end
to wiggle ;; turtle procedure
rt random 40
lt random 40
if not can-move? 1 [ rt 180 ]
end
to uphill-nest-scent
let scent-ahead nest-scent-at-angle 0
let scent-right nest-scent-at-angle 45
let scent-left nest-scent-at-angle -45
if (scent-right > scent-ahead) or (scent-left > scent-ahead)
[ ifelse scent-right > scent-left
[ rt 45 ]
[ lt 45 ] ]
end
to-report chemical-scent-at-angle [angle]
let p patch-right-and-ahead angle 1
if p = nobody [ report 0 ]
report [chemical] of p
end
to-report nest-scent-at-angle [angle]
let p patch-right-and-ahead angle 1
if p = nobody [ report 0 ]
report [nest-scent] of p
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;; Spider-Function ;;;;;;;;;;;;;;;;;;;;;;
to move-spider
rt random 360
lt random 360
fd 3
end
to spider-death ;; turtle procedure
if energy <= 0 [ask antiagents-here [die]]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to catch-ant
let prey one-of agents-here
if prey != nobody
[ ask prey [ die ]
set energy (energy - ant-venom-strength)]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; GUI Function ;;;;
to display-labels
ask antiagents [ set label "" ]
if show-spider-health [
ask antiagents [ set label round energy ]
]
end
;;;;;;; Spider movement GUI ;;;;;;
to setup-spider-movemonet
if setup-spider-movement
[
ask antiagents [ move-spider ]
]
end
To make a monitor that displays the tick count, the complete code to put in the monitor is:
ticks
That's all.
If you want the monitor to show the tick count when there are no agents, but be blank otherwise, you would put this code in:
ifelse-value any? agents
[ "" ]
[ ticks ]
Your syntax is incorrect. You could however include in an observer procedure (e.g., your go procedure) the following: if (count agents = 0) [show ticks]. This would display in the area above the command line.
However it sounds like you are looking for user-message, rather than show. E.g., if (count agents = 0) [user-message (word ticks)]. http://ccl.northwestern.edu/netlogo/docs/dict/user-message.html
If you want to update the monitor each tick but only when there are agents, you need to create a separate global variable (called counter below) and update that instead. The ticks internal variable will always increase so monitoring it will always increment. This is because the ticks variable is the simulation clock, it reports the passage of time.
globals [counter]
to setup
...
set counter 0 ; note, not required as initialise to 0 but helps readability
reset-ticks
end
to go
...
if any? turtles [ set counter counter + 1 ]
...
tick
end
Then create a monitor with just counter (and 0 decimal places etc)

BehaviorSpace freezes on step #0

Sorry if this is obvious, I have searched everything I can think of and asked a colleague and we are both stuck.
I have some code (below - a slight twist on wolf sheep predation) that I want to run behaviorspace (input also below). However, it never runs to completion. It always seems to get stuck on step #0 of a run (the run # it gets stuck on varies). The runs that it sticks on work fine if they are run in isolation (so all the variables seem fine). It just seems to freeze (producing no error messages, nor does it crash).
Any ideas what is causing it?
Thanks,
Simon
Code:
globals [grass] ;; keep track of how much grass there is
;; Sheep and wolves are both breeds of turtle.
breed [sheep a-sheep] ;; sheep is its own plural, so we use "a-sheep" as the singular.
breed [wolves wolf]
turtles-own [energy] ;; both wolves and sheep have energy
patches-own [countdown]
to setup
clear-all
ask patches [ set pcolor green ]
;; check GRASS? switch.
;; if it is true, then grass grows and the sheep eat it
;; if it false, then the sheep don't need to eat
if grass? [
ask patches [
set countdown random grass-regrowth-time ;; initialize grass grow clocks randomly
set pcolor one-of [green brown]
]
]
if fences?[ ; this code adds in blue fences to create patches of various size (if fences are turned on). Turtles cannot pass over fences
ask patches with [pxcor mod connectivity2 = 0]
[ set pcolor blue ]
ask patches with [pycor mod connectivity = 0]
[ set pcolor blue ]
]
set-default-shape sheep "sheep"
create-sheep ((count patches - (count patches with [pcolor = blue])) / 25) ;; create the sheep, then initialize their variables. The starting density always remains constant despite a varying world size
[
set color white
set size 1.5 ;; easier to see
set label-color blue - 2
set energy random (2 * sheep-gain-from-food)
setxy random-xcor random-ycor
]
set-default-shape wolves "wolf"
create-wolves ((count patches - (count patches with [pcolor = blue])) / 50) ;; create the wolves, then initialize their variables. The starting density always remains constant despite a varying world size
[
set color black
set size 2 ;; easier to see
set energy random (2 * wolf-gain-from-food)
setxy random-xcor random-ycor
]
display-labels
set grass count patches with [pcolor = green]
reset-ticks
end
to go
if count wolves = 0 and count sheep = 0 and ((count patches with [pcolor = green]) = (count patches - (count patches with [pcolor = blue]))) [ stop ] ; stop model when the whole world has flipped to grass
ask sheep [
move
if grass? [
set energy energy - 1 ;; deduct energy for sheep only if grass? switch is on
eat-grass
]
death
reproduce-sheep
]
ask wolves [
move
set energy energy - 1 ;; wolves lose energy as they move
catch-sheep
death
reproduce-wolves
]
if grass? [ ask patches [ grow-grass ] ]
set grass count patches with [pcolor = green]
tick
display-labels
end
to move ;; turtle procedure
rt random 50
lt random 50
ifelse [pcolor] of patch-ahead 1 = blue
[ move] ;; If there is a blue patch ahead of you, choose another random direction
[ fd 1 ] ;; Otherwise, it is safe to move forward.
end
to eat-grass ;; sheep procedure
;; sheep eat grass, turn the patch brown
if pcolor = green [
set pcolor brown
set energy energy + sheep-gain-from-food ;; sheep gain energy by eating
]
end
to reproduce-sheep ;; sheep procedure
if random-float 100 < sheep-reproduce [ ;; throw "dice" to see if you will reproduce
set energy (energy / 2) ;; divide energy between parent and offspring
hatch 1 [ ifelse [pcolor] of patch-ahead 1 = blue ;; hatch an offspring
[ move] ;; If there is a blue patch ahead of the offspring, it chooses another random direction
[ fd 1 ] ;;If not, offspring move forward 1 step
]]
end
to reproduce-wolves ;; wolf procedure
if random-float 100 < wolf-reproduce [ ;; throw "dice" to see if you will reproduce
set energy (energy / 2) ;; divide energy between parent and offspring
hatch 1 [ ifelse [pcolor] of patch-ahead 1 = blue ;; hatch an offspring
[ move ] ;; If there is a blue patch ahead of the offspring, it chooses another random direction
[ fd 1 ] ;;If not, offspring move forward 1 step
] ]
end
to catch-sheep ;; wolf procedure
let prey one-of sheep-here ;; grab a random sheep
if prey != nobody ;; did we get one? if so,
[ ask prey [ die ] ;; kill it
set energy energy + wolf-gain-from-food ] ;; get energy from eating
end
to death ;; turtle procedure
;; when energy dips below zero, die
if energy < 0 [ die ]
end
to grow-grass ;; patch procedure
;; countdown on brown patches: if reach 0, grow some grass
if pcolor = brown [
ifelse countdown <= 0
[ set pcolor green
set countdown grass-regrowth-time ]
[ set countdown countdown - 1 ]
]
end
to display-labels
ask turtles [ set label "" ]
if show-energy? [
ask wolves [ set label round energy ]
if grass? [ ask sheep [ set label round energy ] ]
]
end
; Copyright 1997 Uri Wilensky.
; See Info tab for full copyright and license.
behaviorspace input:
variables:
["world-width" 51]
["fences?" true]
["wolf-gain-from-food" 20]
["sheep-reproduce" 7]
["show-energy?" false]
["grass?" true]
["connectivity" 10 25 50]
["wolf-reproduce" 5]
["grass-regrowth-time" [1 1 100]]
["sheep-gain-from-food" 4]
repetitions: 1
reporters:
count wolves
count sheep
count patches with [pcolor = green]
measure runs at each step
setup: setup
Go: go
Stop (I have tried it with and without this): count wolves = 0 and count sheep = 0 and ((count patches with [pcolor = green]) = (count patches - (count patches with [pcolor = blue])))
Time limit: 5000
It's difficult for me to imagine this could be caused by anything other than running low on memory. Running very low on memory can cause the JVM to slow to a crawl.
What do the memory usage stats in the System tab of "About NetLogo" say? You can verify there that you're actually getting the heap size you asked for.

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.