How can I define something similar to a structure in NetLogo? - list

I defined a lot of "object" like this:
Each of them has properties, like color, size, position ecc.
How can I store them in a list and assign to them a unique identifier?
I make them as follow:
let tmploop 0
while [tmploop < 20] [
makecell tmploop
set tmploop tmploop + 1
]
to makecell [ n ]
let head random 360
let jum random 20
create-turtles n
[ set shape "circle"
set color green
set size 2
set heading head
jump jum]
create-turtles n
[ set shape "circle"
set color red
set size 1.33
set heading head
jump jum]
create-turtles n
[ set shape "circle"
set color gray
set size 0.66
set heading head
jump jum]
end
I would like to refer to each ring of the same cell, for example.
Should I use something like breed or do you have other ideas?

Related

Netlogo working with specific items using nested lists

All
I'm trying to simulate a 100 users (breed) each of them having users-own attributes which are tracked over various two runs of ticks say 0 & 1. I store these values as a list within a list and sorted them. They appear like this [[tick 0, user 2, attrib 1 ... attrib 9] [tick 1, user 2, attrib 1 ... attrib 9] [tick 0, user 3, attrib 1 ... attrib 9] [tick 1, user 3, attrib 1 ... attrib 9] ... [tick 0, user 99, attrib 1 ... attrib 9] [tick 1, user 99, attrib 1 ... attrib 9] ]
As I randomly 'ask users' to reassign certain attributes, how do I get the subset of matching values that matches the current user referred by 'who'? for example if the current user in context is 'user 3', how do I get the sublist of the matching entries from the list of lists?
Once I obtain the sublist matching 'user 3'[tick 0, user 3, attrib 1 ... attrib 9] [tick 1, user 3, attrib 1 ... attrib 9], how do I get the index of each line & the items within that list? for example if I need to access the last item on the sublist (corresponding to tick 1) so that I can obtain the value of attrib 9 to process it further?
Thanks in advance for your help!
if your list is structured: [ [ TICK# TURTLE# ATT1 ... ATT9 ] ... ]
[
[ 0 0 1 .. 9 ]
[ 0 1 1 .. 9 ]
[ 0 2 1 .. 9 ]
...
[ 0 99 1 .. 9 ]
[ 1 0 1 .. 9 ]
[ 1 1 1 .. 0 ]
... and so on ...
And the number of turtles is always CONSTANT
Then you can calculate the index of the item you need.
;; attribute data is in list "data"
LET total-turtles COUNT TURTLES
LET desired-tick 1 ;; number of the tick
LET desired-turtle 7 ;; WHO of turtle
LET desired-attribute 4
;; calculate the sublist index
LET sublist-index desired-tick * total-turtles + desired-turtle
;; get the sublist
LET attribute-sublist ITEM sublist-index data
;; get the value from the sublist
LET attribute-value ITEM (desired-attribute + 2) attribute-sublist
If you set desired-turtle to [WHO] OF ONE-OF TURTLES you are working with a random turtle. Or you can ASK a random turtle to do the above, and it can just use its own WHO.
If you add the info to the list strictly in order, then you don't need to store the tick# or who#, and you don't need to sort it.
If you let the turtle store its own list of prior attributes, it might be even easier to store and access, depending on your application.
Now you have the pieces needed for a random turtle to access the history list and grab the values from its own history, or some other turtle's history.

A list containing NOBODY as one of its entities

I have a sub-routing in my code where each patch is asked to pick its closest & farthest turtle based on certain conditions. I keep getting this error after a couple of ticks
OF expected input to be a turtle agentset or turtle but got NOBODY instead.
error while patch 0 30 running OF
called by procedure UPDATE-SUPPORT
called by procedure GO
called by Button 'Go'
There are two other routines where a turtle dies or is born depending on a few other metrics that are measured. I am not able to debug the code but what i have figured so far is that it happens after a turtle dies or is born.
Below is the code based on which the closest & farthest turtles are assigned at each tick.
to update-support
ask patches [
let old-total sum [my-old-size] of parties
set f-party []
set h-party []
set party-list (sort parties)
set voteshare-list n-values length(party-list) [ (([my-old-size] of party ? ) + 1 ) / ( old-total + 1 ) ]
set party-citizen-dist n-values length(party-list) [ ( distance party ? ) ^ 2 ]
set f-list n-values length(party-list) [ ( ( 1 / ( item ? voteshare-list ) ) * ( item ? party-citizen-dist ) ) ]
set f-index position (min f-list) f-list
set h-list n-values length(party-list) [ ( ( item ? voteshare-list ) * ( item ? party-citizen-dist ) ) ]
set h-index position (max h-list) h-list
set f ((-1) * (min f-list))
set h max h-list
set f-party lput item f-index party-list f-party
set h-party lput item h-index party-list h-party
set closest-party first f-party
set farthest-party first h-party
]
After a turtle dies, when I inspected the patch which was throwing the error, i found the word nobody as an element in the list. The error is highlighted to be in the Party ? section while creating the voteshare-list in the above code
When I inspected the patch throwing the error, Party-list which is the list with all the current parties sorted was showing this:
Party-list: [(party 0) nobody (party 2)]
and my f-party list just had [(nobody)]
Has anyone faced such a situation.?
Below is the death & birth routine:
to party-death
ask parties [if (fitness < survival-threshold and count parties > 2)
[ die
] update-support
]
to party-birth
ifelse (endogenous-birth? = true)
[ ask one-of patches with [distancexy 0 0 < 30]
[ if (random-float 1 < (kpi * 1000)) [sprout-parties 1 [initialize-party] ]]
[ create-parties 1 [set heading random-float 360 jump random-float 30 initialize-party] ]
update-support
end

Netlogo list in list iterations with counter

I have read from csv a list of lists named fileList [[id, id2, id3],[10,10,11]]
But i have problem that I want to iterate trough the list and in every iteration create a turtle that contain id1, id3 (not Id2) as variables. My idea in python syntax (I need help to transpose it to NetLogo):
for x, list in enumerate(fileList):
if x==0: #first list is names so I transpose the names to places in
index_id=list.index(id)
index_id3=list.index(id3)
else:
create-turtle_nr1 #not in python syntax but the idea is to create turte to assign variables from list below
ask turtle_nr1 [set id1 item (item as list[index_id])]
Overall output is three turtles with variables id and id3.
In this case, you should just be able use item to index your lists iteratively. Essentially, for each turtle you want it to index the appropriate list-of-variables from the list of lists and then index the appropriate variable from that list. You could start with something like:
turtles-own [
id
id2
id3
]
to list-of-lists
;;; these lists are just placeholders, of course, use your real list of lists
;;; as the "ids_list" variable in this case
let id1list [ 1 2 3]
let id2list [ 44 55 66 ]
let id3list [ "a" "b" "c" ]
let ids_list ( list id1list id2list id3list )
let n 0
while [ n < 3 ] [ ;;; or however many turtles you end up wanting,
;;; as long as you have list variables for them
create-turtles 1 [
set id item n (item 0 ids_list)
set id3 item n (item 2 ids_list)
]
set n n + 1
]
end
This procedure creates three turtles with ids of 1, 2, and 3, id2s of 0, and id3s of a, b, and c.

NETLOGO : Give command to ONLY turtles which variable fulfill the condition - after "foreach"

users,
I am trying to give command for turtles who have various variable. My agent called consumers. Each "consumers" has different need which represent by its color ( I use 14 color-base). When the number of consumers with particular color has reached > 10, I want them to change color into white. I use code below, resulting all of consumers change color into white. Though I only need "consumers" which satisfy the condition to change into white.
to cocreate-value
let a count consumers with [ color = blue]
let b count consumers with [ color = gray]
let c count consumers with [ color = red]
let d count consumers with [ color = orange]
let e' count consumers with [ color = brown]
let f count consumers with [ color = yellow]
let g count consumers with [ color = green]
let h count consumers with [ color = lime]
let i count consumers with [ color = turquoise]
let j count consumers with [ color = cyan]
let k count consumers with [ color = sky]
let l count consumers with [ color = violet]
let m count consumers with [ color = magenta]
let n count consumers with [ color = pink]
ask consumers [set type-of-need ( list a b c d e' f g h i j k l m n ) ]
foreach type-of-need [
if ? > 10 [
let z consumers with [ ? > 10]
ask z [ set color white ]
ask consumers with [color = white] [set need? false]
]]
end
Can someone show me the solution?
Thank you
Your problem is that your list is composed of numbers (the counts for each type of consumer) rather than the type-of-need themselves. You could see this by doing a print type-of-need after you create it. Let's say it looks like [5 12 4 ...]. Then you loop through this list of numbers and eventually get to one that is larger than 10. In the example, you now have ? = 12. Then the condition for creating the agentset z will be true for all consumers.
The following code is untested, but you need to list the types rather than the counts. Try something like this:
to cocreate-value
let a count consumers with [ color = blue]
let b count consumers with [ color = gray]
ask consumers [set type-of-need ( list blue gray ) ]
foreach type-of-need [
if count consumers with [ color = ? ] > 10 [
let z consumers with [ color = ? ]
ask z [ set color white ]
ask consumers with [color = white] [set need? false]
]]
end

How to make turtles move along a list of locations in order

My objective is to have an agentset (named ships) hatch at another agentset (named ports and listed in index) containing 2 locations representing the start and end of a pathway. To go- start moving ships/ heading towards a third agentset (called waypoints and listed in index1) in their given order.
However, if a port is closer to the current waypoint than another waypoint- move to port instead. Once the ships have reached the other port, I would also like them to stop.
Currently I have the model working with only two agentsets (ships and ports) however I would like to include a third set(called waypoints) to prevent ships from hatching at all locations(ports and waypoints) and to have the ships move in a sequential order by traveling along the waypoints (like stepping stones) before reaching the beginning or the end (ports).
Here is an example of my code:
breed [ships ship]
breed [ports port]
breed [waypoints waypoint]
ships-own [target-port
current-port]
to setup
ca
let index 0
create-ports 2
[ let loc item index [ [0 -32] [32 0] ]
setxy (item 0 loc) (item 1 loc)
set index index + 1
set shape "circle"
set size 2
set color red - 1]
let index1 0
create-waypoints 2
[let loc item index1 [[12 -3] [14 -26]]
setxy (item 0 loc) (item 1 loc)
set index1 index1 + 1
set shape "circle"
set size 1
set color red - 1]
ask ports
[ let s who
hatch-ships 1
[ set current-port s
set size 1
set color red
pen-down
set pen-size 1
set target-port min-one-of ports with [ who != s] [distance myself]
set heading towards target-port
]
]
reset-ticks
end
to go
;(obey-elevation)
ask ships
[ if (distance target-port = 0)
[ let other_ports no-turtles
ask target-port [set other_ports (other ports)]
set target-port min-one-of other_ports [distance myself]
face target-port
]
ifelse (distance target-port < 1)
[ move-to target-port
]
[fd 1
]
]
tick
end
Any help would be greatly appreciated.