Netlogo: How to get the weight of the link - netlogo

I got a set of turtles with links connected to each other. I wanted to retrieve the weight of the link between two nodes, i've tried searching but couldn't find any info on how to do it. I'm not using nw cause i don't want the shortest path. Any ideas? This is a section of my code:
to calculate-oldpath
let oldList [ 25 0 1 2 3 4 9 8 7 6 5 10 11 12 13 14 19 18 17 16 15 20 21 22 23 24]
let weighted-dist 0
( foreach ( but-last oldList ) ( but-first oldList ) [
[ a b ] ->
ask turtle a [
let node-link link-with turtle b
;Then retrieve weight link to do adding
]
] )
print weighted-dist
end
enter image description here
The S is my starting point (25 in the list) and E is end (24 in the list) I wanted to calculate the weight of this "orange path"

Jen's answer about how to get the weight of a link is correct, but I would suggest an alternative way of computing the sum of these weights: using the sum primitive!
This requires turning your foreach into a map, but aside from that, it's pretty straightforward:
let weighted-dist sum (map [ [a b] ->
[ [ weight ] of link-with turtle b ] of turtle a
] (but-last oldList) (but-first oldList))
Another small comment: using a list of who numbers might not be the best way to approach things, but I don't know enough about your problem to suggest an alternative...

