Netlogo BehaviorSpace Optmization - Delete Aborted Runs - Final Command - netlogo

I am working with quite a lot of runs and most of them will be aborted. Can I write a Final Command that deletes aborted runs in order to speed up the process and/or make my csv files smaller?

There is nothing allowing you to do that in a straightforward way. And it wouldn't really speed things up anyway.
Unless the size of your csv files is really a problem, the easiest thing is just to filter out aborted runs in whichever data analysis program you use.
The only alternative I can think of is to write your own experiment code. Assuming you have a stop-condition? reporter and you set aborted? true when a run is aborted, you could do something along the lines of:
extensions [ csv ]
globals [ aborted? ]
to experiment
let run-number 1
let file-name "experiment.csv"
if file-exists? file-name [ file-delete file-name ]
file-open file-name
foreach [ 1 2 3 ] [ ; vary x
set x ?
foreach [ "a" "b" "c" ] [ ; vary y
set y ?
set run-number run-number + 1
setup
while [ not stop-condition? ] [ go ]
if not aborted? [
file-print csv:to-row (list
run-number
x
y
(count turtles)
; whatever else you want to measure...
)
]
if run-number mod 10 = 0 [ file-flush ]
]
]
file-close-all
end
This assumes that x and y are, respectively, a slider and a chooser widget. If you want to vary global variables, you'll have to be careful not to override them with a call to clear-all in setup.
As you can see, all of this is far from ideal and probably error-prone.

Related

How do I do a SWITCH or SELECT CASE in NetLogo?

NetLogo does not have a native SWITCH, SWITCH-CASE, or SELECT-CASE type of multiple condition, how do I do the same thing without having a giant mess of nested IF-ELSE statements?
Since NetLogo 6.1, NetLogo has supported multi-case ifelse, which can be used similarly to other languages' switch statements. Here is an example from the docs:
ask patches [
let choice random 4
(ifelse
choice = 0 [
set pcolor red
set plabel "r"
]
choice = 1 [
set pcolor blue
set plabel "b"
]
choice = 2 [
set pcolor green
set plabel "g"
]
; elsecommands
[
set pcolor yellow
set plabel "y"
])
]
And here is the example from the other answer rewritten to use it:
;; example of SWITCH style conditional block
(ifelse
(criteria-1) [ action-1 ]
(criteria-2) [ action-2 ]
(criteria-3) [ action-3 ]
[ default-action ]
)
This does not need to have the "multi-close" of the ] on the last line, so it overall seems cleaner to me.
We can simulate a SWITCH statement with a chain of nested IF-ELSE statements, formatted so it's not a big mess, and actually easy to read and edit.
We will format the chain of IF-ELSE statement with the following goals:
avoid excessive indenting,
avoid overly long code lines
make it look more like switch in other languages
rearranging the cases does not break the syntax
there is a default case
A SWITCH Statement
Here's how it looks:
;; example of SWITCH style conditional block
if-else [ criteria 1 ] [ action-1 ][
if-else [ criteria 2 ] [ action-2 ][
if-else [ criteria 3 ] [ action-3 ][
if-else [ true ] [ default-action ][
]]]]
Other considerations
Some programmers may find it offensive to have a default action that tests for true, and leaves the "else block" if the final "IF" empty. You can do what you like. I find making the default case explicit, makes all the actions formatted exactly the same, and avoids an awkward cluster of punctuation at the end.
Avoid overly long lines--it makes it harder to read
Keep conditions short--wrap them in reporter if needed.
Keep actions short--wrap them in procedures if needed.
This helps the code read more like a story and less like code.

Trying to ask n-of 10 turtles to do a random procedure out of a list of three

This is currently what I am struggling with. I am new to netlogo so any help is nice. This is a modification on the 'virus' tutorial.
ifelse-value
choice = 0 [ go-quarantine ]
choice = 1 [ wear-mask ]
[ get-sick ] ) ]
Brandon, and welcome! Yeah the documentation is a little fuzzy on this.
I think you want just "ifelse", to branch to some action, not "ifelse-value" which returns a value.
you need a "(" before "ifelse" or it won't work. You have the closing ")".
you do not need a closing "]"
"choice" is not a necessary magic word. You can use any logical test.
So the command you're looking for would be this pattern, where white space is flexible in NetLogo and "..." means zero or more things may go there. I'm old-school and prefer to put parentheses around conditional tests but you don't need to.
(ifelse
( test-1 ) [ actions 1 ...]
( test-2 ) [ actions 2 ...]
...
( test-n ) [ actions n ...]
;; else
[ default actions ]
)
So say this person has 33.3% chance of going each of those ways.
you could code an instance of this pattern as follows:
let mood random 3 ;; returns integer values of 0, 1, or 2
( ifelse
( mood = 0 ) [ go-quarantine]
( mood = 1 ) [ wear-mask ]
[ get-sick ]
)
where implicitly that code would expect those actions to be defined later:
to go-quarantine [ do this ] end
to wear-mask [ do that] end
to get-sick [ do other stuff ] end
And the choices of values of 0, 1, 2 etc. and having them in that order is not required.

