Reading two separate CSV files - netlogo

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"

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

is there a way to caa .nls file in 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.

netlogo tie-mode "fixed" fails to maintain link-length in graphs with medium-level degree

I am trying to create a network that moves through the environment as a "static" unit, i.e. nothing in the simulation changes except the position and orientation of the whole, the position and orientation of the individual turtles relative to one another are fixed by their links. Turtles are connected via undirected links, which are tied and set to tie-mode "fixed".
The problem is that in certain situations the links fail to remain fixed and link-lengths begin to change. Initially I noticed that, where the average network degree is relatively low or the network is a complete graph, the tie primitive works. However, when links were created to produce a graph that is moderately connected the link-lengths between the turtles begins to change. Upon further experimentation I can create a network with the same number of links and turtles but with different configurations i.e. the network structure is different, that sometimes maintain the positions and link-lengths but in other situations fail to do so.
How do I get the network to move as a unit no matter how connected the network is or what the configuration of the network is? See example code below, I have added code at the end where you can run multiple configurations of a network with 6 turtles and 6 links to see the issue for yourself, try running at least a half dozen iterations. Thanks!
this produces a network that moves as a unit
to setup
create-turtles 10
ask turtles [fd 2]
ask turtles [create-links-with other turtles [tie] ]
ask links [set tie-mode "fixed"]
reset-ticks
create-turtles 10
ask turtles [fd 2]
ask turtles [create-links-with other turtles [tie] ]
ask links [set tie-mode "fixed"]
reset-ticks
end
to go
ask turtles [lt 1 fd 1]
end
This produces a network whose links are still tied and set to tie-mode "fixed", but change their link-lengths. The more links that are asked to die, the more the link-lengths change.
to setup
clear-all
create-turtles 10
ask turtles [fd 2]
ask turtles [create-links-with other turtles [tie] ]
ask links [set tie-mode "fixed"]
ask one-of links [die]
reset-ticks
end
to go
ask turtles [lt 1 fd 1]
end
Here is additional code showing a specific instance of link-length change. Please input the seed 659269695 when prompted by the button "use-seed-from-user". Apologies if the code is clunky, first time using random-seed. "Print-lengths" button is to confirm that lengths change.
;USE seed: 659269695
to use-new-seed
let my-seed new-seed ;; generate a new seed
output-print word "Generated seed: " my-seed ;; print it out
random-seed my-seed ;; use the new seed
reset-ticks
end
;; Use a seed entered by the user
to use-seed-from-user
loop [
let my-seed user-input "Enter a random seed (an integer):"
carefully [ set my-seed read-from-string my-seed ] [ ]
ifelse is-number? my-seed and round my-seed = my-seed [
random-seed my-seed ;; use the new seed
output-print word "User-entered seed: " my-seed ;; print it out
reset-ticks
stop
] [
user-message "Please enter an integer."
]
]
end
to setup
clear-all
create-turtles 6
ask turtles [
fd 5
set shape "circle"
set size 1
set color yellow
if count links < 7 [ask one-of turtles [create-link-with one-of other turtles
[tie]]]]
reset-ticks
end
to go
ask turtles [lt 1 fd 1]
end
to print-lengths
print sort-by < [precision link-length 2] of links
end
I slightly revised your code so that the go procedure includes breaking a link. I also got rid of the explicit setting of tie-mode since that is done by setting the link to tie and added a tick so I could plot. So the code looks like this:
to setup
clear-all
create-turtles 10 [fd 2]
ask turtles [create-links-with other turtles [tie] ]
reset-ticks
end
to go
ask one-of links [die]
ask turtles [lt 1 fd 1]
tick
end
As far as I can see, the turtles move as a unit until it fragments with the loss of links.
I added a monitor for mean [link-length] of links, which is what I think you are asking about and also a plot of the same calculation. Yes, it is true that the average link length changes, but remember that the links are not all the same length. If a longer one dies, then the average length will reduce, and if a shorter one dies then the average will increase. The plot wanders a little, but it is basically flat until fragmentation.

How would I "target" patches within a certain area?

