List all potential paths between linked turtles - netlogo

Is there an easy way using the NW extension to generate all possible paths without loops (not just the shortest using path-to) between a pair of linked turtles?

Now that this question is open again, I'm just reposting my comment as an answer:
For an undirected network, there are infinite number of paths between two turtles if there are any loops anywhere in the network. Because of that, I'll assume you meant "all paths between turtles that do not contain loops".
The NW extension doesn't support this I'm afraid. It will give you all of the shortest paths (it randomly chooses one each time you call path-to). Non-shortest paths are computationally intense to find (or rather, finding all of them is intense because there are so many) and usually not very useful. Feel free to request the feature though: https://github.com/NetLogo/NW-Extension/issues/new.
Note that finding a single, random non-shortest path is super easy, especially if you don't mind loops: just walk the network, picking random links each time, until you arrive at your destination. To eliminate loops, just make sure you haven't visited that node yet. That is:
to-report random-path-to [ target ]
let path (list self)
while [ last path != target ] [
set path lput [one-of link-neighbors] of (last path) path
]
report path
end
Without loops:
to-report random-path-to [ target ]
let path (list self)
while [ last path != target ] [
set path lput [one-of link-neighbors with [ not member? self path ] ] of (last path) path
]
report path
end
I didn't test either of these, but they should at least point you in the right direction. Also, they should be modified to take into account situations in which turtles aren't connected.

I haven't thought this completely through but it's partially right and I hope it might inspire a solution. Imagine you are trying to find all the paths between nodeA and nodeB. What you could do is run through all possible nodeC (that is, all nodes that aren't A or B) and find the shortest path between nodeA and nodeC, and between nodeC and nodeB. Then the path between A and B is the combination of the two paths. You would then have to throw out paths that backtrack but I think that can be done by rejecting the proposed path if any node (apart from C) appears in both parts. There would also be some duplicates that need to be thrown out and there are also additional paths that are, for eaxmple, the shortest between A and C, then C and D, then D and B.

Related

How can I print the xcor and ycor of all remaining turtles in a file at the end?

I want to know the coordinates of the turtles that survive at the end of my model's run.
Im running everything through BehaviorSpace in a remote cluster, so ideally I'd like to have a table or a similar file that tells me which turtles survived the run, and where they are located.
I've tried writting it this way:
to setup
set-current-directory "/home/lorena/Documents/UNAM/Tesis/versiones_de_corrección"
end
to go
if ticks = 180 [
stop
ask turtles [print-coordinates]
export-plot "Populations" "/home/lorena/Documents/UNAM/Tesis/versiones_de_corrección/populations.csv"
]
end
to print-coordinates
file-open "coordinates.csv"
file-write who
file-write xcor
file-write ycor
file-print ""
file-close-all
end
however, no files seem to be created.
I'm not sure why you are not getting any files, but can help with a couple of issues.
a) When you use BehaviorSpace, writing output files is tricky because if more than one of the model runs tries to use the same output file at the same time there is going to be an error. One solution is to include the behaviorspace run number in the file name, so each run writes its own output file.
b) Your turtle coordinates output file has a clumsy format that will be hard to use--I expect it will be easier to use if each turtle writes one line with all its information.
You can solve both of these problems by using the csv extension and replacing your "print-coordinates" procedure with something like this observer procedure (which I have not tested).
to write-turtle-coordinates
let file-name (word "coordinates-" behaviorspace-run-number ".csv")
csv:to-file file-name [(list who xcor ycor)] of turtles
end

Netlogo BehaviorSpace sweep issue

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

Does netlogo extension rngs work in the new version?

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

Run BehaviorSpace for turtles-own variables

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.

Netlogo error while observer running mean

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?