How to ask turtles with multi attributes in NetLogo - netlogo

How can I ask turtles with more than attributes to do something? These multi attributes should be all considered at the same time. Specifically, what I want to do is:
turtles-own [A B C D]
ask turtles with [A = 1 and B = 1 and C = 1]
[set color red]
Many thanks!

What you wrote should work perfectly. and allows you to connect multiple conditions together.

Related

Counting breeds within an agentset

If I have an agentset called zoo belonging to turtle 1, how do I count all the breed of camels in the zoo? NetLogo has a breed-on command, but not a breed-in. So this won't work, although it will illustrate what I'd like:
ask turtle 1 [let c count camels-in zoo]
Thanks.
Best guess is you want something like:
let c count ([zoo] of turtle 1) with [breed = camels]
But an example of a zoo turtleset would be very helpful in understanding what you are trying to do. Perhaps also what your turtles are representing?

Removing an agentset from another agentset (the agents from the first set which are also present in the second set)

in my netlogo code I have a network with companies (that is my breed). I want to ask the companies to share information with their neighbors and their neighbors and so on, this works (see code below, the agentsets are b, c and d).
However when I ask for information on the third level neighbors my agentset also includes the first level neighbors (obviously since it takes all neighbors into acount), so I want to remove these first level neighbors from the third level neighbors agentset. In the code this means I want to remove agents present in D which are also present in B
But I cant find the way to do it, other doesnt work since it is not the agent asking which has to be removed. And remove also doesnt seem to do the job. I also tried != not equal to the first level but this reports a true or false and I just want to remove these agents from the third level agentset so I dont double count them.
ask companies [
let i who
let b link-neighbors
ask b [ let c link-neighbors
ask c [ let d link-neighbors
ask companies with [who = i] [
set iburen [who] of b
set iiburen [who] of other c
set iiiburen [who] of d
]
]
]
]
can somebody help me with this?
I think what you want is the member? primitive. If D and B are agentsets, the following should give you the members of D that are not members of B.
let DminusB D with [not member? self B]
Many things to say here:
Charles' answer is technically correct.
If a and b are agentsets, a with [ not member? self b ] will give you agents from a that are not already in b.
But I think there are better ways to accomplish what you are trying to do. I will come back to that, but first, a general piece of advice:
Don't use who!
The who primitive has some (very few) legitimate usages, but it's mostly evil. It tends to lead to brittle, inefficient code. For example, when you do:
let i who
; ...
ask companies with [who = i] [ ... ]
NetLogo has to scan all companies to find the one with that specific who number.
NetLogo can store agent references directly. Use that instead! For example:
let this-company self
; ...
ask this-company [ ... ]
Especially don't use lists of who numbers!
NetLogo is adequate for manipulating lists, but its awesome for manipulating agentsets. If you do something like this:
set iburen [who] of b
set iiburen [who] of other c
set iiiburen [who] of d
You are forfeiting the power of agentsets. I don't know why you want to store the three different levels separately, but supposing it's OK to store all your neighbors together, you could do:
set my-neighbors other (turtle-set b c d)
The use of other will exclude the original company and turtle-set will make sure that each agent in the set is unique (as agentsets can only contain unique agents anyway).
If you really want three separate variables, use Charles' answer, but make sure to store agentsets, not lists of who numbers!
If you don't need separate variables, however, I think the best solution would be to:
Use nw:turtles-in-radius.
NetLogo's Networks extension has a primitive that does exactly what I think you want to do:
ask companies [ set my-neighbors nw:turtles-in-radius 3 ]
That's it.

Netlogo conditional statement with random variable and extra stochasticity

