How do I create a randomly distributed boolean variable for a <breed> that will change in the model? - variables

I am writing a model with two breeds:
sexworkers and officers
where sexworkers possess a boolean variable that is randomly distributed at the setup, but then changes at the go according to the behavior of and interaction with officers.
I use sexworkers-own [ trust? ]
in the preamble, but then I am not sure how to distribute y/n of the variable randomly across the sexworkers population. Really appreciate any input!
Thank you so much!

If I understand your question correctly, you're just wanting sexworkers to randomly choose between true and false for the trust? variable on setup. If that's right, then maybe one-of will do the trick for you- for an example, run this simple setup:
breed [ sexworkers sexworker ]
sexworkers-own [ trust? ]
to setup
ca
create-sexworkers 1000 [
set trust? one-of [ true false ]
]
print word "% Trusting: " ( ( count sexworkers with [ trust? ] ) /
count sexworkers * 100 )
reset-ticks
end
If you're looking for some kind of uneven distribution you can do simple ones using the random or random-float primitives. For example, if I want 25% of the sexworkers to start with trust? = true, I can do something like:
to setup-2
ca
create-sexworkers 1000 [
ifelse random-float 1 < 0.25 [
set trust? true
] [
set trust? false
]
]
print word "% Trusting: " ( ( count sexworkers with [ trust? ] ) /
count sexworkers * 100 )
reset-ticks
end
For specific distributions, have a look at the various random reporters
For weighted randomness, have a look at the rnd extension

Related

HAC: Children in a dendrogram

I ran the HAC on a dataset containing 10 samples as follows:
X_train
>>array([[ 0.97699105, 0.22532681],
[-0.73247801, 0.60953553],
[-0.99434933, 0.03124842],
[-0.82325963, 0.57988328],
[ 0.50084964, -0.26616097],
[ 1.94969804, 0.42602413],
[ 1.0254459 , -0.54057545],
[-0.57115945, 0.8495053 ],
[ 1.39201222, -0.34835877],
[ 0.02372729, 0.52339387]])
Here is the result I get by applying HAC using the scipy library:
linkage(X_train, method='single')
>>array([[ 1. , 3. , 0.09550162, 2. ],
[ 7. , 10. , 0.2891525 , 3. ],
[ 6. , 8. , 0.41390592, 2. ],
[ 2. , 11. , 0.57469287, 4. ],
[ 4. , 12. , 0.59203425, 3. ],
[ 9. , 13. , 0.67840909, 5. ],
[ 0. , 14. , 0.6843032 , 4. ],
[15. , 16. , 0.92251969, 9. ],
[ 5. , 17. , 0.95429679, 10. ]])
Here is the resulting dendrogram
dendrogram(linkage(X_train, method='single'), labels=np.arange(X_train.shape[0]))
In the output matrix of the linkage(X_train, method='single'), the first two columns represent the children in our hierarchy.
I would like to know how we do to calculate these children?
For example :
the first fusion of our algorithm involves singleton clusters containing points {1} and {3}. And as children we have [1, 3]
The second merge involves the previously calculated cluster containing the points {1, 3} and the singleton cluster {7}. And like children we have [7, 10]. How was the value 10 obtained?
According to the docs, at the i-th iteration, clusters with indices Z[i, 0] and Z[i, 1] are combined to form cluster n+i, where n is the number of input samples and Z is the linkage matrix. https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.hierarchy.linkage.html.
Thus 10 is just is just 10+0 where 10 is total number of points and 0 is the row where the cluster is combined.
In other words, all cluster indices i>=n actually refer to the cluster formed in Z[i - n].
If that's still unclear you can read the detailed description here https://joernhees.de/blog/2015/08/26/scipy-hierarchical-clustering-and-dendrogram-tutorial/#Perform-the-Hierarchical-Clustering

NetLogo 3D: Printing multiple turtle variables to output at end of run and running through BehaviorSpace

