LIFE OF PI(gs)

LIFE OF PI(gs) preview image

2 collaborators

Dscn3764 Desi Suyamto (Author)
Aritta Suwarno (Author)

Tags

animal welfare 

Tagged by Desi Suyamto almost 7 years ago

Visible to everyone | Changeable by everyone
Model was written in NetLogo 5.3.1 • Viewed 222 times • Downloaded 24 times • Run 0 times
Download the 'LIFE OF PI(gs)' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

breed[pigs pig]
breed[fences fence]
breed[red-zones red-zone]

globals
[
  climate-daylight?
  climate-daylight-start
  climate-daylight-end
  climate-sunhours
  climate-sunhours-statistic
  climate-temperature
  climate-temperature-minimum-statistic
  climate-temperature-maximum-statistic

  schedule-feeding-time?
  schedule-feeding
  sound-rest
  sound-unrest


  time-day
  time-daytime-start
  time-daytime-duration
  time-hour
  time-minute
  time-month
  time-month-name
  time-month-range
  time-year
  time-cumulative-minutes
  time-section
]

patches-own
[
  patch-allowed-for-activities?
  patch-eating-line-occupied?
  patch-eating-line?
  patch-feeder?
  patch-fence?
  patch-slatted?
  patch-slatted-occupied?
  patch-strawed?
  patch-strawed-occupied?
]

pigs-own
[
  pig-dominant?
  pig-eating?
  pig-cumulative-resting-time-on-slatted-floor
  pig-happiness
  pig-has-lying-down-mate?
  pig-has-sleeping-mate?
  pig-lying-down?
  pig-nearest-playmate
  pig-playmates
  pig-playing?
  pig-reach-feeder?
  pig-schedule-urinate
  pig-schedule-urinate-time-hour-start
  pig-schedule-urinate-time-minute-start
  pig-sleeping?
  pig-speed
  pig-time-to-eat
  pig-time-to-lie-down
  pig-time-to-rest
  pig-time-to-sleep
  pig-time-to-walk-to-feeder
  pig-to-eat?
  pig-to-lie-down?
  pig-to-sleep?
  pig-urinating?
]

to initialise-schedule
  let t-hour time-daytime-start
  let t-minute 0
  let t list t-hour t-minute
  set schedule-feeding lput t []

  while[t-hour >= time-daytime-start and t-hour < time-daytime-start + time-daytime-duration]
  [
    set t-minute  (t-minute + pig-feeding-interval) mod 60
    set t-hour floor (t-hour + (pig-feeding-interval / 60))
    set t list t-hour t-minute
    set schedule-feeding lput t schedule-feeding
  ]
end 

to initialise-pig-schedule-urinate
  let pig-urinate-interval (24 * 60 / pig-urinating-frequency) ;;minutes

  ask pigs
  [
    set pig-schedule-urinate-time-hour-start random 24
    set pig-schedule-urinate-time-minute-start random 60

    let t-hour pig-schedule-urinate-time-hour-start
    let t-minute pig-schedule-urinate-time-minute-start
    let t-list list t-hour t-minute
    let t (t-hour * 60) + t-minute

    set pig-schedule-urinate lput t-list []

    repeat (pig-urinating-frequency - 1)
    [
      set t t + pig-urinate-interval
      set t-minute  t  mod 60
      set t-hour floor (t / 60)
      set t-list list t-hour t-minute
      set pig-schedule-urinate lput t-list pig-schedule-urinate
    ]
  ]
end 

to calculate-pig-resting-on-slatted-floor
  ask pigs with[pig-lying-down? or pig-sleeping?]
  [
    if([patch-slatted?]of patch-here = true)
    [
      set pig-cumulative-resting-time-on-slatted-floor pig-cumulative-resting-time-on-slatted-floor + 1
    ]
  ]
end 

to define-time-section
  set time-section ifelse-value(time-hour >= time-daytime-start and time-hour < time-daytime-start + time-daytime-duration)
  [
    "day time"
  ]
  [
    "night time"
  ]
end 

to setup
  ca
  initialise-plot-resting-pigs-time-on-slatted-floor
  initialise-plot-pig-cumulative-resting-time
  initialise-plot-pig-emotion

  initialise-time
  initialise-schedule
  define-time-section
  initialise-climate
  initialise-pig-housing
  initialise-pig
  initialise-pig-schedule-urinate
  calculate-dominance
  calculate-happiness
  color-pigs
  set time-cumulative-minutes 0

  reset-ticks
