[
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
Related
I've been using NetLogo Programming for a few weeks now, but I'm still a little confused with the syntax. I'm trying to create a NetLogo version of the Flappy Bird game, but I can't seem to make the poles work.
I tried using patches, but the poles don't move very smoothly. When I use turtles, I can't make it stop without stopping the whole program. This is part of my code:
to top-pole
crt 1
[set size 1500
set color pink + 2.8
set shape (one-of ["pole 7" "pole 8" "pole 9" "pole 10" "pole 11"])]
ask turtles with [shape = "pole 7"]
[setxy 75 0]
ask turtles with [shape = "pole 8"]
[setxy -1 0]
ask turtles with [shape = "pole 9"]
[setxy 149 0]
ask turtles with [shape = "pole 10"]
[setxy 149 0]
ask turtles with [shape = "pole 11"]
[setxy -1 0]
; facexy 0 1]
end
to move-top
every 10
[top-pole]
every 0.05
[ask turtles with [who > 0]
[setxy (xcor + x-vel) (ycor + y-vel)
set x-vel (x-vel - .01)]]
every 3
[ask turtles with [(shape = "pole 7") and ((round xcor) >= 155) and ((round xcor) <= 145) ] ;150
[die]
ask turtles with [(shape = "pole 8") and ((round xcor) >= 65) and ((round xcor) <= 80)] ;74
[die]
ask turtles with [(shape = "pole 9") and ((round xcor) >= 220) and ((round xcor) <= 230)] ; 225
[die]
ask turtles with [(shape = "pole 10") and ((round xcor) >= 220) and ((round xcor) >= 230)] ; 225
[die]
ask turtles with [(shape = "pole 11") and ((round xcor) >= 70) and ((round xcor) <= 80)] ; 75
[die]]
end
Btw, the "pole 7", "pole 8", etc. are just turtles I created:
enter image description here
Also, my world dimensions are currently 300 by 200 and the patch size is 1.
I am writing a model and want to add a button of chooser to assess alternative management options. Model was working fine as some variables graphs disappeared as I have added chooser for around all of turtles. I have also tried changing parameters from Turtle-own to global and vice versa and not rectified the error. I couldn't figure out the issus. Problem arise when i add chooser in irrigate procedure. Codes are bit longer, please bear with me. I will be grateful for the solution. Please help me to get out of difficult situation.
I have also shared before and after image of interface.
Before
and after
Thanks
Globals [farmlocations farmers-list water eva IWR-perday ASW maxgwater usedflow wdemand wtdmean wtdsd av-wdischarge wdischarge wd totannual-wdischarge current-demand wtd canal-wdischarge water-right alpha beta]
Breed [Farmers farmer]
farmers-own [ irrigation-turn season1-profit swh season2-profit yield myflow DWT total-gw-cost watercosts total-sw-cost raqba sw benefits evapotrans totaldemand swcost gwateruse wneed logging gwcost salinity aw totalcosts dws resources irrigation-turn gw AGW mywaterloss] ;swh= surface water height aw= old surface water height
Patches-own [gw-level]
links-own [flow]
to setup
clear-all
setup-farmers
import-data
reset-ticks
end
to setup-farmers
create-farmers 5 [ set irrigation-turn [0]
ask farmer 0 [ setxy min-pxcor + 1 2 set irrigation-turn 1]
ask farmer 1 [ setxy min-pxcor + 1 0 set irrigation-turn 2]
ask farmer 2 [ setxy min-pxcor + 1 4 set irrigation-turn 2]
ask farmer 3 [ setxy min-pxcor + 3 2 set irrigation-turn 3]
ask farmer 4 [ setxy min-pxcor + 3 0 set irrigation-turn 3]
]
set farmers-list sort turtles
set eva 0.3
ask farmers [set label who + 1
set size 1
set myflow 0
If land = "small farmers" [ set raqba random 5 + 3 ]
If land = "small and large farmers" [ ifelse random 3 = 1 [set raqba 10 + random 20 ] [ set raqba 5 + random 5]]
set total-sw-cost 500 * raqba
Set resources 1
set DWS 5 + 1 * (who)
set evapotrans 5 + 1 * (who)
set water-right 4.5 * raqba
Set logging 500 / DWS
Set salinity DWS / 5
set maxgwater 200
set wtdmean 10
set wtdSD 5
set sw 0
set swh 0
set aw 0
set gw-level 100 / dws
set gw 50000 + random 1000
Depth-to-Water-Table
ground-water-travel
water-losses
set alpha 1
set beta 0.7
]
set av-wdischarge 4.5
end
to Depth-to-Water-Table
set DWT Water-Table-Depth + DWS / 10
end
to ground-water-travel
set gw gw + 10 / (random-float 1 + DWT) * DWS
set AGW gw + (sum [gw-level] of patches in-radius 2)
set pcolor scale-color blue AGW 1 max [gw-level] of patches in-radius 2
end
to water-losses
Set mywaterloss waterloss + 5 * DWS ; 5 + 1* who
end
to go
tick
if ticks = 630 [ stop ]
calculate-sw
evaporate
irrigate
ask turtles [set color scale-color blue swh 30 3]
calculate-profits
update-output
end
to
calculate-sw
let list-element floor ticks / 5
ask item (ticks mod 5) farmers-list [
set canal-wdischarge item list-element totannual-wdischarge
set wdischarge canal-wdischarge - (canal-wdischarge * discharge-reduction)]
end
to evaporate
let list-element floor ticks / 5
ask item (ticks mod 5) farmers-list [
set current-demand item list-element IWR-perday
set Aw swh
if aw >= current-demand [set wneed 0]
set wneed 0
set wneed wneed + current-demand - aw
ask farmers [ set totaldemand current-demand * raqba + evapotrans]
if wneed < 0 [set wneed 0]
set swh swh - swh * eva
if swh < 0 [set swh 0]
]
end
to irrigate
If water-use-mangement = "Business as usual" [
ask turtles with [irrigation-turn = (ticks mod count farmers)]
[ set color red
set shape "person"
set pcolor brown + who
set ASW (Wdischarge - myWaterloss)* raqba
if ASW < 0 [set ASW 0]
set myflow ASW
ifelse wneed > 0 [
set swh myflow / raqba + aw
ifelse swh > current-demand [set swh current-demand ] [use-gw]
ifelse swh > aw [set usedflow (swh - aw) * raqba
if usedflow < 0 [ set usedflow 0]
set ASW ASW - usedflow
if ASW < 0 [ set ASW 0]
update-gw-use ]
[set usedflow 0]] [
set myflow 0
set swh aw + ((myflow ) / raqba)
if swh > current-demand [set swh current-demand ]]]]
end
to use-gw
Ask farmers [ let excess-demand (current-demand - swh) * raqba
if AGW > excess-demand [ ;set excess-demand AGW
set gwateruse agw - excess-demand
set dwt dwt + (0.2 + who / 20)
Ifelse DWT > 8 [ set gwcost excess-demand * 17.5] [set gwcost excess-demand * 5]
set agw agw - excess-demand
If AGW < 0 [ set AGW 0]
Ifelse excess-demand > 0.5 * current-demand [ifelse random 4 = 1 [ set salinity salinity + 0.1
set logging logging - 0.05]
[set salinity salinity + .05
set logging logging + 0.05 ]] [set salinity salinity - 0.05
set logging logging + 0.1]]]
set gw gw - 1
end
to update-gw-use
set salinity salinity + .04
set logging logging + 0.01
set gw gw - 1
set total-gw-cost total-gw-cost + gwcost
end
to calculate-profits
ask farmers [ if ticks mod 630 = 252 [ set total-sw-cost raqba * 500
set watercosts total-gw-cost + total-sw-cost
;update-yield
calculate-season1-profit
]]
ask farmers [ if ticks mod 630 = 0 [ set total-sw-cost raqba * 500
set watercosts total-gw-cost + total-sw-cost
calculate-season2-profit]]
end
to calculate-season1-profit
ask farmers [ set yield yield + ((usedflow / totaldemand ) * alpha + ( gwateruse / totaldemand)* beta) * 55 - 0.1 * (55 * salinity / 12) - 0.1 *(55 * logging / 12) ; 55mon/acre market price
let price 1350 ; price/ maund
set season1-profit yield * price - watercosts
]
end
to calculate-season2-profit
ask Farmers[ set yield yield + ((usedflow / totaldemand ) * alpha + (gwateruse / totaldemand)* beta ) * 100 - 0.1 * (100 * salinity / 18) - 0.1 * (100 * logging / 18) ; 100 mauns/acre market price
let price 2500 ; price/ maund
set season2-profit yield * price - watercosts]
end
to update-output
set-current-plot "Available-surface-water"
set-current-plot-pen _clarify-duplicate-plot-pen-name "Farmer 1" ifelse ticks > 0 [plot [swh] of turtle 0] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 2" ifelse ticks > 0 [plot [swh] of turtle 1] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 3" ifelse ticks > 0 [plot [swh] of turtle 2] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 4" ifelse ticks > 0 [plot [swh] of turtle 3] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 5" ifelse ticks > 0 [plot [swh] of turtle 4] [plot 0]
set-current-plot "Logging"
set-current-plot-pen _clarify-duplicate-plot-pen-name "Farmer 1" ifelse ticks > 0 [plot [logging] of turtle 0] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 2" ifelse ticks > 0 [plot [logging] of turtle 1] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 3" ifelse ticks > 0 [plot [logging] of turtle 2] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 4" ifelse ticks > 0 [plot [logging] of turtle 3] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 5" ifelse ticks > 0 [plot [logging] of turtle 4] [plot 0]
set-current-plot "Salinity"
set-current-plot-pen _clarify-duplicate-plot-pen-name "Farmer 1" ifelse ticks > 0 [plot [salinity] of turtle 0] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 2" ifelse ticks > 0 [plot [salinity] of turtle 1] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 3" ifelse ticks > 0 [plot [salinity] of turtle 2] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 4" ifelse ticks > 0 [plot [salinity] of turtle 3] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 5" ifelse ticks > 0 [plot [salinity] of turtle 4] [plot 0]
end
to import-data
ifelse ( file-exists? "totannual-wdischarge.txt" )
[
set totannual-wdischarge []
file-open "totannual-wdischarge.txt"
while [ not file-at-end? ]
[
set totannual-wdischarge sentence totannual-wdischarge (list file-read)
]
file-close
]
[ user-message "There is no totannual-wdischarge.txt file in current directory!" ]
;; Import water demand
ifelse ( file-exists? "IWR-perday.txt" )
[
set IWR-perday []
file-open "IWR-perday.txt"
while [ not file-at-end? ]
[
set IWR-perday sentence IWR-perday (list file-read)
]
file-close
]
[ user-message "There is no IWR-perday.txt file in current directory!" ]
end
to-report _clarify-duplicate-plot-pen-name [ name ]
let name-map [["Min" "Min"] ["MIn" "MIn_1"]]
let replacement filter [ rename -> first rename = name] name-map
let reported-name name
if not empty? replacement [
set reported-name item 1 replacement
]
report reported-name
end
The plots are working, it's just that they are showing 0. My best guess is that the text you have inside the chooser for the options is different than the text you are testing in your if statement, so the if statement is always false.
Try something like this:
ifelse water-use-mangement = "Business as usual"
[ ; all your irrigation code that you already have
]
[ print "ERROR: Management case not found"
]
This will print an error message if the text doesn't match.
In my model the patches contain eggs, larvae (which hatch from eggs) and pupae (which arise from larvae that have increased their weight to 1 mg) (so these three are variables and not agentsets).
to set-up-patches
ask patches [
set ppupae n-values 20 [0.1]
]
Modeling these works fine: the model runs smoothly and the dynamics of the populations are biologically realistic. But I want the pupae to turn into adult hoverflies when they are 20 days old. So I modeled this like this:
to time
every 144 [ ask patches with [ sum ppupae > 0.1] [sprout-hoverflies] ]
end
to sprout-hoverflies
every 144 [
sprout item dta ppupae [
set shape "hoverfly"
set size 8
]]
end
As soon as I add these lines the model becomes extremely slow. To see if it would run at all I left my PC running when I left for an hour, and when I came back it was at 50 ticks. When I put semicolons (;) in front of these lines and run the model again it works perfectly.By the way, because I wanted to keep this post small and easy to overview I didn't include all code, so there's procedures missing and also within each procedure I excluded some things. But only things that I think are not important to solve this issue. But I'll post my entire code at the bottom if necessary.
globals [ gtimeeating gtimeeating_honey ginterval gvisits genergy deggs gaphids halive R0 lm m meaninterval sdinterval mean_flowers sd_flowers forag_flow_crop forag_flow_strip alpha lambda test mean_S sd resource b y_g F0 gamma_c c waarde voedsel food_density number_to_pup larvae_max K_aphids nectar_production dta sigma]
turtles-own[S energy days trip eggs load target memoryF memoryP timeeating timeeating_honey interval];;R0 net reproducing rate, m list the number of eggs laid /hoverfly per age class
patches-own[ppupae crop pdays flowers nectarflowers fresheggs peggs nl_per_age larvae g visits aphids honeydew l_hatched l_weight_age] ;;lm list with the values of net reproducing rate for every day
;;;;;;;;;
;;Setup;;
;;;;;;;;;
to setup
clear-all
set-up-globals
set-up-patches
set-up-hoverflies
reset-ticks
end
to set-up-globals
set gvisits n-values 133 [[0]] ;; this sets global variables as lists
set deggs n-values 133 [[0]]
set gaphids n-values 133 [[0]]
set ginterval []
set halive []
set m []
set lm []
end
to set-up-patches ;; set-up-patches draws a flower strip with red color and the crop with green color, sets crop variable, flowers,aphids and the appropiate amount of food.
ask patches[
;;larvae and pdays start with 0 because to add a day (last value on the list +1) a value is needed. And to calculate A index, the have to have the same lenght.
set pdays [0 ]
ifelse pxcor = 532 or pxcor = 531 or pxcor = 533 or pxcor = 534 and pycor > 0 and pycor < 132 [
set pcolor red set crop false] [set pcolor green set crop true] ;; in case the condition is true it takes first option if not the second, ifelse-value can be used in a reporter, ifelse not.
;set peggs n-values 4 [0]
set peggs n-values 4 [0.1]
set nl_per_age n-values 20 [0];; number of larvae per age.
set l_hatched n-values 20 [0.0001];; weight of hatched larvae
set l_weight_age n-values 20 [0];; weight of hatched larvae + their individual growth per age
set ppupae n-values 20 [0.1] ;toegevoegd nog kijken of deze ook bij set-up-patches kan
set larvae 0 ;; larvae biomass
set aphids 0
set mean_flowers 125
set sd_flowers 25
set nectar_production 0.1
set K_aphids 1200;carrying capacity of the aphid population (where growth rate = 0)
if crop = false
[set flowers round (0.57 * random-normal mean_flowers sd_flowers) set nectarflowers precision (flowers * nectar_production) 5];; (random-normal mean sd) takes a random number from a normal distribution and multiplies it with the surface of one cell (0.57)
] ;; precision number decimal rounds a number with a number of decimals.
ask patches with [(pxcor > 3 ) and (pxcor < 1062) and (crop = true) and (pycor > 3) and (pycor < 129)] ;; infection * 34320 calculates the number of patches infected, infection is not applied to borders o the flower strip.
[ set aphids round (random-exponential Aphidsmean) if aphids > K_aphids [set aphids K_aphids]]
;; exp = e^
ask patches with [pxcor < 4 or pxcor > 1061 or pycor < 4 or pycor > 128] ;;this shows a border with conditions that prevent hoverfies from leaving the field.
[
set pcolor 52
]
end
to set-up-hoverflies
create-turtles Hoverflies ;creates turtles and gives them a random starting position
ask turtles[
set shape "hoverfly"
set size 8
set color cyan
set xcor 130 + random 800
set ycor 10 + random 112
set energy 0.5 + random-float 0.44
set eggs 100
set load round (random-gamma 3000 1) if load >= 3000 [set load 3000]
set memoryF n-values 50 [1] ;;n-values reports a list of 50 items all with value [].
set memoryP n-values 50 [10]
set interval [0]
]
end
;;;;;;
;;Go;;
;;;;;;
to go
if count turtles < 5
[;export-plot "Oviposition rate (m)" word "S" word S% "Ovr.txt"
;export-plot "Hoverflies alive" word "S" word S% "Hoval.txt"
;export-plot "Net reproducing rate" word "S" word S% "R0.txt"
;export-plot "Mean Time eating" word "S" word S% "TE.txt"
;export-plot "Visits" word "S" word S% "Visits.txt"
;export-plot "Aphids" word "S" word S% "Aphid.txt"
;export-plot "Fresh eggs" word "S" word S% "Fd.txt"
;export-plot "Mean energy level" word "S" word S% "MEnergy.txt"
stop] ;; the model stops when most turtles died
if ticks mod 144 = 0 [
time ;; every 144 ticks it is a new day, days, pdays w, and energy are updated
]
ask turtles [
if energy <= 0 [set ginterval sentence ginterval interval die]
if load <= 0 [set ginterval sentence ginterval interval die]
set trip 0
choose
]
tick
end
to time
let DMR 0.004 ;;daily mortality risk
let E_basic 0.072 ;;basic daily energy need
set sigma 0.28 ;;prey conversion efficiency
let f 0.2 ;;maximum predation rate by predator larvae on aphids
let H 300 ;;Half saturation aphid density
let ma 0.02 ;;maintenance costs
let r 0.23 ;; growth rate aphids
let weight_hatch 0.063 ;hatching weight of eggs
let weight_max 28 ;;maximum weight of larvae
let dtl 3;; developmental time of egg to larvae
set dta 19;; toegevoegd
let honey_production 0.1
let decay_honey 0.5
ask turtles
[if random-float 1 <= DMR * days [set ginterval sentence ginterval interval die] ;;Daily natural mortality risk
set days days + 1 set energy energy - E_basic set eggs 100 set timeeating 0 set timeeating_honey 0 ;; Some energy is discounted ine the beggining of the day assuming that there is basal metabolism at night
]
ask patches with [ (pxcor > 3) and (pxcor < 1062) and (pycor > 3) and (pycor < 129)]
[set pdays lput (last pdays + 1) pdays
set g precision (sigma * f * (aphids / (aphids + H)) - ma) 3;; (g) predator larvae growth rate depending on consumption rate.
set peggs but-last peggs ;;haalt het laatste item van de lijst af omdat die in de volgende developemtal stage gebruikt gaat worden
set peggs fput fresheggs peggs ;;op de eerste plek in de lijst komt het aantal fresheggs
set fresheggs 0
set nl_per_age fput item dtl peggs nl_per_age ;dit is het aantal larven per leeftijdsclasse. dus p(w) in hoverfly IBM explained
;set nl_per_age but-last nl_per_age; als ze langer dan 20 dagen een larve blijven en nog niet gepopt zijn sterven ze
set l_hatched fput (weight_hatch) l_hatched;hier wordt eerste item van de lijst het gewicht van de 0 dagen oude larven.
set l_hatched but-last l_hatched; als ze langer dan 20 dagen een larve blijven en nog niet gepopt zijn sterven ze
set l_weight_age fput weight_hatch l_weight_age
;set l_weight_age but-last l_weight_age ;anders zei die bij larvae map dat de lijsten niet dezelfde lengte hadden
set l_weight_age map [i -> (i * e ^ (g * (aphids)*(1 - (i / weight_max))))] l_weight_age
;set l_weight_age map [ larvae-growth ->
;(larvae-growth * e ^ (g * (aphids)*(1 - (larvae-growth / weight_max))))
;] l_weight_age;;dit is het gewicht van de larven per age. dus in hoverfly IBM explained wt+1. door het gebruik van de anonymous procedure 'larvae-growth ->' in combinatie met 'map'
;wordt voor ieder item van l_hatched l_weight_age uitgerekend.
;gaat dit wel goed? want nu wordt iedere stap in de functie de gewichten van l_hatched gestopt, en dit is dus steeds 0.063 maal de formule, terwijl het moet doorwerken met de
;voorgaande groei.
;nu geeïndigd met l_weight_age ipv l_hatched
;set larvae sum((map * nl_per_age l_weight_age)); hiermee wordt de lijst 'aantal larven / leeftijd' vermenigvuldigt met 'gewicht larven / leeftijd' zodat je het totaal krijgt
; de functie map zorgt er hier voor dat je de items uit twee lijsten met elkaar kunt vermenigvuldigen
set aphids aphids * e ^( r * ( 1 - (aphids / K_aphids))) - larvae * f * (aphids / (aphids + H))
set honeydew honeydew + honey_production * aphids - decay_honey * honeydew;;honeydew productie is dus 0.1 maal aantal aphids per tijdstap ;nu accumuleert en decayhet
if aphids < 0 [set aphids 0];; aphids grow following a logistic curve
;set larvae_max filter [weight -> weight = 0.07] l_weight_age ;;filter functie die alleen de larven van gewicht 28 mg eruit haalt
;set l_weight_age remove 0.07 l_weight_age
;set number_to_pup length larvae_max ;telt hoeveel er zijn van 28 mg
;set ppupae fput number_to_pup ppupae ;voegt het aantal van 28 mg toe aan puppae
; while [
; let n_item 0
; if item n_item l_weight_age > 0.07 [
; set number_to_pup number_to_pup + 1
; set nl_per_age replace-item 0 l_weight_age 0
; set l_hatched replace-item 0 l_weight_age 0
; set l_weight_age replace-item 0 l_weight_age 0
;]
; ] I know that the code below can be code way more efficient, I tried it with the while loop aboven but it didn't work. I however don't think that the long code is very problematic because the code runs fine without the sprout function that I mentioned in the title
if item 0 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 0 l_weight_age 0
set l_hatched replace-item 0 l_weight_age 0
set l_weight_age replace-item 0 l_weight_age 0
]
if item 1 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 1 l_weight_age 0
set l_hatched replace-item 1 l_weight_age 0
set l_weight_age replace-item 1 l_weight_age 0
]
if item 2 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 2 l_weight_age 0
set l_hatched replace-item 2 l_weight_age 0
set l_weight_age replace-item 2 l_weight_age 0
]
if item 3 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 3 l_weight_age 0
set l_hatched replace-item 3 l_weight_age 0
set l_weight_age replace-item 3 l_weight_age 0
]
if item 4 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 4 l_weight_age 0
set l_hatched replace-item 4 l_weight_age 0
set l_weight_age replace-item 4 l_weight_age 0
]
if item 5 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 5 l_weight_age 0
set l_hatched replace-item 5 l_weight_age 0
set l_weight_age replace-item 5 l_weight_age 0
]
if item 6 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 6 l_weight_age 0
set l_hatched replace-item 6 l_weight_age 0
set l_weight_age replace-item 6 l_weight_age 0
]
if item 7 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 7 l_weight_age 0
set l_hatched replace-item 7 l_weight_age 0
set l_weight_age replace-item 7 l_weight_age 0
]
if item 8 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 8 l_weight_age 0
set l_hatched replace-item 8 l_weight_age 0
set l_weight_age replace-item 8 l_weight_age 0
]
if item 9 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 9 l_weight_age 0
set l_hatched replace-item 9 l_weight_age 0
set l_weight_age replace-item 9 l_weight_age 0
]
if item 10 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 10 l_weight_age 0
set l_hatched replace-item 10 l_weight_age 0
set l_weight_age replace-item 10 l_weight_age 0
]
if item 11 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 11 l_weight_age 0
set l_hatched replace-item 11 l_weight_age 0
set l_weight_age replace-item 11 l_weight_age 0
]
if item 12 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 12 l_weight_age 0
set l_hatched replace-item 12 l_weight_age 0
set l_weight_age replace-item 12 l_weight_age 0
]
if item 13 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 13 l_weight_age 0
set l_hatched replace-item 13 l_weight_age 0
set l_weight_age replace-item 13 l_weight_age 0
]
if item 14 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 14 l_weight_age 0
set l_hatched replace-item 14 l_weight_age 0
set l_weight_age replace-item 14 l_weight_age 0
]
if item 15 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 15 l_weight_age 0
set l_hatched replace-item 15 l_weight_age 0
set l_weight_age replace-item 15 l_weight_age 0
]
if item 16 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 16 l_weight_age 0
set l_hatched replace-item 16 l_weight_age 0
set l_weight_age replace-item 16 l_weight_age 0
]
if item 17 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 17 l_weight_age 0
set l_hatched replace-item 17 l_weight_age 0
set l_weight_age replace-item 17 l_weight_age 0
]
if item 18 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 18 l_weight_age 0
set l_hatched replace-item 18 l_weight_age 0
set l_weight_age replace-item 18 l_weight_age 0
]
if item 19 l_weight_age > 0.07 [
set number_to_pup number_to_pup + 1
set nl_per_age replace-item 19 l_weight_age 0
set l_hatched replace-item 19 l_weight_age 0
set l_weight_age replace-item 19 l_weight_age 0
]
set larvae sum((map * nl_per_age l_weight_age)); hiermee wordt de lijst 'aantal larven / leeftijd' vermenigvuldigt met 'gewicht larven / leeftijd' zodat je het totaal krijgt
; de functie map zorgt er hier voor dat je de items uit twee lijsten met elkaar kunt vermenigvuldigen
set ppupae fput number_to_pup ppupae
set nectarflowers flowers * nectar_production ;; all flowers are filled with nectar again
set visits 0
set number_to_pup 0
]
every 144 [
ask patches with [ sum ppupae > 0.1]
[sprout-hoverflies]
]
end
to sprout-hoverflies
every 144 [
sprout item dta ppupae [
set shape "hoverfly"
set size 8
set color cyan
set xcor 130 + random 800
set ycor 10 + random 112
set energy 0.5 + random-float 0.44
set eggs 100
set load round (random-gamma 3000 1) if load >= 3000 [set load 3000]
set memoryF n-values 50 [1] ;;n-values reports a list of 50 items all with value [].
set memoryP n-values 50 [10]
set interval [0]
]
]
end
to choose
set S 1.47 * S% / 100
; set c 2 ;;shape parameter gamma distribution
; set b 0.3 ;;scale parameter gamma distribution (to be optimized)
; set F0 0
; set food_density mean(memoryF)
; set y_g (food_density - F0) / b
; set gamma_c 1; if c=3, then (c-1)! = 2. if c=2, then (c-1)! = 1.
;set S ((1 / b * gamma_c) * y_g ^ (c - 1) * exp(-1 * y_g))
ifelse energy <= S [foraging][oviposition] ;;NL dit betekent <= is less than or equal. Als dit waar is (E<=S) dan wordt gekozen voor foraging, anders voor oviposition
end
to foraging
set forag_flow_crop 0.0005
set forag_flow_strip 0.001
if ([nectarflowers] of patch-here / (1 + (count turtles-here))) > mean ( memoryF)[eat] ;;Compare food
set target patch 532 ycor
face target ;; Search food, A Lévy-flight searching behavior is assumed, first a direction is chosen from a Normal distribution, unprecision (S.D.) increases with denstance form margin.
right random-normal 0 (5 * distance patch 532 ycor) ;; move probability y cannot be smaller than 0.03333 which is the probability of doing up to 60 cells , this avoids theprobability
let y random-float 1 if y < 0.003333 [ set y 0.003333];;to be 0 and inderterminacy while calculating the number of steps (l), it also avoids the hoverflt flies more than 15 cells.
let j round (2 / y) ;;l is calculated from the cummulative distribution of a power law.
while [ j > 0] [ ;;Once the number of steps is calculated the hoverfly moves forward and compares the cell for food.
forward 1
if xcor < 3 or xcor > 1062 [set ginterval sentence ginterval interval die] ;;while creates a loop, the agent repeats it until the condition is not fulfilled.
set j j - 1 ;; in case the hoverfly reaches the border doing the Lévy-flight it will leave the field.
set trip trip + 1
set visits visits + 1
ifelse [crop] of patch-here = true [set energy energy - forag_flow_crop][set energy energy - forag_flow_strip] ;; when the hoverfly is within the flower strip it spends more energy since it searches for flowers, otherwise the energy requirements are lower.
if trip >= 60 [ set interval replace-item 0 interval (first interval + 1) stop]
if (nectarflowers / (1 + (count turtles-here))) > mean ( memoryF)[eat];; Compare nectarflowers
;;in case the hoverfly finds a patch better than the internal index it will stop.
]
end
to oviposition
if eggs <= 0 [set energy energy - forag_flow_strip stop] ;;Rest
if energy <= 1.25 * S and [honeydew] of patch-here > 4 [eat_honey] ;;als dus energy lager dan de helft van de threshold is én honeydew hoger is dan een waarde (dit kan ook nog memoryH worden) wordt er honeydew gegeten
if [P] of patch-here > mean (memoryP) and [P] of patch-here > 10 [lay] ;;Compare oviposition
right random 360 ;; Search host, The direction is chosen randomly within 360 degrees.
let y random-float 1 if y < 0.003333 [ set y 0.003333] ;;The number of cells the hoverfly will travel from a Lévy-flight.
let j round (2 / y)
while [j > 0] [
forward 1
if xcor < 3 or xcor > 1062 [ set ginterval sentence ginterval interval die]
set j j - 1
set trip trip + 1
set visits visits + 1
set energy energy - forag_flow_strip ;; Searching for host places is the most energy requesting behavior.
if trip >= 60 [ set interval replace-item 0 interval (first interval + 1) stop]
set memoryP but-first memoryP ;Part for assessing P index Everytime the hoverfly moves it takes information about memoryP
set memoryP lput P memoryP ;;Everytime the hoverfly moves it takes information about memoryP
if P > mean (memoryP) and P > 10 [lay] ;;Compare oviposition, cells are compared step by step, in case the hoverfly finds a patch better than the internal index it will stop.
]
end
to eat_honey
let a_honey 0.8 ;;honeydew exploration rate: little higher than that of flowers because aphids are closer (same patch) and honeydew is olfactory stimulant
let Th_honey 0.2 ;;honeydew handling time. higher than that of flowers because hoverflies are not as adapted to consuming honeydew as nectar (higher viscosity, harder to reach, etc.)
let HV a_honey * honeydew / (1 + Th_honey * a_honey * honeydew) ;;honeydew visitation (numbers / timestep) ;;similar FR to flowers
if (HV * 0.1) > (1.47 - energy) [set HV (1.47 - energy) * 0.1 ]; if satiated no more honeydew will be consumed
set honeydew honeydew - HV
let EAH 0.05 * HV ;;energy reward. Lower than when flowers are consumed because honeydew is likely nutriotionally inferior ;;als test heel hoog gezet
set energy energy + EAH
set timeeating_honey timeeating_honey + 1 ;;dit zou ik ook nog kunnen veranderen naar timeeatinghoneydew
;; in dit stuk code heb ik de parameters nog niet allemaal aangemaakt in het model. Dit kan later als dit goed blijkt te werken.
end
to eat
let a 0.5 ;; flower exploration rate (surface / timestep)
let Th 0.1 ;; flower handling time (timesteps)
let FV a * nectarflowers / (1 + Th * a * nectarflowers) ;; flower visitation (numbers / timestep)
if (FV * 0.1) > (1.47 - energy) [set FV (1.47 - energy) * 0.1 ] ;; if satiated no more flowers will be visited
set nectarflowers nectarflowers - FV ;; less flowers remain with nectar
let EA 0.1 * FV ;; nectar consumption
set energy energy + EA
set timeeating timeeating + 1
set memoryF but-first memoryF
set memoryF lput (nectarflowers / (1 + count turtles-here)) memoryF
if (first interval) > 0 [set interval fput 0 interval]
end
to lay
let ovi_costs 0.002
set eggs eggs - 1
set load load - 1
set fresheggs fresheggs + 1 ;;Lay it lays one egg
set energy energy - ovi_costs
set interval replace-item 0 interval (first interval + 1)
end
to-report P
report aphids / ( 1 + sum (peggs) + (larvae / sigma)) ;;index P = prey to predator ratio
end
Look up every in the NetLogo dictionary and you will see that you are basically saying to create the turtles then wait 144 seconds in real time. Then the procedure that actually creates the turtles says to wait 144 seconds between each sprout.
In your go procedure you have:
if ticks mod 144 = 0 [ time ]
As you say in your comments, this calls the 'time' procedure every 144 ticks (which represents once per day).
Simply get rid of all the every statements:
to time
ask patches with [ sum ppupae > 0.1]
[ sprout-hoverflies
]
end
to sprout-hoverflies
sprout item dta ppupae
[ set shape "hoverfly"
set size 8
]
end
For that matter, it's not entirely clear why you have a procedure which simply calls one other procedure. You stated that your model is complete, so it's not that you are intending to have other procedures called. So, you could have:
to time
ask patches with [ sum ppupae > 0.1]
[ sprout item dta ppupae
[ set shape "hoverfly"
set size 8
]
]
end
While you're at it, you could change the procedure name to something informative like 'daily-births'
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
This is for a car to not pass a car in front or in front to the left and to the right. how can i use all commands, how do i link them?
to place-cars-level0 ;creating basic real life driver
;make a random placement of cars
ask n-of (num-of-cars-level0 / 3) patches with [meaning = "road-up"] [
if not any? cars-on patch pxcor (pycor + 1) and not any? cars-here and not any? cars-on patch pxcor (pycor - 1) and not any? patches with [meaning = "crossing"] in-radius 2 [
sprout-cars 1 [
set size 3
set will-turn? "maybe"
set will-stop? "maybe"
set shape "car top"
set politeness basic-politeness + random (101 - basic-politeness)
if random 100 > basic-politeness [set politeness random 21]
set heading 0
let s random 10
if s < 7 [set maxSpeed speed-limit - 15 + random 16]
if s = 7 [set maxSpeed speed-limit - 20 + random 6]
if s > 7 [set maxSpeed speed-limit + random 16]
set speed maxSpeed - random 20
]
]
] ; set the number of cars for the level, size, politness,speed
ask n-of (num-of-cars-level0 / 3) patches with [meaning = "road-down" and count turtles-on neighbors = 0] [
if not any? cars-on patch pxcor (pycor + 1) and not any? cars-here and not any? cars-on patch pxcor (pycor - 1) and not any? patches with [meaning = "crossing"] in-radius 2 [
sprout-cars 1 [
set size 3
set shape "car top"
set politeness basic-politeness + random (101 - basic-politeness)
if random 100 > basic-politeness [set politeness random 21]
set heading 180
set will-turn? "maybe"
set will-stop? "maybe"
let s random 10
if s < 7 [set maxSpeed speed-limit - 15 + random 16]
if s = 7 [set maxSpeed speed-limit - 20 + random 6]
if s > 7 [set maxSpeed speed-limit + random 16]
set speed maxSpeed - random 20
]
]
]
ask n-of (num-of-cars-level0 / 3) patches with [meaning = "road-left" and
count turtles-on neighbors = 0] [
if not any? cars-on patch (pxcor + 1) pycor and not any? cars-here and not
any? cars-on patch (pxcor - 1) pycor and not any? patches with [meaning =
"crossing"] in-radius 2 [
sprout-cars 1 [
set will-turn? "maybe"
set will-stop? "maybe"
set size 3
set shape "car top"
set politeness basic-politeness + random (101 - basic-politeness)
if random 100 > basic-politeness [set politeness random 21]
set heading 270
let s random 10
if s < 7 [set maxSpeed speed-limit - 15 + random 16]
if s = 7 [set maxSpeed speed-limit - 20 + random 6]
if s > 7 [set maxSpeed speed-limit + random 16]
set speed maxSpeed - random 20
]
]
]
while [count cars < num-of-cars-level0] [
ask one-of patches with [meaning = "road-right"] [
if not any? cars-on patch (pxcor + 1) pycor and not any? cars-here and not
any? cars-on patch (pxcor - 1) pycor and not any? patches with [meaning =
"crossing"] in-radius 2 [
sprout-cars 1 [
set will-turn? "maybe"
set will-stop? "maybe"
set size 3
set shape "car top"
set politeness basic-politeness + random (101 - basic-politeness)
if random 100 > basic-politeness [set politeness random 21]
set heading 90
let s random 10
if s < 7 [set maxSpeed speed-limit - 15 + random 16]
if s = 7 [set maxSpeed speed-limit - 20 + random 6]
if s > 7 [set maxSpeed speed-limit + random 16]
set speed maxSpeed - random 20
]
]
]
]
end
in the section above is the code for writing the car placement and below is the speed and control
to control-speed
let car-ahead one-of cars-on patch-ahead 4
ifelse car-ahead = nobody [
ifelse speed < maxSpeed [set speed speed + acceleration] [set speed speed
- deceleration]
]
[
ifelse [speed] of car-ahead = 0 [set speed 0] [
ifelse [speed] of car-ahead >= maxSpeed [
set speed maxSpeed
set speed speed - deceleration
] [
;try to overtake
;ifelse [meaning] of patch-left-and-ahead 90 1 = meaning and not any? turtles-on patch-left-and-ahead 90 1 and [meaning] of patch-left-and-ahead 90 1 != "crossroad"
;and meaning != "crossing" and [meaning] of patch-left-and-ahead 180 1.3 != "crossing" and not any? turtles-on patch-left-and-ahead 169 3
;and not any? turtles-on patch-left-and-ahead 45 1 and not any? turtles-on patch-left-and-ahead 135 1 and not any? turtles-on patch-left-and-ahead 23 2
; and not any? turtles-on patch-left-and-ahead 157 2 and not any? turtles-on patch-left-and-ahead 12 3 and [meaning] of patch-ahead 1 != "crossing" [move-to patch-left-and-ahead 90 1] [
; ifelse [meaning] of patch-right-and-ahead 90 1 = meaning and not any? turtles-on patch-right-and-ahead 90 14 and [meaning] of patch-right-and-ahead 90 1 != "crossroad"
;and meaning != "crossing" and [meaning] of patch-right-and-ahead 180 1.3 != "crossing" and not any? turtles-on patch-right-and-ahead 12 3
;and not any? turtles-on patch-right-and-ahead 45 1 and not any? turtles-on patch-right-and-ahead 135 1 and not any? turtles-on patch-right-and-ahead 23 2
;and not any? turtles-on patch-right-and-ahead 157 2 and not any? turtles-on patch-right-and-ahead 169 3 and [meaning] of patch-ahead 1 != "crossing"[move-to patch-right-and-ahead 90 1] [
;set speed [speed] of car-ahead
;set speed speed - deceleration]
]
]
]
i hope this is more usefulthe image shows the overall simulation
if you can see the image, my aim is to keep one car in each lane rather then three side by side per lane, this is to simulate semi autonomous vehicle lane centering
Okay, instead of doing
let car-ahead one-of cars-on patch-ahead 4
what you probably want to do is create a patch-set of all the three patches that a potential car could be occupying. That's a little difficult without knowing how you have created the lanes, and you need to be careful because the car doing the asking could be in any of three patches too. After creating that patchset, you can simply do what you did before but using the patch-set.
let in-lane (patch-set patch-ahead 4 <other patches you need go here>)
let car-ahead one-of cars-on in-lane