模型运行太慢。的NetLogo

时间:2017-12-12 10:11:56

标签: netlogo

我的模型曾经能够在大约10个小时内完成,但现在需要数周时间。有人可以看看我的代码并提出建议。

我是网络标识和现实主义的新手,虽然模型正在做我想做的事情。它可能没有很高效的编码。非常感谢任何可以给出的建议。

奇怪的是,每天的长度是86400蜱。第1天和第2天需要2:30分钟才能运行一天,在第3天这会跳到14分钟,随着时间的推移它会越来越慢。我不知道为什么

;;-------------------------------------------------------------;;
;;------------Kilians Wolf dispersion Model------------------- ;;
;;-------------------------------------------------------------;;

; http://jasss.soc.surrey.ac.uk/20/1/3.html tips for speeding up model
breed [adults adult]
breed [subadults subadult]
breed [juveniles juvenile]
breed [prey a-prey] ; prey is its own plural, so we use "a-prey" as the singular.
breed [livestock a-livestock]
breed [commons a-common]
breed [carcasses carcass]

adults-own [ete energy target-patch]
subadults-own [energy x0 y0 xcar ycar target-patch]
juveniles-own [energy x0 y0 xcar ycar target-patch]
prey-own [energy x0]
livestock-own [energy x0]
carcasses-own [mass decay]
commons-own [energy]

globals [ete-hour day week]

;;-------------------------------------------------------------;;
;;----------------------- SETUP COMMANDS---------------------- ;;
;;-------------------------------------------------------------;;
to setup

clear-all
ask patches [set pcolor green - 1]
 ask patch 50 50
  [
    set pcolor white
    ask patches in-radius 4.5 [set pcolor white]
    ask patches in-radius 30 with [pcolor != white] [set pcolor green - 3]
     ask  patches in-radius 50 with [pcolor != white and pcolor != green - 3] [set pcolor green]
    ]

  ask patch 50 54 [set pcolor 85]
  ask patch 50 50 [set pcolor black]

  create-adults 1
  ask adults [ set color black set shape "wolf" setxy 50 50 set energy 53760 set size 1]


       ask n-of n-parkprey patches with [pcolor = 52] [sprout-prey 1 [

    set color brown
    set shape "deer"
    set size 2
    set energy 5
  ]
]

     ask n-of n-commonsprey patches with [pcolor = 55] [sprout-prey 1 [
  ;
    set color brown
    set shape "deer"
    set size 2
    set energy 172800
  ]
]

   ask n-of n-livestock patches with [pcolor = 54] [sprout-livestock 1 [
  ;
    set color white
    set shape "cow"
    set size 2
    set energy 172800
  ]
]

    ask n-of n-commonstock patches with [pcolor = 55] [sprout-commons 1 [
  ;
    set color white
    set shape "sheep"
    set size 1
    set energy 172800
  ]
]

  ask adults [set ete 0]
  reset-ticks

end

;;-------------------------------------------------------------;;
;;----------------------- GO COMMANDS--------------------------;;
;;-------------------------------------------------------------;;

