Which primitive should be used when comparing coordinates in netlogo? - 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".

Related

How can I change the elements of a matrix if they satisfy some condition?

I'm working with the matrix extension in NetLogo. I want to be able to modify specific elements of the matrix if they equal some number.
For instance if the value is 0.95 I want to run random 2 on it so it comes out as a 1 or a 0. And if it's a 1.75 it comes out as a 1 or a 2 with random (3 - 1) + 1
This would change my matrix m from this:
let m matrix:from-row-list [[1 0.95 0.95] [2 1 1.75] [1 2 1] ]
to this:
[[1 1 0] [.05 1 2] [.05 .25 1] ]
Thanks
I'm not sure if I understand your updated matrix example- for example, why does the 2 in the second row become 0.05 in the output? I'm assuming you have some other rules for dealing with those numbers. Anyway, I think you can use matrix:map to accomplish what you're after- you may just have to set up the rules in your anonymous reporter to reflect what you're after. Here is an example using the rules you supplied for values of 0.95 and 1.75:
extensions [ matrix ]
to matrix-manipulation
let m matrix:from-row-list [[1 0.95 0.95] [2 1 1.75] [1 2 1] ]
let m2 matrix:map [ i -> val-change i ] m
print matrix:pretty-print-text m2
end
to-report val-change [ val ]
if val = 0.95 [
report random 2
]
if val = 1.75 [
report 1 + random 2
]
report val
end
Output becomes:
[[ 1 0 1 ]
[ 2 1 2 ]
[ 1 2 1 ]]

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: How to get the weight of the link

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

Netlogo Diffusion Confusion

I've been playing around with the diffuse keyword.
Consider the following 3x3 world where there's 3 chemical gradients at the top left corner and no chemical elsewhere. Also, there's no wrap on the edges.
[3 0 0 ]
[0 0 0 ]
[0 0 0 ]
If I have a diffusion rate of .5, I would expect that 3 (gradient) * .5 (diffusion rate) / 3 (#neighbors) = .5of the gradient would be given to its 3 neighbors. I would also expect that the original patch has 1.5 units remaining.
However, when I run the diffuse code, it seems that 3 (gradient) * .5 (diffusion rate) / 8 (#neighbors) = .1875 of the gradient is being set to the 3 neighbors. The original patch then has 2.4375 remaining units which isn't .5 of the original gradient. What's going on here? Is this an error or is my understanding incorrect?
See below:
patches-own [value]
to setup
cp
ask patch 0 2 [ set value 3]
diffuse value .5
ask patch 1 1 [ show value]
end
observer: show [value] of patches
observer: [0.1875 0.1875 0 2.4375 0 0 0 0.1875 0]
observer> ask patch 0 2 [ show count neighbors]
(patch 0 2): 3
One quick edit to your code is if you want the top left patch to have a value of 3, you need to ask patch 0 2. You're currently asking the bottom right patch.
Now, your issue is coming from the fact that when you diffuse, it tried to give 1.5 value away spread out over 8 patches, giving each neighboring patch 0.1875. Since your starting patch is in a corner is is only able to spread out across 3 patches and only gives away 0.5625 (3*.1825).
This leaves the original patch with 2.4375.
Note you do get your expected result if you allow the world to wrap around.

How to ask turtles to do different actions according to ticks

I want to ask you if the time step of my model is per month , and I want to divide the actions , the first 6 months the turtle will do some thing and the second six months other action,
what I know if I want to ask the turtle to do every 6 months the same action
if ticks mod 6 = 0
Thanks in advance
If I'm understanding your question right, you can just do:
if ticks < 6 [
do-one-action
]
if ticks >= 6 [
do-other-action
]
Edit: Just saw your comments. If you want to alternate actions every 6 ticks, you could do:
if ticks mod 12 < 6 [
do-one-action
]
if ticks mod 12 >= 6 [
do-other-action
]
If ticks tells us the number of months that have passed in the simulation, then ticks mod 12 tell us which month in the current year it is (e.g. 0, 1, 2, ... 11). So if ticks mod 12 < 6 says "if we're in the first 6 months of the current year".