How to generate an output with values ​of the amount of turtles only from the initial tick and from the final tick in NetLogo 6.2?

Viewed 23

I would like to generate an output that has three columns. A column of the turtle's profile code (code_profile). Another column saying the initial amount of turtle for that profile (no). Another column telling the final turtle quantity for that profile (nf) and finally the total amount of ticks (total_ticks). For example:

enter image description here

But, I'm not able to generate these columns with this information. Only columns with information about the turtle's code, the turtle's quantity and the ticks. can anybody help me?

Thanks in advance!

globals [ edge-size listProfiles ValidHabs balance ]

patches-own [ resources turtle-count ]

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

to setup
  ca
  random-seed 1
  set ValidHabs [ [1] [2] [3] [4] [5] ]
    set edge-size 90
  set-patch-size 12
  ask patches [ set resources random 100 ]
  set listProfiles [ ]
  setup-patches
  reset-ticks
  prepare1
  prepare1.2
end


to setup-patches
  let list1 ( list 4 8 )
  (
    foreach ValidHabs [
      this-profile ->

    foreach list1
    [
  this-metabolism ->
      ask n-of 1 patches
      [
        sprout 1
        [
            set turtle-profiles this-profile
            set metabolism this-metabolism
          setup-turtles who
        ]
          set turtle-count count turtles-here
        ]
    ]
    ]
  )

end

to setup-turtles [which?]
  ask turtle who [
    if   metabolism = 4 [ set code "M1" ]
    if   metabolism = 8 [ set code "M2" ]
    set turtle-code ( word  turtle-profiles code )
    print ( word "turtle-codes: " turtle-code )
  ]
  set resource-turtle [ resources ] of patch-here
end

to go
  listProfiles-turtles
  ask turtles [
    right random 360
    fd 1
    turtle-eat
    reproduction
    if resource-turtle >= 30 [ die ]
    output
  ]
  if ticks >= 8 [ stop ]
  tick
end

to listProfiles-turtles
ask turtles [
    set listProfiles lput turtle-code listProfiles
    set listProfiles remove-duplicates listProfiles
  ]
end

to turtle-eat
 set resource-turtle (resources - metabolism )
end

to reproduction
  if ( resource-turtle > 30 ) [
    hatch 1
    set resource-turtle 0
  ]
end

to prepare1
  carefully
    [ file-delete ( word "output.csv" ) ]
  [ ]
  file-open  ( word "output.csv" )
  file-print ( word "code_profile,quantity_turtle_per_profile,tick" )

   let list-profiles [ ]
    ask turtles [
      set list-profiles lput turtle-code list-profiles
    ]
    set list-profiles remove-duplicates list-profiles
    foreach list-profiles
    [
      x ->
      let n count turtles with [ turtle-code = x ]
      file-print ( word x "," n "," ticks".initial"  )
    ]
  file-close
end

to prepare1.2
  file-open  ( word "output.csv" )
    let list-profiles [ ]
    ask turtles [
      set list-profiles lput turtle-code list-profiles
    ]
    set list-profiles remove-duplicates list-profiles
    foreach list-profiles
    [
      x ->
      let n count turtles with [ turtle-code = x ]
      file-print ( word x "," n "," ticks )
    ]
end

to output
  file-open ( word "output.csv" )
  foreach listProfiles
  [
    x ->
    let n count turtles with [ turtle-code = x ]
  ;  print ( word "code_profile:" x "," "quantity_turtle_per_profile:" n "," "tick: " ticks )
    file-print ( word x "," n "," ticks  )
  ]
end
1 Answers

One approach is:

  • Define a list of metabolisms (codes, here), at the start of a given run
  • Update that list once you've reached your stop condition

I've copied most of your code, but added the csv extension for simpler write-out. I've also altered the output from being a turtle procedure to simply occurring at the stop condition, but you can alter this example to be more in line with your actual needs. More detail in code comments:

extensions [ csv ]

globals [ edge-size listProfiles ValidHabs balance profile-detail-list ]

patches-own [ resources turtle-count ]

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

to setup
  ca
  random-seed 1
  set ValidHabs [ [1] [2] [3] [4] [5] ]
  set edge-size 90
  set-patch-size 12
  ask patches [ set resources random 100 ]
  set listProfiles [ ]
  setup-patches
  set profile-detail-list []
  foreach remove-duplicates [code] of turtles [
    cur_met ->
    set profile-detail-list lput ( list cur_met ( count turtles with [ code = cur_met ] ) ) profile-detail-list
  ]
    
    
  reset-ticks
end


to setup-patches
  let list1 ( list 4 8 )
  (
    foreach ValidHabs [
      this-profile ->
      
      foreach list1
      [
        this-metabolism ->
        ask n-of 1 patches
        [
          sprout 1
          [
            set turtle-profiles this-profile
            set metabolism this-metabolism
            setup-turtles who
          ]
          set turtle-count count turtles-here
        ]
      ]
    ]
  )
  
end

to setup-turtles [which?]
  ask turtle who [
    if   metabolism = 4 [ set code "M1" ]
    if   metabolism = 8 [ set code "M2" ]
    set turtle-code ( word  turtle-profiles code )
    print ( word "turtle-codes: " turtle-code )
  ]
  set resource-turtle [ resources ] of patch-here
end

to go
  listProfiles-turtles
  ask turtles [
    right random 360
    fd 1
    turtle-eat
    reproduction
    if resource-turtle >= 30 [ die ]
  ]
  if ticks >= 8 [
    foreach ( range 0 ( length profile-detail-list) ) [  i -> 
      ; Grab the metabolism / first-n pair
      let met_n item i profile-detail-list
      ; Extract the metabolism and first-n values for later listing
      let cur_met first met_n
      let first_n last met_n
      ; Get the count of turtles with a matching metabolism
      let final_n count turtles with [ code = cur_met ]
      let replacement_list ( list cur_met first_n final_n ticks )
      ; Replace the item in the profile-detail-list
      set profile-detail-list replace-item i profile-detail-list replacement_list
    ]     
    ; Add "header" values to the profile-detail-list
    set profile-detail-list fput ( list "code_profile" "no" "nf" "total_ticks" ) profile-detail-list
    ; Output
    let out_file "output_example.csv"
    csv:to-file out_file profile-detail-list
    print word "Exported to " out_file
    stop 
  ]
  tick
end

to listProfiles-turtles
  ask turtles [
    set listProfiles lput turtle-code listProfiles
    set listProfiles remove-duplicates listProfiles
  ]
end

to turtle-eat
  set resource-turtle (resources - metabolism )
end

to reproduction
  if ( resource-turtle > 30 ) [
    hatch 1
    set resource-turtle 0
  ]
end

The output from something like this is a csv with this format:

enter image description here

Related