end 

to generate-climate
  let i position time-month time-month-name
  let s item i climate-sunhours-statistic
  set climate-sunhours round (max list 0 random-normal first s last s)

  ifelse(climate-sunhours > 0)
  [
    set climate-daylight-start round (12 - round (climate-sunhours / 2))
    set climate-daylight-end climate-daylight-start + climate-sunhours
  ]
  [
    set climate-daylight-start 1e32
    set climate-daylight-end 1e32
  ]

  set climate-daylight? (time-hour >= climate-daylight-start and time-hour <= climate-daylight-end)

  let tmin item i climate-temperature-minimum-statistic
  let tmax item i climate-temperature-maximum-statistic

  set climate-temperature ifelse-value(climate-daylight?)
  [
    random-normal first tmax last tmax
  ]
  [
    random-normal first tmin last tmin
  ]

  if(heater?)
  [
    if(climate-temperature < temperature-threshold)
    [
      set climate-temperature climate-temperature + heating-temperature
    ]
  ]
end 

to initialise-climate
  ;; based on Schipol weather station data of year 2014

  set climate-sunhours-statistic ;;daily sunshine duration each month (mean and sd) in hours
    [
      [1.787096774  1.705626363]  [3.285714286  2.665932439]  [6.44516129  3.174674651]  [5.793333333  3.645113246]  [6.832258065  4.786814327]  [7.9  4.120679556]
      [7.241935484  4.649499915]  [6.238709677  3.989960519]  [5.651724138  3.485441219]  [3.587096774  2.641305477]  [3.09  3.137795185]  [1.729032258  2.034894518]
    ]

  set climate-temperature-minimum-statistic ;;daily minimum temperature each month (mean and sd) in degree Celcius
  [
    [3.687096774  2.822025506]  [4.335714286  1.327825927]  [3.377419355  3.176445569]  [7.066666667  2.787080209]  [8.583870968  3.819868826]  [10.32666667  2.229880673]
    [14.95806452  2.720388966]  [12.11935484  2.995153792]  [10.93103448  2.197742908]  [10.6483871  1.897168235]  [5.75  3.158213157]  [2.5  3.744150994]
  ]

  set climate-temperature-maximum-statistic ;;daily maximum temperature each month (mean and sd) in degree Celcius
  [
    [8.206451613  2.309824738]  [9.6  1.731195263]  [13.50322581  3.658732373]  [16.5  3.127960666]  [17.80967742  3.794237283]  [20.52666667  2.752545322]
    [24.27096774  3.922345688]  [20.33870968  2.898468264]  [20.93448276  2.406579651]  [16.92903226  2.738879765]  [10.99666667  3.194875566]  [7.596774194  3.201196013]

  ]
end 

to initialise-pig-housing
  ask patches
  [
    set patch-allowed-for-activities? false
    set patch-fence? false
    set patch-feeder? false
    set patch-slatted? false
    set patch-strawed? false
    set patch-eating-line? false
    set pcolor 41
  ]

  ask patches with[pxcor = min-pxcor or pxcor = max-pxcor or pycor = min-pycor or pycor = max-pycor]
  [
    set patch-fence? true
    set pcolor 65
  ]

  ask patches with
  [
    (pycor >= min-pycor + 1 and pycor <= min-pycor + 1) and
    not
    (
      (pxcor >= min-pxcor and pxcor <= min-pxcor)  or
      (pxcor >= max-pxcor and pxcor <= max-pxcor)
    )
  ]
  [
    set patch-feeder? true
    set pcolor blue
  ]

  ask patches with
  [
    (pycor = min-pycor + 2 and pycor <= min-pycor + 3) and
    not
    (
      (pxcor >= min-pxcor and pxcor <= min-pxcor)  or
      (pxcor >= max-pxcor and pxcor <= max-pxcor)
    )
  ]
  [
    set patch-eating-line? true
    set pcolor 40.75
  ]

  ask patches with
  [
    (pycor >= max-pycor - 2 and pycor <= max-pycor - 1) and
    not (patch-fence? or patch-feeder? or patch-eating-line?)
  ]
  [
    set patch-slatted? true
    set pcolor grey
  ]

  set-default-shape fences "dot"

  ask patches with[patch-fence?]
  [
    sprout-fences 1
    [
      set color 63
      set size 1
    ]
  ]

  ask patches with[not(patch-fence? or patch-feeder? or patch-slatted? or patch-eating-line?)]
  [
    set patch-strawed? true
  ]

  ask patches with[patch-eating-line?]
  [
    set patch-strawed? true
  ]

  ask patches
  [
    set patch-allowed-for-activities? (patch-strawed?  or patch-slatted?)
  ]

  ask patches with[not patch-allowed-for-activities?]
  [
    sprout-red-zones 1
    [
      ht

      set heading ifelse-value(xcor = min-pxcor)
      [
        90
      ]
      [
        ifelse-value(xcor = max-pxcor)
        [
          270
        ]
        [
          ifelse-value(ycor = max-pycor)
          [
            180
          ]
          [
            0
          ]
        ]
      ]
    ]
  ]
