How to add power law likelihood to Netlogo Model - netlogo

I would like to add a likelihood of natural disaster in my environmantal ABM that follows the power law (often few damage, less often mediocre damage, rarely strong damage, very rarely complete damage).
I coded so far the following:
to environment ;environmental hits
create-hits 1 [ ; I do not know if it makes sense to do that?
set shape "circle"
set color white
set size 0.05
setxy random-xcor random-ycor
]
ask hits [
ifelse pcolor = red [die] ;if already red, then stop
[ ask n-of random (count patches) patches [ set pcolor red ]] ;otherwise turn red on an random amount of patches
]
end
Now, I do not know how to add the stochastic element of how strong the "hit" can be (thus, how much patches can be affected) and how likely (according to the power law) it is to happen (or not to happen) each tick. Can somebody help me out?
This is the final code (answered by Alan):
to environment
create-hits 1 [
set shape "circle"
set color white
set size 0.05
setxy random-xcor random-ycor
]
ask hits [
let %draw (random-float 100)
let %strength 0 ;; no damage
if (%draw < 50) [set %strength (%strength + 1)] ;;1 for little damage
if (%draw < 10) [set %strength (%strength + 1)] ;;2 for middle damage
if (%draw < 5) [set %strength (%strength + 1)] ;;3 for strong damage
if (%draw < 1) [set %strength (%strength + 1)] ;;4 for complete destruction
ifelse pcolor = red [die]
[ ask n-of %strength patches [ set pcolor red ]]
]
end

This is just an elaboration of the comment by #Mars.
to-report hit-strength
let %draw (random-float 100)
let %strength 0 ;; no damage
if (%draw < 50) [set %strength (%strength + 1)] ;;1 for little damage
if (%draw < 10) [set %strength (%strength + 1)] ;;2 for middle damage
if (%draw < 5) [set %strength (%strength + 1)] ;;3 for strong damage
if (%draw < 1) [set %strength (%strength + 1)] ;;4 for complete destruction
report %strength
end

Related

How to create turtles with random xycor but also in the center of a patch?