I am trying to add some additional stochasticity to an event occurring.
Employer breeds have their own probability of taking an action. r is a global random-float variable.
The following, whether in a turtle or observer context, does not provide the expected results:
ask employers [if (prob-action * 0.5 ) > r [set action True]..]
However, what I think is an inferior alternative works:
ask employers [if (prob-action >= 0.90 [set action True]..]
Thanks for any insights!

netlogo mutually exclusive agentsets simultaneously

I have 300 workers, and want to divide them into 3 sets of 100 (for now, eventually I will divide them perhaps unequally) based on two rankings. I have written variations to accomplish the following:
Select the top 100 on X, assign value 1 to variable A. Select the too 100 on Y (who have not ALREADY been assigned value 1 on A) and assign value 2 to variable A. Select remainder and assign value 3 to A
I have tried about a dozen approaches, without success. The closest I have come is to get the first 100. BUT, as there are ties, there are a few workers tied at the 100th-rank on X. Netlogo randomly selects one to receive value 1 on Variable A, which is fine. The problem is, because I haven't been able to figure out how to simultaneously construct three mutually exclusive agentsets using 2 criteria, I end up re-assigning workers from value 1 to value 2 on A. I can fix it by hand now, but eventually that won't be possible.
Here's the code that made the first value assignment:
ask max-n-of 100 workers [LPAdv] [set AWeight 1]
The problem seems to be that I cannot figure a way to reference the top 100 on Y of the remaining 200. Nor can I reference the bottom 100 of the remaining 200, because it is possible for a worker to be in the Top 100 on X but NOT in the Top 100 on Y, in which case they'll (wrongly) fall into the sequentially-identified agentset unless I delete them from the operation. I've tried using member? and man other possibilities. Basically, I don't know how to simultaneously dump the first 100 of 300 and only "sort" the remaining 200 to make the assignment.
I've looked at multiple sources for an answer, so far to no avail. I've made a lot of progress on my code since Saturday, but this is a stumbling block.
Any assistance is greatly appreciated!
Thanks!
SLuke
RESOLVED! (YAY!)
In case anyone else has the problem, what I did is the following:
ask max-n-of 100 workers [LPAdv] [set AWeight 1]
ask workers [ifelse AWeight = 1 [set LPMid2 -1000] [set LPMid2 LPMid]]
ask max-n-of 100 workers [LPMid2] [set MWeight 1]
ask workers [set PreWeight ( AWeight + MWeight )]
ask workers [if PreWeight = 0 [set RWeight 1]]
Note that LPMid2 sets the AWeight=1 to -1000, which is way out of range for the actual LPMid variable I later will need.
Sorry to bother the list; I hope the answer is of use to someone else should they have a similar problem.
here's a perhaps easier to read and more scaleable version (untested) of some of your code to hopefully give you some ideas about how to exclude some turtles when you are trying to select from a subset.
ask max-n-of 100 workers [LPAdv] [set AWeight 1]
ask max-n-of 100 workers with [AWeight != 1] [LPMid2] [set MWeight 2]
Also, you don't need two ask statements at the and (and asking twice is less efficient):
ask workers
[ set PreWeight ( AWeight + MWeight )
if PreWeight = 0 [set RWeight 1]
]

NetLogo: Finding out all turtles linked to each other and assigning them to a group id

I have searched all topics for the last week and haven't be able to yet find a solution to my problem. The scenario is as follows:
I have various groups of turtles that are linked to each other
I would like to assign a group name or id to each group
In order to do the above, I am thinking that I need a procedure that will find out the id of a turtle and then tell me to which other turtles this particular turtle is linked to. So for example I have a group of 4 turtles, all linked to each other. Their ids are 1, 2, 3, 4. Now I would like to get a list of each turtle, which also tells me to whom it is linked to. Here an example list:
Turtle 1 -> Linked to Turtle 2, 3 and 4
Turtle 2 -> Linked to Turtle 1, 3 and 4
...
Turtle 4 -> Linked to Turtle 1, 2 and 3
Once I now which this (although maybe there is a much easier way). I could assign a group id to these 4 turtles.
Any feedback / help very much appreciated.
Here is part of the code:
to go
ask turtles [
set neighbours turtle-set turtles in-radius neighbourhood
set turtle-dim-similar min-one-of other neighbours [myfunc myself self]
create-link-with turtle-dim-similar
]
set beasts sort-by [[who] of ?1 < [who] of ?2] turtles
foreach beasts [
ask ? [ ;; each agent undertakes the list of commands
set closest sort [link-neighbors] of turtle who
print (word "id:" who " ; val1:" value1 " ; val2:" value2 " ; closest:" closest)
]
]
end
If you are using directed links i.e. links created with create-link-to, create-link-from, create-links-to or create-links-from than out-link-neighbors is what you are looking for. It reports a set of the turtles linked to.
If you are using un-directed links, ones created with create-link-with or create-links-with , use link-neighbors.
This solves the first part of the problem for the second half use recursion.
Turtles-own[group-id]
To id [tag]
Set group-id tag
Ask link-neighbors with[Group-id != tag]
[Id tag]
End