I am quite new to Netlogo. I try to create a model for the exchange of opinions in order to finds a suitable location for siting of a unpopular facility. The model contains of three breeds people with different opinions.
I imported a GIS raster layer with four different landuse classes (buildings, agriculture, forest, water). All breeds were randomly assigned to landuse class "buildings". The interaction takes place by randomly paired connections between two agents per tick. The default opinion value on how suitable a location might be should be based on landuse class in a certain distance. Distance should be divided in near (<= 20) , middle ( 21 -50) , and far (> 50 ). If a turtle is asked to give its opinion about a certain patch it should be automatically calculated in what distance to each other both are.
However, I do have quite some issue with finding a code that defines for each turtle what is of near, middle, or far distance to it. So far I had two main ideas, but the code I wrote did not provide a satisying results.
First attempt went this way:
calc-distance
ask turtles [
if (distancexy pxcor pycor) <= 20
[set location near]
if (distancexy pxcor pycor) > 20 and (distancexy pxcor pycor) <= 50
[set location middle]
if (distancexy pxcor pycor) > 50
[set location far]]
end
Second attempt went this way:
Turtels were located at a
Patches were located at b
Calculate automatically the distance between a and b
if ab <= 20
[set location near]
if ab > 20 and ab <= 50
[set location ...}
end
I would be please, if someone could provide any solution for this problem. Thanks in advance!
Jan
You're trying to pass a breed variable name as an argument. That's a syntax error.
When using distancexy the expected values are numbers. You can use loop to check all positions you want to check. However I think your turtles should have variables to store the opinion for every location.
calc-distance
ask turtles [
if (distancexy point1-pxcor point1-pycor) <= 20
[set point1-location "near"]
if (distancexy point1-pxcor point1-pycor) > 20 and (distancexy point1-pxcor point1-pycor) <= 50
[set point1-location "middle"]
if (distancexy point1-pxcor point1-pycor) > 50
[set point1-location "far"]
]
. . . continue with all other locations OR use loop.
end
Related
I am the novice netlogo user. Recently, I am trying to develop a model to simulate the air traffic flow under the terminal area. Now I am able to vector a single aircraft to land, and I want to constantly vector more aircraft to enter the terminal area. My idea is to define an area (named spawnpool) around the entry point as the picture shown. When the preceding aircraft left the area, I hatch a new aircraft to enter. If there is any aircraft in the "spawnpool", no more aircraft should be hatched.
But I am not sure how to fulfill this idea properly. As the picture shown after the first aircraft leave the area of "spawnpool". Tons of aircraft appears, I am not sure this mistake is due to insufficient proficiency or unsuitable idea for this purpose. Sincerely looking for some insights for this issue.
to setup-spawnpool
ask patches
[set spawnpool? (pxcor > -8.4 and pxcor < -5.4 and pycor < 5.8 and pycor >
3.8 ) ]
end
to new-aircraft-approaching0
ask aircrafts [
while [spawnpool? = false ]
[hatch-aircrafts 1
[ set color black
set size 0.7
setxy -7.4 4.8
route-check-02]]]
end
Okay, your problem is that you have many patches each with spawnpool? set to true. Your while statement is then asking each aircraft to check the patch it happens to be sitting on whether that patch has spawnpool set to true. So they continually hatch new aircraft. What you really want to do is create a new aircraft when none of the spawnpool? patches have an aircraft on them. This involves many changes:
sprout or create rather than hatch an aircraft so that it doesn't inherit its parent's attributes
you should not be using while because while operates continuously without advancing the clock
you need to ask all the patches whether there is an aircraft one them
I can't test this as I would need to create breeds etc to make the model run. But you want something like:
to setup
clear-all
setup-spawnpool
...
reset-ticks
end
to go
...
new-aircraft-approaching0
...
tick
end
to setup-spawnpool
ask patches
[set spawnpool? (pxcor > -8.4 and pxcor < -5.4 and pycor < 5.8 and pycor > 3.8 )
]
end
to new-aircraft-approaching0
let aircraft-source patches with [ spawnpool? ]
if not any? aircrafts-on aircraft-source
[ ask one-of aircraft-source
[ sprout-aircrafts 1
[ set color black
set size 0.7
setxy -7.4 4.8
route-check-02]
]
]
]
end
I am trying to use something similar to layout-spring to simulate tissue mechanics but I want to change the "spring-constant" of the links according to patch variables i.e. heterogeneous spring-constants for the links. Is there a way to do this using the layout-spring primitive? Unfortunately I do not know java which I believe is the source code.
I have also tried to begin coding layout-spring using the NetLogo language, but upon testing the code, the nodes are shooting off incorrectly. Below is the relevant code that I have tried.
links-own [fx fy k]
turtles-own [dxx dyy]
to setup
ask patches with [(pxcor <= (-20)) and ((abs pycor) < (max-pycor / 2))] [sprout 1]
ask turtles [create-links-to turtles-on neighbors4]
to deform ; propagate deformation of the material by connecting nodes with springs
ask driver [set heading 90 ;driver is an agentset
fd .01]
ask pinned [set xcor min-pxcor] ;prevents translation of the entire material ;pinned is an agentset
ask links [set fx (k * (link-length - 1) * sin link-heading) * -1
set fy (k * (link-length - 1) * cos link-heading) * -1]
ask not-pinned [set dxx sum [fx] of my-in-links
set dyy sum [fy] of my-in-links]
ask not-pinned [set xcor xcor + dxx
set ycor ycor + dyy]
end
This simple bit of code is not working. If I can get this working, perhaps I could then use the patch variables to change the spring-constant of the links (k). But if there is a way to use the layout-spring primitive to achieve this, then I would prefer such a method.
I am trying to build a butterfly movement model in which butterflies are attracted to patches of their host plant. This attraction is expressed as a probability which is stored in a variable called "attr-prob". If a butterfly is located within 25 m of a host-plant patch (pcolor = 9.9), it will move to the nearest host-plant patch with the probability of attr-prob.
I wrote the following code:
if (distance (min-one-of (patches with [pcolor = 9.9]) [distance myself]) ) <= 25
[if random-float 1 < attr-prob [move-to min-one-of (patches with [pcolor = 9.9]) [distance myself]]]
This code seems to be doing what I want it to do, however, when I add this part into my model, it slows it down immensely. Does anyone have any alternative suggestions for coding this that might be quicker?
I am running Netlogo in 64-bit Java.
Try something like this:
if random-float 1 < attr-prob [
let target-patch min-one-of (patches in-radius 25 with [pcolor = 9.9]) [distance myself]
if target-patch != nobody [
move-to target-patch
]
]
This should be faster for a couple reasons.
First, the fastest code is the code that never runs. So, doing the probability check at the very beginning allows you to skip computing the closest patch whenever you can.
Second, using in-radius upfront rather than checking the distance at the end reduces the number of patches that you're looking over. Basically, you'll only be checking the color and distance of the patches in the radius, rather than all of the patches in the world.
Finally, in your original code, you were finding the closest patch twice. Instead, you can store the patch in a local variable (target-patch in the code I provided) so you only have to find it once. This alone should double the speed of the code (depending on the value of attr-prob). It also increases readability.
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.
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.