How to change a turtle's attribute if one of its links disappear? - netlogo

In NetLogo: suppose the model has
a turtle (0) of breed A with undirected links with 3 turtles (1, 2 and 3) of breed B;
the turtle 0 has an attribute named "number-of-links" that equals 3.
Now, let one of the 3 neighbors of 0 dies..
How can I program turtle 0 to change its number-of-links automatically to 2?

If all you want is a way of keeping track of the number links, use count my-links instead of a custom variable.
In general, the least bug prone way of having a value update when the number of links changes is to compute that value when you need it. For number of links, this is simply count my-links. For more complicated things, wrap them in a reporter:
to-report energy-of-neighbors
report sum [ energy ] of link-neighbors
end
If this doesn't work for whatever reason (agents need to react to a link disappearing or you're seeing a serious, measurable performance hit from calculating on the fly), you'll have to make the updates yourself when the number of links change. The best way to do this is to encapsulate the behavior in a command:
to update-on-link-change [ link-being-removed ] ;; turtle procedure
; update stuff
end
and then encapsulate the things that can cause the number of links to change (such as turtle death) in commands as well:
to linked-agent-death ;; turtle procedure
ask links [
ask other-end [ update-on-link-change myself ]
]
die
end

Related

Introducing IDs to patches in Netlogo

I am working on a model which is supposed to act like a human tissue. It is composed of only patches which represent the human cells. The cells replicate over time, and I want to introduce unique IDs to each of the individual patches (cells). These IDs should also carry over to the daughter cells after the replication. I cannot seem to find a good starting point for this and not sure how to approach it.
This is the code I tried but it is not working to even at least give each patch a unique ID. Track clones is supposed to give each of the patches an ID and I will later introduce more to the code to track individual patches and their division over time.
to track-clones
; let patch-list patches
; let patch-count count patches
; foreach patch-list [
; set id ?1
; ]
end
This will have each patch take unique and sequential IDs:
to assign-ids
ask patches [
set id (max [id] of patches + 1)
]
end
That said, it is not clear to me what you mean, in model's terms, when you say
These IDs should also carry over to the daughter cells after the replication.
given that we don't know how such replication is supposed to take place - but it seems this is a separate issue to address.
PS: the syntax using ? for anonymous procedures is no longer accepted, make sure to check the latest NetLogo Dictionary.
You can quickly assign an initial ID based on the patch coordinates
Ask patches [ set patch-id (pxcor - min-pxcor ) + (pycor - min-pxcor) * world-width
]
Alternately, you can use the patch itself as the patch ID:
Ask patches [ set patch-id self ]
This might make other operations relating to the progenitor patch simpler.

How to report agent variables in a consistent order in Netlogo's Behaviourspace

Picture of my behaviourspace menu
I'm working on an agent based model where a variable (agentvariable1) owned by all agents changes every tick. I want to report a time series for the values of this variable for every agent using Behaviourspace.
However, when I measure runs using the following reporter
[agentvariable1] of turtles
the values that are reported for agentvariable1 are randomly shuffled, because "turtles" calls all turtles in a random order, which is different every tick. Because of this the data that is exported is not usable to create a time-series.
Is it posstible to create a reporter in Behaviourspace that reports the values of the agentvariable1 in a sequence that remains the same every tick?
Using sort on an agentset creates a list of those agents sorting them by some criteria. In the case of turtles, they are sorted by their who which means that their relative order will always be the same.
However you cannot directly do [agentvariable1] of sort turtles, because of expects an agent/agentset but you are giving it a list.
What you can do is creating a global variable as a list: at each tick the list is emptied, and later all turtles (sorted as per sort) will append their value to the list.
That list is what you will report in your Behavior Space.
globals [
all-values
]
turtles-own [
my-value
]
to setup
clear-all
reset-ticks
create-turtles 5
end
to go
set all-values (list)
ask turtles [
set my-value random 10
]
foreach sort turtles [
t ->
ask t [
set all-values lput my-value all-values
]
]
show all-values
tick
end
As an alternative to Matteo's answer (which is perfectly suitable and directly addresses your intention, I just present another option depending on preference) you could also pair the variable of interest with some turtle identifier and report that as a list of lists. This adds a bit of flexibility in cases where the number of turtles increases or decreases. In this example, I use who and xcor for simplicity, but you may want to create your own unique turtle identifier for more explicit tracking. With this toy model:
to setup
ca
crt 5
reset-ticks
end
to go
ask turtles [
rt random 30 - 15
fd 1
]
tick
end
to-report report-who-x
report list who xcor
end
At any point, you can call the list with [report-who-x] of turtles to get a list of lists. With a behaviorspace setup such as:
you get an output that would look something like:

Problem: The turtle variable is of type int (-1, for example), but the patch variable is a one-element list ( [-1] ) in NetLogo 6.2

