Check same value over entire agentset

36 Views Asked by At

I have a model with a bank network. Every bank is connected with a link to every other bank. I need to verify that no pair of banks have the same value for a certain bank attribute.

(This is because, since I need to subtract one from another and put the result at the denominator of an expression, I get a "division by zero" execution error if any two banks have the same value for that attribute).

In order to check for this, I put in the following line:

if (any? banks with [AttributeX = [AttributeX] of other banks]) [set same-risk-value-error? true]

and then I put a stop-execution line inside "go" (immediately after the call to the function where that particular attribute is set):

if same-risk-value-error? = true [stop]

But apparently it does not work, as the execution continues and I still get that "division by zero" error when it occurs.

After I get the error, I check on the Command Center for the specific bank pair that gives me the error.

This is what I get:

observer> show [AttributeX] of bank 63
observer: 0
observer> show [AttributeX] of bank 83
observer: 0
observer> show (any? banks with [AttributeX = [AttributeX] of other banks])
observer: false

So: both banks (bank 63 and bank 83) have the same value for AttributeX (the value is equal to 0 in this case), and yet I get a "false" when I ask whether there are any two banks in the entire agentset of banks with the same value for that attribute.

How come?

My obvious guess is that the check

(any? banks with [AttributeX = [AttributeX] of other banks])

even though grammatically correct, does not do what I want.

How should I write a boolean check line (giving me true/false) to check that no two agents in an agentset have the same value for a particular attribute?

1

There are 1 best solutions below

0
On

The problem is that you are comparing a number to a list. [atrributeX] of other banks creates a list of all the attributeX of the other banks, while for each bank, AttributeX is a single value. In comparing a single value to a list, NetLogo will always yield a false. (It might be more appropriate if NetLogo issued a warning, but that is another issue.)

If each bank needs to check this, then you might try

(any? other banks with [AttributeX = [attributeX] of myself]

Since myself is a single agent, [attributeX] of myself will yield a single value. On the other hand, if you simply need to know if there are any duplicate values of AttributeX among all the banks, the observer could simply ask

if length [AttributeX] of banks != length remove-duplicates [AttributeX] of banks