netlogo

How do I export NetLogo 6.2 View when I have multiple turtle profiles?


I'm trying to implement an output which is an export from the View. I got help from this link: How to export the world of NetLogo 6.2 coloring only the patches that were born turtles? I greatly appreciate Luke C's help, as I managed to implement part of what I want.

However, I still haven't managed to generate the images by turtle profile. I have 16 turtle profiles (see ValidHabs).

I would like for example to export an image for each of the 16 turtle profiles.

For example, turtle profile 1 (can only born and hatch in habitatcover 1), so:

At tick 0, the world started with: 1 turtle no (patch 15 -4),
At tick 1, there are 2 turtles in patches (patch 15 -4) and (patch 14 -8) and
At tick 2, there are 4 turtles in patches (patch 15 -4), (patch 14 -8), (patch 12 -5) and (patch 17 -1).

So for the turtle profile 1 you would have a white exported image with the patches (patch 15 -4), (patch 14 -8), (patch 12 -5) and (patch 17 -1) painted magenta. Then I would export an image to the profile of turtle 2 etc... until I get to the profile of turtle 16 that can only spawn in habitatcovers 4 and 5.

I tried to use a foreach to export the image to each profile, but what happens is that opens a single .png file and generates the results in that single file (“overwriting”). And what I would like is to generate 1 file in .png format for each turtle profile. I thought that using foreach and carefully I could generate the files. But so far I haven't been able to.

The code:

globals [ ListProfiles ]
patches-own [ turtle-born-here ]
turtles-own [ all-code  metabolism reproduction code-metabolism code-reproduction ]

to setup
  ca
  set ListProfiles [ [1] [2] [3] [4] [5] ]
  ask patches [ set turtle-born-here false ]
  ask n-of 5 patches [
    sprout 1
    setup-turtles    
    set turtle-born-here true
  ]  
  reset-ticks
end

to go
  ask turtles [
    rt random 60 - 30
    fd 1
    if random-float 1 < 0.05 [
      hatch 1
      ask patch-here [ set turtle-born-here true]
    ]
  ]
end

to setup-turtles 
  ask turtles [
    (
      ifelse
      metabolism = 2 [set code-metabolism "M1"]
    )
    (
      ifelse
      reproduction = 5 [set code-reproduction "R1"]
    )
    set all-code ( word code-metabolism code-reproduction )
  ]
end

to example-export-image
  setup
  ; Some example go runs
  repeat 50 [ go ]

  ; This is the export-view component
  cd
  ask turtles [
    ht
  ]
  ask patches [
    ifelse turtle-born-here
    [ set pcolor magenta ]
    [ set pcolor white ]
  ] 
  carefully
  [ file-delete ( "View.png" ) ]
  [ ]
  foreach ListProfiles
    [
      y ->
      let k turtles with [ all-code = y ]
      ask k [
      export-view ( "View.png" )
      ]
    ] 
end

Solution

  • As it stands, the code you supplied is way more than what's actually needed for the question itself, which for me would boil down to: "How do I change the file name for multiple experiments / simulations?" This question has been answered before in different ways that may suit your needs.

    For a standalone example, that is really just a refinement of Charles' answer in the first link above:

    patches-own [ hab-type turtle-born-here ]
    
    globals [ profiles ]
    
    to setup
      ca
      resize-world 0 29 0 29
      set profiles [ "a" "b" "c" ]
      ask patches [
        ( ifelse pxcor < ( max-pxcor / 3) [
          set hab-type "a"
        ] pxcor < ( max-pxcor * 2 / 3 ) [
          set hab-type "b"
        ] [
          ; else
          set hab-type "c"
          ]
        )
      ]
    end
    
    to fake-simulate
      foreach profiles [ profile ->
        ask turtles [ die ]
        ask patches [
          set pcolor black
          set turtle-born-here false
        ]
        ask n-of 5 patches with [ hab-type = profile ] [
          sprout 1
          set turtle-born-here true
        ]
        reset-ticks
        repeat 50 [
          ask turtles [
            rt random 60 - 30
            fd 1
            if random-float 1 < 0.1 and [ hab-type ] of patch-here = profile [
              hatch 1
              ask patch-here [ set turtle-born-here true]
            ]
          ]
          tick
        ]
        export-image profile
      ]
    end
    
    
    to export-image [ unique-id ]
      ; Prepare the view for export
      ask turtles [ ht ]
      ask patches [
        ifelse turtle-born-here
        [ set pcolor magenta ]
        [ set pcolor white ]
      ]
      ; Use word to create a file name
      let out-file ( word "example_view_export_" unique-id "_.png" )
      export-view out-file
    end
    

    Outputs something like:

    enter image description here