Associating different breeds through turtle attributes - netlogo

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.

Related

turtles-own variable to global variable on 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

Find the difference between two variables of the agents of two different breeds - Netlogo

I have 2 agent types, boys and girls.
breed [boys boy]
breed [girls girl]
Each turtle has an age from a dataset. Also when an agent is a boy, its boy? is true, and if it is a girl, girl? is true.
turtles-own [
age
boy?
girl?
]
They are connected by some random links. Now I want for each boy, I can access its girl neighbors, and the difference between their ages gets calculated. In other words, the age difference of two different breeds. I wrote this, but it does not work.
ask boys [
ask link-neighbors with [girls? = true]
[
set Gage age]
let H abs(item 0 age - item 0 Gage)
]
Edit When I use ask link-neighbors with [girls? = true]the neighbors are considered all together, while I want them to one by one be considered where I can compare their age difference and base on that do some other stuff.
Any suggestions?
Thanks
This is untested, but I hope it's close enough to get you there if it's not correct.
First, you have some confusion with your breeds and turtles-own sex indicator. It would be much easier to have one or the other. Scrap your turtles-own statement entirely and simply test the breed because then you can't introduce errors where (for example) you have have the flag (girl? or boy?) inconsistent with the breed, or both set to TRUE or whatever. The way you have it set up, it is possible to have a turtle of breed boy but accidentally set its variable boy? to FALSE. There is no need for these variables at all, breed is an automatic variable (like who number or size that is created with the turtle) and you can test on the breed directly.
Getting to your actual error, you are asking the link-neighbors to set their variable Gage rather than setting the value of the original turtle that is doing the asking (that is, the turtle that is the centre of this ego network).
UPDATED from the comments, you want the boy to have a list (called age-diff below) of the difference in age between his own and all the girls he is linked to. The primitive map is used to substract a constant from a list, and asking for the variable values of and agentset constructs the list of those values.
boys-own [age-diff]
ask boys
[ let my-girls link-neighbors with [breed = girls]
if any? my-girls
[ set age-diff map [ x -> abs (x - age) [age] of my-girls ] ]
]

Two Turtles Breeds _ one breed is the variable of the other?

I've got a doozy of a Netlogo question. If I have two different breeds of turtles, can the sum of a specified number of one breed's variables BE THE VARIABLE of the other breed?
Here is my train of thought. I’d like to model water usage of multiple households, but that water usage of a household needs to be dependent on a) the fixed values of the house (like water used by a faucet) * b) frequency of use of faucet by a person. With each household containing either 1 or more person (people) and that frequency of use can vary person to person.
The idea of using two turtle breeds would allow me to see how the decisions made by one breed affects the other.
Here is my pseudo code to help illustrate what I was thinking (not intended to be a working code)
globals []
breed [People person]
breed [Community household]
People-own [frequency]
Community-own [waterusefacuet HouseholdWaterUse]
;; =================================================================================================================
;; =================================================================================================================
to setup
clear-all
HouseholdCreation
PersonCreation
reset-ticks
end
to go
ask Community [WaterConsumption]
tick
end
;; =================================================================================================================
;; =================================================================================================================
to HouseholdCreation
ask patches [ sprout-Community n of 1 [
set size 1.0 set shape "square" set color blue
set waterusefacuet (1)
] ]
end
to PersonCreation
ask Community [ hatch-People 1 [
set size 0.5 set shape "circle" set color red
set frequency (1 + random 4)
]]
end
to WaterConsumption
Set HouseholdWaterUse (waterusefacuet * (frequency * # of people) )
end
Why not simply make each patch a household, have each patch have one or more turtles (persons), and then calculate household factors as patch factors? To define communities one could place patches into zones (e.g., if pxcor >= 5 and pxcor <=8 and pycor >=3 and pycor <= 6 set zone 1) <== not meant to be code, just the idea.
You could set patch size to make each patch small and specify a large zone of patches.
can the sum of a specified number of one breed's variables BE THE VARIABLE of the other breed?
Absolutely.
snipsnip for clarification : In my code here, I do not let the water use of people who live in a household BE the water use variable of that household. And generally I would recommend against a solution that lets the state of one (or more) agents be the state of another variable - unless there is a very good reason for it. Having states depend on each other is dangerous because you always have to make sure that you sync the values between agents. More importantly, it's often unnecessary. In my solution here, each person belongs to a household, and when that households calculates its total water use, it asks all its inhabitants to send them their use on that day, and then returns the sum of all those numbers. I hope that makes sense. If not, please do ask.
*< /snipsnip>
You need to use the of keyword though. of allows you direct access to variables from the context/perspective of one or more individual agent. So, let's say we have households and people, and people (because we all have different water use habits) have some frequency of water tap uses. In fact, we could have people draw the amount of water they use every day from a a normal distribution that is unique to them. Let's do that:
breed [people person]
breed [households household]
people-own [
mean-use-per-day ;; mean use per day
sd-use-per-day ;; standard dev per day
my-household ;; the household to which a person belongs
]
to setup
create-households 10 [
hatch-people random 4 + 1 [ ;; between 1 and 4 people in a household
set mean-use-per-day random 5 + 5 ;; mean 5-9
set sd-use-per-day random-float 3 ;; sd 0.00-2.99
set my-household myself ;; we set the person's household to the household that hatched them
]
]
to-report household-water-use ;; household reporter
report sum [random-normal mean-use-per-day sd-use-per-day] of people with [my-household = myself] ;; this creates a list of water uses based on the random use of each person in the household.
end
in order to run this code, you can simply call
show [household-water-use] of households
from the command center. This will give you a list of the water use of each household. Or if you want to just see the water use of one household on one random day, you can try
show [household-water-use] of one-of households

Average values of a group of turtles

The deal is this:
I have three types of turtles.
Every type of turtles contain three turtles
Each turtle has three variable with its own value
How could i ask netlogo to get the average value of each group of turtles
I think you mean something like
let type1-average-x mean [x] of turtles with [type = "1"]
You'll want to do this for each type and for each variable that you'd like the measure of.

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.