Netlogo Coding - IF codes - netlogo

here is the issue that I am facing in Netlogo. I want a turtle to check one of its own variable and another variable of the other turtle (different breed). based on these 2 two values I want to set a reward for the turtle. Let's say that I have "student" and "teachers" as two breeds. Students may "cheat" (binary) and teachers may catch (binary) - so based on whether they cheat and get caught or not an appropriate reward would follow. I am trying to incorporate this by the following code
if comply? = 1
[
ask students [ set gain reward1 ]
]
if comply? = 0 and caught? < random-float 1
[
ask students [set gain reward2 ]
]
if comply? = 0 and caught? > random-float 1
[
ask suppliers [set gain reward3 ]
]
end
gain is a student own variable and caught? is a teacher-own variable which represents the chances that the teacher may catch a student.
When I run the model there is an error "STUDENTS breed does not own variable CAUGHT? error while running Student1 running Caught?
I was wondering if someone can share some insights about this?
Thanks
Deb

STUDENTS breed does not own CAUGHT? : Insight
When the ownership error pops up, usually, the issue is that a turtle, or in this case student, referenced a variable that does not belong to their breed. Below is an example of how I imagine the model was initialized for your code, along with a passing and failing example.
breed [ students student]
breed [ teachers teacher]
students-own [ gain comply?]
teachers-own [ caught? ]
... ; initialize
to go
ask students [ set gain 3 ] ; this passes
ask students [ set caught? 3 ] ; this fails
end
The importance of context
Most likely your issue is related to adding conflicting variables in a procedure for students. (Example below)
to listen-in-class ; student procedure
if comply? = 0 [ set gain 7 ]
; the comply? variable assumes a student is calling the procedure
if gain = 3 [ set gain 4 ]
; The gain variable assumes a student is calling the procedure
if caught? = 0 [ set gain 2 ]
; The caught? variable assumes a teacher is calling the procedure
end
Since procedures can call other procedures, each procedure assumes their environment (context) from the variables/procedures.
to starting-class ; should be a student procedure
ask student [ listen-in-class ]
; "ask student" assumes listen-in-class only takes global or student only variables
end
Most likely, it could be that the wrong set of variables for a procedure was added. Asks tend to limit the scope of variables depending on the breed.

Related

Netlogo, how to capture values of variables of turtles and add each turtles value, map them, and reduce them to a single #

I am trying to write a procedure where a turtle of a certain breed asks turtles of the same breed, within a certain distance, the value of a certain variable. The asking turtle will then capture the values add them to it's own, map + them and then reduce + to a single number. Here's the code
ask Teams
[ if AsgnE = "E 1"
[
ask Teams with [ distance myself < 25]
[
; assuming that there are no more then 2 teams within distance
let Val1 []
let Val2 []
let Val3 []
set Val1 Value
set Val2 Value
set Val3 [Value] of self
let Val4 (map + Val1 Val2 Val3)
set Val4 (reduce + Val4)
set Storys1 [Stories] of Epic 0
if Storys1 > 0 [ set TotValue1 Val4 ]
]
]
]
The values of each Team continuously update as long as the go button is pressed. The issue is that the resulting number never matches the aggregate of all the values. As the number updates they never match the totals of the separate Teams. Sometimes the number drops to a lower number (I'm assuming it's representing a single teams value) before jumping back to a higher number.
Any idea on how to fix this?
Thanks
Rudy
My guess is that it's a synchronicity problem. The ask will iterate (in random order) through all the turtles. Let's say it starts with turtle 1 - so turtle 1 updates its value to be the sum of its old value and all the values of the nearby turtles. Then the ask moves on to turtle 2, and turtle 2 happens to be nearby to turtle 1. That means turtle 2 adds all the numbers again, with turtle 1 having its adjusted value. With just these two turtles, the value for turtle 2 gets added in twice because turtle 1 also has it hidden in its new value.
If this is not the behaviour you want, the easiest thing to do is to have an extra variable called something like next-value. Calculate next-value for each turtle as the appropriate sum. Then, in a new ask, get each turtle to set value next-value to update them all at the same time.
Also, your map and reduce seems unnecessarily complicated. If what you are trying to achieve is to add the value of a variable over a bunch of turtles, then you can simply do a sum of the variable after constructing the relevant turtle agentset. But it may be that you simplified for the purposes of the question, in which case just ignore this!
UPDATE ---- added complete model example
turtles-own
[ team
myval
nextval
]
to setup
clear-all
create-turtles 20
[ setxy random-xcor random-ycor
set team one-of ["A" "B"]
set myval 1
]
reset-ticks
end
to go
ask turtles
[ let myteam turtles with [team = [team] of myself]
set nextval sum [myval] of myteam
]
type "total before: " print sum [myval] of turtles
ask turtles
[ set myval nextval
]
type "total after: " print sum [myval] of turtles
end

