Extremely slow simulation caused by (?) sprout in NETLOGO - netlogo

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'

Related

NetLogo Burnin / Warmup issue

Using NetLogo 5.3.1, i'm trying to set up BehaviorSpace so that all its model runs start after exactly the same 500-tick warmup period. However, the results are not intuitive to me.
For illustrative purposes, I will use the 'Flocking.nlogo' model in the model library. Below is the model code, with 2 lines of code added to the end of the setup which saves the model's state after 500 ticks.
turtles-own [
flockmates ;; agentset of nearby turtles
nearest-neighbor ;; closest one of our flockmates
]
to setup
clear-all
create-turtles population
[ set color yellow - 2 + random 7 ;; random shades look nice
set size 1.5 ;; easier to see
setxy random-xcor random-ycor
set flockmates no-turtles ]
reset-ticks
; Now execute a 500-tick warm-up period and save the model's state
repeat 500 [ go ]
export-world "Flocking-after-500ticks.csv"
end
to go
ask turtles [ flock ]
;; the following line is used to make the turtles
;; animate more smoothly.
repeat 5 [ ask turtles [ fd 0.2 ] display ]
;; for greater efficiency, at the expense of smooth
;; animation, substitute the following line instead:
;; ask turtles [ fd 1 ]
tick
end
to flock ;; turtle procedure
find-flockmates
if any? flockmates
[ find-nearest-neighbor
ifelse distance nearest-neighbor < minimum-separation
[ separate ]
[ align
cohere ] ]
end
to find-flockmates ;; turtle procedure
set flockmates other turtles in-radius vision
end
to find-nearest-neighbor ;; turtle procedure
set nearest-neighbor min-one-of flockmates [distance myself]
end
;;; SEPARATE
to separate ;; turtle procedure
turn-away ([heading] of nearest-neighbor) max-separate-turn
end
;;; ALIGN
to align ;; turtle procedure
turn-towards average-flockmate-heading max-align-turn
end
to-report average-flockmate-heading ;; turtle procedure
;; We can't just average the heading variables here.
;; For example, the average of 1 and 359 should be 0,
;; not 180. So we have to use trigonometry.
let x-component sum [dx] of flockmates
let y-component sum [dy] of flockmates
ifelse x-component = 0 and y-component = 0
[ report heading ]
[ report atan x-component y-component ]
end
;;; COHERE
to cohere ;; turtle procedure
turn-towards average-heading-towards-flockmates max-cohere-turn
end
to-report average-heading-towards-flockmates ;; turtle procedure
;; "towards myself" gives us the heading from the other turtle
;; to me, but we want the heading from me to the other turtle,
;; so we add 180
let x-component mean [sin (towards myself + 180)] of flockmates
let y-component mean [cos (towards myself + 180)] of flockmates
ifelse x-component = 0 and y-component = 0
[ report heading ]
[ report atan x-component y-component ]
end
;;; HELPER PROCEDURES
to turn-towards [new-heading max-turn] ;; turtle procedure
turn-at-most (subtract-headings new-heading heading) max-turn
end
to turn-away [new-heading max-turn] ;; turtle procedure
turn-at-most (subtract-headings heading new-heading) max-turn
end
;; turn right by "turn" degrees (or left if "turn" is negative),
;; but never turn more than "max-turn" degrees
to turn-at-most [turn max-turn] ;; turtle procedure
ifelse abs turn > max-turn
[ ifelse turn > 0
[ rt max-turn ]
[ lt max-turn ] ]
[ rt turn ]
end
; Copyright 1998 Uri Wilensky.
; See Info tab for full copyright and license.
The BehaviorSpace window looks like this:
The added 2 lines of code, which saves the model's state after 500 ticks, come from the answer to question 6 in Chapter 9 in Railsback & Grimm 2012: Agent-based and individual-based modeling (1st edition). The answer continues by stating the next step: "Then, in BehaviorSpace, change the "Setup commands" to just import the saved world and run 1000 more ticks".
I did this, and then imported the file into R to summarise the data by calculating the mean and SD of number of flockmates at tick 100, 200, 300, 400, and 500. Below the R code:
df <- read.csv("ibm_table_output-test.csv", skip = 6)
df1 <- df %>%
rename(run_number = X.run.number.,
time_step = X.step.,
mean_flockmates = mean..count.flockmates..of.turtles
) %>%
select(run_number,
time_step,
mean_flockmates,
vision) %>%
arrange(run_number,
time_step) %>%
filter(time_step == 100 |
time_step == 200 |
time_step == 300 |
time_step == 400 |
time_step == 500)
df1_long <- melt(df1, # Apply melt function
id.vars = c("run_number", "time_step","vision"))
# Calculate a summary table
df1.summ <- df1_long %>%
group_by(time_step, vision) %>%
summarise(avg = mean(value),
sd = sd(value))
The output is as follows:
# A tibble: 15 × 4
# Groups: time_step [5]
time_step vision avg sd
<int> <int> <dbl> <dbl>
1 100 1 8.34 0
2 100 2 8.34 0
3 100 3 8.34 0
4 200 1 7.83 0
5 200 2 7.83 0
6 200 3 7.83 0
7 300 1 7.95 0
8 300 2 7.95 0
9 300 3 7.95 0
10 400 1 7.45 0
11 400 2 7.45 0
12 400 3 7.45 0
13 500 1 7.92 0
14 500 2 7.92 0
15 500 3 7.92 0
To me this output doesn't make sense.
My question is why is the average number of flockmates the same across different vision levels within the same time_step group? And why are the SDs all 0? In other words, why do the model runs produce identical outputs? I thought that initiating a burnin period would initiate identical starting positions for all simulations, but create different mean and SD values for each run because of different random numbers used? Or am I misunderstanding?
EDIT: The reason why the SDs are 0 is because there is no variation in mean values, but I don't understand why there is no variation. Below is the df1_long data frame:
run_number time_step vision variable value
1 1 100 1 mean_flockmates 8.340000
2 1 200 1 mean_flockmates 7.833333
3 1 300 1 mean_flockmates 7.953333
4 1 400 1 mean_flockmates 7.446667
5 1 500 1 mean_flockmates 7.920000
6 2 100 1 mean_flockmates 8.340000
7 2 200 1 mean_flockmates 7.833333
8 2 300 1 mean_flockmates 7.953333
9 2 400 1 mean_flockmates 7.446667
10 2 500 1 mean_flockmates 7.920000
11 3 100 2 mean_flockmates 8.340000
12 3 200 2 mean_flockmates 7.833333
13 3 300 2 mean_flockmates 7.953333
14 3 400 2 mean_flockmates 7.446667
15 3 500 2 mean_flockmates 7.920000
16 4 100 2 mean_flockmates 8.340000
17 4 200 2 mean_flockmates 7.833333
18 4 300 2 mean_flockmates 7.953333
19 4 400 2 mean_flockmates 7.446667
20 4 500 2 mean_flockmates 7.920000
21 5 100 3 mean_flockmates 8.340000
22 5 200 3 mean_flockmates 7.833333
23 5 300 3 mean_flockmates 7.953333
24 5 400 3 mean_flockmates 7.446667
25 5 500 3 mean_flockmates 7.920000
26 6 100 3 mean_flockmates 8.340000
27 6 200 3 mean_flockmates 7.833333
28 6 300 3 mean_flockmates 7.953333
29 6 400 3 mean_flockmates 7.446667
30 6 500 3 mean_flockmates 7.920000
My understanding is that you're running setup once, manually, and then running your BehaviorSpace experiment. The problem you will have with that is that the random number generator seed is including in the export-world data that you generate once by running the setup procedure. Then when you call import-world in the Setup commands: of each experiment run you will get that RNG seed imported as well. The export actually includes the full state of the RNG, but thinking of it as being the same seed is close enough.
LeirsW is correct that Flocking (as most NetLogo models, and probably the original one you had the problem with) is totally deterministic. So the outcome will be the same with the same RNG seed each time.
The fix is easy, add a second line to your BehaviorSpace experiment Setup commands: after the import-world that runs random-seed new-seed. This will make sure each model run has a new, unique RNG seed to use for the rest of its run.
The simple answer to the lack of variation is that the flocking model (after setup) is as good as 100% deterministic. There are some elements in the code that at first sight you might think have some stochasticity to them but that sochasticity doesn't have any effect.
The order in which turtles flock and move is stochastic, but they all choose how they want to move before any of them do so, which results in this stochasticity having no effect on the outcome.
The other part that you would expect carries some stochasticity is set nearest-neighbor min-one-of flockmates [distance myself], when two birds are at the exact same distance away from the focal bird. But all their locations have floating point numbers in both x and y, so I assume that the chance that you will have a tie in distances is also extremely low.

