Simplifying code - multiples of x (NetLogo - netlogo

I currently have a piece of coding which I want to simplify. Essentially I want a procedure to happen every time a custom value equals a multiple of x. The way I have done it so far is to write a separate line of code for each multiple up to 35, however I want it to go much higher than 35, the way it is so far is messy, time consuming and takes up space! I need some piece of coding that accounts for any whole integer within a range (I think!).
Currently I have this (I have only shown you the first 5 lines so as not to fill up the thread, but I currently go up to 35):
to fly-emergence
ask flies
[if (age >= (((fly-life-expectancy / fly-life-progeny) * 1) - 0.5)) and (age < (((fly-life-expectancy / fly-life-progeny) * 1) + 0.5)) [hatch 1 [set age 0]]]
ask flies
[if (age >= (((fly-life-expectancy / fly-life-progeny) * 2) - 0.5)) and (age < (((fly-life-expectancy / fly-life-progeny) * 2) + 0.5)) [hatch 1 [set age 0]]]
ask flies
[if (age >= (((fly-life-expectancy / fly-life-progeny) * 3) - 0.5)) and (age < (((fly-life-expectancy / fly-life-progeny) * 3) + 0.5)) [hatch 1 [set age 0]]]
ask flies
[if (age >= (((fly-life-expectancy / fly-life-progeny) * 4) - 0.5)) and (age < (((fly-life-expectancy / fly-life-progeny) * 4) + 0.5)) [hatch 1 [set age 0]]]
ask flies
[if (age >= (((fly-life-expectancy / fly-life-progeny) * 5) - 0.5)) and (age < (((fly-life-expectancy / fly-life-progeny) * 5) + 0.5)) [hatch 1 [set age 0]]]
end

You're looking for the mod primitive:
to fly-emergence
ask flies [
if (round age) mod (fly-life-expectancy / fly-life-progeny) = 0
[ hatch 1 [ set age 0 ] ]
]
end
I'm not absolutely sure I've matched your code exactly... but the basic ideas are, use mod to make something periodic, and use round to avoid the "-0.5 to 0.5" part of the check.

Related

Calculate index of dissimilarity in NetLogo

I'm want to calculate the index of dissimilarity in NetLogo. I have a world divided into different regions and want to examine how evenly species are distributed around the world.
Consider this example: A world is divided into 16 different regions. The world is populated with two types of ants, red and blue. It looks like this:
The world in the picture is produced with the following code:
globals[indexdissimilarity] ; where I want the index of dissimilarity to be stored.
to setup
ca
;Setting world.
resize-world 0 19 0 19
set-patch-size 15
;Creating regions.
let x 5
let y 5
let col 45
while [y <= max-pycor + 1 ][
while [x <= max-pxcor + 1][
ask patches with [pxcor < x and pxcor >= x - 5 and pycor < y and pycor >= y - 5][
set pcolor col
]
set x x + 5
set col col + 3
]
set x 5
set y y + 5
]
ask n-of (count patches * 0.85) patches[sprout 1[
set shape "bug"
set color red]]
ask n-of (count turtles * 0.50) turtles [set color blue]
dissimilarity
end
; Here is where I want to calculate the index of dissimilarity.
to dissimilarity
let tot_red (count turtles with [color = red])
let tot_blue (count turtles with [color = blue])
; set indexdissimilarity
end
My main issue is how to iterate parts of the calculations over each neighborhood.
Thanks!
I think I managed to solve it. Please, let me know if it looks correct. Here is the full updated code.
globals[indexdissimilarity
dis
]
patches-own [reg]
to setup
ca
;Setting world.
resize-world 0 19 0 19
set-patch-size 15
;Creating regions.
let x 5
let y 5
let col 45
while [y <= max-pycor + 1 ][
while [x <= max-pxcor + 1][
ask patches with [pxcor < x and pxcor >= x - 5 and pycor < y and pycor >= y - 5][
set pcolor col
]
set x x + 5
set col col + 3
]
set x 5
set y y + 5
]
ask patches [set reg [pcolor] of self]
ask n-of (count patches * 0.85) patches[sprout 1[
set shape "bug"
set color red]]
ask n-of (count turtles * 0.7) turtles [set color blue]
update
end
to update
;Dissimilarity index.
let tot_red (count turtles with [color = red])
let tot_blue (count turtles with [color = blue])
let neighb1 [reg] of turtles
let neighb remove-duplicates neighb1
set dis []
foreach neighb [i -> set dis lput abs((count turtles with [reg = i and color = red] / tot_red) - (count turtles with [reg = i and color = blue] / tot_blue)) dis]
set indexdissimilarity sum(dis) / 2
print(indexdissimilarity)
end

How do I find find out probability of Y occurring depends on both X1 and X2 in Netlogo? Y , X1 and X2 are turtle variables

I have specifically two questions:
How do I find out the probability of leadershipactivation depends on both leaderactivation and othersperception.
How to I find out that the probability of leaderactivation depends on motivation (condition motivation > 3.32), selfschemata (selfschemata >7) and neti (social factors, neti>0) at a particular time.
'''
globals [taskdemand ;; the taskdemand of the turtles
percent-LA;
percent-LP;
percent-LI;
probLA;
probLP;
probLI;
err;;
;percent-LIr
;percent-aLAnLp
;percent-nLALp
;percent-LALP
;percent-nLAnLP
]
turtles-own [
motivation ;;motivation to lead of each turtle
selfschemata ;; self-schemata of each turtle
status ;;status of each turtle
others;;agentset of agents within same group
;;teammates;; agentset of teammates
othersactivation;; othersactivation computes leaderactivation of each turtle within the group of turtle
allothers;;agentset of all other agents within the collective
neti;;neti computes socialfactors and taskdemand of each turtle
x;;x is prospective leader's perceived characteristics
y;;y is perceivers ILT for various dimensions
othersperception ;; leadershipperception of all other turtles in collective level
leaderactivation;;leader self-schema activation
leadershipperception;; leadershipperception of each turtle in relational level
leadershipactivation;; leadershipactivation in a collective level
leadershipactivationr;;
mygroup;; group of turtles
LA?;;
aLA?;;
aLP?;;
aLi?;;
aLir?
aLAnLp?
nLALp?
LALP?
nLAnLP?
]
to setup
clear-all; clears ticks, patches, plots
ask patches[ set pcolor white];;blank background
crt 100 [
setxy random-xcor random-ycor
set shape "person"
set motivation random-normal 3.32 0.75;;motivation to lead is normally distributed with mean 3.32 and standard deviation 0.75
set selfschemata (random 11) + 1 ;;sets selfschemata 1 to 11
set status random-normal 4 0.49 ;;status is normally distributed with mean 4 and standard deviation 0.49
set leaderactivation random 1 ;;sets leaderactivation values 0 to each turtle (self-schema activation, individual level)
set leadershipperception random 1 ;; sets leadershipperception values 0 to each turtle (relational level)
set neti random 1 ;;sets neti values 0 to each turtle, neti calculates the social factors
set othersactivation random 1 ;;sets each turtles, othersactivation (i.e., leaderactivation) value in a group to 0
set othersperception random 1;;sets othersperception values 0 to each turtle
set leadershipactivation random 1;; sets leadershipactivation values 0 to each turtle (collective level)
set leadershipactivationr random 1;;
set others no-turtles
set allothers no-turtles
if ( status > 4) OR (status = 4)[set color blue]
if ( status < 4)[set color red]
set x n-values 5 [random 1];; setting random values 0 or 1 for 5 dimensions of prospective leader's perceived characteristics
set y n-values 5[random 1];;perceiver's ILT for various dimensions
set mygroup -1
]
assign-by-size
set taskdemand [0.1 0.9]
set err random-normal 0 0.01;;setting error with mean 0 and standard deviation 0.01
reset-ticks
end
to assign-by-size
let unassigned turtles
let current 0
while [any? unassigned]
[
;; place a randomly chosen set of group-size turtles into the current
;; group. or, if there are less than group-size turtles left, place the
;; rest of the turtles in the current group.
ask n-of (min (list group-size (count unassigned))) unassigned
[ set mygroup current ]
;; consider the next group.
set current current + 1
;; remove grouped turtles from the pool of turtles to assign
set unassigned unassigned with [mygroup = -1]
]
end
to go
if ticks >= 300 [ stop ]
ask turtles [
if (mygroup != -1) [leader-activation] ;; individual level
if (mygroup != -1) [ checksocialfactors] ;; individual level
if (leaderactivation > 2) OR (leaderactivation = 2) [ set x n-values 5 [random 2]]
if (mygroup != -1) [ ilt-match];;relational level
ifelse (leaderactivation > 2) OR (leaderactivation = 2)[ set leadershipperception leadershipperception ][set leadershipperception 0]
if (mygroup != -1) [set leadershipactivationr leadershipperception + leaderactivation]
if (mygroup != -1) [collectivelevel];;collective level
if (mygroup != -1)[set leadershipactivation othersperception + leaderactivation]
]
update-variables
tick
end
to leader-activation ;; leader activation at the individual level
ifelse (motivation > 3.32) OR (motivation = 3.32) [set leaderactivation leaderactivation + 1 ][set leaderactivation leaderactivation + 0]
if (selfschemata > 0) AND (selfschemata < 5)[set leaderactivation leaderactivation + 0]
if (selfschemata > 4) AND (selfschemata < 8)[set leaderactivation leaderactivation + 0.5 ]
if (selfschemata > 7) AND (selfschemata < 12)[set leaderactivation leaderactivation + 1 ]
ifelse (status > 4) OR (status = 4) [set leaderactivation leaderactivation + 1][set leaderactivation leaderactivation - 1]
show leaderactivation
end
to checksocialfactors ;; turtle procedure to check social factors of the turtles
find-others
if any? others
[ find-others-activation
find-othersactivation-and-taskdemand
let lachange 0
let s (1 - leaderactivation) * neti
let t (leaderactivation + 0.2) * neti
ifelse (neti > 0) [set lachange lachange + (0.4 * s) - (0.1 * leaderactivation)] [set lachange lachange + (0.4 * t) - (0.1 * leaderactivation)]
set leaderactivation leaderactivation + lachange
ifelse (leaderactivation > maxleaderactivation)[ set leaderactivation maxleaderactivation][set leaderactivation leaderactivation]
]
end
to find-others ;; turtle procedure to find others in the group
set others other turtles with [mygroup = [mygroup] of myself]
end
to find-others-activation ;; turtle procedure to find others activation in a group
set othersactivation sum [leaderactivation] of others
end
to find-othersactivation-and-taskdemand
set neti othersactivation + 0.1 + err
end
to ilt-match ;; turtle procedure to match prospective leader's perceived characteristics and perceiver's ILT for various dimensions using hamming distance
find-others
find-yothers
let d 0
let xn 0
let yn 0
let x1 0
let y1 0
let n 1
while [n < 6];; there are 5 values
[ set x1 item xn x
set y1 item yn y
set n n + 1
if (x1 = y1) [ set d d + 0 ]
if (x1 > y1) [ set d d + (x1 - y1) ]
if (y1 > x1) [ set d d + (y1 - x1) ]
]
ifelse d <= 2
[ set leadershipperception leadershipperception + (5 - d)]
[ set leadershipperception leadershipperception + 0]
end
to collectivelevel
find-others
if any? others[
set othersperception sum [leadershipperception] of others
]
end
to find-yothers
if any? others[
set y n-values 5[random 2]
]
end
to update-variables
update-turtles
update-globals
end
to update-turtles
ask turtles[
if(leaderactivation > 2) [ set aLA? 1]
if(leadershipperception != 0) [ set aLP? 1]
if(leadershipactivation > 2) [set aLi? 1]
; if(leadershipactivationr > 2) [set aLir? 1]
;if(leaderactivation > 2) AND (leadershipperception = 0) AND (leadershipactivationr > 0)[ set aLAnLp? 1]
;if(leaderactivation < 2) AND (leadershipperception != 0) AND (leadershipactivationr > 0)[ set nLALp? 1]
; if(leaderactivation > 2) AND (leadershipperception != 0) AND (leadershipactivationr > 0)[ set LALP? 1]
;if(leaderactivation < 2) AND (leadershipperception = 0) AND (leadershipactivationr > 0)[ set nLAnLP? 1]
]
end
to update-globals
set percent-LA (count turtles with [ aLA? = 1]) / (count turtles) * 100
set percent-LP (count turtles with [aLP? = 1]) / (count turtles) * 100
set percent-LI (count turtles with [aLI? = 1]) / (count turtles) * 100
set percent-LIr (count turtles with [aLIr? = 1]) / (count turtles) * 100
;set percent-LAmp (count turtles with [ LA? = 1 AND motivation = 1 AND priorexp = 1]) / (count turtles) * 100
set probLA (count turtles with [aLA? = 1]) / (count turtles)
set probLP (count turtles with [aLP? = 1]) / (count turtles)
set probLI (count turtles with [aLi? = 1]) / (count turtles)
;set percent-aLAnLp (count turtles with [aLAnLp? = 1]) / (count turtles) * 100
;set percent-nLALp (count turtles with [nLALp? = 1]) / (count turtles) * 100
;set percent-LALP (count turtles with [LALP? = 1]) / (count turtles) * 100
;set percent-nLAnLP (count turtles with [nLAnLP? = 1]) / (count turtles) * 100
end
'''

How to make a variable belong to a range Netlogo

I have a turtle who can eat different amount of food for every tick, updating its stomach content every time. I would like to round the stomach content value so that it belongs to a range x.
this is the forage function that updates the stomach-content :
to forage
;; item=0.064g
set patch-n random-float 100
if patch-n <= 8 [set stomach-content (stomach-content + 0.00) ] ; does not find any item
if patch-n > 8 and patch-n <= 99 [set stomach-content (stomach-content + 0.192) ] ; finds 3 items
if patch-n > 99 [set stomach-content 0.4 ]; full stomach
ifelse stomach-content >= 0.132
[set fat-reserves (fat-reserves + 0.132 ) set stomach-content (stomach-content - 0.132)]
[set fat-reserves (fat-reserves + (stomach-content * 1)) set stomach-content 0] ;;
set fat-reserves (fat-reserves - (8 * bmr)) ; metabolic rate removes fat from fat reserves
end
the range I would like the stomach-content to belong to is
set x (range 0 0.4 0.04)
Is there a way to make my stomach-content value to be in this finite range of 11 values?
Something like to round stomach-content to the nearest value with mod=0.04 in the interval (0 , 0.4)
You could do something fancy with the mod reporter, but if you don't need it to be super fast, the following is easy enough, and also more flexible, as it would work with any list of values:
to-report nearest-in-list [ the-value the-list ]
report first sort-by [ [a b] ->
abs (a - the-value) < abs (b - the-value)
] the-list
end
You can then use it like this:
observer> show nearest-in-list 0.11 (range 0 0.4 0.04)
observer: 0.12
observer> show nearest-in-list 0.021 (range 0 0.4 0.04)
observer: 0.04
observer> show nearest-in-list 0.02 (range 0 0.4 0.04)
observer: 0
Note that, in case of ties (like with 0.02 in the example, which is halfway between 0 and 0.04) it gives you the lowest value.

Netlogo, distribution and driving lanes in roundabouts

[
globals [ gl_my-car gl_cars-count gl_accelerator gl_decelerator gl_speed-max gl_speed-min gl_speed-maxcircle gl_speed-maxright gl_distance-straight
gl_distance-left gl_distance-right gl_degree-per-foot gl_side gl_slowdown gl_straightdistance
gl_distanceto00 gl_carminseparation gl_reactiontime
gl_motospeed-max gl_my-moto gl_moto-count
gl_truckspeed-max gl_my-truck gl_truck-count
gl_busspeed-max gl_my-bus gl_bus-count
gl_motominseparation gl_truckminseparation gl_busminseparation
]
breed [cartop car]
breed [mototopview moto]
breed [trucktopview truck]
breed [bustopview bus]
turtles-own [
speed droundabout headingdirection
lane
]
to setup
clear-all
set gl_cars-count 0
; 100 ticks per second
set gl_moto-count 0
set gl_truck-count 0
set gl_bus-count 0
set gl_accelerator 0.001 ; 10 ft per second per second
set gl_decelerator 0.001 ; 10 ft per second per second
set gl_speed-max 0.5133333333333 ; 35mph, 51.3333 ft per second
set gl_speed-min 1.e-10
set gl_speed-maxcircle gl_speed-max * 0.5 ; half normal speed
set gl_speed-maxright gl_speed-max * 0.62 ; about 60% normal speed
set gl_distance-straight 34.77
set gl_distance-left 58.34
set gl_distance-right 35.2
set gl_degree-per-foot 3.81972
set gl_side sqrt (15 ^ 2 - 6 ^ 2)
set gl_slowdown (gl_speed-max ^ 2 - gl_speed-maxcircle ^ 2) / (2 * (gl_decelerator))
set gl_straightdistance gl_slowdown + gl_side
set gl_distanceto00 sqrt (gl_straightdistance ^ 2 + 6 ^ 2)
set gl_carminseparation 15; assume car lenght 10' & distance between cars 5'
set gl_motominseparation 10
set gl_truckminseparation 20
set gl_busminseparation 25
set gl_reactiontime 100; 100 ticks or 1 second reaction time
;;;tao xe oto
create-cartop NumberCartop
[
set color (random 3 * 40 + 15) ; 15=red=go right, 55=green=go straight, 95=sky=go left
set shape "car top"
set size 5
set speed gl_speed-max - random-float .1 ;;thhiet lap hinh dang xe và bao cao vi tri
set droundabout 0
set headingdirection 0
distribute-cars
]
;;;;tao xe may
create-mototopview Numbermotos
[
set color (random 3 * 40 + 15 )
set shape "motortopview"
set size 3
set speed gl_motospeed-max - random-float .1 ;;thhiet lap hinh dang xe và bao cao vi tri
set droundabout 0
set headingdirection 0
distribute-motos
]
;;; tao xe tai
create-trucktopview Numbertruck
[
set color (random 3 * 40 + 15 )
set shape "trucktopview"
set size 7
set speed gl_truckspeed-max - random-float .1 ;;thhiet lap hinh dang xe và bao cao vi tri
set droundabout 0
set headingdirection 0
distribute-truck
]
;; tao xe bus
create-bustopview Numberbus
[
set color (random 3 * 40 + 15 )
set shape "bustopview"
set size 7
set speed gl_busspeed-max - random-float .1 ;;thhiet lap hinh dang xe và bao cao vi tri
set droundabout 0
set headingdirection 0
distribute-bus
]
set gl_my-car one-of cartop
watch gl_my-car
set gl_my-moto one-of mototopview
watch gl_my-moto
set gl_my-truck one-of trucktopview
watch gl_my-truck
set gl_my-bus one-of bustopview
watch gl_my-bus
ask patches
[
;;;tao vong xuyen
if (pxcor ^ 2 + pycor ^ 2 < 35 ^ 2) and (pxcor ^ 2 + pycor ^ 2 > 10 ^ 2)
[
set pcolor grey
]
;;;;tao cay xanh trong vong xuyen
if (pxcor ^ 2 + pycor ^ 2 < 7 ^ 2) and (pxcor ^ 2 + pycor ^ 2 > 0 ^ 2)
[
set pcolor blue
]
;;;; tao duong phan van tri
if abs (pxcor) > 0 and abs (pxcor) < 7 and (pxcor ^ 2 + pycor ^ 2 > 10 ^ 2)
[
set pcolor grey
]
;;;tao giai phan cach giua duong pham van dong
if abs (pycor) >= 0 and abs (pycor) <= 1.5 and (abs (pxcor) > 35)
[
set pcolor yellow
]
;;;tao lan oto duong pham van dong
if abs (pycor) > 1.5 and abs (pycor) < 17 and (pxcor ^ 2 + pycor ^ 2 > 10 ^ 2)
[
set pcolor grey
]
;;tao dai phan cach lan xe may duong pham van dong
if abs (pycor) >= 17 and abs (pycor) <= 19 and (abs (pxcor) > 35)
[
set pcolor yellow
]
;;;tao lan xe may duong pham van dong
if abs (pycor) > 19 and abs (pycor) < 26 and (pxcor ^ 2 + pycor ^ 2 > 10 ^ 2)
[
set pcolor grey
]
;;;tao lan duong re phai pham van dong - phan van tri và phan van tri-pham van dong phia ben duoi
if ((abs (pxcor) - 55 < pycor) and (abs (pxcor) - 35 > pycor) and abs (pxcor) > -10 and pycor < -10)
[
set pcolor grey
]
;;;tao lan duong re phai pham van dong - phan van tri phia ben tren
if (( (- pxcor) + 55 > pycor) and ( (- pxcor) + 35 < pycor) and (pxcor) > 0 and pycor > 10)
[
set pcolor grey
]
;;;tao lan duong re phai phan van tri - pham van dong phia ben tren
if (( ( pxcor) + 55 > pycor) and ( ( pxcor) + 35 < pycor) and (pxcor) < 0 and pycor > 10)
[
set pcolor grey
]
]
reset-ticks
end
;;;phan phoi xe oto
to distribute-cars
set heading random 4 * 90
;;lan phan van tri ben phai
if (heading = 0)
[setxy (1 + random(pxcor + 6)) (40 + random (max-pycor - 30)) * (2 * random 2 - 1)]
;;lan phan van tri ben trai
if (heading = 180)
[setxy (-1 + random(pxcor - 6)) ((40 + random (max-pycor - 30)) * (2 * random 2 - 1))]
;;lan pham van dong ben duoi
if (heading = 90)
[setxy ((40 + random (max-pxcor - 38)) * (2 * random 2 - 1)) (-3 + random(pycor - 13 ))]
;;;lan pham van dong ben tren
if (heading = 270)
[setxy ((40 + random (max-pxcor - 38)) * (2 * random 2 - 1)) (3 + random(pycor + 13 ))]
if any? other turtles-here
[distribute-cars ]
end
;;;phan phoi xe may
to distribute-motos
set heading random 4 * 90
;;;lan phan van tri ben phai
if (heading = 0)
[setxy (1 + random(pycor + 6)) (40 + random (max-pycor - 30)) * (2 * random 2 - 1)]
;;lan pham van dong ben duoi
if (heading = 90)
[setxy ((40 + random (max-pxcor - 38)) * (2 * random 2 - 1)) (-20 + random(pycor - 6 ))]
;;lan phan van tri ben trai
if (heading = 180)
[setxy (-1 + random(pycor - 6)) ((40 + random (max-pycor - 30)) * (2 * random 2 - 1))]
;;;lan pham van dong ben tren
if (heading = 270)
[setxy ((40 + random (max-pxcor - 38)) * (2 * random 2 - 1)) (20 + random(pycor + 6 ))]
if any? other turtles-here
[distribute-motos ]
end
;;;phan phoi xe tai
to distribute-truck
set heading random 4 * 90
;;lan phan van tri ben phai
if (heading = 0)
[setxy (1 + random(pxcor + 6)) (60 + random (max-pycor - 30)) * (2 * random 2 - 1)]
;;lan phan van tri ben trai
if (heading = 180)
[setxy (-1 + random(pxcor - 6)) ((60 + random (max-pycor - 30)) * (2 * random 2 - 1))]
;;lan pham van dong ben duoi
if (heading = 90)
[setxy ((60 + random (max-pxcor - 38)) * (2 * random 2 - 1)) (-3 + random(pycor - 13 ))]
;;;lan pham van dong ben tren
if (heading = 270)
[setxy ((60 + random (max-pxcor - 38)) * (2 * random 2 - 1)) (3 + random(pycor + 13 ))]
if any? other turtles-here
[distribute-truck ]
end
;;;phan phoi xe buyt
to distribute-bus
set heading random 4 * 90
;;lan phan van tri ben phai
if (heading = 0)
[setxy (1 + random(pxcor + 6)) (60 + random (max-pycor - 30)) * (2 * random 2 - 1)]
;;lan phan van tri ben trai
if (heading = 180)
[setxy (-1 + random(pxcor - 6)) ((60 + random (max-pycor - 30)) * (2 * random 2 - 1))]
;;lan pham van dong ben duoi
if (heading = 90)
[setxy ((60 + random (max-pxcor - 38)) * (2 * random 2 - 1)) (-3 + random(pycor - 13 ))]
;;;lan pham van dong ben tren
if (heading = 270)
[setxy ((60 + random (max-pxcor - 38)) * (2 * random 2 - 1)) (3 + random(pycor + 13 ))]
if any? other turtles-here
[distribute-bus ]
end
to go
ask cartop [goallcar]
ask mototopview [goallmoto]
ask trucktopview [goalltruck]
ask bustopview [goallbus]
tick
if ticks > 30000 [stop]
end
to goallcar
let myhd heading
set headingdirection (subtract-headings myhd (towardsxy 0 0))
ifelse color = red and distancexy 0 0 < 31.7
[
goright
]
[
ifelse distancexy 0 0 <= 15
[
gostraightleft
]
[
gomainroad
]
]
plot [speed] of gl_my-car * 360000 / 5280 ; convert from feet per tick to mph
end
to goright
; if [ pcolor ] of patch-ahead 23 != grey
if headingdirection > 0 and headingdirection < 40 ; begin right turn
[
set gl_cars-count gl_cars-count + 1
set droundabout 0
;;;rt 45
if heading = 90
[
;; ask cars
if distancexy 0 0 = -55 and distancexy 0 0 = 55
[
rt 50
if pycor = -20
[
rt 90
]
]
]
rt 45
]
ifelse droundabout < gl_distance-right
[
let myhd heading
let d gl_distance-right - droundabout
ifelse d > 12
[
let t d / speed
let cars-45degrees cartop with [(subtract-headings heading myhd = 45) and (distance myself < 52.35)]
let cars-ininterval cars-45degrees with [(distancexy 0 0) + speed * t > 15 and (distancexy 0 0) +
speed * t < 46.39]
ifelse any? cars-ininterval
[
let deceleration-to-stop (speed ^ 2 / (2 * (d - 12)))
set speed speed - deceleration-to-stop
]
[
set speed speed + gl_accelerator
]
]
[
set speed speed + gl_accelerator
]
if (speed > gl_speed-maxright) [set speed gl_speed-maxright]
if (speed < gl_speed-min) [set speed gl_speed-min]
; plot [speed] of gl_my-car * 360000 / 5280 ; convert from feet per tick to mph
fd speed
set droundabout droundabout + speed
]
[
; plot [speed] of gl_my-car * 360000 / 5280; convert from feet per tick to mph
rt 45
fd speed
set droundabout 0
readjustcordinate
]
end
to gostraightleft
; if [ pcolor ] of patch-ahead 5 != grey
if droundabout = 0 ; begin roundabout left turn or go straight
[
set gl_cars-count gl_cars-count + 1
set speed gl_speed-maxcircle
set droundabout 0.00001 ; start roundabout
rt 66.422
]
ifelse ((color = sky and droundabout < gl_distance-left ) or (color = green and droundabout <
gl_distance-straight ))
[
let carsinroundabout other cartop with [distancexy 0 0 < 15]
fd speed
lt speed * gl_degree-per-foot
set droundabout droundabout + speed
let d distancexy 0 0 / 14.9999
setxy (xcor / d) (ycor / d)
]
[
; plot [speed] of gl_my-car * 360000 / 5280; convert from feet per tick to mph
rt 66.422
fd speed
set droundabout 0
readjustcordinate
]
end
to gomainroad
let myhd heading
let dfromcenter (distancexy 0 0)
let speed1 speed; save the initial speed for later use
if ( dfromcenter > 15)
[
let dseparation gl_carminseparation + speed * gl_reactiontime
let cars-same-direction other cartop with [heading = myhd]
ifelse any? cars-same-direction
[
let cars-ahead other cars-same-direction with [(distance myself) != 0 and (towards myself) != myhd]
ifelse any? cars-ahead
[
let car-nearest (min-one-of cars-ahead [distance myself])
let dtocar-nearest distance car-nearest
let vofcar-nearest [speed] of car-nearest
ifelse dtocar-nearest < dseparation
[
if speed >= vofcar-nearest
[
set speed speed - gl_decelerator
]
if dtocar-nearest < gl_carminseparation
[
set speed speed - gl_decelerator
if speed >= vofcar-nearest
[ set speed vofcar-nearest - gl_decelerator ]
]
]
[ set speed speed + gl_accelerator ] ; end car-nearest
]
[ set speed speed + gl_accelerator ] ; end cars-ahead
]
[ set speed speed + gl_accelerator ] ; end cars-same-direction
] ; end distancexy 0 0
; if ( (distancexy 0 0) > 15 and (distancexy 0 0) < 16.7)
; [
; let cars-45degree cars with [(subtract-headings heading myhd = -45) ]
; ]
if ( dfromcenter > 25 and dfromcenter < 50 )
[
let d dfromcenter - 25
let t d / speed
let cars-inroundabout cartop with [(distancexy 0 0) <= 15]
let cars-ininterval cars-inroundabout with [(subtract-headings heading myhd > 0 )]
if any? cars-ininterval
[
let deceleration-to-stop (speed ^ 2 / (2 * d))
set speed speed - deceleration-to-stop
]
]
let vmax gl_speed-max
if (dfromcenter < gl_distanceto00)
[
let speed2 speed; save the just calculated speed
set speed speed1; restore the initial speed
ifelse color = red
[
let d sqrt ( dfromcenter ^ 2 - 36) - 31 + gl_distance-right
let t d / speed
let cars-perpendicular cartop with [(subtract-headings heading myhd = 90) and (subtract-headings
(towards myself) myhd < 0)]
let cars-ininterval cars-perpendicular with [(distancexy 0 0) + speed * t > 15 and (distancexy 0 0) +
speed * t < 46.39]
ifelse any? cars-ininterval
[
let deceleration-to-stop (speed ^ 2 / (2 * (d - 12)))
set speed speed - deceleration-to-stop
]
[
set speed speed + gl_accelerator
]
if (speed > speed2) [set speed speed2]
]
[
set speed speed2
]
let dfromcircle sqrt(dfromcenter ^ 2 - 36) - gl_side ; gl_side = sqrt(15^2+6^2)
set vmax sqrt(gl_speed-max ^ 2 - 2 * gl_decelerator * (gl_slowdown - dfromcircle)) ; gl_slowdown = (v^2 - v0^2)/(2a)
]
if vmax > gl_speed-max
[
set vmax gl_speed-max
]
if speed > vmax
[
set speed vmax
]
if speed < gl_speed-min
[
set speed gl_speed-min
]
forward speed
end
;;;;;chay xe moto
to goallmoto
let myhd heading
set headingdirection (subtract-headings myhd (towardsxy 0 0))
ifelse color = red and distancexy 0 0 < 31.7
[
goright
]
[
ifelse distancexy 0 0 <= 15
[
gostraightleft
]
[
gomainroad
]
]
plot [speed] of gl_my-car * 360000 / 5280 ; convert from feet per tick to mph
end
;;;chay xe truck
to goalltruck
let myhd heading
set headingdirection (subtract-headings myhd (towardsxy 0 0))
ifelse color = red and distancexy 0 0 < 31.7
[
goright
]
[
ifelse distancexy 0 0 <= 15
[
gostraightleft
]
[
gomainroad
]
]
plot [speed] of gl_my-car * 360000 / 5280 ; convert from feet per tick to mph
end
;;;chay xe bus
to goallbus
let myhd heading
set headingdirection (subtract-headings myhd (towardsxy 0 0))
ifelse color = red and distancexy 0 0 < 31.7
[
goright
]
[
ifelse distancexy 0 0 <= 15
[
gostraightleft
]
[
gomainroad
]
]
plot [speed] of gl_my-car * 360000 / 5280 ; convert from feet per tick to mph
end
to chuyenlan
distribute-cars
goallcar
;;;if heading = 0 and color = red
;;;[
;; if
;;rt 45
;; if (xcor = -10)
;; ]
;;if any? other turtles-here
;; [chuyenlan ]
;; ]
end
to readjustcordinate
if (heading > 355 or heading < 5)
[
set heading 0
set xcor 6
]
if (heading > 85 and heading < 95)
[
set heading 90
set ycor -6
]
if (heading > 175 and heading < 185)
[
set heading 180
set xcor -6
]
if (heading > 265 and heading < 275)
[
set heading 270
set ycor 6
]
end
]
[![1]
how to write Algorithms running cars. 3-lane road. cheap car left lane 1 and lane 2 car go straight, lanes 3 cheap car right. tạo random initial car. (Low red car on the right, go straight green, sky swerved left). to 1 position if the car red là có all moved to the right lane for cheap car. green car vehicle moving straight to go straight lane, sky car move to the left lane cheap.
revise section drive algorithm. because it runs false
[1]]
how to write Algorithms running cars. 3-lane road. cheap car left lane 1 and lane 2 car go straight, lanes 3 cheap car right. tạo random initial car. (Low red car on the right, go straight green, sky swerved left). to 1 position if the car red là có all moved to the right lane for cheap car. green car vehicle moving straight to go straight lane, sky car move to the left lane cheap.
revise section drive algorithm. because it runs false
[1]
: http://i.stack.imgur.com/59IAs.jpg

How to add power law likelihood to Netlogo Model

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