use patches diffusion in netlogo - netlogo

(happy new year)
I am trying to use diffuse patch-variable in Netlogo. I wrote this little script in order to diffuse energy in all patches. Unfortunately, I don't really get what I expect. I want to diffuse randomly energy according to one rate in all the patches. I also know that scale-color is helpful for see the change as observer but I not sure that it works.
Thanks
patches-own [energy]
to setup
clear-all
ask patches [set energy 0]
reset-ticks
end
to go
diffuse energy random 0.3
recolor
tick
end
to recolor
ask patches [set pcolor scale-color blue energy 0 10]
end
I saw in Netlogo dictionary that we can also take in account the neighbors. It will be interesting to se both possibilities

your line
Diffuse energy random 0.3
always comes out 0 because random reports (returns) integers.
try
Diffuse energy random-float 0.3
That makes the patches diffuse a random rate on [0,0.3] every tick. All of them diffusing the same rate in any given tick. This is unusual but totally legal. Most often people set it to a constant on the interval (0,1)
Diffuse will not do any thing unless your patches have different values for the diffusing varible. Are you manually setting them? if not change your GO to
to go
ask one-of patches [set energy 10]
diffuse energy random 0.3
recolor
tick
end
You will see a better show.

Related

NetLogo: Creating Variable from Distance to Specific Patch

I have an issue in Netlogo giving agents the variable with distance (in patches) to a chosen patch that is water.
The agents are supposed to act according to their distance to water.
I want the agent to find the closest patch that has water = true, calculate the distance to that patch and save that specific distance in a variable that may change if the agent moves.
So the way I went about this is as follows:
turtles-own
[
next-river-patch
distance-to-river
]
to go
ask turtles
;find closest patch that has water = true
;calculate distance to that patch and save it in the turtles-own variable "distance-to-river"
set next-river-patch (min-one-of patches in-radius 100 with [water = true] [distance myself])
set distance-to-river distance next-river-patch
;check whether there is water in peception radius
;and whether directed walk is initiated
ifelse random-float 1 > water-directedness
and
patches with [water = true] in-radius 300 = true
[walk-directed]
[walk-undirected]
tick
end
to walk-directed
;deviation of random normal is smaller, the further from next 1st order river and is raised by a high pioneering rate
;with half the distance of the perception radius of 300 resulting in a deviation of 45 degrees and
;a pioneering rate of 0.5 leading to no influence onto the final deivation and a pioneering rate of 1 doubling the deviation.
let deviation 90 * ( 1 - distance-to-river / 300) * ( 2 * pioneering-along-rivers)
face min-one-of patches with [ water = true][distance myself]
rt random-normal 0 (1 * deviation)
fd 1
end
to walk-undirected
ask turtles [
rt random-normal 0 (2.25 * descision-randomness * 100)
fd 1
;it turns into a cumulated normal direction (with the chance of moving straight ahead being highest) and the standard
;deviation being defined by the descision-randomness-slider, with default being 45° and max being 225°
]
end
so the issue I am having is that the line
"set next-river-patch (min-one-of patches in-radius 100 with [water = true] [distance myself])"
returns a variable containing
the coordinates of the next patch that is water. Anyhow, i dont want the coordinates, but the distance to that patch so that i can use it in my cumulated direction.
set distance-to-river distance next-river-patch does not work.
Does anybody have an idea as to why that could be? I am relatively new to NetLogo so the answer may possibly seem very obvious.
Thank you very much!
P.S.: Another, possibly unrelated issue with my model is that agents turn dozens of times before each tick instead of each agent doing a single turn before going forward by 1.
So if you happen to see the solution to that by looking over the code, i would also be forever grateful.
Easiest problem first:
Why are some turtles moving more than once per tick?
walk-undirected is called by turtles, and it does ask turtles. So every turtle is making every turtle so the action. It should be like the other procedure and just do that action, no ask.
patches ...... = true
This is an error. You are comparing the agentset made by patches with ... to the boolean value true. An agentset can never be true OR false. An agentset can be empty, or have one or more agents in it. The any? reporter is used to determine if an agentset has any members.
The main question
It looks like you are already doing what you are asking. The line starting set nearest-river-patch gets the nearest water patch, and the line set distance-to-river calculates the distance to that patch. Not that if there is not any water in radius 100, next-river-patch will be nobody.
I don't see anything that would give you the coordinates (as a list?). Maybe try some parentheses to make sure the reporters are being evaluated in the right order:
set next-river-patch (min-one-of ((patches in-radius 100) with [water = true]) [distance myself])
You could also use with-min
set next-river-patch one-of ( ( ( patches in-radius 100 ) with [ water = true ] ) with-min [distance myself] )
Otherwise, correct the other errors and see what happens.

