Constructing a set of variables - netlogo

I have a question about Netlogo. I have two breeds of turtles, breed1 and breed2. Breed2 has a variable called theta. First, a random number of breed1 turtles change to breed2; the breed2 then theta set at random. Later, another random number of breed1 become breed2, who will also have theta set at random.
My two questions are:
1.) How can I ensure that only those turtles that change their breed in the second round of breed-changing set their theta variable? I don't want those that changed in the first round to lose the theta variable that was set for them immediately after changing.
2.) Is there a way I can construct a set of all the theta variables that have been set so far? I want my breed2 to later on make decisions based on the maximum value of theta that has been set so far.
Thank you!

You should ask two separate questions separately, but ...
There are many ways to do this. Example 1: If you always update to a nonzero value of theta, just test for a zero and only change a zero. Example 2: add a can-change-theta attribute to breed2 and always initialize it to true but change it to false after resetting theta. Example 3: gather your new breed2s into an agentset (or a list, if necessary) and change theta only for these turtles.
If you really want the max, you don't need all values. Just add a global variable max-theta and update it each time you update a theta. If you really want all, add a thetas global and initialize it as an empty list; append to this list each time you update a theta attribute, and use max to pull out the max whenever you wish.

Related

How to create condition in NetLogo that 2 slider values cannot exceed defined value?

I created a model which as 2 different sliders, namely ratio1 and ratio2. They are located on the interface and their values should add up to 1 (here: labour-unit), and also cannot exceed this value. For now, NetLogo let's me exceed the condition.
I tried this:
to setup
create-turtles number-of-turtles ;; number of firms to be defined through slider
set labour-unit ratio1 + ratio2
set labour-unit 1
end
Therefore, my question is: How to create a condition in the setup that 2 slider values cannot exceed a defined value?
Is there any reason you actually need two sliders if the values always add to 1? Could you just have one slider called "proportion with labor-type x" or whatever you're modelling? Then, you can just have reporters to return the values for the actual proportion you're after- for example:
to-report ratio1
report proportion-slider
end
to-report ratio2
report precision ( 1 - proportion-slider ) 2
end
Then on your interface you could have the slider (and monitors if needed):

Can we code to calculate the difference of tick values in netlogo

Actually i need to calculate density increase/decrease rate of human population for my model, the model is same as i asked in unable to make non-stationary turtles change their direction if an obstacle a patch ahead (a specific area and a building within it, people are randomly visiting and going). What i thought that i will be needing to save the tick values for initial population value and after some time difference updated population value. Below is the procedure i want to plot graph for.
to density-increase-rate
store population-density at some initial time (ticks)
store updated-population-density at some later-time (ticks)
calculate density-increase-rate
( ( ( updated-pd - previous-pd ) / (updated-tick - previous-tick ) ) * 100 ) / 10
end
I am calculating population-density in my code as
total-density-inside-boundary count people with [inside-boundary?]
for any suggestion or code help i am very thankful.
If you just want to plot this change, there is no need to store it because the plot will update each tick.
globals [total-density-inside-boundary density-increase-rate]
to calc-plot-vars
let old-density total-density-inside-boundary
set total-density-inside-boundary count people with [inside-boundary?]
set density-increase-rate (total-density-inside-boundary - old-density) / 100
end
Then have a plot on the interface with plot total-density-inside-boundary and plot density-increase-rate. You may need to do some rescaling to have them both on the same plot.
If you want to have the rate based on total time, then create a variable to hold the initial value and calculate it at the specific time you think initial means (such as the end of the setup or at a specific tick).
globals [total-density-inside-boundary initial-density]
to setup
... (commands that create your people)
set initial-density count people with [inside-boundary?]
...
end
to go
...
if ticks = 1 [ set initial-density count people with [inside-boundary?] ]
...
end
Then have the rate plot in the interface have plot (total-density-inside-boundary - initial-density) / 100

NetLogo - Random setup - Random value to each patch - Error

