is there a way to caa .nls file in netlogo - netlogo

please I have a notepad file saved with .nls, how do I call it to my NetLogo ... anybody help me with the code
globals [
letter_E
];
for declaring global var
patches-own [number]
;patch var
to setup
clear-all
setup-patches
reset-ticks
end
ask patches
[
set plabel =
set-pcolor = black
set-patch-size = 10
]
to setup-letter_E
;set your cordinates
ask (patch-set patch 0 0)
[set pcolor 0]
ask (patch-set patch 0 1)
[set pcolor 1]
ask (patch-set patch 1 1)
[set pcolor 2]
ask (patch-set patch 1 2)
[set pcolor 3]
ask (patch-set patch 2 2)
[set pcolor 4]

You have two options. You can copy and paste the code from the .nls file to the Code tab of the NetLogo model, or you can use the __includes feature of NetLogo. If your .nls file is named (for instance) mycode.nls, you can put at the top of your NetLogo model code
__includes["mycode.nls"]
(There are two underscores in front of "includes".) If you check out __inlcudes in the NetLogo dictionary, you will see that you can include more than one .nls file. The files should be in the same directory as your model. When you do that, the NetLogo Code tab will have a drop-down list of "Included Files" and you will find "mycode.nls" there.
All that said, the code you have in the .nls file will not run as is. There are a number of errors. But that is a different question.

Related

Drawing over GIS data in Netlogo

I want a turtle to colour an area (in-radius 5), on top of the GIS data I have imported.
Please find attached the line of code I am using which is not working.
Is there any way how to do this?
Thanks!
extensions[gis]
globals [map-boundary]
to setup
ca
set map-boundary gis:load-dataset "/Users.shp"
create-turtles 50
ask turtles [setxy random-xcor random-ycor set size 1 set color grey]
gis:set-world-envelope (gis:envelope-union-of (gis:envelope-of map-boundary))
gis:import-wms-drawing "https://ows.terrestris.de/osm/service?" "EPSG:4326" "OSM-WMS" 1
reset-ticks
end
to go
ask turtle 1 [ask patches in-radius 5 [set pcolor blue]]
end
I'm not sure what you mean "not working", and lacking the datasets you have I can't reproduce the problem.
The Models Library has some GIS examples. What's not obvious is where Uri Walenski's "shared-dataset" files are that you need to run them models, but I found some version of them here:
https://ccl.northwestern.edu/netlogo/5.0/models/Code%20Examples/GIS/data/
and when I double-click countries.shp and countries.dbf they download and I can move them to my own new "shared-dataset" folder under the folder my model is in. That done, the model "create-turtles-inside-polygon" runs.
I removed most of and added a line to draw a blue region around turtle #1 and it seems to work. What does your attempt to draw the blue region do or not do that's different?
extensions [gis]
globals [dataset]
breed [citizens citizen]
citizens-own [cntry_name curr_type]
breed [manual-citizens manual-citizen]
manual-citizens-own [country-name currency-type]
to test-turtles
clear-all
set dataset gis:load-dataset "./shared-datasets/countries.shp"
gis:set-world-envelope gis:envelope-of dataset
gis:set-drawing-color red
gis:draw dataset 1
foreach gis:feature-list-of dataset [ country ->
gis:create-turtles-inside-polygon country turtles 1
;;clear-turtles
]
ask turtle 1 [ ask patches in-radius 5 [set pcolor blue]]
end

NetLogo: Having a turtle remember its starting location

