NETLOGO中的sprout导致模拟速度极慢
在我的模型中,这些斑块包含卵、由卵孵化的幼虫和由体重增加到1毫克的幼虫产生的蛹,因此这三个是变量,而不是代理集NETLOGO中的sprout导致模拟速度极慢,netlogo,Netlogo,在我的模型中,这些斑块包含卵、由卵孵化的幼虫和由体重增加到1毫克的幼虫产生的蛹,因此这三个是变量,而不是代理集 to set-up-patches ask patches [ set ppupae n-values 20 [0.1] ] 这些模型工作得很好:模型运行平稳,种群的动态在生物学上是真实的。但我希望蛹在20天大的时候变成成年的气垫蝇。因此,我将其建模如下: to time every 144 [ ask patches with [ sum ppupae > 0.1] [
to set-up-patches
ask patches [
set ppupae n-values 20 [0.1]
]
这些模型工作得很好:模型运行平稳,种群的动态在生物学上是真实的。但我希望蛹在20天大的时候变成成年的气垫蝇。因此,我将其建模如下:
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
我一加上这些线,模型就变得非常慢。为了看看它是否能运行,我离开一个小时后让电脑继续运行,回来时电脑的运行时间是50点。当我放分号时;在这些行前面,再次运行模型,它工作得非常好。顺便说一下,因为我想保持这篇文章的篇幅小且易于概述,所以我没有包含所有代码,因此缺少了一些过程,并且在每个过程中我排除了一些东西。但只有我认为对解决这个问题不重要的事情。但如果有必要,我会在底部发布我的全部代码
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
在NetLogo字典中查找每一个,你会发现你基本上是说创建海龟,然后实时等待144秒。然后,实际制造海龟的程序说,在每次发芽之间等待144秒
在go程序中,您有:
if ticks mod 144 = 0 [ time ]
正如您在评论中所说,这每144个刻度调用一次“时间”过程,表示每天一次
简单地去掉所有的语句:
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
就这一点而言,还不完全清楚为什么有一个过程只是调用另一个过程。您声明您的模型是完整的,所以您并不打算调用其他过程。因此,你可以:
to time
ask patches with [ sum ppupae > 0.1]
[ sprout item dta ppupae
[ set shape "hoverfly"
set size 8
]
]
end
当您进行此操作时,您可以将程序名称更改为“每日出生”之类的信息性名称