NetLogo: use value of 'stock' in SDM as input for ABM

I made two simple models; one System Dynamics Model and one Agent Based Model in NetLogo. The SDM has a stock 'tourists' and its value depends on the in- and outflow. The value is re-calculated each tick. The tourists are sprouted in the ABM each tick. Now, I would like to use the value of the touristsStock as an input for the turtles that are sprouted each tick in the Agent Based Model. What is the best way to do this? I have already checked the example codes in the Model Library (Like the Tabonuco Yagrumo model) but it doesn't make any sense to me. What is the best way to integrate these models with each other? Thanks in advance!
The relevant piece of code of the ABM is as given below:
to go
clear-turtles
;sprouts turtles only on patches with value beach AND patches that are close to the lagoon (calculated in ArcMap)
;the initial number of tourists is multiplied by the percentage of tourists that were satisfied in the previous tick.
;percentage of tourists that were not satisfied is subtracted from 100 and divided by 100 to make it a factor between 0-1.
ask n-of (initial-number-tourists * ((100 - percent-dissatisfied) / 100)) (patches with [ beach_close_to_lagoon = 1])
[
sprout 1 [
set color black
set size 10
]
]
ask turtles [
set neighbor min-one-of other turtles [distance myself] ;; choose my nearest tourist based on distance
set distance-tourist distance neighbor ; measure/calculate distance of closest neighboring tourist
]
ask turtles
[ ifelse distance-tourist > 5
[ set satisfied? True]
[ set satisfied? False ] ]
update-turtles ;before the end of each tick, the satisfaction of each turtle is updated
update-globals ;before the end of each tick, the percentage of satisfied tourists is updated in globals
;clear-turtles ;because each tick represents one day, the tourists will leave the beach after one tick so the turtles will die
tick
end

How to add a attribute only to a specific patch out of all patches?

I want a slider based input as an attribute in a specific patch only. For example, I am trying to model the horse [agent] selecting only good quality grass. As an input as patches, I have patches with grasses, crops, barren land etc. The main focus here is the grass patch. It already has a variable high (3), medium (2) and low-quality grass (1). So I wanted to add an attribute to the grass patch only (only grass, this is important) which can be controlled by a slider during the setup (setting up the environment) to experiment.
I tried this code but it does not work (and gives me a java heap error)
set fodder gis:load-dataset "Data/grass.asc"
gis:apply-raster fodder grass
ask patches [if grass = 3 [set pcolor green]
if grass = 2 [set pcolor yellow]
if grass = 1 [set pcolor red] ;; 1 is low quality
]
if any? patches with [grass <= 3] [ask patches [set modified-grass quality-of-grass]] ;; controlled by slider
I can think of a couple of possible problems here. First, I assume that grass is a patches-own variable. I've not worked with the gis extension, but I assume that it is setting the value of grass to 1, 2 or 3 for certain of the patches that are to be grass patches. The value of grass for all patches is initialized to zero, so non-grass patches will have grass equal to zero. Therefore, when you use the expression patches with [grass <= 3] you are referring not to just grass patches, but to all patches. (Of course you may have set grass to some value greater than 3 for non-grass patches, in which case this is not a problem.)
Second, you said that you wanted to set modified-grass (another patches-own variable?) to the slider value only for grass patches. But [ask patches [set modified-grass quality-of-grass]] sets the grass variable for all patches, not just grass patches.
So, I think that what you might really want here is
ask patches with [grass > 0 and grass <= 3] [set modified-grass quality-of-grass]
If there are no such patches, nothing will happen.
Now I don't know why either of these issues would lead to a java heap error. Are you sure that is not happening earlier in your code?

Smell Trail Netlogo

