NetLogo - Have an agent identify one of two other types of agent closest to it and perform actions conditional on which agent type is identified - netlogo

Suppose I have 2 breeds of agents, sharks (stars and dots) and fish. The stars (large sharks) can eat dots (smaller sharks) and fish that are in-radius 0.5 of a star. A star should eat what is closest to it, either a dot or fish. For simplicity purposes, agents cannot move, and whether or not a star can feed depends on its spatial proximity to dots and fish during setup.
But i'm struggling to get the code to work. Specifically,
I am encountering an "expected reporter" error message in my code and i cannot figure out how to solve this, although I don't know if fixing the code will achieve the aim. Any help is greatly appreciated.
Below is my code:
; Create the agents
breed [sharks shark]
breed [fishes fish]
sharks-own
[
energy
]
fishes-own
[
x0 ; Starting x-cor
y0 ; Starting y-cor
]
to setup
; Always start with this
clear-all
reset-ticks
ask patches [set pcolor gray]
create-sharks 666
[
set color blue
set energy 100
ifelse who >= 15
[
set shape "dot" set size 2.5
]
[
set shape "star" set size 3.5
]
setxy random-xcor random-ycor
]
create-fishes 300
[
setxy random-xcor random-ycor
set x0 xcor
set y0 ycor
set shape "fish"
set size 2.5
set color white
]
; this procedure gives an "expected reporter' error
if shape = "star"
[
ifelse min-one-of turtles with [shape = "dot"]
[
ifelse any? sharks with [shape = "dot"] in-radius 0.5 ; namely here
[
fd 0
]
[
set energy (energy + 100)
set color green
] ; End of 2nd ifelse
]
[
if min-one-of turtles with [shape = "fish"]
[
ifelse any? fishes with [shape = "fish"] in-radius 0.5
[
fd 0
]
[
set energy (energy + 1)
set color yellow
]
]
] ; End of 1st ifelse
] ; End of 1st if
end

You core issue is you are using ifelse min-one-of turtles with [shape = "dot"] as though min-one-of will give a true/false, but it will report a turtle. The error message NetLogo is giving is not great in this case. What I think you want to use any? in those cases (there are two of them that I see).
After those are resolved you have a context error, where you are checking if shape = "star" , but you aren't inside an ask [...] block where that check would have a turtle context to be valid. Maybe that's just a copy/paste issue in getting this question code ready, but I thought I'd note that, too.

Ok after many hours of blood, sweat and tears I finally arrived at the solution. In case the issue/objective is of interest to anyone else, the working code is as follows:
; Create the agents
breed [sharks shark]
breed [fishes fish]
sharks-own
[
energy
]
fishes-own
[
x0 ; Starting x-cor
y0 ; Starting y-cor
]
to setup
; Always start with this
clear-all
reset-ticks
ask patches [set pcolor gray]
create-sharks 666
[
set color blue
set energy 100
ifelse who >= 10
[
set shape "dot" set size 2.5
]
[
set shape "star" set size 3.5
]
setxy random-xcor random-ycor
]
create-fishes 300
[
setxy random-xcor random-ycor
set x0 xcor
set y0 ycor
set shape "fish"
set size 2.5
set color white
]
ask turtles [
if shape = "star"
[
let turtleID min-one-of other turtles [distance myself] ; Create a variable that identifies nearest turtle to apex
ifelse [shape] of turtleID = "dot" ; Evaluate whether the nearest turtle is a small meso
[
ifelse distance turtleID < 0.5 ; check if prey is still close enough
[
; do this if within radius 0.5
set color green
]
[
; do this if not within radius 0.5
set color yellow
]
]
[
if [shape] of turtleID = "fish" ; Evaluate whether the nearest turtle is a fish
[
ifelse distance turtleID < 0.5
[
; do this if within radius 0.5
set color red
]
[
; otehrwise do this if not within radius 0.5
set color orange
]
]
]
]
]
end

Related

How to spawn turtles a certain amount of patches away from each other