How to flip the sign of a variable depending on a condition, in Netlogo

The following doesn't work (there must also be a more succinct way of doing it):
ask turtles
[
ifelse return > 0
[
set tot-demand-contrarians (-(tot-demand-contrarians))
set tot-demand-followers ((tot-demand-followers))
]
[
set tot-demand-contrarians ((tot-demand-contrarians))
set tot-demand-followers (-(tot-demand-followers))
]
]
Half of your clauses set a variable to itself anyway so they're not needed. I suspect NetLogo is reading the '-' as an instruction to subtract, but you haven't said what to subtract it from. You could do something like:
set x (0 - x) ; the brackets are optional, for clarity
But personally I feel that it is more readable to multiply by -1
ask turtles
[ ifelse return > 0
[ set tot-demand-contrarians (-1 * tot-demand-contrarians) ]
[ set tot-demand-followers (-1 * tot-demand-followers) ]
]

NetLogo: foreach syntax

Very basic question, I can't see why my foreach code doesn't do anything (no error message but no effect whatsoever). So my turtles have a 3-dimensionnal variable (intention) that is preset to [0 0 0]. My final problem is much more complex than this, but in simple terms I am now trying to get every dimension of this vector to change to one, that is [1 1 1].
I have created a procedure called change-intention that uses foreach to produce this, to no effect:
to change-intention
ask turtles [
(foreach intention [ x -> set x 1])
]
end
I have tried this on the observer & turtle command line as well as on individual turtles' to no results nor errors..
Thanks!
Several problems. The first is that lists are not mutable - if you want to change the value in a list, you have to create a new list with that value. The second is that you can't use set to do that, you have to use replace-item.
The code is self contained - open a new model and try it out, changing the call in the testme procedure to different implementations. The procedure change-intention1 is the way you are currently thinking about it (my interpretation anyway). The procedure change-interpretation2 is the way to implement your approach, replacing each item and creating the new list (addressing the problems identified).
However, a better way to do this is with the map procedure instead of foreach because all the values are changed at once rather than looping through the list and dealing with each. Of course, that may not be so easy to implement in your real model.
turtles-own [intention]
to testme
clear-all
create-turtles 1
[ set intention [0 0 0]
]
ask turtles [ type "before call:" print intention ]
change-intention2
ask turtles [ type "after call:" print intention ]
reset-ticks
end
to change-intention1
ask turtles
[ foreach intention
[ x ->
print "here"
set intention 1
]
]
end
to change-intention2
ask turtles
[ foreach intention
[ x ->
let pp position x intention
type "here:" print pp
set intention replace-item pp intention 1
]
]
end
to change-intention3
ask turtles
[ set intention map [ x -> 1 ] intention
]
end

Turtles not reseting under a conditional

I'm trying to have turtles match based on attraction parameters, however I only get either one match or one mismatch and won't count anymore turtles matching, as well as not moving the counter forward.
to new-couple
set countdown2 5
ask turtles [
;; CREATE NEW COUPLE
ifelse countdown2 <= 0
[ die ]
[ set countdown2 countdown2 - 1 ] ]
end
The code that you have provided does not have an error. Try this version of it and you will see that the new-couple code correctly reduces the countdown 5 times and then kills the rest of the turtles. So it's not producing the problem that you describe of things only happening once.
to testme
clear-all
create-turtles 10
type "Start turtles: " print count turtles
new-couple
type "End turtles: " print count turtles
end
to new-couple
let countdown2 5
ask turtles [
;; CREATE NEW COUPLE
ifelse countdown2 <= 0
[ die ]
[ set countdown2 countdown2 - 1 ] ]
end
Could you please provide more of your code. Also, an explanation of what the countdown is supposed to achieve could be useful. At the moment it basically selects the number of turtles that don't die and there are easier ways to achieve that.