As part of the setup procedure I am trying to use a slider to set the density of patches which will be displayed and assigned a random value. The slider on the interface for density ranges 0 to 100 and the random value of the patch is set using an input on the interface. This will normally be set in the region go 4. So, if 50% is set the the procedure will assign 50% of the patches with a random value.
When i do i get the following error: "Expected command" and the variable 'error-count' is highlighted in the code.
;; The density of patches to be set with a random value is set using variable init-errors on interface.
;; Every patch uses a task which reports a random value.
;; The random value is set using variable error-count on interface
to setup-random
ask patches [
if (random-float 100.0) < init-errors
[setup task random error-count]
]
end
You need to change:
setup task random error-count
to
setup task [ random error-count ]
Anytime the task body isn't just a single primitive (the "concise task syntax"), it needs to be surrounded by square brackets. The error message you're getting happens because task random is valid syntax; NetLogo interprets it as short for task [ random ? ]. Then it doesn't know what to do with the following error-count, hence the error.
I am not entirely clear on the task syntax you are using (in particular, why setup is at the beginning of that line) or why you want to use a task here - but that could be a difference in coding style. But the NetLogo compiler is letting you know that it expected a command after the word random but got a value. Since random does expect a value after, I think it's something to do with the setup and task elements. Here's an alternative way of doing what you want, which might be easier. I have also turned the non-zero error patches green so you can see them. This code assumes you have the two sliders you defined in your problem description.
patches-own [errors]
to setup-random
ask patches [
if (random-float 100.0) < init-errors
[ set errors random error-count
set pcolor green ]
]
end

How to avoid individual patch updates

I am modeling diffusion in my model, but I think I am getting a calculation artifact due to NetLogo sequentially updating individual patches. I will not be using the diffuse command (due to inaccurate diffusion). However, much like how this command works, I would like to update all the calculations of the patches simultaneously, rather than sequentially. I have a slight recollection of seeing some sample code that used values at the beginning of the tick, however I canĀ“t seem to find it now.
Specifically, I need help programming a way to store patch values at the turn of each tick, and then carry out a simultaneous calculation based on these stored values.
Great question. As you indicate, basically you want to calculate the new value of the variable in one ask block, but store it in a separate variable, and then update the actual value of the variable in a second ask block, like so:
turtles-own [
value
new-value
]
...
to go
ask patches [
;; Change this line to however you want the diffusion to work
set new-value 0.5 * value + sum [ 0.5 * value / 4 ] of neighbors4
]
ask patches [
set value new-value
]
end
This way all patches calculate their updated values from the same information, and then actually update the values themselves simultaneously.

NetLogo - compare single agent against many agents (expected input not list)

I am first time poster, six month reader. I love this site and am grateful for the vast array of topics covered. Now that I am feeling a bit more competent using NetLogo, I've tried some harder stuff and got stuck...
Basically, I have created a membership function which measures agents against one another on a vector containing two variables (opinions on rock and hip-hop):
to-report membership [ agent1 agent2 ]
let w 0.5
let w2 sq w
report exp (- d2 agent1 agent2 / w2)
end
where
;;;;;;;;;;;;;;Shortcut functions;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report d2 [agent1 agent2 ]
report ( (sq ([rock] of agent1 - [rock] of agent2)) + (sq ([hip-hop] of agent1 - [hip-hop] of agent2)) )
end
to-report sq [ x ]
report x * x
end
This all works fine, and I am able to compare any two agents without problem.
However, my trouble arises when I try to compare a single agent [agent1] with all of the agents within his neighbourhood.
to go
ask turtles [
let neighbours turtle-set turtles in-radius neighbourhood
show membership self neighbours]
end
Whenever I run this model I receive an error that the d2 reporter expected an input not a list - which I theoretically understand - by having a neighbourhood of 1+ agent(s), the calculation is receiving for example [0.1 0.8] [0.2 0.4] [0.5 0.6]..............
I was just wondering, is there any way that the procedure can consider all of the neighbours and arrive at one single membership number? I have searched extensively through posts and a couple of netlogo books I have, but no luck so far. Thank you for taking the time to read this post and for any helpful comments.
Your understanding of what is happening is correct: your membership reporter expects two individual agents and you are passing it an agent and an agentset. To calculate each membership individually, and get back a list of membership values, you can use of:
to go
ask turtles [
let neighbours turtle-set turtles in-radius 10
show [ membership myself self ] of neighbours
]
end
Notice the use of myself and self, which can sometimes be tricky to understand. In this case, self is the neighbour and myself is the outer asking turtle.
So now you have a list of membership numbers, but you wonder:
is there any way that the procedure can consider all of the neighbours and arrive at one single membership number?
There are plenty of ways! But we can't really tell you which one to use: it depends on your model and what you want to do with it.
If you wanted something very straightforward, you could just take the mean of the list:
show mean [ membership myself self ] of neighbours
...but I don't know if it makes sense in your context. In any case, NetLogo has plenty of
mathematical primitives that you should be able to use to arrive at the number you want.