end 

to initialise-pig
  set-default-shape pigs "pig"
  create-pigs pig-population
  [
    move-to one-of patches with[patch-strawed? and not patch-eating-line?]
    set size 2.7 ;; assumption: adult pig length = 2 m
    set pig-time-to-rest random pig-max-initial-resting-time
    set pig-schedule-urinate[]
    set pig-dominant? false
    set pig-urinating? false


    ;; to be reinitialised at night time
    set pig-reach-feeder? true
    set pig-eating? false
    set pig-to-eat? false
    set pig-playing? false
    set pig-lying-down? false
    set pig-to-lie-down? false
    set pig-has-lying-down-mate? false

    ;; to be reinitialised at day time
    set pig-to-sleep? true
    set pig-sleeping? false
    set pig-has-sleeping-mate? false

    set pig-cumulative-resting-time-on-slatted-floor 0
  ]
end 

to initialise-time
  set time-year 1
  set time-month-range[[1 31] [32 59] [60 90] [91 120] [121 151] [152 181] [182 212] [213 243] [244 273] [274 304] [305 334] [335 365]]
  set time-month-name["jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov" "dec"]
  set time-month first time-month-name
  set time-day 1 ;;julian date
  set time-hour 0
  set time-minute 0
  set time-daytime-start 6
  set time-daytime-duration 12
end 

to update-time
  set time-minute (time-minute + 1) mod 60
  if(time-minute = 0)
  [
    set time-hour (time-hour + 1) mod 24
    if(time-hour = 0)
    [
      set time-day (time-day + 1) mod 366

      if(time-day = 0)
      [
        set time-day 1
        set time-year time-year + 1
      ]

      foreach time-month-range
      [
        let i position ? time-month-range
        if(time-day >= first ? and time-day <= last ?)
        [
          set time-month item i time-month-name
        ]
      ]
    ]
  ]
  set time-cumulative-minutes time-cumulative-minutes + 1
end 

to color-pigs
  let m max [pig-happiness]of pigs
  ask pigs
  [
    ;; not dominant pigs
    set color ifelse-value(not pig-dominant?)
    [
      ifelse-value(m > 0)
      [
        18 - (pig-happiness / m)
      ]
      [
        18 - random-float 1
      ]
    ]
    ;; dominant pigs
    [
      ifelse-value(m > 0)
      [
        23 - (pig-happiness / m)
      ]
      [
        23 - random-float 1
      ]
    ]
  ]
end 

to calculate-speed
  let m max [pig-time-to-rest]of pigs
  let s (pig-base-speed / 60) ;;convert mph into cells per tick
  ask pigs
  [
    set pig-speed ifelse-value(not (pig-eating? or pig-lying-down? or pig-urinating? or pig-sleeping?))
    [
      ifelse-value(m > 0)
      [
        s + random-float (pig-time-to-rest / m);;cells per tick
      ]
      [
        s + random-float 1
      ]
    ]
    [
      0
    ]
  ]
end 

to calculate-dominance
  ask pigs
  [
    set pig-dominant? false
  ]

  let n round ((pig-dominance-percentage / 100) * count pigs)
  let d max-n-of n pigs[pig-happiness]
  ask d
  [
    set pig-dominant? true
  ]
end 

to calculate-happiness
  ask pigs
  [
    ifelse(time-cumulative-minutes > 0)
    [
      set pig-happiness max list 0 ((pig-time-to-rest - resting-time-threshold) / time-cumulative-minutes)
      ifelse(temperature-threshold > 0)
      [
        set pig-happiness pig-happiness * max list 0 (climate-temperature / temperature-threshold)
      ]
      [
        set pig-happiness 0
      ]
    ]
    [
      set pig-happiness 0
    ]
  ]