I have set up agents and nodes to represent people and stores and it is my intention that the agents will "target" the store in their "awareness" space with the highest value ("vulnerability"). I've largely coded what I have so far through trial and error however setting the turtle's target to the patch with the highest value within a 10 unit radius is a hurdle I can't get over. Currently they target the patch with the highest value regardless of its position in the world. Could somebody suggest what I might consider to achieve this please? I have pasted what I have written so far for reference.
Thanks.
breed [shoplifters a-shoplifter]
patches-own [vulnerability]
shoplifters-own [target
awareness]
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to setup-patches
setup-stores
end
to setup-stores
ask n-of num-stores patches [ set pcolor lime ] ;; create 'num-stores' randomly
ask patches [
if pcolor = lime
[ set vulnerability random 100
]
]
end
to setup-turtles
setup-shoplifters
setup-target
end
to setup-shoplifters
create-shoplifters num-shoplifters [ ;; create 'num-turtles' shoplifters randomly
set xcor random-xcor
set ycor random-ycor
set shape "person"
set color red
]
end
to setup-awareness
ask turtles [
set awareness
patches in-radius 10
]
end
to setup-target
ask turtles [
set target
max-one-of patches [vulnerability]
]
end
You are on the right track using max-one-of. At the moment, however, you are sending patches as the space to search through to look for the one with maximum vulnerability value, when you really want patches in-radius 10. So you could simply do this:
to setup-target
ask turtles [
set target max-one-of patches in-radius 10 [vulnerability]
]
end
However, this is going to be inefficient because NetLogo will have to first work out which are the patches within the radius. You have already asked the turtles to work this out and assign it to their variable 'awareness'. What you really want to do is therefore:
to setup-target
ask shoplifters [
set target max-one-of patches awareness [vulnerability]
]
end
Note that I also changed ask turtles to ask shoplifters. It is only shoplifters who have the attribute 'target' so you should only be asking them to calculate it. Same thing goes for 'awareness'. At the moment you don't have any other breeds so it's not causing an error, but it is good practice to use the breed, otherwise there is no point in creating it.

Changing Node ID with every Setup in Netlogo

We try to show a simple infection via Netlogo. For our purpose we need to start the infection with the same turtle for several times.
But right now with every setup another turtle begins with the infection. We already tried to work with the Node ID, but unfortunately the ID of the different turtles changes with every setup, too. We are out of ideas but
maybe there is a way to sove this problem I am happy for any answers :)
This is our Code so far:
extensions [nw]
globals
[
num-informed
informed-size
]
turtles-own
[
informed?
]
to setup
clear-all
nw:load-graphml "JK_nachnamen.graphml"
ask turtles [ set size 1.5 ]
layout-radial turtles links turtle 61
ask turtles [set color red]
ask turtles [set shape "dot"]
ask links [set color grey + 1.5]
ask patches [set pcolor white]
ask turtles [set label-color black]
ask turtles [set informed? false]
ask turtle 72
[
set informed? true
set color green
]
set num-informed 1
set informed-size 2
reset-ticks
nw:save-graphml "JKnachnamennetlogo.graphml"
end
to spread
if (count turtles with [informed? = true] > .7 * count turtles) [stop]
ask turtles with [ informed? = true ]
[
ask link-neighbors with [not informed?]
[
if (random-float 1 <= 0.01)
[
set informed? true
show-turtle
set color green
]
]
]
set num-informed count turtles with [informed? = true]
tick
end
Thank you a lot.
I am a little unclear so am giving bits of different answers for different situations.
If the turtles are different each time, what do you mean by 'the same turtle'. For example, do you mean the turtle in a particular position? If so, you could select the turtle on the appropriate patch.
If it doesn't matter which particular turtle it is (just that it's the same turtle), then the simplest approach is to set the random-seed. Then every time you run any random process (including choosing one-of the turtles to select the starting infection, or ask turtles to do something), NetLogo will use the same chain of random numbers. Of course, if you are still building your model, then adding new pieces of code that change how many calls are made to the random number generator will lead to a different chain, but rerunning with the same code will give the identical run.
You may need to use with-local-randomness and random-seed new-seed if you want to have some parts actually change.
The problem is that nw does not store the WHO variable this is to avoid conflict with already existing turtles in a model.
A work-around would be assigning each turtle a separate id variable and setting that to who.
turtles-own [informed? id]
in turtles creation asign them each the id thus
set id who
you may want to write a conversion procedure like this
to convert
nw:load-graphml "JK_nachnamen.graphml"
ask turtles [set id who]
nw:save-graphml file-name "JK_nachnamen(id).graphml"
end
and use the copy. Of course you would not use
turtle 74
but
one-of turtles with [id = 74]