I want to have my turtles move back and forth between a central area and their starting location. I have set the central area (patch 0 0, and its neighbouring patches). I have set these turtles to begin from random locations on setup.
Now I need them to move to the central area and be able to remember and return to their respective starting positions. Here is my attempt, but one that is not working.
ask patches
[ set target-patch patch 0 0
ask target-patch
[ set pcolor green
ask neighbors [set pcolor green]
set hold-time 5
]
]
create-turtles 10
[ set shape "car"
set size 1
set color white
setxy random-xcor random-ycor
if (patches != patches with [pcolor = green])
[ set start-position (random-xcor random-ycor)] ;; line with error
]
to go
ask turtles
[ set heading target-patch move-to target-patch
set hold-time hold-time + 5
]
ask turtles
[ if hold-time >= 10
[ set heading start-position move-to start-position]
]
end
There are several problems with your code. I strongly suggest that you code in smaller pieces. That is, add some code and make sure it works before writing the next piece. Making sure it works is not just making it through without error messages, it needs to do what you expect it to do.
On your specific question. The line if (patches != patches with [pcolor = green]) is causing an error. First, patches is the set of all patches, not just a particular patch. So you are (sort of) asking whether the set of all patches is not equal to the set of patches that are green. Is that really what you intended? If so, it is easier to simply ask whether there is any patch that is not green:
if any? patches with [pcolor != green]
or to check whether they are all green and continue if not:
if not all? patches [pcolor = green]
However, since you are asking about moving back and forth to and from the central green patches, I think you really want to have the turtle check whether the patch they happen to be located on is green. This code looks at the patch where the turtle is located (patch-here) and checks whether the color (pcolor) is green:
if [pcolor] of patch-here = green [ ]
However, one of the tricks of NetLogo is that turtles can access the variables of the patch they are on directly. Note that a patch cannot access a turtle's variables because there may be multiple turtles on the patch so the patch doesn't know which turtle you want. But a turtle can only ever be on one patch at once. So you could write:
if pcolor = green [ ]
You also need to rethink this code:
ask patches
[ set target-patch patch 0 0
ask target-patch
[ set pcolor green
ask neighbors [set pcolor green]
set hold-time 5
]
]
This suggests to me that you have misunderstood something very fundamental to NetLogo programming. You need to think from the perspective of an individual agent. Looking at this code, you first do ask turtles, so that is going to run through all the turtles in random order. Let's call them A, then B, then C and so on.
What is each turtle going to do? Everything in the [ ]. So, A sets the value of the global variable named "target-patch" to patch 0 0. Then A asks that patch to turn green, have the 8 surrounding patches to turn green, and to set the variable "hold-time" to the value 5.
So far, so good. But then turtle B does exactly the same thing - it assigns "target-patch", turns it and its neighbors green etc. Then turtle C. If you have 100 turtles, this block of code will run 100 times and do exactly the same thing each time.

Reading two separate CSV files

Currently I am struggling to get my netlogo to read two different ckv files to set my global variables. I got two files that needs to read every tick. See my code so far:
to setup
clear-all
file-close-all ;; Close any files open from last run
file-open "oil price.csv"
file-open "co2 price.csv"
setup-patches
create-industries 25
ask industries [set shape "house"]
ask industries [set color red]
ask industries [move-to one-of patches with [pcolor = green] ]
ask industries [set oil-demand 1 + random-float 9]
create-ports 1
ask ports [set shape "pentagon"]
ask ports [set color yellow]
ask ports [setxy 0 0]
reset-ticks
end
to go
if file-at-end? [ stop ]
set oil-price csv:from-file "oil price.csv"
set co2-price csv:from-file "co2 price.csv"
;; model update goes here
if ticks = length "oil price.csv" [stop]
tick
end
I managed to have it read one csv for oil-price and change its variable every tick. However, adding another does not work. I want to have a separate csv for oil price that changes each tick and the same for co2 price. I am missing something? I am new to netlogo by the way. All help appreciated! Thanks:)
My recollection is that the csv extension reads from the most recently opened file. So, if you put each file-open statement just before the respective command to read from the file, it should alternate between them.
file-open "oil price.csv"
set oil-price csv:from-file "oil price.csv"
file-open "co2 price.csv"
set co2-price csv:from-file "co2 price.csv"

How to have turtles in Netlogo compare Variables?

