bathtub model sprout using decision rules - netlogo

newbie in netlogo here. I'm simulating a flood model using cellular-automata. It's just a simple one - cell should be filled (change color) or sprout if water > elevation.
For a dummy code, I'm trying to do it this way:
to go
ask patches with [pcolor != blue] ;remove ocean
water_rise
tick
end
to water_rise ; saturates cell
if not any? turtles [
ask patch x-breach y-breach [ ;;; This will be the breach patch, will start to fill at first tick, a specific location in my map
set cell-storage elevation * fill-rate
]
]
ask patches [
;;; This has a patch check if any neighbors have sprouted.
;;; If any have, that patch starts to fill.
if any? neighbors4 with [ any? turtles-here ] [
set cell-storage elevation * fill-rate
let minv min [ cell-storage ] of patches
let maxv max [ cell-storage ] of patches
set pcolor scale-color green cell-storage 0 5 ;idea is to have a graduated color depending on fill stage
]
]
;;; Once all patches have had a chance this tick to fill,
;;; see if any are "full"
ask patches [
if cell-storage > elevation [
;; If the patch gets "full" and they have not already sprouted,
if not any? turtles-here [
sprout 1 [
set color yellow
set size 1
set shape "square"
]
]
]
]
end
Thanks in advance!
BTW, I'm working on a DEM re: elevation values.
I set fill-rate as a slider with 0.3 for now.
-Nands

I played around with this a little and it seems to me like you want your starting patch to fill up, and once it hits its maximum value, to start flooding into other cells that repeat the process. I think the main problem is that in your water-rise, you have are asking all patches with elevation greater than -9999 to first call the starting-point procedure and then also to sprout a turtle if they have any neighbors with elevation is less than cell-storage. It appears that all patches satisfy that condition, so all patches will sprout a turtle.
It may work better to rework your flow of logic, so that filling up your breach patch is independent of other patches. Something like:
to water_rise ; saturates cell
if not any? turtles [
ask patch 0 0 [ ;;; This would be your breach patch, will start to fill at first tick
set cell-storage cell-storage + fill-rate
]
]
ask patches [
;;; This has a patch check if any neighbors have sprouted.
;;; If any have, that patch starts to fill.
if any? neighbors4 with [ any? turtles-here ] [
set cell-storage cell-storage + fill-rate
]
]
;;; Once all patches have had a chance this tick to fill,
;;; see if any are "full"
ask patches [
if cell-storage > 0 [
if cell-storage > 5 [
set cell-storage 5
;; If the patch gets "full" and they have not already sprouted, sprout 1
if not any? turtles-here [
sprout 1 [
set color yellow
set size 0.5
set shape "circle"
]
]
]
set pcolor cell-storage + 82
]
]
end
Full toy model with variables and setup here.
Obviously, you would need to modify your starting patch (I used 0 0 for convenience and simplicity). Additionally, I included fill-rate as a way to slow the rate of filling as more and more patches begin getting filled up.

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 can I make the model run?