I am trying to spawn turtles 5 patches away from each other but I'm not sure how, right now they all spawn on green patches (I don't want them to spawn on brown ones) and I'm not sure how exactly you control the distance between the spawning of turtles, thanks.
breed [ humans person ]
breed [ zombies zombie ]
to setup_world
clear-all
reset-ticks
ask patches [
set pcolor green
]
ask n-of 100 patches [
set pcolor brown
]
ask n-of 15 patches with [pcolor != brown][sprout-humans 1 [set size 5
set color blue
set shape "person"]]
ask n-of 5 patches with [pcolor != brown][sprout-zombies 1 [set size 4
set color red
set shape "person"]]
end
Have you read this question: NetLogo Create turtle at regular distance from each other?
Anyway, I thought that showing you some working functions would be helpful, here I made two alternatives, sprout-distanced1 and sprout-distanced2, you can test them both by alternating which line is commented; I also added a slider called Min-Distance to control the turtles spacing.
sprout-distanced1 uses the keyword carefully with is basically a try-else block, it's there in case that the turtle doesn't find a patch distanced enough to move to, in which case rather than sending a warning the turtle will stay where it is and print its distance to the closest turtle.
sprout-distanced2 uses a while loop, in case that the turtle doesn't find a place to move to that is at least Min-Distance from another turtle it will reduce the minimum radius by a small amount until it can distance itself from other turtles, if it had to move to a patch where it is less than Min-Distance away from other turtles it will log the distance at the Command Center.
breed [ humans person ]
breed [ zombies zombie ]
to setup_world
clear-all
reset-ticks
ask patches
[
set pcolor green
]
ask n-of 100 patches
[
set pcolor brown
]
ask n-of 15 patches with [pcolor != brown]
[
sprout-humans 1
[
set size 5
set color blue
set shape "person"
;sprout-distanced1
sprout-distanced2
]
]
ask n-of 5 patches with [pcolor != brown]
[
sprout-zombies 1
[
set size 4
set color red
set shape "person"
;sprout-distanced1
sprout-distanced2
]
]
end
to sprout-distanced1
carefully
[
; try to move at least Min-Distance away from other turtles
move-to one-of patches with [not any? other turtles in-radius Min-Distance]
]
[
; if can't move Min-Distance away from other turtles
; stay put and log the min distance to other turtle, just for reference
show distance min-one-of other turtles [distance myself]
setxy random-xcor random-ycor
sprout-distanced1
]
end
to sprout-distanced2
let min-dist Min-Distance
let moved? FALSE
while [not moved? and min-dist > 0]
[
; can distance it self somewhere?
ifelse any? patches with [not any? other turtles in-radius min-dist]
[
; if yes, go there
move-to one-of patches with [not any? other turtles in-radius min-dist]
set moved? TRUE
; if had to reduce the distancing radious log it
if moved? and min-dist < Min-Distance
[
show distance min-one-of other turtles [distance myself]
]
]
[
; no where to go, reduce the distancing radious
set min-dist min-dist - 0.1
]
]
end
Choose whichever suits better your model.

Pedestrian environment: turtles do not recognise patch-type and surroundings

Recently I've started working on a pedestrian model simulation. I'm currently having a difficult time with controlling the movement patterns of my turtles. My code and blueprint.png is uploaded to Github.
So first, I upload a floor plan and tried to setup-variables and ask patches with pcolor = 0 to set as walls, pcolor = white to set as the ground, pcolor = red for doors, etc.
I'm able to create turtles, and let's say they start at the doors. I've tried to instruct them to avoid walls, yet the code breaks with runtime error: MOVE-TO expected input to be an agent but got NOBODY instead. Why can turtles start at patches with a colour but not a patch-type?
Even just the way the turtles are walking is unlike previous models I've tested in the model library. Any feedback would be welcome and appreciated. Thanks
Netlogo Code
extensions [ time ]
globals [
time-passed
walls
doors
exits
ground
art
corners-top-left
corners-top-right
corners-bottom-left
corners-bottom-right
]
patches-own [
patch-type
]
turtles-own
[
speed
wait-time
]
to setup
clear-all
import-dwg
setup-turtles
setup-variables
reset-ticks
end
to go
move
tick
update-time
end
to import-dwg
import-pcolors "blueprint.png"
end
to update-time
let minutes floor (ticks / 60)
let seconds ticks mod 60
if(minutes < 10)[set minutes (word "0" minutes)]
if(seconds < 10)[set seconds (word "0" seconds)]
set time-passed (word minutes ":" seconds)
end
to setup-turtles
create-turtles 2 [
move-to one-of patches with [ patch-type = "ground" ]
set heading towards one-of patches with [ pcolor = 65 ]
]
ask turtles [
set speed 1
set wait-time 0
set size 2
set color blue
pen-down
]
end
to move
ask turtles [
If any? Patches with [ pcolor = white ]
[set heading towards one-of patches with [ pcolor = white ]
fd 1]
]
tick
end
to setup-variables
ask patches with [ pcolor = 0 ] [
set patch-type "walls"
]
ask patches with [ pcolor = 15 ] [
set patch-type "doors"
]
ask patches with [ pcolor = white ] [
set patch-type "ground"
]
ask patches with [ pcolor = 65 ] [
set patch-type "art"
]
set time-passed "00:00"
end
Cross-posted to Reddit and found my answer (link):
You're going to hate this - in your setup function, setup-variables needs to be before setup-turtles, otherwise it doesn't know what "patch-type" is.
Edit: Also using "neighbors" for your move so they're looking at
adjacent patches, may help them not walk through walls and art.

