如何在Net Logo中将所有海龟从一个区域移动到另一个区域?

时间:2018-11-24 03:31:12

标签: netlogo

我正在尝试创建一个用于模拟农业轮牧的模拟。我使用GitHub上的Many Regions Example创建了区域,并创建了基本的乌龟运动和草丛再生。但是,在固定的“轮换时间”之后,我很难使母牛从区域1到区域2,从区域2到区域3等。我希望奶牛保持相同的“健康”,并且希望草在旋转区域时继续生长。关于如何编写“旋转”的任何想法?

breed [cows cow]
globals [
  region-boundaries
]
turtles-own [ health]

patches-own [ energy region ]

to setup
  clear-all
  setup-regions number-of-regions
  color-regions
  setup-turtles
  reset-ticks
end

to setup-regions [ num-regions ]
  foreach region-divisions num-regions draw-region-division
  set region-boundaries calculate-region-boundaries num-regions
  let region-numbers (range 1 (num-regions + 1))
  (foreach region-boundaries region-numbers [ [boundaries region-number] ->
    ask patches with [ pxcor >= first boundaries and pxcor <= last boundaries ] [
      set region region-number
    ]
  ])
end

to-report calculate-region-boundaries [ num-regions ]
  let divisions region-divisions num-regions
  report (map [ [d1 d2] -> list (d1 + 1) (d2 - 1) ] (but-last divisions) (but-first divisions))
end

to-report region-divisions [ num-regions ]
  report n-values (num-regions + 1) [ n ->
    [ pxcor ] of patch (min-pxcor + (n * ((max-pxcor - min-pxcor) / num-regions))) 0
  ]
end

to draw-region-division [ x ]
  ask patches with [ pxcor = x ] [
    set pcolor grey + 1.5
  ]
  create-turtles 1 [
    setxy x max-pycor
    set heading 0
    set color grey - 3
    pen-down
    forward world-height
    set xcor xcor + 1 / patch-size
    right 180
    set color grey + 3
    forward world-height
    die
  ]
end

to color-regions
  ask patches with [ region != 0 ] [
    set pcolor green
    ifelse show-region?
    [set plabel region]
    [set plabel ""]
  ]
end

to setup-turtles
  set-default-shape cows "cow"
    foreach (range 1 (length region-boundaries + 1)) [ region-number ->
    let region-patches patches with [ region = region-number ]
    create-cows number-of-turtles-per-region [
      set color brown
      set size 1.5
      move-to one-of region-patches
      ifelse region = 1
      [ ]
      [ die ]
  ] ]
end

to go
  ask turtles [ move eat ]
  regrow-grass
  tick
  if ticks = rotation-time
  [ rotate ]
end

to rotate 



end

to move
  let current-region region
    right random 360
    forward 1
    set health health - 6
  keep-in-region current-region
  if ticks = 100
  [set current-region = current-region + 1] 
end


to eat
    ifelse pcolor = black
    [ ]
    [ ifelse pcolor = green [
      set pcolor pcolor + 2
      set health health + 10
      set energy energy - 10
    ]
    [ ifelse pcolor = 57
        [
        set pcolor 59
        set health health + 10
      set energy energy - 10
        ]
      [
      set pcolor black
      set health health + 10
      set energy energy - 10
  ] ]
    ifelse show-health?
    [ set label health ]
    [ set label "" ]
  ]
end

to regrow-grass
  ask patches [
    ifelse pcolor = black
    [ if random 100 < grass-growth-rate
    [set pcolor green
     set energy energy + 30
    ]]
      [ ]
  ]
end



to keep-in-region [ which-region ]
  if region != which-region [
    let region-min-pxcor first item (which-region - 1) region-boundaries
    let region-max-pxcor last item (which-region - 1) region-boundaries
    let region-width (region-max-pxcor - region-min-pxcor) + 1
    ifelse xcor < region-min-pxcor [
      set xcor xcor + region-width
    ] [
      if xcor > region-max-pxcor [
        set xcor xcor - region-width
      ]
    ]
  ]

end

to-report energy-sum
  report sum [energy] of patches
end

0 个答案:

没有答案