NetLogo: foreach syntax

Very basic question, I can't see why my foreach code doesn't do anything (no error message but no effect whatsoever). So my turtles have a 3-dimensionnal variable (intention) that is preset to [0 0 0]. My final problem is much more complex than this, but in simple terms I am now trying to get every dimension of this vector to change to one, that is [1 1 1].
I have created a procedure called change-intention that uses foreach to produce this, to no effect:
to change-intention
ask turtles [
(foreach intention [ x -> set x 1])
]
end
I have tried this on the observer & turtle command line as well as on individual turtles' to no results nor errors..
Thanks!
Several problems. The first is that lists are not mutable - if you want to change the value in a list, you have to create a new list with that value. The second is that you can't use set to do that, you have to use replace-item.
The code is self contained - open a new model and try it out, changing the call in the testme procedure to different implementations. The procedure change-intention1 is the way you are currently thinking about it (my interpretation anyway). The procedure change-interpretation2 is the way to implement your approach, replacing each item and creating the new list (addressing the problems identified).
However, a better way to do this is with the map procedure instead of foreach because all the values are changed at once rather than looping through the list and dealing with each. Of course, that may not be so easy to implement in your real model.
turtles-own [intention]
to testme
clear-all
create-turtles 1
[ set intention [0 0 0]
]
ask turtles [ type "before call:" print intention ]
change-intention2
ask turtles [ type "after call:" print intention ]
reset-ticks
end
to change-intention1
ask turtles
[ foreach intention
[ x ->
print "here"
set intention 1
]
]
end
to change-intention2
ask turtles
[ foreach intention
[ x ->
let pp position x intention
type "here:" print pp
set intention replace-item pp intention 1
]
]
end
to change-intention3
ask turtles
[ set intention map [ x -> 1 ] intention
]
end

Is it possible to have enumerated data types in NetLogo?