How do you go about making a convert probability chance for an agent in netlogo?

I'm currently working on a zombie apocalypse simulation in NetLogo 2D. In my current simulation, when the zombies collide with humans the humans have a 100% chance to turn into a zombie. I would like to change this so that I can use a slider to set the conversion rate - for example if I have a rate of 50% on my slider, then when the zombies collide with humans there's a 50% chance they would turn into a zombie, otherwise kill the attacking zombie.
This is how I have currently setup my project, the way I've done this so far is to detect when they collide, and set the humans health to -1, and then made an if statement that says if the health is below 1, to make them a zombie.
I would appreciate any help in the right direction as I've spent time pondering about this and haven't come up with any solution.
breed[humans person]
breed[zombies zombie]
globals[rad]
humans-own[zombies_seen zombies_hit humans_speed per_vis_rad per_vis_ang health]
zombies-own[zombies_speed humans_around_me closest_humans]
patches-own[solid]
to setup_world
clear-all
reset-ticks
set rad 1
ask patches[
set pcolor green
]
create-humans number_of_humans [
setxy random-xcor random-ycor
set color blue
set size 5
set shape "person"
set humans_speed 1 + random-float 0.1
set health 100
adjust_vision_cone
]
ask humans[
ask patches in-radius 5[
set pcolor yellow
]
]
create-zombies number_of_zombies [
setxy random-xcor random-ycor
set color red
set size 4
set shape "person"
set zombies_speed 0.5
]
ask zombies[
move-to one-of patches with [pcolor = green]
]
ask humans[
ask patches in-radius 5[
set pcolor green
]
]
draw_solid_patches
end
to draw_solid_patches
ask patches [
set solid false
]
ask n-of 100 patches with [pcolor = green][
set pcolor brown
set solid true
]
end
to detect_wall
if[solid] of patch-ahead 1 = true[
right 90
left 90
]
end
to run_model
Every 0.01 [
make_humans_move
make_zombies_move
tick
reset_patch_colour
if not any? humans [stop]
if ticks = 500000 [stop]
]
end
to make_humans_move
ask humans[
if health > 1 [
show_visualisations
right 45
left 45
let have_seen_zombies human_function
detect_wall
forward humans_speed
]
if health < 1 [
; ; ; this is where it looks at the health of the humans,
; ; ; and checks to see if they have been hit by a zombie,
; ; ; because their health would be below 1.
set breed zombies
set color red
set size 4
set shape "person"
set zombies_speed 0.5
]
]
end
to make_zombies_move
ask zombies[
detect_wall
right 45
left 45
smell_humans
detect_wall
forward zombies_speed
]
end
to smell_humans
if any? humans in-radius 10 [
set heading towards one-of humans in-radius 10]
detect_wall
end
to show_visualisations
if show_vis_cone = true [
ask patches in-cone per_vis_rad per_vis_ang with [pcolor = green] [
set pcolor orange
]
]
end
to reset_patch_colour
ask patches with [pcolor = orange] [
set pcolor green
]
end
to adjust_vision_cone
set per_vis_rad 10 + random 10
set per_vis_ang 90
end
to-report human_function
let seen [false]
let hit [false]
ask zombies in-cone per_vis_rad per_vis_ang [
set seen true
]
ask zombies in-radius rad [
set hit true
]
ifelse seen = true [
set zombies_seen zombies_seen + 1
right 180
][
right (random zwr - (zwr / 2))
]
if hit = true [
set zombies_hit zombies_hit + 1
set color black
set health -1
; ; ; this is where I make the human into a zombie,
; ; ; I need to have a method here that will refer to
; ; ; the convert_probability variable.
]
report seen
end
Making minimal adjustments to your code you could substitute the set health -1 to
if random 100 < conversion_probability [ set health 0 ]
where conversion_probability is a slider going from 0 to 100, as you said in the question.
I also want to point out that you could detect walls like this
to detect_wall
if [solid] of patch-ahead 2
[
face one-of patches in-radius 2 with [not solid]
]
end
doing left x and then right x only makes the turtle turn to the left and then face back where it was originally facing, unless it sees a zombie it won't change the behavior of the humans all that much.

Making a turtle stop and then go after a special event - Robotic lawn mower simulation project

