1

I'm trying to vary the global variables - 'experience' and 'involvement' both from 10 to 90 using BehaviorSpace. I then use these variables to set individual turtle involvement (cit-inv) and experience (cit-inv) values. r/NetLogo - BehaviorSpace sweep issue - need help!

However, when I run BehaviorSpace, it doesn't cycle through the values and the global variables stay at 0 for the 81 runs. ie: cit-inv and cit-exp just vary randomly from 0 - 10 based on the random part of the code.

I'm happy to share other code snippets or add more context if that'll help. I guess it's worth noting that I don't explicitly assign any values to 'experience' or 'involvement' in the main body of the code.

Thanks for reading and any suggestions!

In behaviorspace:

["experience" [10 10 90]]
["involvement" [10 10 90]]

Main code:

globals [rain involvement experience]

to setup-involvement-expertise-links

  ask ssystems [
    set cit-exp ((experience) + random(10))
    set cit-inv ((involvement)+ random(10))]
  ask lim-ssystems [
    set cit-exp ((experience) + random(10))
    set cit-inv ((involvement)+ random(10))]

2 Answers2

3

I thought this might be a bug after Luke C's answer, but in looking into it I think I found the likely cause.

With BehaviorSpace, it's common to run setup for the Setup commands of the experiment. In setup, most models will usually do clear-all. But clear-all, per the documentation, will also clear-globals, so the value is cleared out before the run. This isn't a problem for widget-based (slider, input) variables, because those are not affected by clear-all. So I think this is what is happening.

So the order of events when BehviorSpace runs such an experiment is:

  • BehaviorSpace prepares to run the iteration, and sets the global value variables.
  • BehaviorSpace runs the setup procedure.
  • The setup setup procedure runs clear-all, which resets the global variables' values to 0.
  • Then the run goes as normal, but with the 0 global value.

One workaround would be to split up clear-all into its component pieces, and then only clear-globals when not running BehaviorSpace. I ran the below example to be sure I was right about the cause, and the experiment run varying speed with setup-succeed did properly vary the value of the global.

globals [ speed ]

to setup-fail
  ; when run all globals are cleared, including the values
  ; set by BehaviorSpace
  clear-all
  create-turtles 100
end

to setup-succeed
  ; everything `clear-all` does, but do not `clear-globals` for BehaviorSpace
  ; note if you had globals you *did* want to clear that are not
  ; going to be controlled by BehaviorSpace, you'd also have to 
  ; handle those manually
  if behaviorspace-run-number = 0 [ clear-globals ]
  clear-ticks
  clear-turtles
  clear-patches
  clear-drawing
  clear-all-plots
  clear-output
  create-turtles 100
end

to go
  ask turtles [ fd speed ]
end

This isn't much easier than the workaround Luke C proposed with input widgets, so that's a fine way to go, too, but I wanted to present the alternative and explain what was happening.

Jasper
  • 2,610
  • 14
  • 19
  • 1
    I think this is the correct answer too. I had a BehaviorSpace bug recently that is the other way around - I was getting bleed through from one run to the next for some of the values on sliders. It was because I changed the slider values during the run with set statements and the choice of code block was the BehaviourSpace parameter, so the 'end of run' values were still in place at the 'start of run' for the next run. That bug was caused by this same sequencing. – JenB Aug 06 '20 at 20:00
  • 1
    Thanks for the troubleshooting and explanation, that makes good sense! – Luke C Aug 06 '20 at 22:03
  • 1
    wow, interesting! thanks for clarifying. I was using clear-all in my setup, so it makes sense that it cleared the global variables too (when I didn't have sliders). – Abhishek Vishwanathan Aug 07 '20 at 17:26
2

Edit: Leaving this up as a possible alternative, but see Jasper's answer for an explanation of the actual cause of this issue along with a more proper approach rather than a workaround.

I've run into this before, and although the BehaviorSpace documentation says

Settings can be sliders, switches, choosers, or any global variable in your model

I've never been able to make global variables, as defined in the Code tab, work in BehaviorSpace. My solution for a quick fix would be to just add two Input widgets to your interface to declare your variables of interest and remove the declaration from your Code tab. That got me from 0 outputs to the expected with this toy code:

globals [rain ]

to setup
  ca 
  
  reset-ticks
end

to go
  tick
end

enter image description here

enter image description here

Luke C
  • 10,081
  • 1
  • 14
  • 21