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.
Related
ask turtles
[ ask patches in-cone 3 60
[ set pcolor red ] ]
The code above means to have a cone of radius 3 and angle of 60 degrees. But I want a radius in range of 5 to 10. Is there a way to accomplish this?
What you can do is set up a variable that will hold the radius's that you have asked for. Let's call this variable vision. So,
let vision
set vision 5 + (random 5)
and then you can substitute vision instead of the 3 in your code.
ask turtles in-cone vision 60 [
set pcolor red
]
Here are a couple of ways you could do it. These are adapted into the Vision Cone Example model that comes in the models library with NetLogo, so we're using slightly different distances (between 9 and 30) to make the effect visually obvious. I'd recommend method1, but I'm including method2 as it's useful if you have some alternate logic to apply to the patches that are too close.
to method1
; use `distance` to filter those that are too close
; `myself` refers to the turtle doing the asking
ask patches in-cone 30 60 with [distance myself >= 9] [
set pcolor grey
]
end
to method2
; create a local variable to store the turtles that
; will be too close. `self` refers to the patch
; being asked
let too-close patches in-cone 9 60
ask patches in-cone 30 60 with [not member? self too-close] [
set pcolor gray
]
; I could use the `too-close` agentset here and ask them
; to do something, also.
end
The objective of my submodel is to simulate how wolf agents avoid patches that have human densities above the wolves' tolerance threshold. When running my model, the sprout command is not generating the number of human agents in the urban patch like I would expect it to. The code for creating humans in the urban patch is:
ask patches [ if self = urban-patches [sprout-humans initial-number-humans]]
Here is the image of my Interface Tab:NetLogo space
The grey is coded as my urban-patches, brown is grass-patches, and green is forest-patches. Why are my human agents not appearing in the grey (urban) patches with the number of agents reflecting the initial-number-humans?
Here is the code for creating the human agents:code
I have specified the xy coordinates of the human agent to be located within the urban (grey) patch, but only one human agent appears when I run the model. How do I correctly code the initial-number-humans to connect with the sprout command?
As I think you have now discovered, the easiest way to distribute a set number of turtles randomly across a group of patches is to use create-turtles rather than sprout. This is because sprout creates the specified number of turtles on each patch that is sprouting them so you need to juggle the total to be created and the number of patches. But that option is useful if you want to achieve even distribution rather than random locations. Here is code that does both.
globals [urban-patches n-humans]
to setup
clear-all
set urban-patches n-of 20 patches
ask urban-patches [set pcolor gray]
set n-humans 100
make-humans-sprout
make-humans-create
end
to make-humans-sprout
ask urban-patches
[ sprout n-humans / count urban-patches
[ set color red
set xcor xcor - 0.5 + random-float 1
set ycor ycor - 0.5 + random-float 1
]
]
end
to make-humans-create
create-turtles n-humans
[ set color blue
move-to one-of urban-patches
set xcor xcor - 0.5 + random-float 1
set ycor ycor - 0.5 + random-float 1
]
end
Note that the adjustments to xcor and ycor are because sprout and move-to always place the turtle in the centre of the patch and there is no primitive for placing in a random place on a particular patch.
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.
In the image below, the black box represents a patch. I wish to create agents of size-
(Patch-size/8)
and distribute them at the top of the patch as such.
Also, possible to to create a code such that it takes :
Patch-Size as input and distributes them accordingly.
Previous Code Used:
My previous approach sprout at the centre and moves them to align with the desired positions but it is considerably length and not effective if I wish to variate number of agents.
`
Ok, so, forget about patch-size for a moment, as we are going to be working with unit patches, without regard to the number of pixels.
I'm assuming the number of turtles is variable, and that we want the turtles to fit exactly inside the width of the patch, and with the tops of the turtles aligned with the top edge of the patch.
If the number of turtles is constant, or the maximum number is constant, and/or the size is constant, then the code can be a little more simple, as we can avoid recalculating some things.
Anyway, for C turtles:
The gap between the turtles is 1 / C
The "half-gap" is also useful, that's gap / 2
This is also the size of the turtles
Hence, the turtles will definitely just fit across the patch
The ycor of the turtles will be pxyor + .5 - half-gap
Since gap is the size, this puts the turtles along the top
The xcor will be pxcor - .5 + gap * N - half-gap
N is the number of the current turtle. So, the xcor varies from a half-gap from the left edge to a half-gap from the right edge.
Here, pxcor - .5 shifts the turtle center all the way to the left edge
Then, + gap * N shifts the turtle over N turtle widths,
Then, - half-gap shifts the turtle back one half turtle size.
This makes sure the first and last turtles are just touching the edge.
So, let's do it:
to align-inside-at-top ;; patch procedure
let counter count turtles-here ;; we will use this as a count-down, after using it in some calculations
if counter > 0 ;; could assume there are turtles, but we are not.
[ let gap 1 / counter ;; size of turtles, gap between turtles
let half-gap gap / 2 ;; half-size of turtles
let ytop pycor + .5 - half-gap
let xleft pxcor - .5 - half-gap
ask turtles-here
[ set size gap
set ycor ytop
set xcor xleft + gap * counter
set counter counter - 1 ;; so we're placing them from right to left
]
]
end
Hope this helps!
keep in mind no matter the patch-size patches are always 1 step across.
( pxcor - .5, pxcor + .5) X (pycor - .5, pycor +.5)
Patch-size is a sort of zoom it doesn't normally effect a simulation.
With that said and assuming you are doing this as some sort of demonstration or visualization. Anything you do for aesthetics tends to slow things down but here you go.
To line-up
Let c count turtles-here
Ask turtles-here
[
Set ycor pycor - .45
Set xcor pxcor + .45 - c / patch-size
Let c c - 1
]
End
If turtles on a patch > patch size it will mess up.