NetLogo input slider problem, slider disappearing after reset - netlogo

I want the max value of my input slider to be equal to the number of turtles in my environment. I've done this by inserting "count turtles" in the maximum input box in the slider settings. However, the problem I am facing is that when I (significantly) decrease the number of turtles and setup/reset the slider sort of glitches, stays on the old value (which is larger than the new max value which causes the red knob to disappear) and cannot be changed anymore. A workaround could be to drag the slider completely to the left before resetting the model but this does seem somewhat silly imo. Does anyone know how to fix this? Thanks in advance.

In my minimal working example, I could reset the slider value to turtle count, e.g. during setup. I think you could use if slider_value > count turtles [set slider_value count turtles] also in the end of the go procedure, in case the number of turtles decreases in your model and you want to keep the slider value updated.
globals [
; n_turtles - given by slider
; slider_value - given by slider
]
to setup
clear-all
crt n_turtles
if slider_value > count turtles
[
set slider_value count turtles
]
end

Related

How to generate turtles at random time intervals and at random points in NetLogo

I am quite new to NetLogo, so I need some help figuring out a few things.
I have a requirement to generate turtles at random points and intervals when the go button is clicked. I have written the code mentioned below, but it generates turtles only when I click on the go button, and they are not moving properly. The go button I am using is a forever one, not the single-step button. Kindly help me where I am doing wrong.
to go
repeat num-students[
every t [
set t random 60
create-students 1 [
set xcor random -32 - 32
set ycor random -13 - -15]
ask students [
set size 2
set color blue
set heading ( 90)
forward random 10
]]]
end
I strongly recommend not using "every" -- it executes something every specified number of seconds in real time, which is very different from at random simulated times. You need code that decides at which ticks turtles are created. (I did not even know "every" existed until seeing this and have never seen it used.)
My first suggestion would be to indent your code, that makes it much easier to see which command fits into which block. You can do this by simply highlighting all of your code and clicking tab.
I am not very familiar with the every primitive but the following code works as intended and I can't seem to reproduce your problem of no extra turtles being made. Maybe it is just that your time interval is too big?
to setup
ca
set num-students 5
set t random 5
end
to go
every t [
set t random 5
create-students 1 [
set xcor random -32 - 32
set ycor random -13 - -15
]
ask students [
set size 2
set color blue
set heading ( 90)
forward random 10
]
if count students >= num-students [stop]
]
end
I removed the repeat num-students[] part since it didn't add anything to the code. It just makes you repeat the every command a few times inside each tick. Instead, I used if count students >= num-students [stop] in order to stop when the desired amount of students is reached.

Set value of speed slider programmatically

In setup, I draw a bunch of turtles--as small circles--to display two curves defined by functions. A very simple way to do this is
ask patches with [pycor = (myfunction pxcor)] [sprout 1 [...]]
and that's what my code does at present. It's kind of wasteful, since every patch has to be consulted--in random order--for each curve, but it's simple and easy to read, and it only happens during setup.
However, there's a little bit of a pause as the curves are constructed. If I move the speed slider all the way to the right, the pause is not noticeable. If I wrap the curve display routines in no-display and display, the user doesn't see the curves being constructed, but the speed is unchanged, AFAICS. If I move the slider to the left, it takes a long time to construct the curves even with no-display; the user doesn't see the points being placed one by one, but nevertheless has to wait while twiddling her/his thumbs.
Is there a way to set the model speed programmatically (for normal, "headfull" use)? I don't want to tell users "Move the speed slider to the right, then press setup, then move it back to the center before pressing go.
If not, maybe I'll code the curves properly using loops, but I thought I'd ask. Seems like there would be a way to do this, but I haven't found anything in the dictionary or programming docs so far.
(edit: no-display, if it did help, isn't available in NetLogo Web, which I am targetting along with regular NetLogo.)
I don't believe there is. However, you are asking all patches, when you could simply ask the pxcor values. This should speed it up a lot - square root of the number of iterations if a square world. Something like:
to testme
clear-all
let counter min-pxcor
while [counter <= max-pxcor]
[ let fn-out (function counter)
if fn-out >= min-pycor and fn-out <= max-pycor
[ ask patch counter fn-out [ set pcolor red]
]
set counter counter + 1
]
end
to-report function [#invalue]
report #invalue ^ 2
end

Query about hidden turtles

What actually happens to hidden turtle? I mean after we hide the turtle it continue to live in invisible mode occupying memory as I guess.
I hide few turtles but did not ask them to be shown back and when I inspected the hidden turtles continuing simulation their attribute were changing as per my commands. So, what exactly hiding a turtle sense for.
In one of my simulations, turtles represent people making decisions about whether to protect themselves during an epidemic. There are tens of thousands of these turtles, with potentially hundreds on some patches. The turtles don't move, but they each make their own decision based on personal characteristics like attitude and environmental perception such as how close the epidemic is.
Having these turtles visible would just clutter up the screen. Instead, I hide them and colour the patch based on what proportion have adopted protective behaviour. This is much more informative.
In my most recent simulation, though, I make the turtles size 0 instead of hiding them. This still makes them disappear, but I can still right-click on the world view to access the list of turtles where I have clicked.
Another reason to hide turtles is when you are simulating an infinite plane and turtles outside the view should simply be hidden.
Note that if you are moving turtles using setxy rather than forward you should test to make sure the patch you are about to move to exists since setxy throws a runtime error if it is given coordinates outside the world. From NetLogo documentation:
ifelse patch-at (new-x - xcor) (new-y - ycor) = nobody
[ hide-turtle ]
[
setxy new-x new-y
show-turtle
]

When using the forward command is the movement specified carried out after each tick?

I am trying to make turtles move along fixed paths that the user can draw in the u.i. The forward command can make turtles move a certain fraction of a patch forward per tick I assume, however to instigate smooth movement would it be possible to specify a fixed movement per tick in the setup commands for turtles? If this is possible what would be the basic structuring of the code I would use to achieve this?
The fd command (bk as well) accept floating-point inputs. I.e.
Ask turtles [ fd .01 ]
Makes each turtle move forward 1/100th of a patch. This movement happens at the time of the command.
Tick does not have any connection to when commands are carried out. If you set view updates to on ticks it can effect when you see updates otherwise it is usually a scheme for keeping track of how many times go has run.
A sample model of turtles moving at different speeds.
Turtles-own [speed]
To setup
Crt 100[
Set speed random-float 1
]
End
To go
Ask turtles[ rt 1 fd speed]
End
Copy and paste that into a new model make setup and go buttons. Mess with it for a while.

Turtle that has no effect on other turtles implementation but speeds up the reaction

I am using an existing model in netlogo called Chemical Equilibrium and am adding some more code. I want to add turtles (catalyst) which have no effect on the reaction/other turtles but speeds up the FORWARD reaction, which has been defined as follows:
to react-forward [t]
ask t [ set color red ]
set color green
rt random-float 360
jump 2
end
I was thinking that I should put a switch and a slider, make the turtles into whitemols or I do a turtles-own [catalyst] and then define that like I have done with temperature and pressure. I tried the following but it didnt work.
turtles-own [speed catalyst]
crt whitemols
[ set color white
randomize
set speed 1
]
I know the above code is incorrect but am not sure how to code this particular feature.
There are many ways to do this, of course. I can't tell what is going on in your program from the little snipped you include.
One way would be to have the catalyst be of a different breed:
breed [catalysts catalyst]
breed [chemical-x chemical-x]
;and so on
;then the forward reaction is sped up by the existence of catalysts
to react-forward
let num-catalysts count catalysts
;speed up by num-catalysts
;...
end