I'm trying to find an efficient method of outputting a large number of turtle variables (20+) from a random selection of 100 turtles into an output field or text file. So far I have:
turtles-own [
variable1
variable2
variable3
variable4
.
.
.
]
to go
if (ticks < 1) [reset-timer]
ticks
if count turtles >= end-population [
ask n-of 100 turtles [
output-show variable1
output-show variable2
output-show variable3
output-show variable4
]
I then get a list of the variables of each cell in a single column:
1
My question is how can I get these variable values on the same line so of the output or a text file so I can easily work with these data? Furthermore, how would I implement this in BehaviorSpace? Using this same command:
ask n-of 100 turtles [
output-show variable1
output-show variable2
output-show variable3
output-show variable4
]
... in the final commands field does not result in any of these data showing up in the output file.
Thanks!
There are a variety of ways to do this- for example, the csv extension is great candidate if you want to manually output your values. If you want to do this quickly in BehaviorSpace, here's how I usually go about it.
I'm assuming you want the variable values for the same 100 turtles each time, rather than sampling a new 100 turtles for each variable. So, I think the easiest way is to just make a globals variable for each of the variables of interest, then make a procedure to fill those lists as needed. For example, with this setup:
globals [ a-final b-final c-final ]
turtles-own [ a b c ]
to setup
ca
crt 100
reset-ticks
end
to go
ask turtles [
set a random 100
set b one-of [ "Beep" "Boop" ]
set c precision random-float 10 2
]
end
Each tick the turtles are just randomly updating their a, b, and c variables for the sake of this toy version. Then, you have a procedure that subsamples some number of turtles (here, 10) from your total population and updates the storage lists:
to output
let selected-turtles n-of 10 turtles
set a-final [a] of selected-turtles
set b-final [b] of selected-turtles
set c-final [c] of selected-turtles
end
Now as long as that output runs right before your BehaviorSpace experiment ends, you can output those lists as a string, which you can easily separate and clean with R or similar software. For example, if you have a setup like:
You will get output that looks something like:

Dynamic turtle creation in netlogo 2 [contd..]

In the interface tab i have a slider whose value ranges between 2 & 10. Depending on the value defined by the user using this slider, that many number of turtles should be created.
I tried using multiple if statements but there is a problem in the succeeding steps.
if (slider-value = 2) [create2]
if (slider-value = 3) [create3]
if (slider-value = 4) [create4]
if (slider-value = 5) [create5]
After creating the turtles using the above if conditions, i have to assign additional rules to each individual turtle like co-ordinate position, rules for how they should move etc. and i tried again using multiple if statements. But it doesn't seem to work.
for example the sub-query for create is as follows
to create2
create-challengers 2
ask turtle 0 [set color blue set label-color blue set size 2
set xcor party1-left-right ]
ask turtle 1 [set color red set label-color red set size 2
set xcor party2-left-right ]
ask turtles [ update-rule set old-mysize 0 set shape "default"]
end
for creating 3 turtles:
to create3
create-challengers 3
ask turtle 0 [set color blue set label-color blue set size 2
set xcor party1-left-right ]
ask turtle 1 [set color red set label-color red set size 2
set xcor party2-left-right ]
ask turtle 2 [set color green set label-color green set size 2
set xcor party3-left-right ]
ask turtles [ update-rule set old-mysize 0 set shape "default"]
end
so on and so forth.
The main problem is even though program works irrespective of how many turtles the user has defined, all the 10 gets created but only user defined number of turtles move, i.e. if the user has assined 3 then when i run the program 10 turtles are created but only 3 move.
Is there a way to get around like in other programming languages where one can simply use an if-else statement.
Can someone suggest a way, would really appreciate the help.
Thanks in advance!
After the turtles are created i assign certain rules for them to move:
to update-rule
if (slider-values = 2) [update2]
if (slider-values = 3) [update3]
if (slider-values = 4) [update4]
if (slider-values = 5) [update5]
end
And once again i create multiple sub-rule for update2, update3 which are basically for the each turtle depending on how many the user has defined:
If there are 2 turtles:
to update2
ask turtle 0 [set my-rule party1-rule]
ask turtle 1 [set my-rule party2-rule]
end
in case of 3 turtle:
to update3
ask turtle 0 [set my-rule party1-rule]
ask turtle 1 [set my-rule party2-rule]
ask turtle 2 [set my-rule party3-rule]
end
Below are the movement rules
to adapt
if (my-rule = "hunter") [hunt]
;;NB stickers do nothing
if (my-rule = "aggregator") [aggregate]
end
to hunt
ifelse (mysize > old-mysize) [jump 1] [set heading heading + 90 + random-float 180 jump 1]
set old-mysize mysize
end
to run-general
create2 create3 create4 create5 create-voters update-support
ask challengers [adapt] update-support
end
to go
run-general
tick
end
As noted by Seth, the problem is likely to be in the move part of the code since the turtles are being created. Just as general comments, it looks like your code for creating 3 turtles is identical to that for creating 2 turtles except for the additional turtle. If this is generally true, your code would be much easier if you used a format like:
to setup-challengers
create-challengers 1 [set color blue set label-color blue set size 2
set xcor party1-left-right ]
create-challengers 1 [set color red set label-color red set size 2
set xcor party2-left-right ]
if slider-value >= 3
[ create-challengers 1 [ set color green set label-color green set size 2
set xcor party3-left-right ] ]
if slider-value >= 4
[ create-challengers 1 [ <whatever you want number 4 to look like> ] ]
ask challengers [<do stuff>]
end
This way you only need the code for each turtle to be written once so if you change your mind about colours or something, it is much easier to change.
Okay, try editing your code to look like this:
to adapt
type "Hunters: " print count turtles with [my-rule = "hunter"]
type "Aggregators: " print count turtles with [my-rule = "aggregator"]
if (my-rule = "hunter") [hunt]
if (my-rule = "aggregator") [aggregate]
end
This is a standard debugging trick. It will print the number of hunters and aggregators to the Command Center (bottom of the interface tab). This allows you to work out where code is going wrong. For example, in your case, it will let you know if the problem is with the movement code or with the code that assigns rules to turtles.
Another trick is to have something like print "Got to hunt procedure" if you are not sure whether a procedure is even being reached.
this if-else thing for a slider based setup is very nasty looking.
an alternative is to have a set of (global) lists for all properties that are set seperately for each individual.
then you can use the following code
;assuming global_list1 and global_list2 exist
to setup
let index 0
create-turtles slidervalue [
set turtle_value1 item index global_list1
set turtle_value2 item index global_list2
set index index + 1
]
end
then you only need to make sure that each turtles will know what to do when asked to do something
Geerten

change rgb dynamically in netlogo

So I'm writing a short programme in net logo where I want to color code my turtles based on a variable that they own view which varies between -1 and 1. I tried using color-scale in netlogo to define the colour, but it doesn't do quite what I want.
I wrote this to describe what I want, but netlogo seems to be getting confused when I pass the col variable to the set color command.
to colorise;;------------------------------------------------------------
; this changes the agent's colour based on their current [view] score.
; We could use the color-scale in netlogo, but it only works for one colour
; and it's easy to end up with a colour so dark we can't see it against black patches.
moderate ; resets any agents which have somehow ended up with a view score outside -1 to +1
ifelse view > 0
[ let col ( 1 - view )
set col col * 255
set color [ 255 col col ]
]
[ let col ( 1 + view )
set col col * 255
set color [ col col 255 ]
]
end
does anyone have any ideas?
Thanks!
Will
Assuming you have correctly limited the range of view, you will just run into a list creation problem: you cannot use the bracket notation with variables. Instead try
set color (list col col 255)
etc

trying to create a sequential ID variable for breeds in netlogo

There is probably a very simple solution to this, but I am having trouble coming up with it since I'm still pretty new to netlogo. I'd be very grateful for any suggestions. My model has multiple breeds of turtles with different numbers of turtles in each breed set by sliders. To make it easier to set some breeds-own variables later in the model, I want to create separate sequential ID variables for each breed. These would basically be like who numbers, but would run from 0 to n for each breed. So, let's say I create male and female breeds like this:
breed [males male]
breed [females female]
to setup
clear-all
create-breeds
set-id
ask turtles [setxy random-xcor random-ycor ]
reset-ticks
end
to create-breeds
create-males number-males [set color yellow]
ask males [set shape "circle"]
create-females number-females [set color yellow]
ask females [set shape "circle 2"]
end
to set-id
...
end
I then want to run a "set-id" procedure that will create a male id var that will number the male turtles from 0 to male n, and another female id var that will number the female turtles from 0 to female n. I am assuming the solution should include some combination of foreach or n-values, but I'm having a lot of trouble nailing it down exactly. Thanks!
Don't do it!
I'm pretty sure it won't "make it easier to set some breeds-own variables later in the model". Using who numbers is error prone enough as it is without adding another separate index on top of it. (You shouldn't use who numbers either: there is almost always a better way to do things.) I'd suggest you ask another, separate, question and tell us why you think you need that and what you plan to do with it. I'm confident that someone will be able to suggest an alternative approach.
That being said...
You are right that a combination of foreach and n-values would do the trick.
Assuming you have:
males-own [ id ]
females-own [ id ]
You can use the following procedure:
to set-id-for [ breed-agents ]
(foreach (sort breed-agents) (n-values count breed-agents [?]) [
ask ?1 [ set id ?2 ]
])
end
By using parenthesis around foreach, you can pass it as many lists as you like: it will "zip" them together and give you variables like ?1, ?2, etc., to address elements of each list. In our case, the first list is the agents and the second list is indices generated with n-values.
Call the procedure once for each breed:
set-id-for males
set-id-for females
Supposing you have created 10 males and called set-id-for males, you can verify that it worked using the command center:
observer> show sort [id] of males
observer: [0 1 2 3 4 5 6 7 8 9]
But please...
...don't do it.
You can do it if you create a global variable and you use it inside of create male/female procedures:
globals [id]
breed [males male]
breed [females female]
males-own [id-male]
females-own [id-female]
to setup
clear-all
create-breeds
ask turtles [setxy random-xcor random-ycor ]
reset-ticks
end
to create-breeds
set id 0
create-males number-males
[
set id-male id
set color yellow
set shape "circle"
set id id + 1
]
set id 0
create-females number-females
[
set id-female id
set color yellow
set shape "circle 2"
set id id + 1
]
end
And you can include the "set shape" inside of create procedure.