I have one doubt:
Context: I have a code in which, briefly, turtles have an integer variable (energy-collected) and from that, patches update their own variable (energy-of-my-agent), as described in the code snippet below.
Problem: The turtle variable is of type int (-1, for example), but the patch variable is a one-element list ( [-1] ).
Question: Should this happen? Otherwise, how can I make the patch variable just an integer value?
ask turtles
[
set energy-collected (energy - euse)
]
ask patches
[
set energy-of-my-agent [energy-collected] of turtles-here
]
Thanks in advance
The main thing you have to consider is what of reports.
In your case turtles-here is an agentset, not a specific agent.
This is because, although you might have a single turtle on a patch, you may also have multiple turtles on a patch. Therefore turtles-here reports an agentset, even if that agentset may be made of a single turtle.
It follows that a collection of values from an agentset, obtained with of (and [energy-collected] of turtles-here is exactly that), will be a list of values - even if that list contains only one element.
Therefore I would say:
Is your model made in such a way that each patch cannot have more than one turtle at a time? Then you could do:
ask patches [
if any? turtles-here [
set energy-of-my-agent [energy-collected] of one-of turtles-here
]
]
In the code above, one-of turtles-here reports a specific agent - not an agentset anymore.
So its variable's value, obtained with of, will be stored as a single value (provided that the agent's variable is not a list itself, but that's not your case).
Can it happen that your patches have more than one turtle at a time? Then, if you're interested in the single patch holding "its" turtles' values, dealing with lists is probably necessary.
Update
I made a connection between this question and your other one suggesting that you want to use patches as elements of matrices.
Maybe this is useful to your case: if your model allows for the possibility of having more than one turtle on the same patch, you might be interested in doing something like:
ask patches [
set energy-of-my-agent sum [energy-collected] of turtles-here
]
As you can see, sum takes a list as input and reports a number. Each patch will take the sum of all the values of energy-collected by turtles standing there, or you can change the calculation using whatever you want (e.g. mean, max etc).
Actually, you can use this approach regardless: this way, even when you have a single turtle on a patch, sum (or any other function taking a lost and returning a value) will give you a single value where before you had a list of one value.

Order of processing in netlogo

I am not sure how netlogo processes commands. Consider the following scenario. There are 100 turtles. The "go" procedure calls the following other procedures: A, B, and C. Procedure A tells turtles to do some things, Procedure B tells turtles to do some things, and Procedure C tells turtles to do some things.
Understanding X:
It is my understanding that all turtles would complete the commands in A, then all turtles would complete the commands in B, then all turtles would complete the commands in C.
Now, inside procedure A there are commands A1, A2, and A3.
Understanding Y:
It was my understanding that, inside procedure A, one turtle would do command A1, then A2, then A3, then a second turtle would do command A1, A2, and A3, and so on.
Is my understanding Y correct?
Thanks for any insights you can provide.
It depends. The basic rules is that the user presses the go button (or types 'go' in the command center) and that tells NetLogo to run the go procedure. NetLogo runs that procedure from top to bottom. But how you include the called procedures changes the order.
Consider version 1. The first line ask turtles says to choose a random turtle, make it do everything in the code block (delimited by the [ ] symbols) and then choose the next random turtle, make it do everything and then the next turtle until all the turtles are done. In this case, both proc-A and proc-B are within the same code block, so the turtle would do both before NetLogo switches to the next turtle.
to go
ask turtles
[ proc-A
proc-B
]
end
to proc-A
forward 1
set heading heading + random 10
end
to proc-B
forward 3
set heading heading - random 20
end
How about version 2? The first line ask turtles [proc-A] says to choose a random turtle, make it do everything in the code block (delimited by the [ ] symbols) which in this case is only proc-A. And then choose the next random turtle, make it do everything and then the next turtle until all the turtles are done. Only after ALL the turtles are done with the ask turtles will the code move on to the next line. In this case therefore, all turtles do proc-A and then all turtles do proc-B.
to go
ask turtles [ proc-A ]
ask turtles [ proc-B ]
end
to proc-A
forward 1
set heading heading + random 10
end
to proc-B
forward 3
set heading heading - random 20
end
You could also do something like version 3. The first line says to run through proc-A, which gets every turtle to do something. Then, at the end of proc-A, control returns to the go procedure and moves to the next line, which is to run the procedure proc-B. This would achieve the same outcome as version 2.
to go
proc-A
proc-B
end
to proc-A
ask turtles
[ forward 1
set heading heading + random 10
]
end
to proc-B
ask turtles
[ forward 3
set heading heading - random 20
]
end
When I am teaching NetLogo, I encourage my students to construct the code along the lines of version 3. This is because the ask is within the same procedure as the actions they are being asked to do. This has several advantages. (1) Much easier to read because you don't have to try and read across procedures that could be a long way apart. (2) Avoids one of the most common beginner issues of nested ask - with an ask turtles in the go procedure and then ask turtles again as the first line of the called procedure. This is actually such a common bug that the NetLogo developers have made it impossible to have nested asks.
However, if you truly want a turtle to do more than one thing before the next turtle starts, then you have two options. You either code as in version 1. Or you put both things that you want the turtle to do in the same procedure.

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