NetLogo update list when a new agent is born - list

I am barely starting to use netlogo, and I created a model in which my agents have a list referencing a unique value for all (so its length is equal to the number of agents present at time t and the item 1 in the list corresponds to the value of turtle 1). I don’t manage to update the list when a new agent is born. How can I do that?
Regards

It sounds like you want something like this:
turtles-own [listOfTurtleVals val]
to init-turtle
set val random-float 1 ;just for illustration
set listOfTurtleVals ([val] of other turtles)
end
Then just run init-turtle on each turtle you create.
Alteratively you might have meant this:
globals [listOfInitialVals]
turtles-own [val]
to init-turtle
set val random-float 1 ;just for illustration
set listOfTurtleVals (lput val listOfInitialVals)
end

Related

finding member in nested lists netlogo

i'm trying to solve a problem in netlogo that has me stuck for a while now. i've got two lists (of turtles i've collaborated with and of "successful"/"unsuccessful" judgments). the two lists are mapped like so [[(turtle 10) "successful"] [(turtle 11) "unsuccessful"] with the following:
let general-history (map list collaborators my-success)
where the collaborators are the who numbers and my-success is a string (either "successful" or "unsuccessful")
now, i would like to check whether a turtle has, in its general-history list, at least one successful and one unsuccessful collaborator, to be able to proceed. this is where i've gotten to:
ifelse not empty? general-history and member? "successful " last general-history and member? "unsuccessful" last general-history
i know this is wrong because last here implies that i'll be looking only at the last list of general-history (i.e., [] [] [this one]). what i want it to do is assess whether there are at least two lists (one with "successful" as index 1 and one with "unsuccessful" as index 1) in the whole general-history nested list.
would foreach work better here or is it possible to still use member? but with some kind of element + list indexing? thank you very much for the help!
If I have understood your question correctly, you can use map to create a list of all the last items (success or not) and then apply member? to that list. Here is a complete model example that constructs some test data and the applies this approach.
to testme
clear-all
; create some test data
create-turtles 10
let collaborators sort n-of 3 turtles
let list-both (map list collaborators (list "yes" "no" "no"))
print list-both
; check condition
print member? "yes" map last list-both
end

NetLogo - Delaying the execution of certain commands based on ticks

Hello NetLogo community,
I am trying to ask agents named "users" to save certain value (string) of a variable for last two ticks (last two instances when "Go" command is executed). But, users have to store these values after first two ticks. Can anyone suggest me a way out? I have tried implementing the following logic but it does not seem to work.
ask users
[
set history-length-TM 2
if ticks > 2
[
set TM-history n-values history-length-TM [mode-taken]
foreach TM-history [x = "car"]
[
commands that are to be executed
.....
......
]
]
]
"history-length-TM" is the extent of ticks for which the values are to be stored. "TM-History" is the list to store the values of variable "mode-taken". Please advise a better method that could help me achieve the intent. Thanks in advance.
I am not sure I completely understand how ticks relates to this question. My suggestion would be something along these lines:
globals [history-length-TM]
users-own [TM-history]
to setup
set history-length-TM 2
...
end
ask users
....
set TM-history fput mode-taken TM-history
if length [TM-history] > history-length-TM [set TM-history but-last TM-history]
end
The idea is that the memory fills up (using fput) by placing the new mode-taken at the front of the list. Once the memory is too long, then the last (which is oldest) is dropped off the list.

Netlogo: delete list content

How do I delete the content of a list in Netlogo?
This is a tuned-down version of my code to function as an example:
to calculate_SN
ask turtles [
set subjective_norm_list []
set subjective_norm_list [1 2 3 4 5]
set subjective_norm ( sum subjective_norm_list / length subjective_norm_list)
*delete content of subjective_norm_list so that it is empty again*
end
The part between asterisks I don't know.
Based on your shared code so far, you should take a different approach: create a function.
to-report subjective-norm [#lst]
report (sum #lst) / (length #lst)
end
It is unclear that you will ever need to assign a variable name to your list. You may be able to use it upon creation and then forget about it. (It will be garbage collected.)
If you want subjective_norm_list to be an empty list, you can set it to an empty list, just like you did when you initialized it the first time around:
set subjective_norm_list []
Note that, technically, NetLogo lists are immutable, so you're not deleting the elements in the list: you're just creating a new list with no elements in it and assigning it to the same variable. But for all intents and purposes, it's the same: subjective_norm_list is empty again.

How to implement a numerical formula across the items in a netlogo list

I have to do some operations in netlogo using Lists. While i can do simple tasks with them i am not yet proficient enough to code my current requirements.
I have a scenario where turtles have variables called Current-Age and Previous-Age. And turtles can be born and die if they don't meet a certain threshold.
I want to implement the following formula for each patch.
Best-list = (-1/Previous-Age) * (Distance between Patch & Turtle) for all the turtles
Best = Min [ Best-list]
I know the steps involved but have been unsuccessful in coding them. Following are the steps:
Create a list with all the current turtles that are alive
Create a second list which contains the Previous-Age
Create a third list with the distance between an individual patch and each of the live turtles
Then create another list with the output from the the Best-List formula for all the turtles in the list
Finally find the Min value in the list and store the name/who# of turtle with the minimum value in a separate variable called Best-Turtle
This is the code that i tried but didn't work.
set turtle-list (list turtles)
set turtle-age-list n-values length(turtle-list) [0]
set turtle-patch-dist-list n-values length(turtle-list) [0]
set best-list n-values length(turtle-list) [0]
ask patches[
foreach turtle-list(
set turtle-age-list replace-item ?1 turtle-age-list Previous-Age of turtles with [turtle= ?1]
)
]
I couldn't proceed to the next steps since the above code itself was not correct.
Would appreciate help with the code, thanks in advance.
Regards
First, lists are probably not the simplest way to do this. However, if you must use lists for some reason, I think what you're asking for is possible. I'm not exactly sure what you mean with best- are you trying to have each patch assess which turtle is the best turtle for that patch, and store that variable in a global list? I'm going to assume that's what you mean, but if I'm misunderstanding I think you can adapt what I do here to what you need.
First, any list passed to foreach must be the same length. So, since you mean to do this per-patch, make sure that every patch calls the procedure of list creation, not just for checking the lists. Next, review the dictionary for n-values- the syntax for the reporter means you need to use the reporter you're trying to receive- using n-values length(turtle-list) [0] will just give you a list of zeroes that is the same length as the number of turtles.
So each patch needs to create these lists- make sure you either define the patches-own for the list variables, or just use let to define the lists inside the procedure. You would need a list of ordered turtles, their previous ages, and the distance from the patch calling the procedure to each turtle. Next, you can create a list that generates a value according to your formula. Then, you can use the position primitive to find the location of the minimum value in your formula-generated list and use that to index the turtle with that value.
It might look something like
to numerical
set best-turtle []
ask patches [
let turtle-list (sort turtles) ;;; list of sorted turtles
let turtle-prev-age-list n-values length(turtle-list) [ [i] -> [pre_age] of turtle i ] ;;; list of previous ages of turtles, in same order as above
let turtle-patch-dist n-values length(turtle-list) [ [i] -> distance turtle i ] ;;; list of distance from this patch to each turtle, in same order
set best-list n-values length(turtle-list) [ [i] -> ( ( -1 / ( item i turtle-prev-age-list ) ) * ( item i turtle-patch-dist ) ) ] ;;; list of calculated values for each turtle
let best-position position (min best-list) best-list ;;; gets the index of minimum value
set best-turtle lput item best-position turtle-list best-turtle ;;; adds the best turtle for this patch to the global list of best turtles
]
end
The above procedure assumes that your turtles have a pre_age variable, patches have a best-list variable, and the list of each patches 'best turtle' is held in the global variable best-turtle. From there, you can use foreach to ask turtles in the list to do something. Note that if a turtle's previous age is 0, you will get a divide by zero error.
turtles-own [age previous-age]
to-report evalfrom [_patch]
report (- distance _patch) / previous-age
end
to test
ca
crt 25 [
set age (10 + random 75)
set previous-age age - random 10
]
print min-one-of turtles [evalfrom (patch 0 0)]
end

Netlogo adding to list of lists

I am looking to add patch variable values to a list of empty lists. The patches are divided into different zones, and I'm trying to see how certain patch variables differ by zone.
I have an empty list of lists (actually contains 12 lists, but for simplicity):
set mylist [[] [] [] []]
And a list corresponding to the different zones:
set zone-list [1 2 3 4]
Here's how I'm trying to build the lists:
(foreach mylist zone-list [set ?1 lput (sum-zone-variable ?2) ?1])
to-report sum-zone-variable [ n ]
report (sum [patch-variable] of patches with [zone = n])
end
When I run this, mylist stays empty (ie unchanged). I think the problem is with the foreach statement, but I can't figure out what it is. Any help?
I can see the thinking behind foreach mylist [ set ?1 ... ], but NetLogo doesn't work that way. set ?1 ... has no effect on the original list. NetLogo lists are immutable, and ?1 is not a reference to an updatable location in a list — it's just a temporary variable into which a value has been copied. So set ?1 ... is something you will basically never write.
If I understand your question correctly, the relevant primitive here is map. This should do the job:
set mylist (map [lput (sum-zone-variable ?2) ?1] mylist zonelist)
Your basic approach is ok except that you must assign to a name. E.g.,
globals [mylist zone-list n-zones]
patches-own [zone zone-variable]
to setup
set n-zones 4
set zone-list n-values n-zones [?]
ask patches [set zone one-of zone-list]
set mylist n-values n-zones [[]]
end
to go
ask patches [set zone-variable random-float 1]
foreach zone-list [
let total sum [zone-variable] of patches with [zone = ?]
let oldvals item ? mylist
set mylist replace-item ? mylist (lput total oldvals)
]
end
However, you might want to use the table extension for this.