I am attempting to assign values to patches in NetLogo based upon raster values: 0, 1, and 2. These patches need only relate to the values of my raster, which does display properly using a greyscale, and then 'paint' themselves the colors blue, green, and white, respectively.
This raster data loads fine using the gis extension. Following gis:load-dataset, I attempt to use the apply-raster command and ifelse in order to give options based on the values. I believe I am misusing a boolean operator but very few examples online are as extensive as what I am attempting.
patches-own [value]
; Draws raster dataset (terrain of each Millenium)
to display-terrain
gis:paint terrain 62
ask patches [
(ifelse
value = 0 [
set pcolor blue
]
value = 1 [
set pcolor green
]
; elsecommands
[
set pcolor white
])
]
end
I currently cannot tell if the values are properly assigned and keep receiving the error that 'ifelse expects this to be a command block' so I assume the formatting is incorrect and/or a value association is missing.
Actually, you are using it exactly as the documentation says to use it, but you probably don't have the current version. The multiple choice ifelse is brand new in NetLogo v6.0.4. You need to explicitly include the cf extension and you need the extension name when calling the new ifelse syntax.
Earlier versions of NetLogo won't do this at all. The syntax you have is for v6.1 which has been released only in the last couple of weeks.
Try this for v6.0.4:
extensions [cf]
patches-own [value]
to testme
clear-all
ask patches [ set value one-of [0 1 2] ]
ask patches [
(cf:ifelse
value = 0 [
set pcolor blue
]
value = 1 [
set pcolor green
]
; elsecommands
[
set pcolor white
])
]
end
Related
This one is long so please bear with me. I have Two rasters in my world, one of a department and one of a very important lake (it was a shape file that I rasterized). I load both rasters and use them to set the variable lake (tota) and the elevation. Then Im trying to set the distance between non lake patches and lake patches but Im getting an error "DISTANCE expected input to be an agent but got NOBODY instead.
error while patch 571 969 running DISTANCE"
This error doesn't make sense to my as the variable im checking is only for those who have tota (water) I know the code will be long and i tried my best to simplify but it is still a bit cluttered
extensions [ gis ]
globals [ world lake show-lake water]
patches-own[
distance-water
tota
]
to draw-tota
let min-elevation gis:minimum-of world
let max-elevation gis:maximum-of world
ask patches [
set elevation gis:raster-sample worldself
if (elevation <= 0) or (elevation >= 0) [
set pcolor scale-color green elevation 2900 max-elevation
]
]
end
to draw-lake
ask patches [
set water gis:raster-sample lake self
if (water >= 0) [
set pcolor blue
set tota 1
]
]
end
to setup-map
set world gis:load-dataset "D:/Geografico/DEM_Lago_Tota.asc"
gis:set-transformation [-72.994844704 -72.835891153 5.418413284 5.648012857666661] [0 1000 0 1000]
set lake gis:load-dataset "D:/Geografico/Lago_Tota_Raster.asc"
draw-tota
draw-lake
end
;;set up conditions
to set-houses
ask patches [
set dano_suelo (1 + random 5) ;;
set distance-lake distance min-one-of patches with [tota = 1] [distance myself]
]; This is the line that is giving me trouble!
set-default-shape turtles "person"
create-turtles 100
end
to setup
ca
resize-world 0 1000 0 1000
;set-lago
; set-predios
set-houses
reset-ticks
end
It is a lot, and unfortunately i dont know how to show you a picture. Any guidance will be appreciated as Im really lost. Thanks in advance.
Edit: Now the names are in English, Tota is the name of the lake in question. Thanks Matteo for the tip.
What I know about your problem indicates that there is not always a patch that fulfills the requirements of min-one-of patches with [tota = 1] [distance myself]. You could try running the code with an added if statement to check for that.
ifelse any? patches with [tota = 1] [
set distancia_agua distance min-one-of patches with [tota = 1] [distance myself]
] [
set distancia_agua "na"
]
As an edit based on your comment:
In order to reduce the number of operations I introduced let tota-edge <...>. This means that you check only once which patches qualify as tota rather than doing it 1002001 times (since every single patch would do this same operation). So 1002001 times instead of 1.004006e+12.
I don't know how big tota itself is but if it is a sizable portion of the world, ask patches with [tota != 1] will speed up your program some more. Finally, I only used the edges of tota since the middle of the lake is never the closest part of the lake to a non-lake patch.
ifelse any? patches with [tota = 1] [
let tota-edge patches with [tota = 1 and any? neighbors with [tota != 1]]
ask patches with [tota != 1] [ set distancia_agua distance min-one-of tota-edge [distance myself] ]
ask patches with [tota = 1] [ set distancia_agua 0 ]
] [
ask patches [ set distancia_agua "na" ]
]
I have a NetLogo model and I would like to assign values to patches from a .csv file. The patches have a certain value that I want to use as an "ID" and if "item 0" in a line of that .csv file matches the ID, the rest of the values in the line would be assigned to the patches with that ID.
I made a workaround that creates invisible turtles for each line in the files and then assigning the values is no problem, but is there a way to do this directly using just the csv file?
EDIT: Here's what the workaround does, first opens a file and then creates "helper" turtles like this:
while [ not file-at-end? ] [
let data csv:from-row file-read-line
create-turtles 1 [
set xcor 0
set ycor 0
set size 0
set color 0
set HPJid item 0 data
set A item 1 data
set B item 2 data
set iC item 3 data ] ]
Then I can just use:
ask patches [ let helper one-of turtles with [HPJid = HPJ of myself]
set D [A] of helper
set E [B] of helper
set F [C] of helper ]
Then all works, but I'd like a way to do this without the turtles.
Yes, having looked at your code, I think you can simply do:
while [ not file-at-end? ]
[ let data csv:from-row file-read-line
let in-ID item 0 data
ask one-of patches with [ID = in-ID]
[ set var1 item 1 data
set var2 item 2 data
]
]
or something like that anyway - with appropriate variable names of course
I'm new to NetLogo so probably it's a dumb question.
while I'm trying to export turtles, patches, global variables to separated CSV files, this one works:
csv:to-file "turtles.csv" [ (list xcor ycor color shape) ] of turtles
but the following two don't:
csv:to-file "patches.csv" [ (list xcor ycor cluster-number) ] of patches
error: this code can't be run by a patch, only a turtle
csv:to-file "statistics.csv" (list meet meet-agg meetown meetown-agg meetother meetother-agg coopown coopown-agg coopother coopother-agg defown defown-agg defother defother-agg)
error: Extension exception: Expected a list of lists, but 1016 was one of the elements.
could someone help me with it? Thanks in advance!
About:
csv:to-file "patches.csv" [ (list xcor ycor cluster-number) ] of patches
I'm guessing the only problem is that you're trying to use xcor and ycor, which are turtle variables, instead of pxcor and pycor, which are patches variables. You need:
csv:to-file "patches.csv" [ (list pxcor pycor cluster-number) ] of patches
As for this one:
csv:to-file "statistics.csv" (list meet meet-agg meetown meetown-agg meetother meetother-agg coopown coopown-agg coopother coopother-agg defown defown-agg defother defother-agg)
one thing to keep in mind is that csv:to-file expects a "list of lists", and your code produces a single list, which is likely the problem. If all those variables are global variables, and you just want them in a single row in your CSV file, you could just wrap your list in another list:
csv:to-file "statistics.csv" (list (list meet meet-agg meetown meetown-agg meetother meetother-agg coopown coopown-agg coopother coopother-agg defown defown-agg defother defother-agg))
But it's hard to diagnose your problem precisely without more information. Can you tell us what makes you think it's not working? What are you trying to do?
I want turtles to read and adopt data from csv file. I have written the following code: the problem is even-though the data gets loaded, i'm unable to make the individual turtles take on each of the income values. Any assistance to this effect would be appreciated
extensions [csv]
breed [households household]
households-own [income]
globals [income-data]
to setup
load-income-data
setup-households
end
to load-income-data
set income-data []
file-open "income.csv"
while [ not file-at-end? ]
[ set income-data sentence income-data ( file-read-line)
]
user-message "income data loading complete!"
file-close
end
to setup-households
create-households 700
ask one-of households
[ setxy random-xcor random-ycor
set income income-data
]
end
Have a look at the File Input Example in the NetLogo Model Library (Code Examples). You need to use a foreach to loop through the imported values / agents.
I have animals walk around and a line then connects all the locations that they walked to. The line forms a closed polygon. I also used the graphics extension to fill the polygon for visual purposes. But I don't know how to have all of the patches that fall within the polygon become the territory of the owner (i.e., the animal that formed the polygon). It is possible for patches to be owned by multiple animals. The code below illustrates overlapping polygons. I'd really appreciate any help on this. Thanks!
extensions [graphics]
breed [animals animal]
breed [homeranges homerange]
animals-own
[
Name
X
Y
]
patches-own
[
owner
]
homeranges-own
[
Name
]
to setup
clear-all
random-seed 4
ask patches
[
set owner nobody
set pcolor grey
]
let $colors [brown orange violet sky lime]
let $Name ["t6" "t7" "t8" "t9" "t10"]
ask n-of 5 patches with [(pycor < 10 and pycor > -10) and (pxcor < 10 and pxcor > -10)]
[
sprout-animals 1
[
set shape "circle"
set color item who $colors
set pcolor color
set X (list xcor)
set Y (list ycor)
set Name item who $Name
set owner self
]
]
graphics:initialize min-pxcor max-pycor patch-size
reset-ticks
end
to go
repeat 5
[
ask animals
[
rt 45
fd 2
set X lput pxcor X
set Y lput pycor Y
set pcolor [color] of self
]
]
ask animals
[
pen-up
let tempXY (map [list ?1 ?2] X Y)
graphics:fill-polygon tempXY
; create a turtle, which draws the homerange boundary
hatch-homeranges 1
[
hide-turtle
set Name [Name] of myself
set color [color] of myself
]
; draw the homerange boundary
foreach tempXY
[
ask homeranges with [Name = [Name] of myself]
[
move-to patch (item 0 ?) (item 1 ?)
pen-down
]
]
; connect the last point of the homerange with the first one, to close the polygon
ask homeranges with [Name = [Name] of myself]
[
let lastpoint first tempXY
move-to patch (item 0 lastpoint) (item 1 lastpoint)
]
]
end
If you look at http://netlogo-users.18673.x6.nabble.com/Netlogo-Point-in-Polygon-td4980030.html you'll find a past (2012) discussion of solutions to this problem.
At the end of the thread, Jim Lyons posts a link to a model on the Modeling Commons, but the model doesn't seem to exist there anymore. He's here on Stack Overflow if you want to ask him about it.
An answer was provided in this post: NetLogo - misalignment with imported GIS shapefiles. The polygons are exported as a GIS shapefile and imported back in using the GIS extension. Then the gis:intersecting was used to give a variable to those patches that fall within the GIS-imported polygons.