end 

to feed
  ;; walking to feeder; i.e. eating line
  ask patches
  [
    set patch-eating-line-occupied? false
  ]

  ask pigs
  [
    set pig-to-eat? true
    set pig-eating? false
    set pig-reach-feeder? false
    set pig-time-to-walk-to-feeder 0
    set pig-time-to-eat 0
    set pig-playing? false
    set pig-to-lie-down? false
    set pig-lying-down? false
  ]
end 

to walk-to-feeder
  ask pigs with[not pig-reach-feeder?]
  [
    repeat animation-frame-rate
    [
      if([patch-eating-line?]of patch-here = false and ([pycor]of patch-here > min([pycor]of patches with[patch-eating-line?])))
      [
        set heading ifelse-value(any? patches with[patch-eating-line? and not patch-eating-line-occupied?])
        [
          min list 215 max list 145
          towards one-of patches with[patch-eating-line? and not patch-eating-line-occupied?]
        ]
        [
          min list 215 max list 145
          towards one-of patches with[patch-eating-line?]
        ]
      ]

      ifelse([patch-eating-line?]of patch-here = true)
      [
        ask patch-here
        [
          set patch-eating-line-occupied? true
        ]
        set pig-speed 0
        set pig-reach-feeder? true
        set pig-eating? true
        set pig-time-to-rest max list 0 (pig-time-to-rest - pig-time-to-walk-to-feeder)
        set pig-time-to-walk-to-feeder 0
      ]
      [
        set pig-time-to-walk-to-feeder pig-time-to-walk-to-feeder + (1 / animation-frame-rate)
      ]

      fd pig-speed / animation-frame-rate
    ]
    display
  ]
end 

to eat
  ask pigs with[pig-eating?]
  [
     set pig-speed 0

     ifelse (pig-time-to-eat < pig-eating-duration)
     [
       set pig-time-to-eat pig-time-to-eat + 1
       repeat animation-frame-rate
       [
         set heading 175 + random-float 10
         fd pig-speed / animation-frame-rate
       ]
       display
     ]
     [
       set pig-time-to-rest max list 0 (pig-time-to-rest - pig-time-to-eat)
       set pig-eating? false
       set pig-to-eat? false
       set pig-time-to-eat 0
       set heading 180 - heading ;;turn-away after eating
       avoid-forbidden-zone
     ]
  ]
end 

to play-or-lie-down
  ask pigs with[not pig-to-eat? and not pig-lying-down?]
  [
     set pig-playing? (random-float 1 < pig-happiness)
     set pig-to-lie-down? not(pig-playing?)
  ]

  play
  lie-down
end 

to avoid-forbidden-zone
  let zone-forbidden-neighbors red-zones in-radius sqrt 2
  let n count zone-forbidden-neighbors

  if(any? zone-forbidden-neighbors)
  [
    ifelse(n < 2)
    [
        let h [heading]of one-of zone-forbidden-neighbors

        if(h = 0 or h = 180)
        [
          set heading 180 - heading;;turn-away
        ]

        if(h = 90 or h = 270)
        [
          set heading (- heading);;turn-away
        ]
    ]
    [
      set heading heading + 180 ;;turn away
    ]
  ]
end 

to play
  ask pigs with[pig-playing?]
  [
    ;; play

    repeat animation-frame-rate
    [
      flock
      avoid-forbidden-zone

      fd (pig-speed / animation-frame-rate)

      set pig-time-to-rest max list 0 (pig-time-to-rest - (1 / animation-frame-rate))
    ]
    display
    avoid-forbidden-zone
  ]
end 