Assuming you called the weight weight (in your links-own statement that you haven't shown) then something like this should work:
to calculate-oldpath
let oldList [ 25 0 1 2 3 4 9 8 7 6 5 10 11 12 13 14 19 18 17 16 15 20 21 22 23 24]
let weighted-dist 0
( foreach ( but-last oldList ) ( but-first oldList ) [
[ a b ] ->
ask turtle a [
let node-link link-with turtle b
set weighted-dist weighted-dist + [weight] of node-link
]
] )
print weighted-dist
end
Getting the attribute value for a link is exactly the same as getting the attribute value for a turtle or patch, you use of

Related

Which primitive should be used when comparing coordinates in netlogo?

I am still learning Netlogo and not sure how to go on about comparing the coordinates from a heading against a fixed set of coordinates in a data set. 'One-of' primitive does not seem to be the right tool for the job nor is 'member?', as I am not getting the intended result.
let wallcoor []
set wallcoor [ [-17 11] [-16 11] [-15 -11] [-14 11] [-13 11] [-12 11] [-11 11] [-10 11] [-9 11] [-8
11] [ 0 11 ] [ 1 11 ] [ 2 11 ] [ 3 11 ] [ 4 11 ] [ 5 11 ] [ 6 11 ] [ 7 11 ] [ 8 11 ]
[12 11] [13 11 ] [14 11] [15 11] [16 11] [17 11] ]
let heading-equ calculate-line (xcor) (ycor) (heading-to-angle heading)
ifelse ( heading-equ = one-of wallcoor )
[ bk 0.5 lt random 30 rt random 30 fd 1 ]
[ rt random 30 lt random 30 fd 1]
The simple answer is member?. If you want to know if a given item exists in a list, then member? item wallcoor will return true if item is in wallcoor, and false if it is not.
But, I fear that you are comparing apples and oranges here. It seems from your code that heading-equ is giving you the equation of line describing the path of a turtle at a given location with a given heading, but you are comparing that to a list of points describing a wall. I would think that you would instead look to see where the equation for the path intersects with the equation for the wall to see where the turtle will hit the wall. Moreover, what if the turtle hits the wall between -17 11 and -16, 11? The location of the turtle is not always in the center of a patch. Of course I may have completely misinterpreted what heading-equ is.
A question similar to this is at How to implement obstacle avoidance in Netlogo using the concept of two intersecting lines ( turtle heading vs wall made of patches). You might check that out.
Just to elaborate on the intersecting line approach, your wall has the equation y = 11 If you have an equation for the path of the turtle, then you can figure out where those two lines intersect by solving them simultaneously. Then you can look to see if that point of intersection lies between (say) x > -9.5 and x < -0.5 to see if the intersection is in that doorway. (I've put the door between the edges of patches -10 11 and 0 11.) You also need to see if the intersection is outside the boundaries of the world (x < -17.5 or x > 17.5). If so the turtle will hit the side of the world before it hits the wall.
There is another whole approach to this that involves the turtle looking ahead for an obstacle. In the Models Library you will find two nice examples under Code Examples, "Look Ahead Example" and "Wall Following Example".

How can I shuffle the order of a matrix in NetLogo?

How can I shuffle the contents of a matrix and preserve it as a matrix? The shuffle function works for lists but not for matrices
show shuffle [1 2 3 4 5]
1 2 3 5 4
set m matrix:from-row-list
[[1 2 3 4 5]]
show shuffle m
SHUFFLE expected input to be a list but got the
org.nlogo.extensions.matrix.MatrixExtension$LogoMatrix {{matrix: [ [
1 2 3 4 5 ] ]}} instead.
There may be an easier way, but you could jump to a list, shuffle, then come back to a matrix based on the dimensions of your original matrix.
extensions [ matrix ]
to setup
ca
let m matrix:from-row-list [ [ 1 2 3 ] [ 4 5 6 ] [ 7 8 9 ] ]
let sm shuffled-matrix m
print matrix:pretty-print-text sm
reset-ticks
end
; Reporter returns a shuffled matrix
to-report shuffled-matrix [ mat ]
; Get the number of columns
let cols last matrix:dimensions mat
; Shuffle the matrix values as a list
let shuf-vals shuffle reduce sentence matrix:to-row-list mat
; Use the shuffled values to generate a new matrix
; with the same dimensions as the original
report matrix:from-row-list ( subsetter shuf-vals cols )
end
; Reporter returns a list cut into sublists
; based on the len value passed
to-report subsetter [ ls len ]
; Generate subsetting indices for the sublists
let vals ( range 0 ( length ls ) len )
; Make subsets of ls based on the subsetting indices
report map [ i -> sublist ls i ( i + len ) ] vals
end
A few example outputs from setup:
[[ 1 6 7 ]
[ 9 5 8 ]
[ 3 4 2 ]]
[[ 4 6 8 ]
[ 1 9 2 ]
[ 7 5 3 ]]
[[ 2 9 4 ]
[ 6 3 8 ]
[ 5 1 7 ]]

NetLogo Behavior Space doesn't count one of my breeds

I'm working on a model to simulate tick bites in the Netherlands. My code is finished and now I want to use Behavior Space to generate output files. I've got two breeds in my model, 'residents' and 'tourists-2d'. From both breeds, I want to count how many turtles got a tick bite. The residents-breed is working but the tourists-breed isn't. Can anybody help me how to fix this? I think it is because my tourists-breed comes from a list but I don't know how to fix it. Even if I simply use 'count tourists-2d', the output of Behavior Space is 0.
globals [ month month-day week week-day tourist-2d-list ]
breed [ residents resident ]
breed [ tourists-2d tourist-2d ]
to setup
ca
file-close-all
reset-ticks
; ---------- Creating tourist-lists -----------
set tourist-2d-list (list 1 1 2 4 8 17 38 85 188 420 935 2086 4651 10371 18750 18750 10371 4651 2086 935 420 188 85 38 17 8 4 2 1 1)
end
to go
set month ceiling(ticks / 30)
set month-day (ticks mod 30)
set week ceiling(ticks / 7)
set week-day (ticks mod 7)
; ---------- Set tick dynamics per month ----------
if month = 13 and month-day = 1 [reset-ticks]
; ---------- Set 2-day tourist dynamics per week ----------
initialize-tourists-2d
tick
remove-tourists
end
to initialize-tourists-2d
if week-day = 6 [
if week > 14 and week < 45 [
create-tourists-2d item (week - 15) tourist-2d-list [set color pink set shape "person" setxy -14 -7 set stay-period 2 set day-counter 0 ]
]
]
end
to remove-tourists
ask tourists-2d [set day-counter (day-counter + 1)
if day-counter = stay-period [die]
]
end
Additional info to my code, tourist-2d-list is a global and tourists-2d a breed. It would be great if somebody could help me!
Please post a minimal working example for such a question. This would look like the following:
globals [tourist-2d-list week-day week]
breed [tourists-2d tourist-2d]
to setup
set tourist-2d-list (list 1 1 2 4 8 17 38 85 188 420 935 2086 4651 10371 18750 18750
10371 4651 2086 935 420 188 85 38 17 8 4 2 1 1)
reset-ticks
end
to go
if (ticks > (45 * 7)) [stop]
set week-day ticks mod 7
set week (int ticks / 7)
add-tourists
tick
end
to add-tourists
if week-day = 6 and week > 14 and week < 45 [
create-tourists-2d item (week - 15) tourist-2d-list [init-tourist]
print (count tourists-2d) ;print to debug
]
end
to init-tourist
; put initializations here
end
As you can see, the count is not zero.

When extra turtle is created turtle-own variable value increases when it shouldn't

I have a number of global variables where values are set and a number of turtle-own variables where an initial value is set but change by the associated global variable once the model beings to run.
For example:
globals [dis-id]
turtles-own [identity]
to determine-parameters
set dis-id -1
ask turtles [
set identity 0
end
So when a turtle acts its identity goes up or down 1 depending on how successful an action has been.
So say an action was successful, identity should move from 0 to -1. If it is successful again it will move -1 to -2 etc. and vice-versa for failure 0 to 1, 1 to 2 etc.
My problem is that for every turtle that is added to the model (either created from the observer when running or setup procedure) this amount of change increases.
1 turtle 0 to 1, 1 to 2, 2 to 3 (correct + 1)
2 turtles 0 to 2, 2 to 4, 4 to 6 (incorrect + 2)
3 turtles 0 to 3, 3 to 6, 6 to 9 (incorrect + 3)
etc.
If the global variable is -6 instead of 1 i.e.
globals [dis-id]
turtle-own [identity]
to determine-parameters
set dis-id -6
ask turtles [
set identity 0
end
The same pattern occurs
1 turtle 0 to 6, 6 to 12, 12 to 18 (correct + 6)
2 turtles 0 to 12, 12 to 24, 24 to 36 (incorrect + 12)
3 turtles 0 to 18, 18 to 36, 36 to 54 (incorrect + 18)
etc.
The procedure/calculation to determine identity is
to succeed
ask turtles [
if outcome > expectation [
set identity (identity - dis-id)]
]
end
to fail
if outcome < expectation [
ask turtles [
set identity (identity - dis-id)]
]
end
This problem is consistent for every turtle-own variable calculated the same way.
I have attempted many fixes to no avail.
Any help with this would be much appreciated!
If any further explanation or code is needed to help problem solve this please let me know as I'm not sure how much or how little code I should put here. FYI the actual code is quite long.
UPDATE
the bit of code that was the problem was
to dothething
ask turtles [
if X >= Y [
succeed
fail]
]
Thank you to JenB for pointing this out!

NetLogo histogram data

I tried posting this to the NetLogo user group on Yahoo but wasn't successful in getting the post accepted. So I'm trying here.
NetLogo can plot histograms. Is there any way to get access to the histogram data, i.e., the data generated for the histogram plot? Thanks.
Happy Holidays, Russ!
I don't think it's possible to get the values. Though if you wanted to implement your own histogram for data, you could use something like:
to-report calc-histogram [ aList numBars aMaxValue ]
let minValue min aList
let interval (aMaxValue - minValue) / numBars
let hist []
foreach n-values numBars [?] [
let lowerBound minValue + (? * interval)
let upperBound lowerBound + interval
let x (lowerBound + upperBound) / 2
let y length filter [? >= lowerBound and ? < upperBound] aList
set hist lput (list x y ) hist
]
report hist
end
example usage:
observer> calc-histogram [0 1 18 2 3 4 5 6 7 7 7 9 10 7 15 7 17 18 19 ] 5 20
observer: [[2 4] [6 8] [10 2] [14 1] [18 4]]