I am trying to simulate a robotic lawn mower on Netlogo. My first goal was it finds its way home to recharge itself when the battery is low.
This is now done (thanks Luke !) but I can't figure how to make the lawnmower stop when it reaches the house (it keeps moving 2 patches around). Therefore my energy slider goes infinitely below zero.
I first thought of adding an event "recharge" and placing it after "check death" in to go with a if instance. But :
How do I say if car goes on specific patch : stop and get energy ? Should I use a special patch color for the house and then code if car goes on blue patch..... ?
Could adding the recharge event at the end of the to go elements would possibly create a conflict because recharge only goes when on low battery ? Would putting it in check death be a solution ?
Then I would like it to go back to work after instant recharge.
Any ideas ?
Here's the code :
breed [cars car]
cars-own [target]
breed [houses house]
to setup
clear-all
setup-patches
setup-cars ;;represent lawn mower
setup-house
reset-ticks
end
to setup-patches
ask patches [set pcolor green] ;; Setup grass patches
ask patches with [
pxcor = max-pxcor or
pxcor = min-pxcor or
pycor = max-pycor or
pycor = min-pycor ] [
set pcolor red ;; Setup the borders of the garden
]
end
to setup-cars
create-cars 1 [
setxy 8 8
set target one-of houses
]
end
to setup-house
set-default-shape houses "house"
ask patch 7 8 [sprout-houses 1]
end
to place-walls
if mouse-down? [
ask patch mouse-xcor mouse-ycor [ set pcolor red ]
display
]
end
to go
move-cars
cut-grass
check-death ;; Check % battery.
tick
end
to move-cars
ask cars
[
ifelse [pcolor] of patch-ahead 1 = red
[ lt random-float 360 ] ;; see red patch ahead turn left.
[ fd 1 ] ;; otherwise it is ok to go.
set energy energy - 1
]
tick
end
to cut-grass
ask cars [
if pcolor = green [
set pcolor gray
]
]
end
to check-death ;; when low energy lawn mower will go back to house
ask cars [
ifelse energy >= 150
[ set label "Energy ok" ]
[ set label "Low Energy, returning to base"
set target min-one-of houses [distance myself]
face target
ifelse distance target < 1
[ move-to target ]
[ fd 1 ]
]
]
end
You can use a logical flag (eg charging?) in conjunction with a counter (eg charge-time) to do this. Try modifying your cars-own definitions like this:
cars-own [target charging? charge-time]
and setup-cars like this:
to setup-cars
create-cars 1 [
setxy 8 8
set target one-of houses
set charging? false
]
end
Then you can have the mower do different things based on whether charging? is true or false. Try modifying your move-cars and check-death to accommodate these changes (side note- you've got a tick in both go and move-cars):
to move-cars
ask cars [
ifelse charging? [
set charge-time charge-time + 1
if charge-time > 14 [
set energy 200
set charging? false
set charge-time 0
]
] [
ifelse [pcolor] of patch-ahead 1 = red
[ lt random-float 360 ] ;; see red patch ahead turn left.
[ fd 1 ] ;; otherwise it is ok to go.
set energy energy - 1
]
]
end
to check-death ;; when low energy lawn mower will go back to house
ask cars [
ifelse energy >= 150
[ set label "Energy ok" ]
[ set label "Low Energy, returning to base"
set target min-one-of houses [distance myself]
face target
ifelse distance target < 1
[ move-to target
set charging? true
]
[ fd 1 ]
]
]
end
I suggest you change energy a turtle variable like car-energy so that the turtles values are independent of your slider. That would mean you could have multiple mowers and they could reset their energy level to the slider setting once they are charged! You would just include a line like
set car-energy energy
in your setup-cars (and then modify so that energy in your car procedures is instead car-energy).

Netlogo Sprouting turtles spaced at less than one patch

I want to place turtles on each of the black patches(below Figure) such that there is no gap between turtles at all:
Code I use right now:
ask patches with [pcolor = black][sprout-dead-turtles wall-agents [set color red]]
This gives the following result:
But I want to place turtles in between each of the two patches as well. So that I can cover the showing black part.
Note: Changing the shape of turtles is no use to my purpose though it would cover the black area. My aim to create a replusion force field from these agents and gaps in between are loop holes from where agents may escape.[Somewhat similar to agents bouncing back on a wall].
Here is a fun solution:
breed [ dead-turtles dead-turtle ]
to setup
ca
; draw the background:
ask patches with [ abs pxcor != max-pxcor and abs pycor != max-pycor ] [ set pcolor grey ]
ask patches with [ pycor = max-pycor and abs pxcor <= 1 ] [ set pcolor white ]
set-default-shape dead-turtles "circle"
; sprout a first set of turtles:
ask patches with [ pcolor = black ] [
sprout-dead-turtles 1 [ set color red ]
]
; create temporary links between these, and use the
; links to place a new set of turtles in between:
ask dead-turtles [
create-links-with turtles-on neighbors4
]
ask links [
let target end2
ask end1 [
hatch 1 [
face target
fd distance target / 2
]
]
die ; remove the link
]
end
I'm not saying that it is the only possible solution, but it's simple enough, and it works. (World wrapping has to be turned off, though, which I assume is the case.)