to lie-down-not-dominant-pigs
  ask pigs with[pig-to-lie-down? and not pig-lying-down? and not pig-dominant?]
  [
    let g pig-dominant?
    let p one-of patches with[(patch-strawed? and not patch-strawed-occupied?) or (patch-slatted? and not patch-slatted-occupied?)]
    let x [pxcor]of p
    let y [pycor]of p
    let d1 distancexy x y
    let t1 d1 * pig-speed ;;time to find place to lie down

    set pig-time-to-rest max list 0 (pig-time-to-rest - t1)

    move-to p

    ask p
    [
      if(patch-strawed?)
      [
        set patch-strawed-occupied? true
      ]
      if(patch-slatted?)
      [
        set patch-slatted-occupied? true
      ]
    ]

    if(climate-temperature <= temperature-threshold)
    [
      ifelse(any? other pigs with[pig-to-lie-down? and not pig-has-lying-down-mate?])
      [
        let m min-one-of other pigs with[pig-to-lie-down? and not pig-has-lying-down-mate?] [distance myself]
        let d2 distance m

        let t2 d2 * pig-speed ;;time to find place to lie down
        set pig-time-to-rest max list 0 (pig-time-to-rest - t2)

        ask patch-here
        [
          if(patch-strawed?)
          [
            set patch-strawed-occupied? false
          ]

          if(patch-slatted?)
          [
            set patch-slatted-occupied? false
          ]
        ]

        move-to m

        set pig-has-lying-down-mate? true

        ask m
        [
          set pig-has-lying-down-mate? true
        ]

        set pig-to-lie-down? false
        set pig-lying-down? true
        set pig-time-to-lie-down 0

      ]
      [
        set heading random-float 360
        set pig-to-lie-down? false
        set pig-lying-down? true
        set pig-time-to-lie-down 0
      ]
    ]
  ]
end 

to lie-down-dominant-pigs
  ask pigs with[pig-to-lie-down? and not pig-lying-down? and pig-dominant?]
  [
    let g pig-dominant?
    let p one-of patches with[patch-strawed? and not patch-strawed-occupied?]
    let x [pxcor]of p
    let y [pycor]of p
    let d1 distancexy x y
    let t1 d1 * pig-speed ;;time to find place to lie down

    set pig-time-to-rest max list 0 (pig-time-to-rest - t1)

    move-to p

    ask p
    [
      set patch-strawed-occupied? true
    ]

    if(climate-temperature <= temperature-threshold)
    [
      ifelse(any? other pigs with[pig-to-lie-down? and not pig-has-lying-down-mate?])
      [
        let m min-one-of other pigs with[pig-to-lie-down? and not pig-has-lying-down-mate?] [distance myself]
        let d2 distance m

        let t2 d2 * pig-speed ;;time to find place to lie down
        set pig-time-to-rest max list 0 (pig-time-to-rest - t2)

        ask patch-here
        [
          set patch-strawed-occupied? false
        ]

        move-to m

        set pig-has-lying-down-mate? true

        ask m
        [
          set pig-has-lying-down-mate? true
        ]

        set pig-to-lie-down? false
        set pig-lying-down? true
        set pig-time-to-lie-down 0

      ]
      [
        set heading random-float 360
        set pig-to-lie-down? false
        set pig-lying-down? true
        set pig-time-to-lie-down 0
      ]
    ]


  ]
end 

to lie-down
  ask patches
  [
    set patch-strawed-occupied? false
    set patch-slatted-occupied? false
  ]

  lie-down-dominant-pigs
  lie-down-not-dominant-pigs


  ask pigs with[pig-lying-down?]
  [
    let annoying-index 0
    if any? (pigs with[pig-playing?])
    [
      set annoying-index
        (count other pigs with[pig-playing?] in-radius sqrt 2) /
        (count other pigs with[pig-playing?] ); meters
    ]

    repeat animation-frame-rate
    [
      set pig-speed 0
      fd pig-speed / animation-frame-rate
      set pig-time-to-rest max list 0 (pig-time-to-rest + (1 / animation-frame-rate) - (annoying-index / animation-frame-rate))
    ]
    display
    ifelse(pig-time-to-lie-down < pig-resting-duration)
    [
      set pig-time-to-lie-down pig-time-to-lie-down + 1
    ]
    [
      set pig-lying-down? false
      set pig-has-lying-down-mate? false
    ]
  ]
end 