How can you have two turtles in NetLogo share and compare variables?
I need a series of turtles to compare their SN variables with their neighbors to see who has a larger value. Right not I'm testing with agent 0 sharing the SN variable with its linked neighbors. If the statement is true, it should set another variable (SocialST) to 1. I can not figure out the correct syntax and I can't seem to find a good example to work from.
Should be something like this...
to go
ask HHAgent 0 [
if [SN] of self > [SN] one-of link-neighbors [Set SocialST 1]
]
end
Are you just forgetting an of?
turtles-own [SN SocialST]
to setup
ca
crt 25 [set SN one-of [1 2 3] setxy random-xcor random-ycor]
ask turtles [create-links-with n-of 5 other turtles]
end
to go
let _aset n-of 5 turtles
ask _aset [
if (SN > [SN] of one-of link-neighbors) [ ;note the `of`
set SocialST 1
]
]
end
Or is there something additional you wish to accomplish? (Note that this code has the chosen turtles compare to only one of their link partners, randomly chosen.)

NetLogo: Apply set [variable] from sub-procedure to global process?

I want to simulate reproduction process of turtles in time when single patch can by used only once. If the patch is red and ticks mod 50 = 0 then turtles-here (on this patch) hatch new 10 turtles. Every patch can be used only once during the whole simulation run.
Please, how can I include this condition into my code? I tried simple to change patch color to green with hope that the next hatching process will run only with red ones. However next time step NetLogo doesn't keep this patch green but changes it back to red. Thus my reproduction run from the same patch.
Any suggestions will be highly appreciated
The part of my code:
to go
if ticks mod 50 = 0 [ask patches with [pcolor= red] [reproduce] ]
end
to reproduce
ask one-of turtles-here
[hatch 10 ;
die]
; set pcolor green - change infestlev from 2 to 5 only for specific tick, not for the rest of the simulation
end
The code you have should work fine. In your description, you state that the colour turns back to red - so that's why this code isn't working, somewhere else you have a colouring procedure. Alternatively, if you don't want to rely on colour (or if you want colours to mean something else), then you can add a variable to each patch to keep track of whether it has already reproduced.
patches-own [reproduced?]
to setup
...
ask patches [set reproduced? FALSE]
...
end
to go
if ticks mod 50 = 0 [ask patches with [not reproduced?] [reproduce] ]
end
to reproduce
ask one-of turtles-here
[ hatch 10
die ]
set reproduced? TRUE
end
Just as a general comment, it is a little odd to ask the patch to reproduce when what you are really trying to do is have the turtle on a patch reproduce. Logically you are saying that once one turtle on a patch has reproduced, then no other turtle on that patch can ever reproduce. If the reproduction is truly governed by the patch, it would be more usual to use sprout instead of hatch. That gets you code that looks like this:
to reproduce
sprout 10 [ any commands you want the new turtles to do ]
set reproduced? TRUE
end
My final working code with steps (available here: http://ulozto.cz/xRqtDDfV/timing-of-turtle-sprout-nlogo):
setup turtles
if turtle touch red patch, turn this patch blue
at the same time - tick 10 -> sprout from every blue patch 10 nwe turtles
every patch can be used only once during simulation run (turn red, assured by reproduced? variable)
enter code here
patches-own [reproduced?]
to setup
clear-all
setup-turtles
setup-patches
change-color
reset-ticks
end
to setup-patches
ask patches [set reproduced? FALSE]
ask patches [set pcolor green]
ask n-of 80 patches [set pcolor red] ; identify turles which could be a source for new turtles
end
to setup-turtles
crt 1
ask turtles [set color yellow]
end
to go
if ticks mod 10 = 0 [
ask patches with [(pcolor = blue) and not (reproduced?)]
[reproduce] ; set reproduction to every 10 ticks for all blue patches
]
move-turtles
change-color
tick
end
to move-turtles
ask turtles [fd 1]
end
to change-color ; if turtle touch red patch, red turns blue
ask turtles [if pcolor = red
[set pcolor blue]
]
end
to reproduce ; sprout 10 new turtles from blue patches at defined time step (multiply of 10 ticks)
sprout 10
set reproduced? TRUE
end