I have mouse and cats moving across the view, and I'd like to be able to make a smell trail to the mouses. The smell should fade away with time, simulating the intensity of the smell. If a cat enter on the smell trail, the cat must follow the smell trail to kill the mouse.
I will leave a part of my code, in case it helps:
...
mice-own [energy refX refY]
...
to setup
ca
setup-patches
setup-agents
reset-ticks
end
to setup-patches
ask patches[
let x 28
let y 48
if pycor mod 2 = 0
[set x 48 set y 28]
ifelse pxcor mod 2 = 0
[set pcolor x]
[set pcolor y]
]
end
to setup-agents
create-mice N-mice
[
set shape "mouse side"
set color 4
setxy random-pxcor random-pycor
set energy 50
set refX 25
set refY 25
....
to move-mice
ask mice
[
let x one-of neighbors
move-to x
set energy energy - 1
if energy <= 0 [die]
ifelse show-energy?
[set label energy set label-color black]
[set label ""]
]
end
Thanks for the help.
There is a primitive diffuse that does exactly that. Look it up in the truly amazing netlogo dictionary. There are several models in the model library that use it most famously the ant foraging model.
Pair that with the primitive downhill and your model will almost write itself.
you might want to consider adding some decay to the smell. if not, there's mouse smell everywhere!
additionally some minimal awareness threshold will possibly also get your cats to pursue other leisures than teasing mice.

In-Cone only works for patch centers Netlogo

I'm having some issues with the in-cone command in Netlogo. I am trying to identify the sum / mean of all the patch variables directly in front of my turtles current location (ie the sum of all the variables it crosses). However, this only appears to be working when my turtle is at the center of a patch (co-ordinates are integers not decimals), which also means I can only move my turtles at right angles. I'm yet to find any other questions pertaining to the same issue on Stackoverflow or elsewhere. So if anyone could offer some insight, I'd be greatly appreciative.
Below is the simple sample code. And I've annotated where making the changes causes this to not work.
Cheers
Paul
turtles-own [value]
patches-own [value-test]
to Set-Up
ca
reset-ticks
ask patches [if pycor > 150 [set value-test 1]]
ask patches [if pxcor > 150 [set value-test 1]]
ask patches [if value-test = 1 [set pcolor red]]
create-turtles 1
ask turtles[
;It works when the turtle is created at the origin (0 0), or at defined coordinates (but not random-xcor random-ycor)
;setxy random-xcor random-ycor
set value 0
set size 10
set color yellow]
end
to go
ask turtles[
;heading has to be 0, 90, 180, or 270.
set heading 270]
ask turtles[
let test mean [value-test] of patches in-cone 10 1
print test
print xcor
print ycor
ask patches in-cone 10 1 [set pcolor blue]
forward 10]
end
in-cone is not the right tool for the job. Unfortunately, NetLogo doesn't have a primitive that looks ahead in a straight line. It does, however, have patch-ahead, which reports a single patch at a given distance. We can use that to build something similar to what your looking for:
to-report patches-ahead [ dist step ]
report patch-set map patch-ahead n-values (dist / step) [ step + ? * step ]
end
This code may look puzzling at first, but what it does it actually quite simple:
It uses n-values to build a list of incrementing values: n-values (dist / step) [ step + ? * step ]. For example, if dist was 1 and step was 0.2, you'd get [0.2 0.4 0.6 0.8 1]. These values represent the distances at which we are going to be looking for a patch.
It uses map to call patch-ahead for each of values in the list and build a list of patches. Note that this list can contain duplicate patches, especially if step is small, since patch-ahead 0.1 and patch-ahead 0.2, for example, may very well be the same patch.
It uses patch-set to turn that list in a proper agentset of patches, without duplicates.
(You could achieve the same thing with a while loop and an incrementing counter, but the code would be longer, more error prone, and much less elegant.)
To use it, just replace instances of patches in-cone 10 1 in your code by something like patches-ahead 10 0.1.
You will notice that there is a trade-off between precision and speed: the smaller step is, the less likely it is to "skip" the corner of a patch, but the longer it will take to run. But I'm sure that you can find a value that works well for you.
Nicolas has a much better answer solving the problem of looking in a straight line but if you simply what look at the patch directly ahead use patch-ahead 1 it works at all angles and coordinates and is much faster than in-cone.
Completely an aside but probably the reason you found this bug is because your cone was set to 1 degree wide and 10 patches long. Long narrow cones tend to break up.