to sleep-not-dominant-pigs
  ask pigs with[pig-to-sleep? and not pig-sleeping? and not pig-dominant?]
  [
    let g pig-dominant?
    let p one-of patches with[(patch-strawed? and not patch-strawed-occupied?) or (patch-slatted? and not patch-slatted-occupied?)]
    let x [pxcor]of p
    let y [pycor]of p
    let d1 distancexy x y
    let t1 d1 * pig-speed ;;time to find place to lie down

    set pig-time-to-rest max list 0 (pig-time-to-rest - t1)

    move-to p

    ask p
    [
      if(patch-strawed?)
      [
        set patch-strawed-occupied? true
      ]
      if(patch-slatted?)
      [
        set patch-slatted-occupied? true
      ]
    ]

    if(climate-temperature <= temperature-threshold)
    [
      ifelse(any? other pigs with[pig-to-sleep? and not pig-has-sleeping-mate?])
      [
        let m min-one-of other pigs with[pig-to-sleep? and not pig-has-sleeping-mate?] [distance myself]
        let d2 distance m

        let t2 d2 * pig-speed ;;time to find place to lie down
        set pig-time-to-rest max list 0 (pig-time-to-rest - t2)

        ask patch-here
        [
          if(patch-strawed?)
          [
            set patch-strawed-occupied? false
          ]

          if(patch-slatted?)
          [
            set patch-slatted-occupied? false
          ]
        ]

        move-to m

        set pig-has-sleeping-mate? true

        ask m
        [
          set pig-has-sleeping-mate? true
        ]

        set pig-to-sleep? false
        set pig-sleeping? true
        set pig-time-to-sleep 0

      ]
      [
        set heading random-float 360
        set pig-to-sleep? false
        set pig-sleeping? true
        set pig-time-to-sleep 0
      ]
    ]

  ]
end 

to sleep-dominant-pigs
  ask pigs with[pig-to-sleep? and not pig-sleeping? and pig-dominant?]
  [
    let g pig-dominant?
    let p one-of patches with[patch-strawed? and not patch-strawed-occupied?]
    let x [pxcor]of p
    let y [pycor]of p
    let d1 distancexy x y
    let t1 d1 * pig-speed ;;time to find place to lie down

    set pig-time-to-rest max list 0 (pig-time-to-rest - t1)

    move-to p

    ask p
    [
      set patch-strawed-occupied? true
    ]

    if(climate-temperature <= temperature-threshold)
    [
      ifelse(any? other pigs with[pig-to-sleep? and not pig-has-sleeping-mate?])
      [
        let m min-one-of other pigs with[pig-to-sleep? and not pig-has-sleeping-mate?] [distance myself]
        let d2 distance m

        let t2 d2 * pig-speed ;;time to find place to lie down
        set pig-time-to-rest max list 0 (pig-time-to-rest - t2)

        ask patch-here
        [
          set patch-strawed-occupied? false
        ]

        move-to m

        set pig-has-sleeping-mate? true

        ask m
        [
          set pig-has-sleeping-mate? true
        ]

        set pig-to-sleep? false
        set pig-sleeping? true
        set pig-time-to-sleep 0

      ]
      [
        set heading random-float 360
        set pig-to-sleep? false
        set pig-sleeping? true
        set pig-time-to-sleep 0
      ]
    ]

  ]
end 

to sleep
  ask patches
  [
    set patch-strawed-occupied? false
    set patch-slatted-occupied? false
  ]

  sleep-dominant-pigs
  sleep-not-dominant-pigs


  ask pigs with[pig-sleeping?]
  [
    repeat animation-frame-rate
    [
      set pig-speed 0
      fd pig-speed / animation-frame-rate
      set pig-time-to-rest max list 0 (pig-time-to-rest + (1 / animation-frame-rate))
    ]
    display
    ifelse(pig-time-to-sleep < pig-resting-duration)
    [
      set pig-time-to-sleep pig-time-to-sleep + 1
    ]
    [
      ifelse(time-section = "night time")
      [
        set pig-time-to-sleep pig-time-to-sleep + 1
      ]
      [
        set pig-sleeping? false
        set pig-has-sleeping-mate? false
      ]
    ]
  ]
end 

to do-during-nighttime
  ;; reinitialise
  ask pigs
  [
    set pig-speed 0
    set pig-reach-feeder? true
    set pig-eating? false
    set pig-to-eat? false
    set pig-playing? false
    set pig-lying-down? false
    set pig-to-lie-down? false
    set pig-has-lying-down-mate? false

    set pig-to-sleep? true
  ]

  sleep
end 

