turtles-own variable to global variable on NetLogo - netlogo

I am creating a NetLogo model to simulate the use of appliances in a household and I've got 2 turtles: people & appliances. I have declared a peoples-own for appliances_hit and appliances-own for times_used.
I want to be able to calculate the total times_used from ALL of the appliances ie. times_used of (appliance1 + appliance2 + ... appliance6) I will use that total to plot a graph by performing some calculations to show the usage data.
I would highly appreciate any sort of help.

You can use the sum command and it will return the total of time_used for all appliances. It will be like this:
sum [time_used] of appliances

Related

AnyLogic variable for cumulative sum in system dynamics

Good morning, in a System Dynamics model created on AnyLogic, I would like to compute the cumulative sum of a flow of the previous 7 days.
My purpose is to calculate the reproduction ratio of a disease starting from the infectious population at time t over the cumulative sum of the infectious in a fixed time interval. The formula is the following:
Formula
where:
I(t) = infectious population at time t --> I(t) is a flow in the model that changes a stock
I(t-s) = infectious population at time t-s
w(s) = gamma distribution
s represents the time interval of the previous 7 days
I have all the data but I am not able to calculate the sum of I(t-s).
Thanks.
You have to do this manually. Create a variable mySum of type double. Then, add a cyclic event that regularly adds to it from the stock (something like myVar += myStock).
You may need to use an additional variable that stores the temporary stock value from the last time you added, so you only add what was "new" since the last cycle.
In short: use a cyclic event to "approximate" your integral.

Random select and median of medians

I was planning to implement those algorithms in C++ and came upon one problem. What happens when I pass array with elements that repeats and ask for some order statistics? When I ask for a second order statistic from array [1,2,1] should it return 1 or 2 ? If 2, is there any quick way to edit those algorithm to adapt them to that kind of arrays? Is moving elements that repeats to the end of array is good way to do it?

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

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.

Associating different breeds through turtle attributes

Say I have two classes of turtles, cars and insurers. There are 5000 cars and 100 insurers. Initially, cars are assigned a random insurer 1 through 100. Cars and insurers have several attributes:
cars-own [make model age insurance capacity]
insurers-own [number-of-customers minimum-premium maximum-premium average-premium]
What I want to do is count the number of cars with insurance = x and assign that value to number-of-customers for insurer x. For example, if there fourteen cars with insurer 24, I want number-of-customers for insurer 24 to take the value 14.
This seems like it should be straightforward, but since I'm operating between two agentsets I'm having difficulty implementing. Help would be greatly appreciated. Thank you!
EDIT: Additionally, is there a way to generalize this to a links breed? For example, a road network consists of directed links between nodes. I want to count the number of cars on any given link:
breed [cars car]
breed [insurers insurer]
breed [road_nodes road_node]
directed-link-breed [road_segments road_segment]
cars-own [make model age insurance capacity current-road-segment]
insurers-own [number-of-customers minimum-premium maximum-premium average-premium]
road-segments-own [number-cars-here]
As in the cars/insurers case, I'd like the value of number-cars-here for road_segment x y to be number of cars with current-road-segment = "road_segment x y".
There are many ways to do this, but directed links seem an obvious way. Unless you will otherwise compute the same number over and over, do not keep a number-of-customers attribute. Just make one directed link from each customer to its insurer, and then count the insurer's in-links whenever you want number-of-customers.