I am working with the daisyworld model and am planning to add a predator to the model which will eat the daisies (this is for a class project). I have successfully added the predators (brown rabbits eat the white daisies, grey rabbits eat the black daisies). But in order for them to eat the daisies, their coordinates have to line up perfectly causing very little "eating" to occur. Is there a way for me to generate rabbits with random xy cor but be generated in the exact center of the pixel?
globals [
max-age ;; maximum age that all daisies live to
global-temperature ;; the average temperature of the patches in the world
num-blacks ;; the number of black daisies
num-whites ;; the number of white daisies
scenario-phase ;; interval counter used to keep track of what portion of scenario is currently occurring
]
breed [daisies daisy]
breed [brown-rabbits brown-rabbit]
breed [grey-rabbits grey-rabbit]
patches-own [temperature] ;; local temperature at this location
daisies-own [
age ;; age of the daisy
albedo ;; fraction (0-1) of energy absorbed as heat from sunlight
daisy-black ;; boolean if daisy is black
]
brown-rabbits-own
[energy]
grey-rabbits-own
[energy]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Setup Procedures ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup
clear-all
set-default-shape daisies "flower"
ask patches [ set pcolor gray ]
set max-age 25
set global-temperature 0
if (scenario = "ramp-up-ramp-down" ) [ set solar-luminosity 0.8 ]
if (scenario = "low solar luminosity" ) [ set solar-luminosity 0.6 ]
if (scenario = "our solar luminosity" ) [ set solar-luminosity 1.0 ]
if (scenario = "high solar luminosity") [ set solar-luminosity 1.4 ]
seed-blacks-randomly
seed-whites-randomly
ask daisies [set age random max-age]
ask patches [calc-temperature]
set global-temperature (mean [temperature] of patches)
update-display
;reset-ticks
create-grey-rabbits grey-rabbit-number
[
setxy random-xcor random-ycor
set shape "rabbit"
set size 1.5
set color grey
set energy random (2 * grey-rabbit-gain-from-food)
]
create-brown-rabbits brown-rabbit-number
[
setxy random-xcor random-ycor
set shape "rabbit"
set size 1.5
set color brown
set energy random (2 * brown-rabbit-gain-from-food)
]
reset-ticks
end
to seed-blacks-randomly
ask n-of round ((start-%-blacks * count patches) / 100) patches with [not any? daisies-here]
[ sprout-daisies 1 [set-as-black] ]
ask daisies [set daisy-black true]
end
to seed-whites-randomly
ask n-of floor ((start-%-whites * count patches) / 100) patches with [not any? daisies-here]
[ sprout-daisies 1 [set-as-white] ]
ask daisies [set daisy-black false]
end
;to daisy-black
; ifelse color = 0
; [daisy-black true]
; [daisy-black false]
;end
;ask daisies if color = 0 [set daisy-black true]
;ask daisies if color = 9.9 [set daisy-black false]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Runtime Procedures ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to go
ask grey-rabbits [
move
set energy energy - 1
eat-black-daisies
death
reproduce-grey-rabbits
]
ask brown-rabbits [
move
set energy energy - 1
eat-white-daisies
death
reproduce-brown-rabbits
]
ask patches [calc-temperature]
diffuse temperature .5
ask daisies [check-survivability]
set global-temperature (mean [temperature] of patches)
update-display
tick
if scenario = "ramp-up-ramp-down" [
if ticks > 200 and ticks <= 400 [
set solar-luminosity precision (solar-luminosity + 0.005) 4
]
if ticks > 600 and ticks <= 850 [
set solar-luminosity precision (solar-luminosity - 0.0025) 4
]
]
if scenario = "low solar luminosity" [ set solar-luminosity 0.6 ]
if scenario = "our solar luminosity" [ set solar-luminosity 1.0 ]
if scenario = "high solar luminosity" [ set solar-luminosity 1.4 ]
end
to move
rt random 20
lt random 20
fd 1
end
to eat-black-daisies ;grey rabbits eat black daisies
ask grey-rabbits
[if any? daisies-here with [daisy-black = true]
[let prey one-of daisies-here; with [daisy-black = true]
if prey != nobody
[ask prey [die]
set energy energy + grey-rabbit-gain-from-food]
]
]
end
to eat-white-daisies ;brown rabbits eat white daisies
; ask brown-rabbits
; ;[if any? daisies-here with [daisy-black = false]
; patch-here
; [let prey one-of daisies-here with [daisy-black = false]
; if prey != nobody
; [ask prey [die]
; set energy energy + brown-rabbit-gain-from-food]
; ]
; ;]
end
to reproduce-grey-rabbits
if random-float 100 < grey-rabbits-reproduce [
set energy (energy / 2)
hatch 1 [rt random-float 360 fd 1]
]
end
to reproduce-brown-rabbits
if random-float 100 < brown-rabbits-reproduce [
set energy (energy / 2)
hatch 1 [rt random-float 360 fd 1]
]
end
to death
if energy < 0 [die]
end
to set-as-black ;; turtle procedure
set color black
set albedo albedo-of-blacks
set age 0
set size 0.6
end
to set-as-white ;; turtle procedure
set color white
set albedo albedo-of-whites
set age 0
set size 0.6
end
to check-survivability ;; turtle procedure
let seed-threshold 0
let not-empty-spaces nobody
let seeding-place nobody
set age (age + 1)
ifelse age < max-age
[
set seed-threshold ((0.1457 * temperature) - (0.0032 * (temperature ^ 2)) - 0.6443)
;; This equation may look complex, but it is just a parabola.
;; This parabola has a peak value of 1 -- the maximum growth factor possible at an optimum
;; temperature of 22.5 degrees C
;; -- and drops to zero at local temperatures of 5 degrees C and 40 degrees C. [the x-intercepts]
;; Thus, growth of new daisies can only occur within this temperature range,
;; with decreasing probability of growth new daisies closer to the x-intercepts of the parabolas
;; remember, however, that this probability calculation is based on the local temperature.
if (random-float 1.0 < seed-threshold) [
set seeding-place one-of neighbors with [not any? daisies-here]
if (seeding-place != nobody)
[
if (color = white)
[
ask seeding-place [sprout-daisies 1 [set-as-white] ]
]
if (color = black)
[
ask seeding-place [sprout-daisies 1 [set-as-black] ]
]
]
]
]
[die]
end
to calc-temperature ;; patch procedure
let absorbed-luminosity 0
let local-heating 0
ifelse not any? daisies-here
[ ;; the percentage of absorbed energy is calculated (1 - albedo-of-surface) and then multiplied by the solar-luminosity
;; to give a scaled absorbed-luminosity.
set absorbed-luminosity ((1 - albedo-of-surface) * solar-luminosity)
]
[
;; the percentage of absorbed energy is calculated (1 - albedo) and then multiplied by the solar-luminosity
;; to give a scaled absorbed-luminosity.
ask one-of daisies-here
[set absorbed-luminosity ((1 - albedo) * solar-luminosity)]
]
;; local-heating is calculated as logarithmic function of solar-luminosity
;; where a absorbed-luminosity of 1 yields a local-heating of 80 degrees C
;; and an absorbed-luminosity of .5 yields a local-heating of approximately 30 C
;; and a absorbed-luminosity of 0.01 yields a local-heating of approximately -273 C
ifelse absorbed-luminosity > 0
[set local-heating 72 * ln absorbed-luminosity + 80]
[set local-heating 80]
set temperature ((temperature + local-heating) / 2)
;; set the temperature at this patch to be the average of the current temperature and the local-heating effect
end
to paint-daisies ;; daisy painting procedure which uses the mouse location draw daisies when the mouse button is down
if mouse-down?
[
ask patch mouse-xcor mouse-ycor [
ifelse not any? daisies-here
[
if paint-daisies-as = "add black"
[sprout-daisies 1 [set-as-black]]
if paint-daisies-as = "add white"
[sprout-daisies 1 [set-as-white]]
]
[
if paint-daisies-as = "remove"
[ask daisies-here [die]]
]
display ;; update view
]
]
end
to update-display
ifelse (show-temp-map? = true)
[ ask patches [set pcolor scale-color red temperature -50 110] ] ;; scale color of patches to the local temperature
[ ask patches [set pcolor grey] ]
ifelse (show-daisies? = true)
[ ask daisies [set hidden? false] ]
[ ask daisies [set hidden? true] ]
end
; Copyright 2006 Uri Wilensky.
; See Info tab for full copyright and license.
Instead of checking whether 2 turtles are exactly on the same spot, I would use patch-here to see if they are on the same patch.
https://ccl.northwestern.edu/netlogo/docs/dictionary.html#patch-here