Action not happening every certain amount of ticks

this code is for the reproduction of an animal. It should happen every 10 ticks, but it is happening instantly and i dont know why. They also have energy. Reproduction is set to be random to show females.
thanks in advance for the help.
turtles-own[ energia edad]
breed[perros perro]
to setup
resize-world 0 80 0 60
clear-all
ask patches[
set pcolor green]
create-perros 50
[
set size 3 ;; easier to see
set color yellow
setxy (41 + random 39) random 60
set heading random 360
set energia 100
set edad 1
]
reset-ticks
end
to go
ask perros [ set edad edad + 1]
ask perros [
morir-perros
moverse-perros
reproducirse
]
tick
end
to morir-perros
if edad > 15 [die]
if energia < 1 [die]
end
to moverse-perros
set heading random 360 fd 5
set energia energia - 5
end
to reproducirse
if ticks mod 10 = 0 [ if random 100 > 50 [set energia energia - 60
hatch 1 [ rt random-float 360 fd 2
set energia (energia + 60) / 3]]]
end
Right now you have tick to increment your time step at the end of your go procedure. In any model, ticks start at 0 by default, so your model is evaluating ticks mod 10:
observer> show 0 mod 10
observer: 0
Since that would satisfy the condition in reproducirse, the code is evaluated. You can handle this in a variety of ways. The simplest may be to modify your if statement in reproducirse- for example:
to reproducirse
if ticks > 0 and ticks mod 10 = 0 [ if random 100 > 50 [set energia energia - 60
hatch 1 [ rt random-float 360 fd 2
set energia (energia + 60) / 3]]]
end