to urinate
  ask pigs
  [
    set pig-urinating? member? (list time-hour time-minute) pig-schedule-urinate
  ]

  ask pigs with[pig-urinating?]
  [
     ifelse any? other pigs-here
     [
       let distuberd-pigs other pigs-here
       ask distuberd-pigs
       [
         ifelse random-float 1 < .5
         [
           rt random-float 45
         ]
         [
           lt random-float 45
         ]
         repeat animation-frame-rate
         [

           fd pig-speed / animation-frame-rate
           avoid-forbidden-zone
           set pig-time-to-rest max list 0 (pig-time-to-rest - (1 / animation-frame-rate))
         ]
         display
       ]
     ]
     [
       repeat animation-frame-rate
       [
         set pig-time-to-rest max list 0 (pig-time-to-rest - (1 / animation-frame-rate))
       ]
       display
     ]
  ]
end 

to do-during-daytime
  ask pigs
  [
    set pig-sleeping? false
    set pig-to-sleep? false
    set pig-has-sleeping-mate? false
  ]

  let t list time-hour time-minute
  set schedule-feeding-time? member? t schedule-feeding

  if(schedule-feeding-time?)
  [
    feed
  ]

  walk-to-feeder
  eat
  play-or-lie-down
end 

to go
  calculate-pig-resting-on-slatted-floor
  define-time-section
  generate-climate
  calculate-speed
  urinate


  ifelse(time-section = "day time")
  [
    do-during-daytime
  ]
  [
    do-during-nighttime
  ]
  calculate-dominance
  calculate-happiness
  color-pigs
  plot-resting-pigs-time-on-slatted-floor
  plot-pig-cumulative-resting-time
  plot-pig-emotion
  update-time

  tick ;;per minute
end 
;;

;; pig playing rule following flocking rule

to flock
  find-playmates
  if(any? pig-playmates)
  [
    find-nearest-playmate
    ifelse(distance pig-nearest-playmate < 1) ;;meters
    [
      separate
    ]
    [
      align
      cohere
    ]
  ]
end 

to find-playmates
  let group pig-dominant?
  set pig-playmates other pigs with[pig-playing? and pig-dominant? = group] in-radius sqrt 8 ;;meters
end 

to find-nearest-playmate
  set pig-nearest-playmate min-one-of pig-playmates [distance myself]
end 

to separate
  turn-away ([heading] of pig-nearest-playmate) 1.5 ;;degrees
end 

to align
  turn-towards average-playmate-heading 5 ;;degrees
end 

to-report average-playmate-heading
  let x-component sum [dx] of pig-playmates
  let y-component sum [dy] of pig-playmates
  ifelse(x-component = 0 and y-component = 0)
  [
    report heading
  ]
  [
    report atan x-component y-component
  ]
end 

to cohere
  turn-towards average-heading-towards-playmates 3 ;;degrees
end 

to-report average-heading-towards-playmates
  let x-component mean [sin (towards myself + 180)] of pig-playmates
  let y-component mean [cos (towards myself + 180)] of pig-playmates
  ifelse(x-component = 0 and y-component = 0)
  [
    report heading
  ]
  [
    report atan x-component y-component
  ]
end 

to turn-towards [new-heading max-turn]
  turn-at-most (subtract-headings new-heading heading) max-turn
end 

to turn-away [new-heading max-turn]
  turn-at-most (subtract-headings heading new-heading) max-turn
end 

to turn-at-most [turn max-turn]
  ifelse(abs turn > max-turn)
  [
    ifelse(turn > 0)
    [
      rt max-turn
    ]
    [
      lt max-turn
    ]
  ]
  [
    rt turn
  ]
end 

;;

to initialise-plot-pig-cumulative-resting-time
  let plot-pen-color n-values pig-population [5 + ? * 10]
  let plot-pen-list
  [
    "pig 1" "pig 2" "pig 3" "pig 4" "pig 5" "pig 6" "pig 7" "pig 8" "pig 9" "pig 10"
    "pig 11" "pig 12" "pig 13" "pig 14" "pig 15" "pig 16" "pig 17" "pig 18" "pig 19" "pig 20"
  ]

  let plot-pen sublist plot-pen-list 0 pig-population
  set-current-plot "pig cumulative resting time"
  foreach plot-pen
  [
    let i position ? plot-pen
    set-current-plot-pen ?
    set-plot-pen-color white
  ]

  foreach plot-pen
  [
    let i position ? plot-pen
    set-current-plot-pen ?
    set-plot-pen-color item i plot-pen-color
  ]
end 

