I have two lists for each agent. They show 1) to whom they are attracted and 2) whom they attracted.
I would like to make a new set variable that shows only those agents that are equally attracted to each other. Equal attraction is: the number of the agent (self) is in the 'attracting' list of the other agent and the number of the other agent is in the 'attracting' list for the first agent (self). My code so far:
if attracted != nobody [set attractinglists fput ([self] of attracted) attractinglists]
if attracted != nobody [set attrlists fput ([self] of attracting) attrlists]
set attractinglist [self] of other turtles with [member? myself attrlists]
Edited to better answer the question as clarified in the comments below.
Alright so now, turtles will have a running list of all turtles to which they have ever been attracted. Every tick, turtles become attracted to three other turtles. They add those turtles to their "attracted-to" list (if they are not already on that list). Next, the turtles check if any of their "attracted-to" turtles have ever been attracted to them- if so, they will add that turtle to the "reciprocal-attraction" list (if it's not there already). Is that more what you were after?
turtles-own [
attracted-to
reciprocal-attraction ;;; the turtles to which this turtle is attracted
]
to setup
ca
create-turtles 10 [
set attracted-to []
set reciprocal-attraction []
]
end
to go
ask turtles [
let temp-attraction sort n-of 3 other turtles
show temp-attraction
foreach temp-attraction [
[x]->
if ( member? x attracted-to = false ) [
set attracted-to lput x attracted-to
]
]
]
ask turtles [
foreach attracted-to [
[x]->
if member? self [attracted-to] of x [
if ( member? x reciprocal-attraction = false ) [
set reciprocal-attraction lput x reciprocal-attraction
]
]
]
]
end
Related
My model involves one turtle surveying the area and it should keep track of the ID/ 'who' of the turtles it bumps into. Importantly, the surveyor could potentially record the same ID/ 'who' twice but I'm not sure how to implement that memory side of things.
breed[boats boat]
boats-own [
my-neighbors
num-neighbors
count-neighbors]
to setup
clear-all
crt 100 [
move-to one-of patches with [ not any? turtles-here ]
]
crt 1 [set breed boats
set count-neighbors 0
]
reset-ticks
end
to go
ask turtles [
fd 1
]
ask boats [
survey
]
tick
end
to survey
set my-neighbors (other turtles) in-radius 3
set num-neighbors count my-neighbors
set count-neighbors [who] of my-neighbors
end
UPDATE
In light of the answer below I came up with the following which also records when the interaction took place by creating a new time variable.
breed[boats boat]
boats-own [
my-neighbors
num-neighbors
id-neighbors
survey-time]
turtles-own [
time
]
to setup
clear-all
crt 10 [
move-to one-of patches with [ not any? turtles-here ]
]
crt 1 [
set breed boats
set shape "circle"
set id-neighbors []
set survey-time []
]
reset-ticks
end
to go
ask turtles [
fd 1
set time ticks
]
ask boats [
survey
]
tick
end
to survey
set my-neighbors (other turtles-here )
set num-neighbors count my-neighbors
set id-neighbors (sentence id-neighbors [who] of my-neighbors )
set survey-time (sentence survey-time [time] of my-neighbors )
end
Currently, you are always setting count-neighbors to the list of all its current neighbors, but this list is cleared and made anew with each tick. Instead of that, you can use primitives like sentence to combine the old list of count-neighbors with the new list you are generating this iteration and set the new list of count-neighbors to be this combined list.
set count-neighbors (sentence count-neighbors [who] of my-neighbors )
I also initialised your count-neighbors as an empty list rather than as 0, since 0 is already a turtle who so that would give you false information
crt 1 [set breed boats
set count-neighbors []
]
To check if the memory is working as intended, you can use inspect one-of boats in the command center and see how each new entry gets added to the end of the list.
Edit for followup question
Your solution here definitely works. Allow me a few suggestions for it as well:
There is not need to create the time variable, each turtle can already access the ticks global variable
set id-neighbors (sentence id-neighbors [who] of my-neighbors )
set survey-time (sentence survey-time [ticks] of my-neighbors )
As an alternative, this one creates a list of lists, with each item containing the turtle that was encountered and the time when it was encountered. Slightly harder to work with but clear at a glance which values belong together.
set id-neighbors (sentence id-neighbors [list who ticks] of my-neighbors )
I am a beginner with NetLogo and I am trying to ask some turtles (students from different social classes) to link to other turtles (schools). What I would like is for the working class students to look for the school with the highest achievement which is at the same not expensive and has not reached the max number of links allowed. If the desired school has reached the max number of links allowed I want the student to look for the next school with the highest achievement which has not reached the max number of links allowed and so on.
This is the code. I get the following message "ANY? expected input to be an agentset but got the turtle (school 1) instead."
breed [schools school]
breed [upperclass upperclass-student]
breed [workingclass workingclass-student]
upperclass-own [achievement enrolled? target]
workingclass-own [achievement enrolled? target]
schools-own [schoolachievement expensive? ]
to setup
clear-all
set-default-shape schools "house"
set-default-shape upperclass "person"
set-default-shape workingclass "person"
ask patches [ set pcolor 8 ]
create-schools num-of-schools [ setxy random-xcor random-ycor set schoolachievement random-normal 5 1
set expensive? one-of [ true false ] ]
create-upperclass num-of-upperclass [ set color white setxy random-xcor random-ycor set achievement
random-normal 5 1 ] ;Students from upper class have higher achievement
create-workingclass num-of-workingclass [ set color red setxy random-xcor random-ycor set achievement
random-normal 4 1 ]
end
to go
ask workingclass [
choose-school ]
end
to choose-school
if breed = workingclass [
set target one-of schools with-max [ schoolachievement ] with [ expensive? = false ] ]
if any? target with [ count link-neighbors < max-link-count ] [
create-link-with target ]
set enrolled? TRUE
end
Your problem is the difference between an agent and an agentset, which is a somewhat subtle problem. The with-max returns an agentset - a set of agents (in this case turtles). That agentset can have 0 members, 1 member, 2+ members but is a set even if it is empty. However, the one-of selects one agent from the agentset and returns it as an agent, not an agentset. That is, NetLogo knows anything that is returned by one-of must be exactly one agent. At this point, primitives that are for agentsets (like any?) will throw an error unless they can also be used on individual agents.
So, back to your code. I like the readability of checking whether there are viable schools and then selecting one, which is what I think you meant to do. That would be:
to choose-school
if breed = workingclass
[ set targets schools with-max [ schoolachievement ] with [ expensive? = false ]
set candidates targets with [ count link-neighbors < max-link-count ]
if any? candidates
[ create-link-with one-of candidates
set enrolled? TRUE
]
]
end
Note that I also changed to targets instead of target, which is one way to keep track of whether something is an agent or an agentset.
The other way you could do this and keep it as an agent is:
to choose-school
if breed = workingclass [
set target one-of schools with-max [ schoolachievement ] with [ expensive? = false ] ]
if target != nobody and [count link-neighbors] of target < max-link-count [
create-link-with target ]
set enrolled? TRUE
end
So you can use nobody instead of any? but you can't also use with in that line because the with is really a filter on a set.
I also think you have a bracketing issue - I assume you want set enrolled? TRUE inside the brackets. I left it in the second fix, but changed in the first error (as well as changing bracket position convention to make the code block structure more visible)
I would like to ask how I can add an element created by one turtle to its own list and its neighbour's lists.
I created a local variable, neighbours, defined as turtle-set in-link-neighbors with [breed=breed1]. Then I asked neighbours to add the element created (new_elem), following an answer provided me by an user in this forum.
The code is:
to create_elements
create-elements 1[
set person one-of turtles with [breed = breed1 or breed = breed2]
ifelse [breed = breed1] of person
[
set attribute1 random-float 1
set attribute2 random-float 1
set function1 (1 + attribute1)
]
[
set attribute1 random-float 1
set attribute2 random-float 1
set function2 (1 - attribute1)
]
let neighbours (turtle-set self in-link-neighbors with [breed = breed1] of person)
ask neighbours
[set my-list fput new_elem my-list]
]
]
end
I know that there is an error in let neighbours (turtle-set self in-link-neighbors with [breed = breed1] of person)..., but also in putting an element in the list, as I think the new element is defined in the breed elements. In fact, the error message says that
elements breed does not own variable my-list
Would it be possible to specify the breed of the turtle-set? What I would need is consider only breed1 and breed2, not elements breed.
For example, since I am considering person as creator of the element, is it right to write
let neighbours (turtle-set person in-link-neighbors with [breed = breed1] of person)
ask neighbours
[set my-list fput new_elem my-list]
And if it would be correct, how can I add a new element as "New element" to the list, taking into account its attribute for further consideration.
For example: set my-list fput (list attribute1 attribute2) of elements my-list) returns me list of values like (0.134531 0.14141) (0.91844 0.42176) ... Any suggestions to have something nicer to see but with the same information? I will need to keep track of this values in the next steps.
I hope you can shed some light on what I am doing incorrectly.
Thanks
Val, here's some working code that creates a new element and updates everyone's my-list.
It's probably not exactly what you wanted but I hope it provides some help anyway.
You didn't include all the info on breeds and who owns what so I punted.
I broke up more complex commands into multiple shorter ones in order to debug what was going on and where it was breaking down. I added some print statements.
If you load this up, run setup, and hit "go" once, you can see agents in a network linked up with their "who" numbers besides them so you can easily inspect the ones you
want. Values of my-list are also printed out in the command section.
Two more iterations of the "go" step should verify that they are outputting what you wanted.
I didn't know whether you wanted new_elem to be just local to the create_elements section, of if you wanted it globally available, so I put comments in both places.
I hope this helps! There are more comments in the code itself. Note especially that you must initialize my-list as an empty list, or it will default to the number zero, and your fput statement will fail with a comment about expecting a list and finding zero.
globals [
;;new_elem ;; might be needed as a global ??
]
;; ///////////////////// note that there is no breed required called "people"
breed [ elements element]
breed [ breed1 lion ]
breed [ breed2 tiger ]
;; ///////////////////// i punted on which entity owned "function1 and function2
breed1-own [ attribute function1 function2 my-list]
breed2-own [ attribute function1 function2 my-list]
to setup
clear-all
;; make some turtles with breed = breed1 (say, red lions)
create-breed1 2 [ set size 2 set shape "circle" set color red
setxy random-xcor random-ycor
]
;; make some turtles with breed = breed2 (say, green tigers)
create-breed2 2 [ set size 2 set shape "circle" set color green
setxy random-xcor random-ycor
]
;; //////////////////////////////////////////////////////////////////////////
;; NOTE!!! If you don't initialize my-list to an empty LIST type,
;; fput will generate an error when you try to fput a new element onto it
;; this is ugly but fast:
ask breed1 [create-links-with other breed1
set my-list [] ]
ask breed1 [create-links-with breed2
set my-list [] ]
ask breed2 [create-links-with other breed2
set my-list [] ]
ask breed2 [create-links-with breed1
set my-list [] ]
ask turtles [set label who] ;; ///////////////////////// for debugging
;; set new_elem nobody ;; this could be a global so it can persist
;; outside create_element, in which case
;; initilize it here
reset-ticks
end
to go
create_elements
tick
end
to create_elements
;; declare the local variables, all agent-sets of some type
let new_elem nobody ;; declare this locally if we don't need to persist it
;; outside the create_elements context
;; and remove the declaration in [ globals ]
;; and initializtion in setup
let personx nobody
let xx-neighbors nobody
;; do just this creation here in order to see what's going on more easily
;; than combining multiple operations into a long complex command
create-elements 1 [ set new_elem self ]
type "new element is this agentset " show new_elem
set personx one-of turtles with [breed = breed1 or breed = breed2]
ifelse [breed = breed1] of personx
[ ;; if it is breed1
ask personx [
set attribute random-float 1
set function1 (1 + attribute)
]
]
[ ;; else, it must be breed2
ask personx [
set attribute random-float 1
set function2 (1 - attribute)
]
]
;;//////////////////////////////////////// for debugging
type "the personx we defined above is " print personx
ask personx [set xx-neighbors (turtle-set self in-link-neighbors )]
if-else xx-neighbors != nobody ;; If it has any members, which should
;; always be true since we added self to the set
[
ask xx-neighbors
[
set my-list fput new_elem my-list
;; ////////////////////////////////////for debugging
type "here's the resulting my-list: " show my-list
]
]
[ print "ERROR didn't find any neighbors or self" ]
end
I'm modeling territory selection in NetLogo, and would like my turtles to calculate the perimeter of their territory once established. I've been trying to come up with ideas for how to do this, but haven't found a good means yet. Any ideas?
patches-own
[ owner ] ;; patches know who owns them
turtles-own
[ territory ;; agentset of patches I own
food ;; food acquired in my territory
threshold ] ;; food required, will build territory until meet this
to go
tick
ask turtles [ build-territory ]
end
to build-territory
if food > threshold [ calculate-perimeter ] ;; stop building when enough food
pick-patch ;; keep picking patches until meet threshold.
end
to calculate-perimeter
;; what could I use to add up the perimeter of the territory?
end
Thanks in advance for any suggestions!
A modification of my last answer to you:
to setup
ca
ask patches with [pxcor > 0 ] [
set pcolor white
]
crt 1
end
to go
ask turtles [
let blacklist patches with [ pcolor = black ]
let northpatches patches with [ pycor > 0 ]
let northred ( northpatches with [ member? self blacklist = false ] )
ask northred [ set pcolor red ]
let border northred with [ any? neighbors4 with [ pcolor != red ] ]
ask border [
set pcolor blue
]
print count border
]
end
You can designate border/perimeter patches as any of you territory patches with neighbors that are not territory. For you it might look something like:
ask turtles [
print count territory with [ any? neighbors4 with [owner != myself ]
]
]
Again, I can't test it without your setup so you would have to modify.
Edited below
To count the edges of patches that are on the border, you could have them count their neighbors4 that belong to another turtle. Then, they can add them to that turtle's perimeter length. For example:
to assess-perimeter ;;; must be called by a turtle
print ("Assessing perimeter")
let current-turtle who
let temp-per-len 0
let border-patches patches with [ owner = current-turtle and any? neighbors4 with [ owner != current-turtle ] ]
show (word "I have " count border-patches " border patches")
ask border-patches [
;; One way to get each border patch to check each of its neighbors
let nobodies 4 - count neighbors4 ;; if any patches are on the edge of the world, returns the number of those edges
let non-territory-edges count neighbors4 with [ owner != current-turtle ]
let border-edges nobodies + non-territory-edges
set temp-per-len temp-per-len + border-edges
]
show (word "My perimeter length: " temp-per-len )
set perimeter-length temp-per-len
end
If that is called after all turtles have chosen their entire home range, the idea is that each turtle assesses the border of its home range. Then, it has each of those border patches count its neighbors4 that have a different owner. I used "temp-per-len" as a summing variable within the loop, which is then used to set the turtles-own "perimeter-length". Full model code, including setup and definitions, here. Note- you'll have to download or copy the code, the model is too bulky to run well in the HTML format.
Also, I didn't actually count to make sure this worked perfectly- I did a quick version and crossed my fingers, but I think the idea makes sense and hopefully gets you started.
I'm new to NetLogo and am attempting to model home range selection of subsequent colonizers. The model should follow simple steps:
Individual 1 picks a home range (a subset of patches).
When individual 1 is done picking its home range, it hatches new
individual 2.
Individual 2 picks a home range, then hatches individual 3.
Individual 3 picks a home range, and so on.
I'm having trouble figuring out how to get this to work. I can get the first turtle to pick a home range. But the offspring do not. Writing the code numerous ways has only accomplished two unintended outcomes. Either endless new individuals are hatched simultaneously, before the first turtle has a home range, and the new turtles fail to pick a home range. Or, the first turtle picks its home range and hatches a new turtle, but that new turtle doesn't pick a home range. Neither outcome is what I want.
How do I set this up to run as intended, so that hatchlings pick home ranges too? Here is one simplified version of my code:
to setup-turtles
crt 1
[setxy random-xcor random-ycor]
end
to go
ask turtles [pick-homerange]
tick
end
to pick-homerange
while [food-mine < food-required] ;; if not enough food, keep picking patches for home range
[;; code to pick home range until it has enough food; this is working okay
]
[;; when enough food, stop picking home range
hatch 1 fd 20 ;; now hatch 1, move new turtle slightly away
]
end
So it is at this last part, once the home range is built, that I want a new turtle to hatch from its parent. I then want that turtle to repeat the pick-homerange procedure. How could that be coded to happen? I've tried writing this every way I can think of; nothing is working. Thanks in advance for any assistance!
One way to do this is to have each patch equal one "food value", and have turtles grow their home range until their home range supplies them with enough food. I would set this up so that patches "know" to which turtle they belong, and so that turtles know how much food they need, which patches are part of their home range, and the food supplied by their homerange. Example patch and turtle variables would then be:
patches-own [
owned_by
]
turtles-own [
food_required
my_homerange
homerange_food
]
Then, your turtles can add patches into their home range until they hit their "food_required", whatever you set that as. For simplicity, in this example I assume that turtles are territorial and so won't "share" home ranges. Further explanation of steps is commented in the code below. This is intended just to get you started- for example, it will hang if you run pick-homerange too many times.
to setup-turtles
crt 1 [
set size 1.5
setxy random-xcor random-ycor
set food_required 5 + random 5
set homerange_food 0
set my_homerange []
]
end
to pick-homerange
ask turtles [
;; Check if the current patch is owned by anyone other than myself
if ( [owned_by] of patch-here != self ) and ( [owned_by] of patch-here != nobody ) [
;; if it is owned by someone else, move to a new patch that is not owned
let target one-of patches in-radius 10 with [ owned_by = nobody ]
if target != nobody [
move-to target
]
]
;; Now add the current patch into my homerange
ask patch-here [
set owned_by myself
]
set my_homerange patches with [ owned_by = myself ]
;; calculate the number of patches currently in my homerange
set homerange_food count patches with [owned_by = myself]
;; Now grow the homerange until there are enough patches in the homerange
;; to fulfill the "food_required" variable
while [ homerange_food < food_required ] [
let expander one-of my_homerange with [ any? neighbors with [ owned_by = nobody ] ]
if expander != nobody [
ask expander [
let expand_to one-of neighbors4 with [ owned_by = nobody ]
if expand_to != nobody[
ask expand_to [
set owned_by [owned_by] of myself
]
]
]
]
;; Reassess homerange food worth
set my_homerange patches with [ owned_by = myself ]
set homerange_food count patches with [owned_by = myself]
]
ask my_homerange [
set pcolor [color] of myself - 2
]
;; Now that my homerange has been defined, I will hatch a new turtle
hatch 1 [
set color ([color] of myself + random 4 - 2)
]
]
end