In Netlogo, why are the blue patches not overtaking the orange patches? why is it pulsing?

I´ve in through this in previous posts and i got a new problem. Im trying to abandoment a restoration processes. At the beginning orange and bue flow swiftly, but as soon as they interact non win and just change between them over and over. In the code, I stablished that if a patch is orange and it sees at least 3 blues it should change to blue, and i didnt stablish that if a blue patch is surrounded by orange it changes, however it happens. The idea is that at set up I have 3 color, and with some sliders (that would be in - if reforestado >= 3 [if random 100 <= SLIDER]) the patches changes and ultimetly blue (75) and a little bit of green (66) should win showing restauraton
This code can be copy and pasted and it should work, ignore the red patches that appear. Please set vertical limits to the world.
Why isnt the orange loosing agains the blue?
Thanks in advance.
patches-own
[bordear
abandono
reforestado
potrerizado
temperatura
humedad
dosel
]
breed [ potreros potrero ]
breed [ bordes borde ]
breed [ bosques bosque ]
to setup
resize-world 0 90 0 60
clear-all
ask patches with [
pxcor <= 30 and
pxcor >= min-pxcor and
pycor <= 60 and
pycor >= min-pycor ] [
set pcolor 35
set temperatura 26.5
set dosel 1
set humedad 90.4
]
;Potrero
ask patches with [
pxcor <= 60 and
pxcor >= 30 and
pycor <= 60 and
pycor >= min-pycor ] [
set pcolor 44
set temperatura 29
set dosel 84.3
set humedad 79.3;
]
;Borde
ask patches with [
pxcor <= 90 and
pxcor >= 60 and
pycor <= 60 and
pycor >= min-pycor ] [
set pcolor 66
set temperatura 26.3
set dosel 85.2
set humedad 94 ;
]
;Bosque
;se establece la forma de la rana
; Se establecen las condiciones de las ranas, tamaño color y lugar de aparicion. La energia sera igual en todas las ranas en el set up.
create-potreros 50
[ set size 3 ;; easier to see
set color yellow
setxy random xcor random ycor
move-to one-of patches with [pcolor = 35]
set heading random 45 + 45
set energia 50
] ;; red = not carrying food
;Potrero
reset-ticks
end
to go
ask potreros [
if energia < 50 [descansar-potrero]
if energia >= 50 and energia <= 80 [move-potrero]
if energia > 80 [ if ticks mod 50 = 0 [reproducirse]]
set heading random 270
fd 1
set energia energia - 2
morir]
ask patches [ restauracion ]
ask patches [ deforestacion ]
ask patches [abandonacion]
ask patches [borderizacion]
if ticks >= 500 [stop]
tick
end
to restauracion
if pcolor = 44 or pcolor = 25 or pcolor = 35[
set reforestado count neighbors with [pcolor = 66 or pcolor = 75] ]
if reforestado >= 3 [if random 100 <= 100 [
set pcolor 75
set temperatura 24.4 + random 3
set humedad 91 + random 9
set dosel 82 + random 5 ]
]
end
to deforestacion
if pcolor = 44 or pcolor = 25 or pcolor = 75 or pcolor = 66 [
set potrerizado count neighbors with [pcolor = 35] ]
if potrerizado >= 3 [if random 100 <= 0 [
set pcolor 35
set temperatura 22.7 + random 5
set humedad 84.5 + random 10
set dosel 0 + random 10]
]
end
to abandonacion
if pcolor = 35 [
set abandono count neighbors with [pcolor = 44 or pcolor = 25] ]
if abandono >= 3 [if random 100 <= 50 [
set pcolor 25
set temperatura 24.6 + random 8
set humedad 68 + random 14
set dosel 79 + random 9]
]
end
to borderizacion
if pcolor = 66 [
set bordear count neighbors with [pcolor = 44] ]
if bordear >= 3 [if random 100 <= 50 [
set temperatura 24 + random 6
set humedad 80 + random 20
set dosel 80 + random 5]
]
end
to move-potrero
ask neighbors in-radius 2
[if temperatura >= 28.6 or temperatura <= 22.6 or dosel >= 5 or humedad <= 84
[ set pcolor red
;facexy random 30 random 60
;fd 5
;set energia energia - 10]]
;[ask potreros [descansar-potrero]]
]]
end
to descansar-potrero
ifelse pcolor = 35 [
set energia energia + 6]
[set energia energia + 1]
end
to reproducirse
if energia > 80 [ if random 100 > 60 [set energia energia - 70
hatch 1 [ rt random-float 360 fd 2
set energia energia / 3]]]
end
to morir
if energia <= 1 [die]
end ```
Right now, once your patches set abandono to some value in to abandonacion, they never reset it unless their pcolor is returned to 35. So, you have all patches evaluating the if abandono >=3... code. For a quick fix to see what I mean, change your to abandonacion chunk to:
to abandonacion
set abandono 0
if pcolor = 35 [
set abandono count neighbors with [pcolor = 44 or pcolor = 25] ]
if abandono >= 3 [if random 100 <= 50 [
set pcolor 25
set temperatura 24.6 + random 8
set humedad 68 + random 14
set dosel 79 + random 9]
]
end
The above simply resets the abandono to 0 right before evaluating the abandano count to ensure that all patches are starting "fresh." There are other ways to address this, but how you actually go about it may depend on if you are tracking the number of abandono patches every tick.

Rules of use of chooser in Netlogo Model

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.

How can I move a turtle to a patch with a probability?

It is a pretty simple model so far. I want the walker to move to the patches, but not randomly how it is now but with a probability. In this case it is the list "priority" which describes the probability for each patch (used vehicle).
So my question: How can I implement the calculated priority to the move statement?
Here's the code:
breed [nodes node]
breed [walkers walker]
globals [
cost
time
wellbeing
efficiency
vec1
vec2
vec3
nodelist
; rain
]
walkers-own [
Temp_best ;aim temp
capital ; capital for each timestep to invest in transportation
adaptation
location
priority
delta-priority
] ;; holds a node
patches-own [
]
to setup
clear-all
set-default-shape nodes "circle"
;; create a random network
create-nodes 1 [ set color white ]
create-nodes 1 [ set color blue ] ;car
create-nodes 1 [ set color yellow ] ;bike
create-nodes 1 [ set color green ] ;public transportation
ask node 0 [ create-link-with node 1 ]
ask node 0 [ create-link-with node 2 ]
ask node 0 [ create-link-with node 3 ]
;; lay it out so links are not overlapping
repeat 500 [ layout ]
;; leave space around the edges
ask nodes [ setxy 0.95 * xcor 0.95 * ycor ]
;; put some "walker" turtles on the network
create-walkers 1 [
set color red
set location node 0
move-to location
]
reset-ticks
ask walkers [
set capital 5
set adaptation 0.1
set wellbeing n-values 3 [0]
set priority n-values 3 [0] ;sum of all must be 1
set delta-priority n-values 3 [0] ;sum of all must be 0
set efficiency n-values 3 [0]
]
set cost [3.5 1 0.1]
set time [0.3 0.6 0.7]
set vec1 [0.7 0.7 0.7] ;if change one, than also the other --> sum must be 1
set vec2 [0.3 0.3 0.3] ;
set vec3 [1 1 1]
ask walkers[
output-print wellbeing
]
set cost (map * cost vec1)
set time (map * time vec2)
ask walkers[
output-print cost
output-print time
]
ask walkers [
set wellbeing (map + cost time)
set wellbeing (map / vec3 wellbeing) ;devided by 1 to have a high number for wellbeing
set priority [0 0.1 0.9]
; set priority (map + priority delta-priority)
if rain? [
set priority replace-item 2 priority 0
set priority replace-item 1 priority (1 - item 0 priority + item 2 priority)
]
]
ask walkers[
output-print wellbeing
output-print priority
]
;set efficiency replace-item 1 efficiency (wellbeing 1 / )
set efficiency (map / wellbeing cost)
output-print efficiency
;end
;to calculate
;ask walker[
;set delta-priority replace-item 0 delta-priority ( adaptation * item 0 priority * ((item 0 wellbeing) - ) - ((item 1 priority * item 1 wellbeing)+(item 2 priority * item 2 wellbeing)))
; set priority replace-item 0 priority (priority 0 + delta-priority 0)
;set priority replace-item 1 priority (priority 1 + delta-priority 1)
;set priority replace-item 2 priority (priority 2 + delta-priority 2)
; set priority (map + priority delta-priority)
;ifelse wellbeing 2 > wellbeing 1
; output-print delta-priority
; ]
end
to layout
layout-spring nodes links 0.5 2 1
end
to go
ask links [ set thickness 0 ]
ask walkers [
let new-location one-of nodes ;i want to introduce the priority for each node
;one-of [link-neighbors] of location
;; change the thickness of the link I just crossed over
;ask [link-with new-location] of location [ set thickness 0.5 ]
face new-location ;; not strictly necessary, but improves the visuals a bit
move-to new-location
set location new-location
]
tick
ask nodes
[show count walkers-here]
end