I am trying to plot the mean of a variable which is possessed by some of my agents (turtles). However, when I click setup an error occurs: error while observer running mean...
This is my code: plot mean [score] of turtles with [not document?]
To summarize it, each turtle possesses two variables, score and document?. If the agent is not a document then use it in the mean calculation.
I created some working code under the assumption that the error was related to initialization.
Work-around code: if (ticks > 0) [plot mean [score] of turtles with [not document?]]
However, I'm looking for a better solution, I think its possible to leave out the if and somehow initialize the score variable before the mean function is called. If someone has another way to solve this I'm open to it.
Assuming you clear ticks at the beginning of setup, if (ticks > 0) should always return false when run from setup. Are you sure the mean is working, or is it possible it's just never being run?
Related
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))]
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.
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
Currently I am constructing a model on opinion dynamics and want the model to stop automatically when a certain global variable global-participation-rate remains unchanged for X amount of ticks. I probably should include something like
if stop-ticking? [stop]
in my go procedure. With the report looking something like this:
to-report stop-ticking?
ifelse (??) = ?? [report true] [report false]
end
What code should I use to check whether the global remained unchanged for a certain amount of ticks?
The easiest way is to add a new global to keep count. E.g., (abbreviating global-participation-rate to gpr):
globals [gpr ct-gpr]
to update-gpr
let old-gpr gpr ;store old value
set gpr get-gpr ;compute new value
;increment or reset the counter:
set ct-gpr ifelse-value (gpr != old-gpr) [1] [1 + ct-gpr]
end
You need a global variable for either the participation-rate or total converted or whatever. Then after your diffusion process, you do something like let new-adopters <calculation> and if-else new-adopters = total-adopters [stop] [set total-adopters new-adopters]
If you need more than one time point comparison, then you need to create a list rather than a simple value and add the new value to the end of the list and check the end of the list is all the same number.
Building off my last post, I'm trying to draw a random-beta distribution using the rngs extension of netlogo but none of the primitives seem to be working for me. Does this extension (build for netlogo 4.1) work for the new version? If not, is there a new extension that can help me draw the distribution?
If it does, then I have to assume it's a code error on my part. As per the guide at: https://github.com/NetLogo/NetLogo/wiki/Extensions My code is currently:
extensions [rngs]
to setup
make_turtles
end
to make_turtles
create-turtles 10000
ask turtles
[
rngs: init
rngs: set-seed
let dist rngs: rnd-beta random-float 999 0.9 0.5
set target_Factor dist
]
end
This is the first time I'm employing an extension so I very well may be confused as to how they work, even though I have RTFM, but that to me is what the guide says to do...
Summary of errors:
for the code above I get "nothing named RNGS has been defined"
If I remove the ":" I get "nothing named init has been defined" so it looks to me like it's not reading the primitives correctly, or am I employing it wrong?
It looks like you have a syntax error:
rngs: init should be rngs:init. Note that there is no space after the colon.
The "no-space-after-the-colon rule" should be the case for all extensions when calling their procedures (i.e. table:make or array:item).
For those trying to do this in the future, here is the correct code (I've double checked it using the histogram plot inset). I want the random number seed and stream ID to vary, so I set them to random-float of some arbitrary large number.
extensions [rngs]
turtles-own [target_factor]
to setup
clear-all
make_turtles
end
to make_turtles
create-turtles 1000
ask turtles
[
rngs:init
let stream_id random-float 999
let seed random-float 999
rngs:set-seed stream_id seed
let dist rngs:rnd-beta stream_id 0.8 0.5
set target_Factor dist
]
set-current-plot "plot 1"
histogram [target_factor] of turtles
end
How to run behaviour space experiment for turtles-own variables ?
Also how to generate output in tabular form in the output box ?
If you want only the mean, sum etcetera of the turtles-own variables use the form
mean [size] of turtles
if you want all of them it is a bit trickier I use something along the lines of
[(word who "," size) ] of turtles
it produces an assorted list. I hope somebody else has a cleaner solution but I fear that is the best one.
I parse the pens in a netlogo script (workspace.plotManager.plots.map{_.pens.map{_.saveString}). Often it represents a plot of a given variable in function of ticks. I was wondering if it was possible to hook each ploted value for every tick, so that I can get an Array of each value taken for each tick ?
Thanks