How to ask turtles to avoid a patch with specific color at patch-ahead 1 but turtles move forward by bigger number (not fd 1) in one step in NetLogo

I am trying to ask turtles with any heading (random 360) to avoid the patches with red color. But, I observed that if a turtle is asked to move " fd 1 + random-float 2.0" then sometime turtles turns (set heading heading - 180) when there is a red patch ahead and sometimes(even most of the times) do not turn. Also when I ask the turtles to move " fd 1 " or " fd 0.1 + random-float 0.9 " the code works all fine. Hopefully the reason behind is the number of patches I am asking the turtles to move in one step. What will be the next patch for the move "fd 0.1 + random-float 0.9" and how could I make this working with patch-ahead 1. My code and the interface is added.
to setup
clear-all
ask patches [set pcolor green ]
ask patches with [pycor = 3] [set pcolor red]
create-turtles 40
[
set color blue
set xcor random-pxcor
set ycor random-pycor
set heading random 360
set size 1
set speed 1 + random-float 2.0
]
end
to go
ask turtles [
fd speed
avoid-walls
]
end
to avoid-walls
if [pcolor] of patch-ahead 1 = red [set heading heading - 180]
end
Try using in-cone instead of patch-ahead
to avoid-walls
let front-patches patches in-cone 2 75
if pcolor of one-of front-patches = red [set heading heading - 180]
end

