How to change the go botton into a Loop botton on netlogo - netlogo

Change the ‘go’ button from looping infinitely (‘forever’) to running for 1000 ticks.
How do I do that?

One way, based on pure ticks:
to setup
ca
reset-ticks
end
to go
if ticks = 1000 [ stop ]
; Actions here
tick
end

Related

How to do equation in Slider Netlogo

Im trying to change the maximum of a slider based on a Chooser. Something like
if confidence = "None" [set Maximum 1]
This is on the part of the slider that indicates the Maximum. However, i get the Expected Reporter Error, but i dont know how to do this right.
The best solution, in my opinion, is writing a reporter in your code tab and using that reporter to determine the maximum of your slider:
to-report slidermaximum
if confidence = "none" [report 1]
end
Another option is using ifelse-value, which allows you to return a reporter rather than a command. This one you can put directly into the slider menu.
ifelse-value confidence = "none" [1] [10]
I prefer the first one, since code hidden within your different submenus is easy to forget or overlook when you are making changes to the model later on.

How to change light breed colour in Netlogo

I am currently trying to remodel the town and traffic crowd simulation found here to support self driving AI cars, which can be found here.
What I am currently stuck is is when the traffic lights where by the lights go from red straight to green, I want to add the yellow color into the simulation so the lights go from red, then to yellow then to green this is what i have but it just seems to freeze on yellow.
Please any help would be helpful, thanks.
I have below reformatted your control-traffic-lights code
to control-traffic-lights
if ticks mod (100) = 0
[ change-color lightsR ; red to yellow; others to red
change-color lightsL ; red to yellow; others to red
change-color lightsU ; red to yellow; others to red
change-color lightsD ; red to yellow; others to red
yellow-color lightsR ; yellow to green; others to yellow
yellow-color lightsL ; yellow to green; others to yellow
yellow-color lightsU ; yellow to green; others to yellow
yellow-color lightsD ; yellow to green; others to yellow
]
end
Imagine that you hit a relevant tick (every 100), start with the lightsR - any that are red get turned yellow on the first line, the get turned green on the fifth line. If they are any colour other than red on entry, they get turned red on the first line and yellow on the fifth line.
All four breeds of lights are going through the same logic. That is, the yellow-color procedure is being immediately applied to the lights based on the colour that they exit the change-color procedure.
UPDATE: I didn't make any changes to your code, I just formatted it so that it was readable to try to explain your logic issue.
If they enter as red, they get changed to green (via yellow)
If they enter as yellow, they exit as yellow (via red)
If they enter as green, they exit as yellow (via red)
What you need to do is change when the yellow-color procedure runs, so it's a few ticks later than the other colour change rather than in the same tick.
Try changing the second if ticks mod (100) = 0 [yellow-color l ...] to if ticks > 10 and ticks mod (105) = 0 [yellow-color l ...]

How to read a text file in netlogo with as many characters as the world has patches and give the imported information to the patches?

patches-own[ a ]
to startup
set a []
file-open "test.txt"
while [ not file-at-end? ] [
set a lput file-read a
]
file-close
end
this is how my code looks like (the important part for that problem).
I have a text file with as many characters as patches are exist:
[
1 2 3
4 5 6
7 8 9
]
I now want to give the information from the text-file to the patches of my world. so the first patch (upper left end) should get the information 1. It should be really easy but it does not work. The code is just my test, probably there is a way nicer way to do it. I hope this is specific enough. Would be great if you guys can help me, thanks a lot.
sort patches will create a list of patches from top left to bottom right (row-major order). Then, you can use foreach to iterate through the patches and file contents together:
(foreach (sort patches) a [ [p x] ->
ask p [
; do stuff with x
]
])

Dynamic turtle creation in netlogo

I am new to netlogo and was hoping if someone can help me with how to create turtles based on the user input.
In the interface tab i have a slider whose value ranges between 2 & 10. Depending on the value defined by the user using this slider, that many number of turtles should be created.
I tried using multiple if statements but there is a problem in the succeeding steps.
if (slider-value = 2) [create2]
if (slider-value = 3) [create3]
if (slider-value = 4) [create4]
if (slider-value = 5) [create5]
After creating the turtles using the above if conditions, i have to assign some rules to each individual turtle, and i tried again using multiple if statements. But it doesn't seem to work.
Can someone suggest a way, would really appreciate the help.
Thanks in advance!
Regards
You could more simply use the slider thus
create-turtles slider-value [
;things you want the turtles to do for example
set heading 4 * random 90
set shape "turtle"
set color green + random-normal 0 4
]
is this what you are looking for?
I recommend a switch statement.
A switch statement cycles through all your possible commands ,typically with an int. And then selects the match command.
So for example I could make a switch statement that when user inputs the up arrow. the int 1 is the input. this is matched to a command that tells the turtle to move up so many pixels/units/cubes.
I hope that helps.

Netlogo export-world format

I have been experimenting trying to work out how does the export-world work, in particular how does the DRAWING section work.
I created the following code to experiment using the default environment size of max-pxcor and max-pycor of 16. If you run this code shown at the end of this post, each file produced will be 5 megabytes, so it is easy to use up over a gigabyte of data after a minute of running.
Anyway, my question is this: How does the DRAWING section work? I have seen that the first entry is at -16.4, 16.4. I have summarised some of my observations in the simple table below. The first column is how much the turtle has moved, while the second column shows partial output in the CSV file.
0.001 FF1D9F78
0.016 FF1D9F78FF1D9F78
0.093 FF1D9F78FF1D9F78FF1D9F78
I have also seen that the first entry is created when the turtle moves by 0.001.The second entry seems to happen when the turtle has moved by 0.016 and the third entry is 0.093.
I am trying to work out what the pattern could be, but there doesn't seem to be one. How much turtle movement does one of the entries represent in the CSV file?
Thanks.
---- The code is below.
globals
[
totalAmount
]
to setup
ca
crt 1
[
setxy -16.4 16.4
pd
set heading 90
set color turquoise
]
set totalAmount 0
end
to go
ask turtles
[
fd moveAmount
]
set totalAmount moveAmount + totalAmount
export
end
to export
let filetemp word "turtletest" totalAmount
let filename word filetemp ".csv"
;print filename
export-world filename
end
The drawing layer is just a bitmap – a grid of pixels. It doesn't know what turtles moved and how far, it only knows what pixels the turtles colored in while moving. Internally, it's a java.awt.image.BufferedImage with TYPE_INT_ARGB encoding.
It's written to an exported world file by this code:
https://github.com/NetLogo/NetLogo/blob/533131ddb63da21ac35639e61d67601a3dae7aa2/src/main/org/nlogo/render/TrailDrawer.java#L217-L228
where colors is the array of ints backing the BufferedImage, and toHexString just writes bytes as hexadecimal digits (code).
If your image is mostly black, you'll mostly see a bunch of 00 bytes in the file.
As for your non-zero bytes, it appears to me that FF1D9F78 is a pixel with alpha = FF (opaque), red = 29, green = 159, blue = 120. At least, I think that's the right interpretation? Is that plausible for what you're seeing on screen? Maybe the A-R-G-B bytes are in the reverse order? To double check it, I'd need to do export-view and then look at the resulting PNG file in a program that could tell me the RGB values of individual pixels -- I don't have such a program handy right now. But hopefully this'll put you on the right track.