Allot Different Random Name to Turtles in Netlogo - random

I have created 10 turtles in netlogo using
create-ordered-turtles 10
[
let b median (list -17 (random-normal 0 5) 16)
setxy b -12
set size 3 ;
set color black
]
Now I want to assign each turtle a random and different name using a random function like this
one-of [ "Name1" "Name2" "Name3" "Name4" "Name5" "Name6" "Name7" "Name8" "Name9" "Name10"]
Which means that all 10 turtles will have names from Name1 to Name10. But no two turtles will have same name.
Also I want to assign each turtle a different integer amount.
Can anyone please guide me how can I do it. I will be really thankful. Thanks

If we stick to your problem description, the simplest answer is the following
turtles-own [name]
to setup
ca
let names [ "Name1" "Name2" "Name3" "Name4" "Name5"
"Name6" "Name7" "Name8" "Name9" "Name10"]
create-ordered-turtles 10
[
let b median (list -17 (random-normal 0 5) 16)
setxy b -12
set size 3 ;
set color black
set name item who names
]
end
You can assign the "integer amount" in the same way. If you want an approach that is not so specialized to your problem description, you could create a name-random-turtles procedure and call it with the list of names at the end our your setup. For example,
to name-random-turtles [#names]
let _n length #names
let _turtles [self] of (n-of _n turtles) ;list of turtles, in random order
foreach n-values _n [?] [
let _turtle item ? _turtles
let _name item ? #names
ask _turtle [set name _name]
]
end

Related

Calculate turtle variable for different patch types

I'm trying to calculate a turtle variable (district-winner) for every value of a certain patch variable (district). Patch district ranges from 0 to 100. The patches are also endowed with a feature votes. The turtle (party) receives a patches' votes if it is the closest turtle to that patch. For every patch district I wish to indicate the turtle that receives most votes in that particular patch district, and stall the information in some variable (e.g. district-vote or district-winner). That is where I got stuck.
I have tried working with foreach and sort-by, but so far I haven't managed to create a code without error that accounts for every different district value. The code for assigning votes to some closest turtle works (see code). But I haven't figured out how to calculate the votes won by turtles per district for each district.
How to create a turtle variable that is conditionalised on some patch variable value?
Working code, relevant lines:
to update-support
ask patches [set closest-party min-one-of parties [distance myself]]
;;patches find their closest party
ask parties [set mysize sum [votes] of patches with [closest-party = myself]]
;;each party sums the votes on patches for which it is the closest party
end
Some attempt to run the code for different patch districts:
to update support
ask patches [
set closest-party min-one-of parties [distance myself]
;;patches find their closest party
set closest-party-list [ (list closest-party) ] of patches
(foreach district-number
[set district-vote-map map [ifelse-value (? != district) [? = district] [?]] closest-
party-list])
;;and then link this closest-party-list to some code for asking parties??
]
]
end
Another attempt
to update support
ask patches [
set closest-party min-one-of parties [distance myself]]
;;patches find their closest party
ask parties [
set district-vote [
(foreach [(district) of patches] sum [votes] of patches with [closest-party = myself] and
[district = [?]])]
end
I answered in google groups. The routine "Tally" does relevant calculations and can easily be tweaked to cope with ties if you want to. I didn't even try to compress this into one long command line. I added party names. Here's the tally section, the rest is in netlogo-users group. I didn't try to make it polished but it seems to work.
By doing things the long way I never needed to use an anonymous block so I think it should run in any version of NetLogo back to and including 5.
;;========================== Wade added 6/17/2022
to tally
;; to make party-names equal to party-number so my code fits your code
set party-names []
ask parties [ set name self set party-names lput name party-names]
set party-names sort party-names
print (word "Number of PATCHes: " ( count patches ) )
print (word "Number of VOTES: " ( sum [votes] of patches ) )
print " "
ask parties [ print (word name " got this many PATCHES: " (count patches with [closest-party = [name] of myself ])) ]
print " "
ask parties
[
print
(word
name " got this many VOTES: "
(sum [votes] of patches with [closest-party = [name] of myself ])
)
]
;; generate a list of districts in use
let district-list ( remove-duplicates ( sort [district] of patches ) )
print (word "list of districts in use: " district-list )
;;===================================================================================
let working-dislist district-list
let dist 0
let dist-count 0
let total-vote 0
while [ length working-dislist > 0 ][
set dist first working-dislist
set dist-count sum [votes] of patches with [ district = dist ]
print (word " for district: " dist " this many votes: " dist-count)
set total-vote total-vote + dist-count
set working-dislist butfirst working-dislist
]
print ( word "========================\nTotal votes by district: " total-vote )
print " now analyzing by district and party "
set working-dislist district-list
let working-party-list party-names
let pname " unkown "
set dist 0
set dist-count 0
set total-vote 0
let dp-sum 0
while [ is-list? working-dislist and length working-dislist > 0 ][
set dist first working-dislist
set dist-count sum [votes] of patches with [ district = dist ]
print (word " ------> for district: " dist " this many votes: " dist-count " broken out below" )
set working-party-list party-names
set dp-sum 0
let district-winner ""
let winning-votes -99
while [ is-list? working-party-list and length working-party-list > 0 ][
set pname first working-party-list
set working-party-list butfirst working-party-list
let details sum [votes] of patches with [ district = dist and closest-party = pname ]
set dp-sum (dp-sum + details)
print ( word ".....in district " dist " party " pname ": has this many votes: " details )
if ( details > winning-votes ) [
set winning-votes details
set district-winner pname
]
]
print (word ".................... subtotal: " dp-sum " District Winner with " winning-votes " is " district-winner "\n")
set dp-sum 0
set working-party-list party-names
set total-vote total-vote + dist-count
set working-dislist butfirst working-dislist
]
print ( word "======== \nTotal votes by party by district: " total-vote )
;;===================================================================================
end
The output produces totals at increasing levels of granularity ending with what I THINK you want. You still need to break ties but that's easy to do with this code.
------> for district: 49 this many votes: 12801.954344857973 broken
out below .....in district 49 party (party 0): has this many votes:
302.9649675865895 .....in district 49 party (party 1): has this many votes: 1457.5052709454735 .....in district 49 party (party 2): has
this many votes: 0 .....in district 49 party (party 3): has this many
votes: 830.4743848217157 .....in district 49 party (party 4): has this
many votes: 1393.6496323186223 .....in district 49 party (party 5):
has this many votes: 4418.5271517613455 .....in district 49 party
(party 6): has this many votes: 147.01914867815847 .....in district 49
party (party 7): has this many votes: 3005.0957136873785 .....in
district 49 party (party 8): has this many votes: 976.2462740954787
.....in district 49 party (party 9): has this many votes:
270.4718009632105 .................... subtotal: 12801.954344857973 District Winner with 4418.5271517613455 is (party 5)

NetLogo get each time different random number from certain list

let's say I have list
let mylist [0 1 2 3]
and I would like to generate random number from this array which is in every tick different than previous one.
Example: Tick one - generates 0
Tick two - generates 2
Tick three - generates 1
Tick four - generates 3
Now I have
let mylist [0 1 2 3]
let x one-of mylist
But that returns for example for two consecutive ticks number 0.
Any tips? Thank you.
One way is to store the number that was used in the last tick, compare it to the one chosen in the current tick, and choose a different one if they are the same.
globals [
previous_number
]
to generate
let current_number previous_number
let mylist [ 0 1 2 ]
while [ current_number = previous_number ] [
set current_number one-of mylist
]
set previous_number current_number
print current_number
end

shift position in matrix

I have a n x 1 matrix. I'm trying to find a way to "shift" all the elements position (loosing the last element) and then add an element in position 0,0 in this way:
From
[[ 10 ]
[ 5 ]
[ 2 ]
[ 3 ]
[ 1 ]
[ 5 ]]
to (adding a new element 2 in position 0,0)
[[ 2 ]
[ 10 ]
[ 5 ]
[ 2 ]
[ 3 ]
[ 1 ]]
I'm pretty close to the solution but I don't know how add elements to a nested list.
;initial matrix
set mymatrix matrix:from-column-list [[10 5 2 3 1 5]]
;temp
let list matrix:to-column-list mymatrix
let tmplist matrix:to-column-list states
; ERROR here: the result of fput is [2[10 5 2 3 1 5]]
set tmplist fput 2 tmplist
;new matrix
matrix:set-column mymatrix 0 tmplist
EDIT: I realized that indeed for my needs a matrix is an overkill. I solved switching to pure netlogo lists and doing my business in map-reduce.
If you just need elementwise multiplication of objects that are fundamentally one-dimensional, just use map.
Example: (map * [1 2] [3 4]) reports [3 8].
Now you can just do your bookkeeping with lists, which is much easier.
Even if (for some reason you have not stated) you really need matrix operations elsewhere, you almost surely should use lists for the bookkeeping you describe and then convert when necessary.

setting variable in netlogo ifelse function and making turtle to wait in the patch

I have the following in netlogo
ask m-depts [ ; this check the values of turtles against some set numbers
ifelse
(m- k >= 5) and (m-t >= 3) and (m-c >= 3 ) and (m-s >= 4) and (m-b >= 3) and (m-d >= 0) [
move-to one-of patches with [pcolor = yellow]
] [
ifelse
(m-k >= 5) and (m-t >= 5) and (m-c >= 5 ) and (m-s >= 5) and (m-b >= 3) and (m-d >= 1) [
move-to one-of patches with [pcolor = green]
] [
action-m-depts
] ]]]
]
1st I want to add some conditions (set ……… ) if the turtle will move to yellow and also some other condition if it will move to green eg
If pcolor = yellow [
set m-k m-k + 0.5
set m-t m-t + 1
] ;
If pcolor = green [
set m-k m-k + 0.8
set m-t m-t + 2
] ; etc otherwise do action-m-depts (defined elsewhere)
2nd For the next move (ticks) I want to fix the turtle to wait at the patch eg (for yellow patch, wait for 5 years (5 ticks) and for green patch wait for 2 year (2 ticks). How should I incorporate the two issues in this model?
The code you have for the first part looks good to me. Have you tried it? I say go for it!
The second part is very similar to netlogo: how to make turtles stop for a set number of ticks then continue and Making turtles wait x number of ticks — check out those answers.

NetLogo: assign matrix values to patches

Let's say I wanted to assign the values of a 4 x 5 matrix to patches such that
patch 1 1 [x] = matrix 1,1
patch 1 2 [x] = matrix 1,2
..
patch 4,5 [x] = matrix 4,5
is there a way to do this in NetLogo?
This depends on how you're representing the matrix, but in general doing something like
ask patches [ set x matrix pxcor pycor ]
should do the trick (assuming x is a patch variable and matrix is a reporter that gets a value from the matrix).

Resources