Netlogo reading from file - netlogo

I am trying to read these values from a file,
[-1 -5 270]
[-2 -5 270]
[-3 -5 270]
[-4 -5 270]
[-5 -5 270]
I need to get the first, second and third value from each line.

Open the file, Read it in like any other file. Then, use the file-read to read the line in. The format you provide seems to be in the same format as a list, so you can just interpret the line directly. As a side note, if not, you could do something like read-line to get the line as a string and parse it however you want.
Then use the list extraction operator to get items from the relevant indices.
file-open “filename.txt”
while [not file-at-end?]
[
let line file-read
show item 0 line
show item 1 line
show item 2 line
]
file-close

Related

Is it possible to change patch color for a range of patches?

Is it possible to change a range of patches color in the code tab? Instead of setting each individual coordinate. This would be done as part of initial model setup. I am essentially trying to "draw" a map for a battle simulation with patch colors. Below is the code I have but its obviously slow going. If i could use a range of coordinates it would be way faster
ask patches at-points[[-16 -16] [-15 -16] [-14 -16] [-13 -16] [-12 -16] [-11 -16] [-10 -16] [-9 -16] [-8 -16] [-7 -16] [-6 -16]] [ set pcolor brown ]
You can definitely address patches within a range of coordinates values, by using with and remembering that pxcor and pycor are nothing more than patch variables for which each patch holds specific values:
to setup
clear-all
ask patches with [pycor = -16 AND pxcor < -5] [
set pcolor brown
]
end
The code above does the exact same thing as your example.
If instead you have a situation in which there is no particular pattern to follow, but you still want some very specific patches to change color, I am afraid that you will have to hard code it. Doing it with at-points is a way to do it, and I don't think it is particularly inefficient.
Maybe creating an agentset of patches (i.e. a patch-set) and then asking them to change color would be faster? I don't know, you can find it out using the Profiler extension if you want.
If instead for some reason you don't want to have those lines of code in your model, an option would be to create your initial environment once and then use export-world to export it as a file that can be read at later moments using import-world.

Netlogo code adding people on the same patch

I'm actively trying to reprogram the sample model of Traffic 2 lanes but with my own addition, I have added what looks like a foothpath with people at the bottom, but when I run the code it adds the 4 people required sometimes on the same patch. How do I fix this?
to make-people
create-people 4 [setup-turtles]
end
to setup-turtles ;;Turtle Procedure
set shape "person"
let y-coordinates (list -8 -7 -6 -5)
let remove-index random length y-coordinates
set ycor item remove-index y-coordinates
set y-coordinates remove-item remove-index y-coordinates
set xcor 19
end
The rest of the code is the same as the sample model in Netlogo under social science called Traffic 2 Lanes, with a breed of people he only other difference.
The problem is that each person is defining again the y-coordinates list for their own creation. The list does not carry over from the creation of one person to the next, so one person's removing one of the items from the list will not have any affect on the list that the next person defines anew when they are created. The easiest way around this is to define y-coordinates as a global variable so that each person will work on the same list. Thus when one person eliminates a coordinate, the next person will get that shortened list. Try
breed [people person]
globals [y-coordinates]
to make-people
set y-coordinates (list -8 -7 -6 -5)
create-people 4 [setup-turtles]
end
to setup-turtles ;;Turtle Procedure
set shape "person"
let remove-index random length y-coordinates
set ycor item remove-index y-coordinates
set y-coordinates remove-item remove-index y-coordinates
set xcor 19
show y-coordinates
end
The show statement will show you that the y-coordinates list is indeed being shortened by each new turtle.

How to plot a distribution in netlogo?

