Netlogo Diffusion Confusion - netlogo

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.

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".

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!

MATLAB pcolor/surf bilinear interpolation (shading interp)

Consider the following MATLAB code:
C = [ 0 0 0 0 0
0 1 2 1 0
0 2 4 2 0
0 1 2 1 0
0 0 0 0 0 ];
pcolor( C );
shading interp;
axis square
Note that C is invariant under 90 degree rotations. Also note this sentence from the help for pcolor:
With shading interp, each cell is colored by bilinear interpolation of the colors at its four vertices, using all elements of C.
However, the plotted image is as follows:
Note that the image is not invariant under 90 degree rotations (consider e.g. the four corners). Now, unless I horribly misunderstand bilinear interpolation, this must be wrong. MATLAB seems to be interpolating on triangles, which is not the same as bilinear interpolation.
Is there any way of working round this MATLAB bug, and getting correct bilinear interpolation? (Other than manually interpolating additional points myself, which still would not cure the issue if one zoomed in far enough.)
I remember having read a few threads concerning this weird behavior in the official Matlab forums, in the past. Unfortunately, I found none right now with a quick search. Anyway... you aren't the first used pointing out that shading interp, used in combination with with pcolor, behaves in a weird manner, creating shapes that don't reflect the underlying data.
The main problem is that shading interp interpolates between data points without caring about how sensible your grid is to smoothing. If you want a result that doesn't look jagged, you have to provide data sampled at a higher resolution:
C = [
0 0 0 0 0
0 1 2 1 0
0 2 4 2 0
0 1 2 1 0
0 0 0 0 0
];
C = interp2(C,5,'cubic');
pcolor(C);
shading interp;
axis square;
This produces an amazing result, and the output doesn't show any artifact or asymmetry:

Simple Netlogo programming

In my netlogo program I have a ratio that fluctuates over time.
In the program time is set to weeks. There are 5200 weeks in each simulation.
If the ratio falls below 0.70 I would like to set a parameter R0 to zero for 520 weeks then go back to the baseline of 0.03.
Right now I have
ifelse ( ratio < T ) [ set R0 0 ] [set R0 0.03]
where T is set to 0.70, however, how do I set R0 to 0 for ten years and then back to 0.03?
Thank you,
With the code you have RO is always set to 0 or 0.03.
Assuming each tick is a month use a variable called timer
if timer > 0 [set timer timer - 1 if timer = 0 [set ratio 0.03]]
if (ratio < T)[set ratio 0 set timer 120]
it works as well for a patch, a turtle or a global

Finding Co-varying Regions in a 3D Matrix

I wonder if anyone has a suggestion on how to solve the following problem.
I have a matrix of size n*p*t. I would like to find areas in the plane n*p, that co-vary (in the t dimension). In other words, sub-regions on the plane (n*p) whose values vary together along the t dimension.
An example:
` t=1
[ 0 0 0
[ 0 1 1
[ 0 1 1
--->
t=2
[ 3 2 7
[ 0 2 2
[ 4 2 2 `
So the bottom corner would be assigned to a group since the values co-vary.
Do you have any ideas how to tackle such a problem?
Thanks!