Arrebimbomalho
Arrebimbomalho

Reputation: 186

Matching two different agent types by their properties in Netlogo

I have agents of two types in Netlogo (firms and workers) and they are connected through a common identifier, that, however, is an agent property (called ID for Firms, and FirmID for workers). I am interested in asking firms to display their workforce by performing an ask for firms, asking them to display a count of all workers such that the workers' "firmID" variable equals the firms "ID" variable, as described below:

I have tried to ask firms to count all workers such that the workers ID firms equals the asking firms' ID, but that is not working inside an ask procedure. If I try one by one, curiously it does work.

some reproducible toy code:

breed [Firms firm]
breed [Workers worker]
Firms-own[
 ID
] 

Workers-own[
 FirmID
]

to agent-creation
create-workers 3; There are 3 workers
create-firms 2; There are 2 firms
  ask worker 0 [set FirmID "333-1"]
  ask worker 1[set FirmID "333-1"]
  ask worker 2[set FirmID "444-1"]
  ask firm 3 [set ID "333-1"]
  ask firm 4 [set ID "444-1"]
end
; Thus, firm 333-1 has 2 workers, 444-1 has only one.

and now,

to matching-procedure
  ask firms [show count workers with [FirmID = [ID] of self]]
end

does not work, but

ask firms with [ID = "333-1"][show count workers with [FirmID = "333-1"]]

does.

The procedure should output [2 1] (I will put the result in a list, but clearly the idea is that firm 333-1 has 2 workers and 444-1 just one) So Firm 333-1 should declare 2 and 444-1 just one.

Upvotes: 0

Views: 149

Answers (1)

JenB
JenB

Reputation: 17678

It is very common to mix up self and myself, which is what you have done here. Try:

to matching-procedure
  ask firms [show count workers with [FirmID = [ID] of myself]]
end

The reason you need myself is because the variable ID belongs to the firm, which is the agent doing the asking. In contrast, self is the one being asked. So your original code generates an error letting you know that workers don't own the attribute ID.

That fixes your problem. Just as a general comment on linking together firms/workers or other one to many relationships in NetLogo, it is best to avoid using identifiers and simply store the agent. That would look like this:

breed [Firms firm]
breed [Workers worker]
Firms-own[
 my-workers
] 

Workers-own[
 my-firm
]

to agent-creation
  clear-all
  create-firms 2; There are 2 firms
  create-workers 3
  [ set my-firm one-of Firms
  ]
  ask Firms
  [ set my-workers workers with [my-firm = myself]
  ]
end

to check-allocation
  ask Firms [show my-workers]
  ask Workers [show my-firm]
end

Another option commonly used is to create a link between the firm and worker. Links are the natural way to express a relationship, and can be hidden so they don't clutter up the visualisation.

Upvotes: 2

Related Questions