I have a NetLogo model. each turtle has two attributes, "closeness" and "deviation_from_oracle". Now let's say there are 1000 agents in the model. The question is, how can I plot the "closeness" against "deviation_from_oracle" ?
It would also be helpful if I can get a csv file from NetLogo that has the value of closeness and deviaiton_from_oracle of all turtles after for example 1500 steps.
I definitely agree with Hugh_Kelley regarding using Behaviorspace to output your values (or custom export functions that might make for easier data cleanup if you're looking to report values for a large dynamic number of turtles- depends on your comfort with your statistical software of choice).
If you do need to plot something on the interface to show your users or something, you may find the plotxy function does what you need. For example, you'll need a plot on the interface called "plot 1" and a single blank pen in that plot called "pen-0".
You can control that plot either by manually setting up its x and y extent or by using the set-plot-... commands as in this setup:
to setup
ca
crt 10
set-current-plot "plot 1"
set-current-plot-pen "pen-0"
set-plot-pen-mode 2
set-plot-x-range 0 17
set-plot-y-range 0 25
reset-ticks
end
If you need to have a value plotted for each of your turtles, you can get the turtles to call plotxy for whatever values you're looking to plot- here I just use their absolute x coordinate and distance to the center as an example:
to go
ask turtles [
rt random 61 - 30
fd 1
set-plot-pen-color color
plotxy ( abs xcor ) distance patch 0 0
]
tick
end
This gives output like:
Where each point was plotted by an individual turtle.
If you want instead some reported mean value, have the observer call plotxy instead- another example that plots the average distance to other turtles and the average distance to center:
to go
plotxy mean-closeness-to-others mean-distance-center
ask turtles [
rt random 61 - 30
fd 1
]
tick
end
to-report mean-closeness-to-others
report mean [ mean map distance sort other turtles ] of turtles
end
to-report mean-distance-center
report mean [ distancexy 0 0 ] of turtles
end
For an output like:

Netlogo how to add a legend?

I started to use netlogo and I would like to add a legend to the model.
I didn't found any way to do it.
Is there some standard way to do it?
I thought about adding image to the interface but I didn't find any way to do it.
For example I want that user will know is the green particle means and what the read means
You can use the bitmap extension.
Here is a result example:
the code:
extensions [bitmap]
....
let legend bitmap:import "symbology.png"
bitmap:copy-to-drawing legend 40 275
"symbology.png" is the name of the graphics file, and 40 275 are the location coordinates. You will have to test several image sizes and coordinates until it suits your needs.
I don't know a great way to do this, the closest I've ever been able to get is to use a combination of specific plot set-ups and note widgets. As an example, start out with a plot called "Legend" with this setup to start:
Now size it to look something like this:
Now make a procedure to 'draw' legend guides (some detail in comments):
to setup-legend-plot
; Choose correct plot
set-current-plot "Legend"
clear-plot
; Define starting y and color
let starts [ [ 10 green ] [ 7 red ] [ 4 blue ] ]
; for each value in starts
foreach starts [ start ->
; make a range of values starting at the initial
; y value from 'starts'
let s first start
let f s - 2.5
let ran ( range s f -0.01 )
create-temporary-plot-pen "temp"
set-plot-pen-color last start
; draw lines at each y value to make it
; look like a solid drawing
foreach ran [ y ->
plot-pen-up
plotxy 1 y
plot-pen-down
plotxy 2 y
]
]
end
Calling setup-legend-plot should now make your "Legend" plot look like
Now you can make some note widgets and layer them over the blank space in the plot:
Not exactly straightforward, but definitely the closest I've found.
Edit:
"Closest I've found for building a legend within NetLogo." Javier's answer is a far better approach if you can swing it.

How to put a turtle's position in a table?

I'm doing a school project in Netlogo for the first time.
I need to use a table that saves the turtle's position as key and a value. The especific turtle has a breed (pacman). I've tried:
set tbl table:make
set xx [xcor] of pacman 10
set yy [ycor] of pacman 10
table:put tbl [xx yy] 1
//(the value 1 is not important)
Why doesn't it work this way?
If I make:
table:put tbl [-2 -5] 1 //(-5 and -2 are the xcor e ycor of pacman 10)
it works. But the pacman changes its position so I need to save each position in the table.
Thank you
http://ccl.northwestern.edu/netlogo/docs/faq.html#listexpectedconstant says:
If a list contains only constants, you can write it down just by putting square brackets around it, like [1 2 3].
If you want your list to contain items that may vary at runtime, the list cannot be written down directly. Instead, you build it using the list primitive.