Iterating over a list of booleans values - netlogo

**I have a list called ( a) which holds booleans values thus I want to implement an if statement if each item value in the list ( a )is true and implement another if statement if each item value in list ( a) is false.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
I tried this but it does not work !!!
1- foreach a [ x -> if ( x = true) [ask pharmacists [ do the first if statment ]
2- foreach a [ x -> if ( x = false) [ask pharmacists [ do the second if statment ]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
I want to cycle at each boolean value in list (a) and perform an if statement based on whether the value is true or false.**

I'm not sure I fully understand what you're trying to do- you may want to have a look at the Asking Help for some guidelines. Note the comment:
DO NOT post images of code, data, error messages, etc. - copy or type the text into the question. Please reserve the use of images for diagrams or demonstrating rendering bugs, things that are impossible to describe accurately via text.
Also, have a look at the section on making a Minimal, Complete, and Verifiable Example. This will help users provide you with a useful answer that is definitely applicable to your issue. If my answer below is not helpful, I suggest you make a toy model that acts as a working example of the problem you're running into.
If you just want to iterate over a list of true / false values, I think you're on the right track- have a look at the very simple example below which just prints a statement depending on whether the current iterated value is true or false:
to setup
ca
let boolean-list [ true true true false true false false false ]
foreach boolean-list [
i ->
ifelse i [
print "Current item is true!"
] [
print "Current item is false!"
]
]
reset-ticks
end

Related

Get a count of turtles with a combination of values

I am trying to count the number of "buyer" type turtles, which have a certain surplus (turtle variable) greater than or equal to zero, and price (another turtle variable) greater than the current turtle's price (already grabbed in local variable myprice...although there may be a more direct way to put it in)
let countup count buyers with ([surplus >= 0] and [price > myprice])
NetLogo returns
Expected a TRUE/FALSE here, rather than a list or block.
let countup count buyers with (surplus >= 0 and price > myprice) returns
WITH expected this input to be a TRUE/FALSE block, but got a TRUE/FALSE instead
Close! You're looking for:
let countput count buyers with [ surplus >= 0 and price > myprice ]
with is a report that takes two arguments, like so
<turtleset> with <report block>
where the reporter block is a clump of code surrounded by [ ] that will result in either true or false. In general [ ] is netlogo's way of grouping together code so you can doing something special with it, such as having each agent in an agentset run it. Hope that helps!
Also, I assume you've got something like let myprice price on, say, the line above this one. You can combine those lines like so (not saying this code is the right way to do it, just wanted to show another option):
let countput count buyers with [ surplus >= 0 and price > [ price ] of myself ]
Checkout the docs for (the very poorly named) myself.

Accessing owned traits in netlogo with nested ask

This is what i want to do:
patches-own[
trait1
trait2
trait3
]
let similarityCounter 0
ask one-of patches[
ask one-of neighbors[
**for-each trait[
if neighborTrait = patchTrait**[
set similarityCounter (similarityCounter + 1)
]
]
]
]
The part between ** is what I'm unsure about. How does one iterate over the patch-own parameters and compare between patch and neighbor?
How about you create a list for each patch of their trait values and count matches in the two lists? It would looks something like this.
to testme
let similarityCounter 0
ask one-of patches
[ let mytraits (list trait1 trait2 trait3)
let theirtraits [(list trait1 trait2 trait3)] of one-of neighbors
set similarityCounter length filter [ xx -> xx ] (map = mytraits theirtraits)
]
end
The final line is a little dense. What it does is compare the two lists of traits using the map function with the = operator, which will return a list of true and false values indicating whether that specific trait matches. The filter then creates a list of just the true values and the length counts the number of those true values.
Unfortunately, NetLogo doesn't do the trick of treating a true as 1 and false as 0 that you see in some languages, so you can't simply sum the match results list.
I really like Jen's answer, but just for fun, I'd like to provide an alternative way to approach the problem that uses Jen's idea of treating true as 1 and false as 0.
But first, I think that, depending on the rest of your model, it could have been a good idea for you to store your traits directly in a list instead of separate variables. In programming, having variable names with a numeric suffix like trait1, trait2, etc. is usually a hint that a list should be used instead.
Nevertheless, we will leave your general design alone for now and just provide a small function that makes it easy to package your traits into a list:
to-report traits ; patch reporter
report (list trait1 trait2 trait3)
end
Once you have that, you can write something like [ traits ] of one-of patches to get a list of the patche's traits.
Now let's attack the problem of turning true and false into ones and zeros in a similar way. It's true that NetLogo doesn't provide that conversation automatically (which I think is a good thing) but it's easy to write our own function for that:
to-report bool-to-int [ yes? ]
report ifelse-value yes? [ 1 ] [ 0 ]
end
We are now ready to write our main function. We will use Jen's approach of mapping over the = operator to convert our lists of traits to a list of boolean (i.e., true/false) values, and we will then use map again to convert that list into a list of 1 and 0. Once we have that, all that is left is to sum it! Here we go:
to-report similarity-with [ other-patch ] ; patch reporter
report sum map bool-to-int (map = traits [ traits ] of other-patch)
end
Having that reporter makes it really easy to get the similarity between two patches. You can now say things like:
print [ similarity-with one-of neighbors ] of one-of patches
Notice how I have approached the problem by building small pieces that be combined together. I really like this way of proceeding: it allows me to concentrate on one part of the problem at a time, but it's also more easy to test and results in code that I find very readable. NetLogo's to-report procedures are a great tool to achieve that kind of modularity.

How to compare two turtles in Netlogo by going through a list of their attributes?

My turtles have more than 30 attributes of boolean values and I would like to use a foreach loop to compare turtles and rank them based on their similarity without the need to compare each attribute individually. I might be missing an obvious point here, I have tried having a list of attributes, but it didn't work and all turtles got the maximum similarity score.
Here's some code that calculates the Hamming distance between two lists. Note that the very clever reduce code is taken directly from the NetLogo dictionary.
to testme
let ll1 (list TRUE TRUE FALSE FALSE)
let ll2 (list TRUE FALSE TRUE FALSE)
let ll3 ( map = ll2 ll1 )
show ll3
show reduce [ [occurrence-count next-item] ->
ifelse-value (next-item) [occurrence-count + 1] [occurrence-count] ] (fput 0 ll3)
end
If you were wanting to calculate the similarity score of a pair of turtles, you could turn this into a reporter that takes the two turtles as arguments. But it's not clear that comparing two turtles is what you want to do, so I haven't written code for that.

How to compare two lists in Netlogo?

I have two breeds, buyers and suppliers, and buyers are building a list (sup_list) of suppliers that have attributes stored in list 'att' that are greater than a list of criteria stored in list 'b'. The following line does this for the first criteria - is there an easy way to add in all the others?
ask buyers [set sup_list suppliers with [item 0 att > [item 0 b] of myself]]
So in English the criteria would be: item 0 > item 0 AND item 1 > item 1 AND item 2 > item 3 etc.
Thank you.
The expression you want is:
suppliers with [ reduce and (map > att [ b ] of myself) ]
This is a tricky bit of functional programming. Let's see how it works.
Our first goal is to take the two lists of numbers and turn it into a single list of boolean values, where each item will be true if the item at the same location in the buyer's list is greater than the item at the same location in the supplier's list. For example, if we have:
Buyer's list: [1 1 1 1]
Supplier's list: [2 1 1 1]
...only the first item in the supplier's list fits our criteria, so we want our resulting list to be:
[true false false false]
Whenever we want to turn one or more lists of things into a single list of things, the NetLogo primitive to use is map. The map primitive takes a reporter and one or more lists. It applies the reporter to item(s) taken from the list(s) and builds a new list out of that. This is exactly what we need. Try this in the NetLogo command center:
observer> show (map > [2 1 1 1] [1 1 1 1])
observer: [true false false false]
A couple of things to note:
Since we are passing more than one list to map, we need to put the whole expression inside parentheses.
We are using concise syntax for passing > as a reporter. This could also have been written [ [a b] -> a > b ].
Now that we have our list of boolean values, we want to check if all these values are true, i.e., if all supplier items fit the buyer's criteria. NetLogo has an all? primitive that does something like that for agentsets, but we cannot use it here since we are dealing with a list. We will have to use reduce instead.
The reduce primitive is the one to use whenever we want to turn a list into a single value. Here, we want to turn a list of booleans into a single boolean value, that will be true if all the values in the list are true and will be false otherwise.
As the NetLogo documentation says, "it can be difficult to develop an intuition about what reduce does". (I strongly urge you to read the documentation and try experimenting with the primitive.) In a nutshell, it traverses a list and applies a reporter to each item and an "accumulator" value, storing the result of that operation in the accumulator. The first item of the list is used to initialize the accumulator.
In our case, the reporter used with reduce will be and, since we want to check that the first item is true, and that the second item is true, and that the third item is true, etc.
Let's try to reduce our previously obtained list of booleans:
observer> show reduce and [true false false false]
observer: false
(Not that we're, again, using the concise syntax to pass and as a reporter. This could have been written [ [p q] -> p and q ].)
The end result is false, because not all values are true. Let's see how this works, step by step:
it stores the first item from the list in the accumulator, so the accumulator now holds the value true.
it passes the value of the accumulator and the value of the second item to the and reporter. The accumulator is true but the second item is false. The result of true and false is false, so it stores false in the accumulator.
it passes the value of the accumulator and the value of the third item to the and reporter. The accumulator is now false and the second item is also false. The result of false and false is false, so again, it stores false in the accumulator.
the fourth step is just like the third: the accumulator holds false and the fourth item is also false. The result of false and false is false, so again, it stores false in the accumulator.
Once we run out of list items, reduce reports the value of the accumulator, in this case false. The only case where it would report true is if all values in the list are true, leading to a sequence of true and true comparisons that all result in storing true in the accumulator. That's exactly what we want:
observer> show reduce and [true true true true]
observer: true
If you put all of this together, you should be able to see how:
suppliers with [ reduce and (map > att [ b ] of myself) ]
...gives you the agentset of suppliers that fit all criteria of the buyer!
(Note that with returns an agentset, not a list, so you should probably rename your sup_list variable...)

Comparing Turtle Color In Netlogo

I am trying to do something I imagine is relatively simply but for some reason I am having a heck of a time figuring it out and all my searches are turning up blank.
I want to query the color of a specific turtle and check if it matches another color. I want to do something like this:
if color targetTurtleNum = red [set target-confirmed true] ;
But I keep getting an error:
IF expected this input to be a command block, but got a true/false instead.
Any ideas?
Assuming targetTurtleNum is the "who" number of the turtle you are interested in, try:
if [ color ] of turtle targetTurtleNum = red [ set target-confirmed true ]
The error you are getting is because if expects two inputs: a boolean (the condition you want to check) and a command block (what to do if the condition is true). In your version of the code, the first input thatif is getting is color, and the second one is targetTurtleNum = red, so the compiler complains about getting a boolean as the second input.
In the correct version up here, the whole [ color ] of turtle targetTurtleNum = red part is the boolean that counts as the first input, and [ set target-confirmed true ] is the command block that is expected as the second input.