2022-02-17

How can we count agents according to specific variables?

I am trying to solve the following problem:

Each agent has its specific code and at each iteration the list of agents alive in that iteration is updated. We want to calculate how many agents are die of each code present in the list per iteration. We have these procedures below (code). Obs: I need to use the profile list because of the outputs that are exported by turtle profile

Sorry for the big code below, but I tried our best to reduce it to be reproducible code

Thanks in advance

globals [ ListProfiles Death ]

turtles-own [ profiles-code metabolism-code reproduction-code metabolism reproduction resource-turtle ]

patches-own [ resources ]

to setup
  ca
  prepare
  ask patches [ set resources random 100 ]
  let list1 ( list 2 4 )
  let list2 ( list 5 10 )
  (
    foreach list1
  [
    this-metabolism ->

      foreach list2
      [
        this-reproduction ->
        ask n-of 1 patches
        [
          sprout 1
          [
            set metabolism this-metabolism
            set reproduction this-reproduction
            setup-turtles
          ]
        ]
      ]
    ]
  )
  reset-ticks
end

to setup-turtles
  (
    ifelse
    metabolism = 2 [ set metabolism-code "M1" ]
    metabolism = 4 [ set metabolism-code "M2" ]
  )

  (
    ifelse
    reproduction = 5 [ set reproduction-code "R1" ]
    reproduction = 10 [ set reproduction-code "R2" ]
  )
  set profiles-code ( word metabolism-code reproduction-code )
  print ( word "profiles-code: " profiles-code )
end

to go
  ListProfilesProc
  MetaboProc
  ProbDieProc
  output

  tick
end

to ListProfilesProc
  set ListProfiles [ ]
  ask turtles [
    set ListProfiles lput profiles-code ListProfiles
  ]
  set ListProfiles remove-duplicates ListProfiles
end

to MetaboProc
  ask turtles [
    (
      ifelse
    metabolism = 2
      [
        set resource-turtle ( resources - metabolism )
        if resource-turtle <= 60 [ DieProc ]
        (
          ifelse
          reproduction = 5
          [
            if resource-turtle >= 5 [ hatch 1 ]
          ]
          reproduction = 10
          [
            if resource-turtle >= 10 [ hatch 1 ]
          ]
        )
      ]
    )
  ]
end

to DieProc
  let code profiles-code
  foreach ListProfiles [ lp ->    ;; I think, here's the problem... I can't individualize the kills by code. I've already tried several things. And therefore I can't get the output of the deaths by code. It is always repeated (general calculation)...
    
    if lp = code
    [
      set Death Death + 1
    ]
  ]
  die
end

to ProbDieProc
  ask turtles
  [
    let prob-die random-float 1.01
    if prob-die < 0.77 [ DieProc ]
  ]
end

to prepare
  carefully
    [ file-delete ( word "output.csv" ) ]
  [ ]
  file-open  ( word "output.csv" )
  file-print ( word "code_profile,death,tick" )
  file-close
end

to output
  file-open ( "output.csv" )
  foreach ListProfiles
  [
    t ->
    file-print ( word t "," Death "," ticks )
  ]
  file-close
end


from Recent Questions - Stack Overflow https://ift.tt/X8SkdlP
https://ift.tt/Ujxtz3f

No comments:

Post a Comment