I am essentially trying to combine elements of the 'Segregation' model and 'Rebellion' model to form a model that is representative of alliance forming.
Here is what I have so far- when I attempt to run it I receive the error: THREATS breed does not own variable ACTIVE?
error while threat 0 running ACTIVE?
called by procedure GO
called by Button 'go'
breed [ agents an-agent]
breed [ threats threat ]
globals [
k ; factor for determining attack probability
threshold ; by how much must D - BS > A to make a state burden share
percent-similar ; on the average, what percent of a turtle's neighbors
; are the same color as that turtle? Likely to ally
percent-unhappy ; what percent of the turtles are unhappy? Or percieve threats?
visualization
]
agents-own [
allied-states ; R, fixed for the agent's lifetime, ranging from 0-1 (inclusive)- for each turtle, indicates whether at least %-similar-wanted percent of
; that turtle's neighbors are the same color as the turtle
perceived-threat ; T, also ranging from 0-1 (inclusive)- how many have a turtle of another color?
active? ; if true, then the agent is actively allied
; if false, then the agent is free-riding
conflict ; how many turns in conflict remain? (if 0, the agent is not in conflict)- sum of previous two variables
total-nearby ; sum of previous two variables
]
patches-own [
neighborhood ; surrounding patches within the vision radius
]
to setup
clear-all
; set globals
set k 2.3
set threshold 0.1
ask patches [
; make background a slightly dark gray
set pcolor gray - 1
; cache patch neighborhoods
set neighborhood patches in-radius vision
]
if initial-threats-density + initial-agent-density > 206 [
user-message (word
"The sum of INITIAL-THREATS-DENSITY and INITIAL-AGENT-DENSITY "
"should not be greater than 206.")
stop
]
; create threats
create-threats round (initial-threats-density * .01 * count patches) [
move-to one-of patches with [ not any? turtles-here ]
display-threats
]
; create agents
create-agents round (initial-agent-density * .01 * count patches) [
move-to one-of patches with [ not any? turtles-here ]
set heading 0
set allied-states random-float 1.0
set perceived-threat random-float 1.0
set active? false
set conflict 0
display-agent
]
; start clock and plot initial state of system
reset-ticks
end
to go
if all? turtles [ active? ] [ stop ]
move-unhappy-turtles
update-turtles
update-globals
tick
end
; unhappy turtles try a new spot
to move-unhappy-turtles
ask turtles with [ not active? ]
[ find-new-spot ]
end
; move until we find an unoccupied spot
to find-new-spot
rt random-float 360
fd random-float 10
if any? other turtles-here [ find-new-spot ] ; keep going until we find an unoccupied patch
move-to patch-here ; move to center of patch
end
to update-turtles
ask turtles [
; in next two lines, we use "neighbors" to test the eight patches
; surrounding the current patch
set allied-states count (turtles-on neighbors) with [ color = [ color ] of myself ]
set perceived-threat count (turtles-on neighbors) with [ color != [ color ] of myself ]
set total-nearby allied-states + perceived-threat
set active? allied-states >= (percent-similar * total-nearby / 100)
; add visualization here
if visualization = "old" [ set shape "default" set size 1.3 ]
if visualization = "square-x" [
ifelse active? [ set shape "square" ] [ set shape "X" ]
]
]
end
to update-globals
let similar-neighbors sum [ allied-states ] of turtles
let total-neighbors sum [ total-nearby ] of turtles
set percent-similar (similar-neighbors / total-neighbors) * 100
set percent-unhappy (count turtles with [ not active? ]) / (count turtles) * 100
; Agents engaged in conflict have the duration reduced at the end of each clock tick
ask agents [ if conflict > 0 [ set conflict conflict - 1 ] ]
; update agent display
ask agents [ display-agent ]
ask threats [ display-threats ]
; advance clock and update plots
tick
end
; AGENT AND THREAT BEHAVIOR
; move to an empty patch
to move ; turtle procedure
if movement? or breed = threats [
; move to a patch in vision; candidate patches are
; empty or contain only jailed agents
let targets neighborhood with [
not any? threats-here and all? agents-here [ conflict > 0 ]
]
if any? targets [ move-to one-of targets ]
]
end
; AGENT BEHAVIOR
to determine-behavior
set active? (burden-sharing - allied-states * estimated-conflict-probability > threshold)
end
to-report burden-sharing
report perceived-threat * (1 - alliance-protection)
end
to-report estimated-conflict-probability
let t count (threats-on neighborhood)
let a 1 + count (agents-on neighborhood) with [ active? ]
; See Info tab for a discussion of the following formula
report 1 - exp (- k * floor (t / a))
end
to alliance
if any? threats [attack]
set active? true
end
; THREAT BEHAVIOR
to attack
if any? (agents-on neighborhood) with [ active? ] [
; arrest suspect
let suspect one-of (agents-on neighborhood) with [ active? ]
move-to suspect ; move to patch of the jailed agent
ask suspect [
set active? false
set conflict random conflict-term
set color pink
]
]
end
; VISUALIZATION OF AGENTS AND COPS
to display-agent ; agent procedure
set color cyan
set shape "triangle"
end
to display-active?
set color pink
set shape "triangle"
end
to display-threats
set color red
set shape "circle 2"
end
The problem is that you have two breeds of turtles, agents and threats, and only agents "own" the variable active?. turtles is a superset of all breeds, so when the all? primitive tries to query the active? variable of literally all turtles, it tries to query active? for threats too, and can't find it. The line should be
if all? agents [ active? ] [ stop ]

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: Can Netlogo set up an infinite number of turtles for only one specific patch?

Can Netlogo set up an infinite number of turtles for only one specific patch? And the patch is road setting. This link is the image of that specific patch. https://i.stack.imgur.com/DdBF0.jpg And the following is the sample code. However this is not compleated.
turtles-at 0 0 of patch min-pxcor 0 ; this is not compleated
Not exactly sure what you're asking, but there is no limit to the number of turtles that can be on a patch (save the limit imposed by your computer's memory).
Also, the code you're probably looking for is something like:
turtles-on patch 0 0
for the left patch and
turtles-on patch 1 0
for the right patch.
As per Bryan's answer, there is no theoretical restriction on the number of turtles in a single patch, although your computer will have a limit- the more turtles in your model (on any patch) the more memory your model will use. So the short answer is, as far as I know, there is no way to just say to Netlogo, "Spawn infinite turtles on this patch."
If, however, by infinite you really just want enough turtles that you won't run out of them for specific interactions, you could probably get by either by just spawning a large number on that patch or by just sprouting more as needed (my preference).
For the first option, you can have a bunch of turtles on the same patch:
to setup
ca
reset-ticks
ask patch 0 0 [
sprout 10000
]
ask patch 0 0 [
print count turtles-here
]
end
Alternatively, if your turtles on the patch get used up or become unavailable in some way, just have more sprout as needed to keep your numbers high enough for what you're trying to do. Here's an example where red turtles walk to a patch with "infinite" (1000) blue turtles, link to one of the blue turtles, and take them away. However, at the end of each tick, the "infinite" patch checks if there are fewer than 1000 turtles-here. If there are, it spawns enough turtles to bring that count back up to 1000. Try this code in a new file:
to setup
ca
reset-ticks
infinite-sprout
source-sprout
end
to go
ask turtles with [ color = red ] [
fd 0.5
if any? ( turtles-on patch-ahead 1 ) with [ color = blue ] [
create-link-with one-of turtles-on patch-ahead 1 [
tie
]
set color green
]
]
ask turtles with [color = green] [
move-to patch-right-and-ahead 90 1
if pycor = max-pycor [
ask link-neighbors [
die
]
die
]
]
infinite-sprout
source-sprout
tick
end
to source-sprout
ask patch max-pxcor 0 [
if not any? turtles-here and random 3 = 1 [
sprout 1 [
set shape "arrow"
set color red
set heading 270
]
]
]
end
to infinite-sprout
ask patch 0 0 [
if count turtles-here < 1000 [
sprout ( 1000 - count turtles-here) [
set shape "circle"
set color blue
]
]
]
end
Then set up your interface like this:
If you run that model for a while, you will see that at the end of every tick, the count turtles of patch 0 0 is brought back up to 1000, effectively giving you an infinite source of turtles that you can "use up." Does that accomplish what you need?

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.)