to plot-pig-cumulative-resting-time
  let plot-pen-color n-values pig-population[5 + ? * 10]
  let plot-pen-list
  [
    "pig 1" "pig 2" "pig 3" "pig 4" "pig 5" "pig 6" "pig 7" "pig 8" "pig 9" "pig 10"
    "pig 11" "pig 12" "pig 13" "pig 14" "pig 15" "pig 16" "pig 17" "pig 18" "pig 19" "pig 20"
  ]
  let plot-pen sublist plot-pen-list 0 pig-population
  let p sort-on [who] pigs


  set-current-plot "pig cumulative resting time"

  foreach plot-pen
  [
    let i position ? plot-pen
    set-current-plot-pen ?
    plot [pig-time-to-rest]of item i p
  ]
end 

to initialise-plot-resting-pigs-time-on-slatted-floor
  let plot-pen-color n-values pig-population [5 + ? * 10]
  let plot-pen-list
  [
    "pig 1" "pig 2" "pig 3" "pig 4" "pig 5" "pig 6" "pig 7" "pig 8" "pig 9" "pig 10"
    "pig 11" "pig 12" "pig 13" "pig 14" "pig 15" "pig 16" "pig 17" "pig 18" "pig 19" "pig 20"
  ]

  let plot-pen sublist plot-pen-list 0 pig-population
  set-current-plot "resting pig cumulative time on slatted floor"
  foreach plot-pen
  [
    let i position ? plot-pen
    set-current-plot-pen ?
    set-plot-pen-color white
  ]

  foreach plot-pen
  [
    let i position ? plot-pen
    set-current-plot-pen ?
    set-plot-pen-color item i plot-pen-color
  ]
end 

to plot-resting-pigs-time-on-slatted-floor
  let plot-pen-color n-values pig-population[5 + ? * 10]
  let plot-pen-list
  [
    "pig 1" "pig 2" "pig 3" "pig 4" "pig 5" "pig 6" "pig 7" "pig 8" "pig 9" "pig 10"
    "pig 11" "pig 12" "pig 13" "pig 14" "pig 15" "pig 16" "pig 17" "pig 18" "pig 19" "pig 20"
  ]
  let plot-pen sublist plot-pen-list 0 pig-population
  let p sort-on [who] pigs


  set-current-plot "resting pig cumulative time on slatted floor"

  foreach plot-pen
  [
    let i position ? plot-pen
    set-current-plot-pen ?
    plot [pig-cumulative-resting-time-on-slatted-floor]of item i p
  ]
end 

;;

to initialise-plot-pig-emotion
  let plot-pen-color n-values pig-population [5 + ? * 10]
  let plot-pen-list
  [
    "pig 1" "pig 2" "pig 3" "pig 4" "pig 5" "pig 6" "pig 7" "pig 8" "pig 9" "pig 10"
    "pig 11" "pig 12" "pig 13" "pig 14" "pig 15" "pig 16" "pig 17" "pig 18" "pig 19" "pig 20"
  ]

  let plot-pen sublist plot-pen-list 0 pig-population
  set-current-plot "pig emotion"
  foreach plot-pen
  [
    let i position ? plot-pen
    set-current-plot-pen ?
    set-plot-pen-color white
  ]

  foreach plot-pen
  [
    let i position ? plot-pen
    set-current-plot-pen ?
    set-plot-pen-color item i plot-pen-color
  ]
end 

to plot-pig-emotion
  let plot-pen-color n-values pig-population[5 + ? * 10]
  let plot-pen-list
  [
    "pig 1" "pig 2" "pig 3" "pig 4" "pig 5" "pig 6" "pig 7" "pig 8" "pig 9" "pig 10"
    "pig 11" "pig 12" "pig 13" "pig 14" "pig 15" "pig 16" "pig 17" "pig 18" "pig 19" "pig 20"
  ]
  let plot-pen sublist plot-pen-list 0 pig-population
  let p sort-on [who] pigs


  set-current-plot "pig emotion"

  foreach plot-pen
  [
    let i position ? plot-pen
    set-current-plot-pen ?
    plot [pig-happiness]of item i p
  ]
end 

There is only one version of this model, created almost 7 years ago by Desi Suyamto.

Attached files

File Type Description Last updated
LIFE OF PI(gs).png preview Preview for 'LIFE OF PI(gs)' almost 7 years ago, by Desi Suyamto Download

This model does not have any ancestors.

This model does not have any descendants.