From GIS extension, I imported into Netlogo a raster that I represented in orange in figure below. My objective is to randomly place one turtle in each block of the raster represented in blue (namely in the 9 blocks). The blue raster is a portion of the orange raster.
Here is my code to randomly place turtles in blocks
let number 1
ask n-of number patches with [ (max-pxcor - ((x-increment + 1) * (max-pxcor / 3))) <= pxcor and pxcor <= (max-pxcor - (x-increment * (max-pxcor / 3))) and (max-pycor - ((y-increment + 1) * (max-pycor / 3))) <= pycor and pycor <= (max-pycor - (y-increment * (max-pycor / 3))) ] [
sprout 1 ]
From my orange raster in Netlogo, how can I apply the code above in only the blue raster ?
Here my rasters:
Thanks very much for your help.
How about this?
ask patches with [pcolor = blue] [
sprout 1
]
I suspect that doesn't answer your question. But that's because I don't understand your question. If you can tell me why the above code isn't what you're looking for, then perhaps I can improve it to be more helpful.
Note that it doesn't really make sense to talk about the "orange raster" and "blue raster". The GIS extension knows how to import rasters into the NetLogo world, but once they've been imported, they aren't rasters anymore. All that's left after the import finishes is turtles and patches. If you can use the vocabulary of turtles and patches to ask your question, rather than the vocabulary of "rasters", you'll have a better chance of formulating a clear, answerable question.
Related
I 'm new in Netlogo programming. I would like to make turtles with cloud shape and big size so if another turtle i.e. a person be at the same patch with the cloud to lose energy. The problem is that I can't have a turtle to be in more than one patches, netlogo "can see" that it's in only one patch.
Regardless of the size of an icon depicting a turtle, the turtle is located only at a single point (defined by the variables xcor and ycor). However, you can instead use distance to find if other turtles are close
As JenB said, the turtle only exists as a point, you'll have to come up with logic to make the clouds seem bigger than they are if you want them to be turtles.
Here is some code that demonstrates how to use size and in-radius to make the clouds breed affect the leaves breed color as they move past. It works best with shape = "circle" since then the radius of the cloud will match where the leaves are affected. You can add this code to a basic new NetLogo model to see it work:
breed [ clouds cloud ]
breed [ leaves leaf ]
to setup
clear-all
ask patches [
set pcolor blue + 2
]
create-clouds 10 [
set xcor random-xcor
set ycor random-ycor
set size 1 + random 4
set color white - 2
set shape "circle"
]
create-leaves 35 [
set xcor random-xcor
set ycor max-pycor
set shape "leaf"
set color green
set heading 180
]
end
to go
ask clouds [
ask leaves in-radius (size / 2) [
set color (color - 1)
]
]
ask leaves [
fd (1 + random 10) / 10
]
end
You can also reverse the logic a bit so it's the leaves that check if they are inside a cloud using distance. I find this option more confusing, but it might work better in your case:
to go-leaves
ask leaves [
if any? clouds with [distance myself < (size / 2)] [
set color (color - 1)
]
fd (1 + random 10) / 10
]
end
And finally, instead of using turtles to represent your large areas that turtles move through, you could use patches instead. It would simplify some things, but wouldn't work in every case.
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
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'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.