why does the hatchign stop after some time?

Morning everybody
After editing my model, by adding a random spawn rate:
to migrate
if random-float 100 < random-spawn-rate
[create-turtles 2 [rt random-float 360 fd 1]]
end
i get the problem that after one kind of turtles (the boats) die out, even their random hatching seems to die out?
i tried other ways, but nothing really works
Please help me
thank you
Full code:
breed [fish a-fish]
breed [boats boat]
boats-own [profit]
to setup
clear-all
ask patches [set pcolor blue]
set-default-shape fish "fish"
create-fish initial-number-fish
[
set color grey
set size 1.0
setxy random-xcor random-ycor
]
set-default-shape boats "boat"
create-boats initial-number-boats
[
set color black
set size 1.5
set profit random (1 * profit-per-fish)
setxy random-xcor random-ycor
]
reset-ticks
end
to go
if not any? turtles [stop]
ask fish
[
move
fish-reproduce
]
ask boats
[
move-boats
catch-fish
death
reproduce-boats
migrate
]
tick
end
to move
rt random 50
lt random 50
fd 1
end
to fish-reproduce
if random-float 100 < fish-growth
[hatch 1 [rt random-float 360 fd 1]]
end
to move-boats
rt random 50
lt random 50
fd 1
set profit profit - 0.1
end
to catch-fish
let prey one-of fish-here
if prey != nobody
[ask prey [die]
set profit profit + profit-per-fish]
end
to death
if profit < 0 [die]
end
to reproduce-boats
if profit > 1
[
set profit (profit / 2)
hatch 1 [rt random-float 360 fd 1]]
end
to migrate
if random-float 100 < random-spawn-rate
[create-turtles 2 [rt random-float 360 fd 1]]
end
If there are no boats to hatch new boats, then no more boats will be created. Your hatch is dependent on how many boats there are.
Essentially, for a boat:
if profit > 1
[
set profit (profit / 2)
hatch-boat 1 ...
]
You cut the profit in half for every boat spawned. Unless your profit grows or remains the same, your boats will eventually die out since no more will be hatching.

Changing patch colors except one color

I'm creating a program in Netlogo which has shoppers (turtles) moving through a grocery store layout. When they step on a patch it increases in color and when it has no agent on it, it decreases in color, as this will show the paths shoppers take through a store.
My code is:
ask turtles
[ rt random 360
fd 1
set pcolor pcolor + 1 ]
ask patches with [ (pcolor > 9.9) or (pcolor < 0.1) ]
[set pcolor 0]
ask patches with [ (count turtles-here = 0) and (pcolor <= 9.9) and (pcolor > 0) ]
[ set pcolor pcolor - 0.1 ]
However, as the aisle patches are blue this is turning them back to black as well. I was wondering what code I could use so patches with pcolor = 105 will stay blue and not change to black?
Don't change the color of the patches with pcolor = 105. You'll just need to add an additional condition to anywhere you modify the patch color.
ask turtles
[ rt random 360
fd 1
if pcolor != 105[set pcolor pcolor + 1 ]
]
ask patches with [ pcolor != 105 and ((pcolor > 9.9) or (pcolor < 0.1))]
[set pcolor 0]
ask patches with [pcolor != 105 and (count turtles-here = 0) and (pcolor <= 9.9) and (pcolor > 0) ]
[ set pcolor pcolor - 0.1 ]

How can I include the effect of patches age in netlogo?