to go
  if ticks = day-length  [set day day + 1 create-next-day]
  if not any? turtles [ stop ]
  if not any? adults [ user-message "your wolves have gone extinct" stop ]
  if day = 365 [stop]


  if management
  [if count carcasses < 1 [

    if day = 0 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 7 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

      if day = 14[ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

     if day = 21 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

     if day = 28 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 35 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 42 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 49 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 56 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 63 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 70 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 77 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 84 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 91 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 98 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 105 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 112 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 119 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 126 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 133 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 140 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 147 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 154 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 161 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

      if day =  168[ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

     if day = 175 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

     if day = 182 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 189 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 196 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 203 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 210 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 217 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 224 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 231 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 238 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 245 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 252 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 259 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 266 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 273 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 280 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 287 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 294 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 301 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 308 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

        if day = 315 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 322 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 329 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 336 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 343 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 350 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 357 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]

    if day = 364 [ ask patches with [pcolor = 85] [sprout-carcasses 1 [
    set shape "carcass"
    set size 2
      set mass 172800]]]
  ]]

  if day = 150 [ask prey [reproduce]] ;day 150 of the year is in May which is the start of fawning season for deer
  if day = 180 [ask prey [reproduce]] ;day 180 of the year is in June which is the end of fawning season for deer

 ask adults [collar]
 ask adults [be-active]

 ask prey [graze]

 ask commons [browse]

 ask livestock [farm]

 ask carcasses [decompose]

tick

end

;;-------------------------------------------------------------;;
;;------------------- WOLF COMMANDS----------------------------;;
;;-------------------------------------------------------------;;

to collar
  if pcolor = 55 [set ete ete + 1]
  if pcolor = 54 [set ete ete + 1]
  if ete = ete-hour-length [set ete-hour ete-hour + 1 track]
  if pen-mode-down [set pen-mode "down"]
end


To be-active ;when wolf is full
  if energy < 40000 [ask adults [search hunt scavange take-oppurtunity invade-farm]]
  if energy > 40000  [ask adults [go-home den]]
end

to den
  if pcolor = black [set energy energy - 0.0012]
end

to search ;when wolf is hungry
  set energy  energy  - 0.06
    fd v-wolf
   if random 600 = 1 ;; frequency of turn
  [ ifelse random 2 = 0 ;; 50:50 chance of left or right
    [ rt 10 ] ;; could add some variation to this with random-normal 45 5
    [ lt 10 ]] ;; so that it samples from a dist with mean 45 SD 5
  if any? prey in-radius smell [set heading towards one-of prey in-radius smell]
  if any? livestock in-radius smell [set heading towards one-of livestock in-radius smell]
  if any? commons in-radius smell [set heading towards one-of commons in-radius smell]
  if any? carcasses in-radius smell [set heading towards one-of carcasses in-radius smell]
  if energy < 0 [die]
end


To hunt ;to kill prey and eat it
  if random-float 1 < hunt-success
  [let kill one-of prey-here in-radius smell
  if kill != nobody
   [ask kill [ die ]
      set energy energy + 6000 ]]
end

To scavange ;to kill prey and eat it
  if random-float 1 < carrion-quality
  [let carrion one-of carcasses-here in-radius smell
    if carrion != nobody
   [ask carrion [ die ]
    set energy energy + 7500 ]]
end


To invade-farm ;to kill prey and eat it
 let predation one-of livestock-here in-radius smell
  if predation != nobody
  [ ask predation [ die ]
      set energy energy + 11400 ]
end

To take-oppurtunity ;to kill prey and eat it
 let oppurtunity one-of commons-here in-radius smell
  if oppurtunity != nobody
  [ ask oppurtunity [ die ]
      set energy energy + 10059 ]
end


to go-home ;to head home after they've eaten and den until they need to feed again
  set target-patch min-one-of (patches  with [pcolor = black]) [distance myself]
  face target-patch
  fd v-wolf
  set energy  energy - 0.036
end

;;-------------------------------------------------------------;;
;;------------------- COMMONS COMMANDS-------------------------;;
;;-------------------------------------------------------------;;
to browse
    fd v-domestic
   if random 600 = 1 ;; frequency of turn
  [ ifelse random 2 = 0 ;; 50:50 chance of left or right
    [ rt 20 ] ;; could add some variation to this with random-normal 45 5
    [ lt 20 ]] ;; so that it samples from a dist with mean 45 SD 5
  if pcolor = 54 [fd -3]
  if pcolor = 52 [fd -3]
end

;;-------------------------------------------------------------;;
;;------------------- LIVESTOCK COMMANDS-----------------------;;
;;-------------------------------------------------------------;;
to farm
    fd v-domestic
   if random 600 = 1 ;; frequency of turn
  [ ifelse random 2 = 0 ;; 50:50 chance of left or right
    [ rt 20 ] ;; could add some variation to this with random-normal 45 5
    [ lt 20 ]] ;; so that it samples from a dist with mean 45 SD 5
  if pcolor = 55 [fd -5]
  if pcolor = 52 [fd -50]
end


;;-------------------------------------------------------------;;
;;------------------- PREY COMMANDS----------------------------;;
;;-------------------------------------------------------------;;

to graze
    fd v-prey
   if random 600 = 1 ;; frequency of turn
  [ ifelse random 2 = 0 ;; 50:50 chance of left or right
    [ rt 20 ] ;; could add some variation to this with random-normal 45 5
    [ lt 20 ]] ;; so that it samples from a dist with mean 45 SD 5
  if pcolor = 54 [fd -5]

end

to reproduce  ; prey procedure
    if count prey < K
  [hatch 1  [ rt random-float 360 fd v-prey ]]   ; hatch an offspring and move it forward 1 step
end


;;-------------------------------------------------------------;;
;;------------------- PREY COMMANDS----------------------------;;
;;-------------------------------------------------------------;;

to decompose
  set mass mass - 1
  if mass = 0 [die]
end
;;-------------------------------------------------------------;;
;;------------------- GENERAL COMMANDS-------------------------;;
;;-------------------------------------------------------------;;
to create-next-day
  reset-ticks
end
to track
  set ete 0
end

1 个答案:

答案 0 :(得分:3)

您的问题是be-active程序。首先,你要求所有成年人(狼)执行be-active程序,但程序也要求所有成年人做某事。你可能在其他地方也犯了同样的错误。

此外,您应该查找mod运算符,这会大大缩短您的if management代码。它还可以加快运行速度,因为代码只需要检查一天是7的倍数,而不是现在检查的次数。