Is it possible to have enumerated data types in NetLogo?
Let's say that I have a model of marital status change.
An agent can have 3 marital status states: single, married, divorced.
I would like to map those states into numbers so that it takes less memory when executed.
single = 1
married = 2
divorced = 3
so that I can just do
ask agents with [ marital-status = single ][ get-married ]
I have found a trick to do that with "to-report"
eg:
to-report single
report 1
end
But this means I have to create many to report functions if I were to have many categories in many variables. Is there a better workaround than this?
Thanks :)
How big is your model? My understanding is that an agent attribute is minimum 8 bytes anyway (see https://github.com/NetLogo/NetLogo/wiki/Optimizing-NetLogo-Runs)
I can't think of a natural way to do this. However, if you really wanted to, this workaround would work: store the marital status as 0, 1, 2. Also store a global variable called marriage-status-map and use the item primitive. So it would look like this:
globals [ marriage-status-map]
turtles-own [ marriage-status ]
to testme
clear-all
set marriage-status-map ["single" "married" "divorced"]
create-turtles 10
[ set marriage-status random 3
setxy random-xcor random-ycor
set color blue
]
ask turtles with [item marriage-status marriage-status-map = "single"] [set color red]
end

NetLogo: create a dynamic number of breeds

how can i create a dynamic number of breeds at runtime?
I want my user to be able to choose the amount of breeds.
I thought about something like
to setup_breeds
let j 1
while[j <= n_groups][
breed[j]
]
end
where n_groups is the number of breeds whick is taken from a slider.
But unfortunatly i cannot use breed inside a method...
Any ideas?
Thanks!
You need to explicitly declare each breed with the breed keyword, so the short answer is: no, you can't have a dynamic number of breeds.
But do you really need actual NetLogo breeds? The main purpose of having different breeds is to have different variables for each breed. If that is not case, perhaps you can get away with having a group-id turtle variable. To create a certain number of turtles for each of n_groups, you could just do something like:
turtles-own [ group-id ]
to setup
clear-all
let n_groups 10
let n-turtles-per-group 5
foreach n-values n_groups [ ? ] [
create-turtles 10 [ set group-id ? ]
]
ask turtles [ set label group-id ]
; do something with only turtles of, e.g., group 2:
ask turtles with [ group-id = 2 ] [
fd 5
]
end
If you think you really need breeds, edit your question to tell us why, and we'll see if we can find a solution for you.
Side note:
I used foreach n-values n_groups [ ? ] to loop through your n groups. That's the equivalent of:
let i 0
while [ i < n_groups ] [
set i i + 1
]
...but arguably more "NetLogo-ish".

NetLogo : what is a good way for Storing link values and deducting the dead link values without calling links more than 1 time?

I am sorry to keep asking about links, but one of the features that I am going to add to my model is considering collective mutual relationship of people of different villages in village's future relationships,
I have a few thousand links and it's not efficient to call links and get their value whenever the village wants to make a decision (the decision is made every 48 ticks at clock 0)
Agents own belongs_to which is one one "Village1" Village2" Village3" or "Village4"
Links have a Value of Relationship.
This is the function I used to update links value:
to Update_link_Values [Other_Agent Value]
if self != Other_Agent
[
ifelse out-link-neighbor? Other_Agent
[
ask out-link-to Other_Agent
[
set Value-Of-The-Relationship Value-Of-The-Relationship + Value
set-List-of-Mutual-Obligations
]
]
[
create-link-to Other_Agent
[
set Value-Of-The-Relationship Value-Of-The-Relationship + Value
set-List-of-Mutual-Obligations
]
]
]
end
if I use following formula to store sum of relationship values for different villages it takes 0.003 MS to calculate all mutual relationship values
if [belongs_to] of end1 = "Village1" and [belongs_to] of end2 = "Village2"
[
set List-of-Mutual-Obligations replace-item 0 List-of-Mutual-Obligations (item 0 List-of-Mutual-Obligations + Value-Of-The-Relationship)
]
While this one takes 1.002 MS to execute,
if [belongs_to] of end1 = "Village1" and [belongs_to] of end2 = "Village2"
[
set List-of-Mutual-Obligations replace-item 0 List-of-Mutual-Obligations sum [Value-Of-The-Relationship] of links with [[Belongs_to] of end1 = "Village1" and [Belongs_to] of end2 = "Village2"]
]
my problem with first version is that it adds the value of each link to sum of all values of previous links in that group and does not consider if a link is dead or not, but second one is more accurate.
Since Value of relationship is link property I don't want to ask links more than once in the code and I update the sum values whenever a link is being changed or created.
I thought it might be better to update the values every 48 ticks , since many agents might call this function every tick, but for doing that I have to call links and I am not sure which way is better?
Update:
I have changed my code so I will calculate the links I need whenever a decision is made:
to-report Value-of-Mutual-Obligations [Village1 Village2]
report sum [Value-Of-The-Relationship] of links with [[Belongs_to] of end1 = Village1 and [Belongs_to] of end2 = Village2]
end
Another way of thinking of it.
Create a breed for the Villages.
breed [ villages village ]
Create new link breeds, one for the villages, and one for people. (you can't use the built-in links breed when you have any custom links breed):
directed-link-breed [ village-links village-link ]
directed-link-breed [ person-links person-link ]
give village-links a variable VALUE-OF-MUTUAL-OBLIGATIONS
give the villages a variable VALUE-OF-SELF-OBLIGATIONS -- this is to handle cases where both persons are from the same village -- sadly, netlogo does not allow self-links.
Create a village turtle for each village. They can be hidden. You can apply the name of the village ("Village 1") to the label of the village.
Link all the villages to each other, in both directions.
A person's belongs-to contains a village turtle.
e.g. to randomly assign a village:
set belongs-to one-of villages
So, now, rather than having to calculate the value of mutual obligations wholesale, you can alter it directly, as it changes.
Whenever you change the value of a link, you can also change the VOMO variable for the village link. You use the who numbers of the villages to figure out the link ID, or to use the SELF-OBLIGATIONS version in that special case.
to update-relationship-value [ #value ] ;; run by the person's LINK
set value-of-the-relationship value-of-the-relationship + #value
let from-village [ belongs-to ] of end1
let to-village [ belongs-to ] of end2
ifelse from-village != to-village
[ ask village-link ([who] of from-village ) ([who] of to-village)
[ set value-of-mutual-obligations value-of-mutual-obligations + #value
]
]
[ ask from-village ;; update self-obligation value
[ set value-of-self-obligations value-of-self-obligations + #value
]
]
end
So, you only touch the value of mutual obligations once, when you update the relationship value.
You could probably make this slightly more efficient by extracting the village link update code so that it's run by the turtle, not by the link, so that you don't have the extra "[stuff] of end1" stuff.