How can I accomplish the following in the code below:
patches change color reflective of their distance from the row "min-pycor"
For example, colors alternate from yellow to red and then to black (signifying death).
But this should take into account that the production of yellow patches > red > black.
turtles-own
[
stem? ;; true for stem cells, false for transitory cells
age ;; age of cell. changes color with age
metastatic? ;; false for progeny of stem cell 0, true for progeny of stem cell 1
]
globals
[
cell-count
]
to setup
clear-all
set-default-shape turtles "square"
ask patches[
if pycor = min-pycor [
ifelse random 10 <= 2
[set pcolor white]
[sprout 1 [set shape "square" set color blue] ]
]
]
evaluate-params
reset-ticks
end
to go
ask patches with [pcolor = yellow]
[if count neighbors with [pcolor = black] > 0
[ask one-of neighbors with [pcolor = black][set pcolor yellow]
]
]
ask patches with [pcolor = white]
[if count neighbors with [pcolor = black] > 0
[ask one-of neighbors with [pcolor = black][set pcolor yellow]
]
]
tick
end
;;transitional cells move and hatch more. Turtle proc.
to move-transitional-cells
if (not stem?)
[
set color ( red + 0.25 * age )
fd 1
if (age < 6)
[
hatch 1
[ ;amplification
rt random-float 360
fd 1
]
]
]
end
to mitosis ;; turtle proc. - stem cells only
if stem?
[
hatch 1
[
fd 1
set color red
set stem? false
ifelse (who = 1)
[ set age 16 ]
[ set age 0 ]
]
]
end
to death ;; turtle proc.
if (not stem?) and (not metastatic?) and (age > 20)
[ die ]
if (not stem?) and metastatic? and (age > 4)
[ die ]
end
to evaluate-params
set cell-count count turtles ;cell count
if (cell-count <= 0)
[ stop ]
end
to kill-original-stem-cell
ask turtle 0
[ die ]
end
to kill-moving-stem-cell
ask turtle 1
[ die ]
end
to kill-transitory-cells
ask turtles with [ age < 10 and not stem? ]
[ die ]
end
You seem to have two conflicting requirements, the color change based on proximity in your code, and the color change based on PYCOR that you ask about.
Ignoring the code for a moment, we can set color based on PYCOR in many ways. For example, we can create bands of color, we can create dithered interminglings of color, or we can create a "smooth" transition between colors.
The first is easy. We can use an IFELSE structure. This example creates even bands, but you can change the "divide" variables to create bands of any height.
let color1 red
let color2 yellow
let color3 black
let divide1 (min-pycor + world-height * 0.33)
let divide2 (min-pycor + world-height * 0.66)
ask patches
[ ifelse pycor < divide1 [ set pcolor color1 ][
ifelse pycor < divide2 [ set pcolor color2 ][
set pcolor color3
]]
]
We can also do it in a mathy way. This example creates even bands.
let colors [ red yellow black ]
let bands length colors
let band-height floor ( world-height / bands + .5 )
;; pycor - min-pycor shifts the range from 0 to world-height
ask patches [ set pcolor item ( floor ( ( pycor - min-pycor ) / band-height ) ) colors ]
We can create dithered bands by introducing a random element to the pycor. we also have to add some tests to keep the random numbers in range.
let colors [ red yellow black ]
let bands length colors
let band-height floor (world-height / bands + .5)
let dither band-height * .5 ;; adjust this to change dithery-ness
ask patches
[ let py pycor - min-pycor + dither - random-float ( dither * 2 )
;; you might want to really study the above line to fully grok what's happening there
if py < 0 [ set py 0 ]
if py > world-height [ set py world-height ]
set pcolor item ( floor ( py / band-height ) ) colors
]
Graduated (gradiant) bands are tougher, because the NetLogo color space doesn't do gradual shifts in hue, only tint and shade, so there's really no way to get from red to yellow that way. So we would have to use RGB (three value list)colors, instead of NetLogo (single value) colors. And that's beyond what I'm willing to type out at the moment, so there you go--left as an exersize.