How to check if a patch remains a state for a certain ticks\time? - netlogo

Based on the “sheep eat grass” model. The patches are changing color with the agents move. I want to check if a patch has remain a centain color for a while (such as 5 ticks). If a patch has stayed in a centain color for a centain times without any change it will turn into black.
I try to use countup but it didn’t work. I need a accumulated state. Thanks a lot
If pcolor=green[
Ifelse countup>=5[
Set pcolor black
Set countup 0]
[set countup countup+1]]

Can you give a little more detail as to what is going wrong with the code you've shown? For example, with this setup:
patches-own [ time-spent-green ]
to setup
ca
crt 3
reset-ticks
end
Something very similar to your example works fine for me:
to go
ask turtles [
rt random 61 - 30
fd 1
ask patch-here [
set pcolor green
]
]
ask patches with [ pcolor = green ] [
ifelse time-spent-green >= 5 [
set pcolor black
set time-spent-green 0
] [
set time-spent-green time-spent-green + 1
]
]
tick
end
Where the patches stay green for 5 ticks then turn back to black.

Related

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.

set a demand and supply curve for the model tragedy of the commons in the case of an overfished pond

I am new at net logo and I want to write a model based on tragedy of the commons in the case of an overfished pond. The purpose is to find an equilibrium between fishers and fishes based on an economic model with demand and supply. If there are less fishers, more fishes will be in the pond, then after a certain time (ticks) the number of fishers increases and less fishes will be in the pond. Maybe set like a number of fishes per day that can be fished. Thus, the solution is to find a convenient number of fishers as the fishes can still reproduce. I want to have a box in the interface where I can type in a number and see what happens with the number of fishes.
I have no idea how to set this up. I hope to hear from you :)
I started with this code:
breed [fishers fisher]
breed [fishes fish]
to setup
clear-all
reset-ticks
ask patches [set pcolor blue ] ;; lake/pond in form of a rectangle in color
ask patches [ if pxcor > 8 [ set pcolor green ]]
ask patches [ if pycor > 8 [ set pcolor green ]]
ask patches [ if pycor < -8 [ set pcolor green ]]
ask patches [ if pxcor < -8 [ set pcolor green ]]
ask one-of patches with [ pcolor = blue ] [ sprout 20 [set shape "fish" set color pink set size 1.5 ]] ;; creates fishes
ask one-of patches with [ pcolor = green ] [ sprout 2 [set shape "person" set color black set size 3 ] ] ;; creates fishers
end
to go
tick
;;fishes
ask turtles with [ shape = "fish" and color = pink ]
[ right random 360 forward 1
if [pcolor] of patch-ahead 1 = green [ right 180 fd 1 ]]
;; fishers
ask turtles with [ shape = "person" and color = black]
[;right random 360 forward 1
if any? patches with [pcolor = blue]
[set heading towards one-of patches with [pcolor = blue] forward 1]
if [pcolor] of patch-ahead 1 = blue [ right 180 fd 2 ]]
ask turtles with [shape = "person" and color = black]
[if any? turtles with [shape = "fish" and color = pink] in-radius 2
[ask turtles with [shape = "fish" and color = pink] in-radius 2 [die]]]
end
Firstly, I suggest you look through existing models in the Netlogo library (Wolf-sheep-predation model may help). You roughly have the right idea in your current code, but you should look at other models to improve. You've already set your different breeds of turtles, but you should also set up their respective shapes under 'setup'. This would help you a great deal later - instead of calling for
ask turtles with [ shape = "fish"...]
you can simply
ask fishes [do sth...]
For that 'box at the interface', you can have a slider at the interface determining the number of fishers you want your run to start with. With another slider, you can set the fishing pressure in your simulated run (i.e. how many fish each fisher will catch) and I suppose you can also consider how this changes when population of fish decreases.
Finally, for a model like yours, you can observe the supply and demand trend by plotting the curves of no. of fishers over time and no. of fishes over time. Again, look at the wolf-sheep-predation model to have an idea of how to do this.
I can't give you more than this I'm afraid since I'm no pro myself but hope this helps a little. Hope someone else would be able to give you a clearer idea.

how to see the color identification signal lights?

layout intersections with traffic lights. I want the car to stop when the red lights and the lights go xanh.thi what I should use for car algorithm can recognize the color of the lights
I have used light layout algorithm as follows
to draw-stop-light
ifelse stop-light = "north"
[
ask patch -7 12 [set pcolor green ]
ask patch 10 10 [ set pcolor red ]
ask patch -10 -10 [ set pcolor red ]
]
[
ask patch -7 12 [ set pcolor red ]
ask patch 10 10 [ set pcolor green ]
ask patch -10 -10 [ set pcolor green ]
]
end
This is really a question better suited to the NetLogo users group here than Stack Overflow since you are asking how to program a poorly defined specification. For example, the users group may be able to point you to a similar model. Stack Overflow is really about fixing specific problems with your code. However, here's something to get you started until the question gets deleted.
Break down what you want to happen into very small steps and then code each step. The steps will be something like:
When the car gets near the intersection, check to see what colour the traffic light is
If the light is red, remember to stop at the intersection
Stop at the intersection if required
If at the intersection, wait until the light turns green then go
Do NOT try to write the whole thing. Instead, write one bit at a time and test is to see if it works. Here is some approximate code for the first bit:
ask cars
[ ifelse heading = 180 and ycor > 20 and ycor < 25 and not stop-flag
[ if [ pcolor of patch -7 12 ] = red [ set stop-flag true ] ]
[ ... (cars from other directions)
]
ask cars
[ forward speed
if stop-flag and ... (on intersection patches)
[ ... (move backwards to be outside the intersection)
set stop-flag false
]
]
patch-right-and-ahead (http://ccl.northwestern.edu/netlogo/docs/dictionary.html#patch-lr-and-ahead) could be useful here. Perhaps something like:
if [pcolor] of patch-right-and-ahead 90 1 = red
[ set speed 0 ]

NetLogo Turtle position

I'm really new at programming in NetLogo and i need a little help. I have an assignment and i did most of it. The thing left to do is to make robot walk in labyrinth. Robot can walk only on a black patches (violet patches represent the obstacles).
So, the thing i need help with is to position robot in the center of the labyrinth - i must do it with "patch-here" (...i did it little bit differently in procedure "stvori-agenta") and mark that patch on which robot stands as black. So, afterwards i could write procedures for robots movements only on a black patches.
Here is the code:
breed [robots robot]
to crtaj-zidove
ask patches with
[
( pxcor = max-pxcor)
or (pxcor = min-pxcor)
or ( pycor = max-pycor)
or (pycor = min-pycor) ]
[ set pcolor violet]
end
to labirint
ask n-of 15 patches with [ pcolor != violet ] [
set pcolor violet]
end
to stvori-agenta
set-default-shape robots "robot"
ask patch 5 5 [ sprout-robots 1 ]
ask turtles [
set heading 0
set color grey
]
end
to setup
clear-all
crtaj-zidove
labirint
stvori-agenta
end
This will make the robot turn the patch it is standing on black:
ask robots [ set pcolor black ]
You say you must use patch-here. That isn't actually necessary, since turtles have direct access to the patches they are standing on. But you could also write it as:
ask robots [ ask patch-here [ set pcolor black ] ]
It does the same thing.