Netlogo - Setting turtles energy with a slider - netlogo

I know this is some simple thing but i cant seem to get it right and i cant seem to find this answer anywhere.
I have a 2 armies each have "turtles-own[energia base]" . I connected a slider with the global variable "energia" and it said the global variable already exists. So i changed it to nenergia and the notice went away. How do i make the slider value go to energia ?

When you make a slider, it automatically includes an associated global variable of the same name. You don't need to separately declare a variable.
It isn't clear from your question what your intent in writing turtles-own [energia] was. If you use turtles-own, it isn't a global variable, it's a turtle variable, so each turtle has its own value for it. It's not like a slider, which has only a single value which is globally visible.
Do you mean for the slider to be the initial energy value for all turtles, after which each turtle's value can vary as the simulation progresses? If so, then make a slider named something like initial-energy, and in the Code tab do something like:
turtles-own [energy]
to setup
...
create-turtles 100 [ set energy initial-energy ]
...
end
You'll see this pattern used frequently throughout the NetLogo Models Library.

Related

NetLogo input slider problem, slider disappearing after reset

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

In a Netlogo network, how can turtles "see" properties of other turtles?

I am trying to build a model in which turtles decide to change colour depending on their environment in a network.
The approach would be to "check" the colour of the surrounding turtles and then set an if statement for the turtle in question to switch colour (there will only be 2 colours).
Specifically I would like to know how can a turtle "see" or check other turtles' colour (or other properties).
If possible I would also like to create a slider for "how many links away" can turtles see their neighbouring turtles' (or neighbours of neighbours, etc) colour.
I am new to both Netlogo and Stackoverflow, so please let me know if I should make any modifications to my model and/or question.
Thanks!
Welcome to Stack Overflow! Typically you'll want to stick to a single question per post, both for simplicity and for the benefit of future users with similar questions. As well, in cases where its applicable you should try to include some code to show what you've tried so far, as well as any setup necessary- you want to make a minimal, complete, and verifiable example. In this case, I think you're okay since your questions are clear and well explained, but if you have more complex questions in the future you will be more likely to get useful answers by following those guidelines.
For your first question, it looks like you want the of primitive- check out the dictionary entry for details. of can be used in a few ways, including allowing agents to check the value of a variable (such as color) of another agent. Check out this example code:
to setup
ca
reset-ticks
crt 10 [
setxy random 30 - 15 random 30 - 15
create-link-with one-of other turtles
]
end
to go
ask turtles [
set color [color] of one-of link-neighbors
]
end
Every time the go procedure is called, one of the turtles changes its color to the color of one of its link-neighbors. If you run it long enough, all connected turtles should end up with the same color.
For your second question, I suggest you check out the Nw extension, which is an extension built to deal more easily with Netlogo networks. Specifically, have a look at nw:turtles-in-radius, which should work with your slider approach. To get it working, include the extension using
extensions [ nw ]
at the start of your code. Then, assuming the same setup as above, you can play around with something like
to network-radius
ask one-of turtles [
set color red
ask other nw:turtles-in-radius 2 [
set color white
]
]
end
When you call the network-radius procedure above, you should see one turtle turn red, and any turtles within 2 links of that turtle turn white. To switch to a slider, just swap the "2" out for your slider variable. Hope that helps!

How do I make linked turtles move together in NetLogo?

I'm trying to model fish movement and need them to form schools when there is more than 1 of the breed in a given patch. So far I have managed to get them to form links when they encounter each other with the function below, but then they continue moving independently. I'd also like to re-scale the color of turtles in a linked group so that the more turtles in the group the darker the color is (I'm guessing this is similar to the way you make contour maps according to environmental gradients but I haven't figured it out yet).
Any assistance is always appreciated!
to form_link
if count breed_1-here > 1
[
ask breed_1
[create-links-with other breed_1-here]]
end
If linking isn't the way to get them to move together, I'm fine with another method.

How to read node ids from an input box?

I am pretty sure there are some basic glaring flaws here but assistance would be great. What I am trying to do is to use an input box so that a user can specify connections between specific nodes. The data that is being inputted is in the form of a string (reporter) and I am having problems getting the programme to recognise the input. The code is as follows;
ask circle 1 [ create-links-with n-of 3 read-from-string connect-with ]
There's not enough information, but I'm going to guess. I'll assume that circle is a turtle-breed.
create-links-with wants an agentset, and n-of therefore needs an agentset as its second argument here. I don't think you can create an agentset with read-from-string, since the documentation says that read-from-string will only produce 'a number, list, string, or boolean value, or the special value "nobody".'
However, you could do something like this:
create-links-with n-of 3 circles with [color = read-from-string connect-property]
I'm using connect-property instead of connect-with; it's the variable that would be attached to the input box, and you can replace color with some other circles-own variable that you've defined.
If you have a fixed set of properties with which to identify the circles, it might be better to use a Chooser object, or even a slider, rather than an Input box.
(Outside of testing and experimentation, I think it's probably not the best strategy to identify turtles by their numbers. It's better to give them variables, and use the values of the variables to identify them, e.g. by using with.)

reporting changes of turtle heading in NetLogo

I need to learn concurrently when a turtle changes its heading. Namely, when the turtle changes its direction, a procedure or a reporter will change the value of a boolean. But this reporter won't be called by any other procedures, it will be always running (checking turtle's heading) while the turtle is moving. Is there any way of this in NetLogo?
I think you can achieve something similar to what you want with a "forever" button: that is, a button that runs a procedure constantly. (People usually have at least one button like that, typically named "go", in their models.)
Assuming the heading you want to track is that of turtle 0, you can have code like this:
globals [
current-heading
heading-has-changed
]
to check-heading-changes
if [ heading ] of turtle 0 != current-heading [
set heading-has-changed true
set current-heading [ heading ] of turtle 0
]
end
To have the check-heading-changes code run constantly, you just need to call it from a "forever" button:
You have to remember to click the forever button when you want to start the monitoring. Now, of course, the code above also assumes that you will have some other procedures running that controls the turtle, and also that will actually do something (and reset the variable) when heading-has-changed becomes true.