z5182
z5182

Reputation: 69

Ask agent to join an agentset if attributes of agents in the agentset isn't match

I'm having trouble to make "members" join "households" correctly. Each member has pre-defined variables, 'mbzone' 'abshid' 'abspid' and 'absfid', and some members could have exactly the same set of values of those pre-defined variables. In the same sense, household agents can have the same set of values for their pre-defined 'hhzone' and 'abshid'.

I would like to make member agents join household agents through the given conditions in the code below. But it turned out that some [memberlist] of households didn't contain only member agents with unique 'abspid' and 'absfid'. Meaning in the memberlist of a household agent there could be members with the same abspid and absfid. My question is how can I prevent this from occuring?

Thank you in advance. Sorry if what I wrote isn't clear to you, I'm more than happy to explain in a greater detail if necessary.

    breed [members member]
    breed [households household]

    members-own [mbzone abshid abspid absfid]
    households-own [hhzone abshid]

    to members-join-households
          ask members [
            set hhid one-of households with [hhzone = [mbzone] of myself and abshid = [abshid] of myself and [abspid] of hhmemberlist != [abspid] of myself and [absfid] of hhmemberlist != [absfid] of myself]
            ask hhid [
              let newmember myself
              set hhmemberlist (turtle-set hhmemberlist newmember)
            ]
          ]
          output-print "member-join-households is done"
        end

Upvotes: 0

Views: 203

Answers (1)

Alan
Alan

Reputation: 9620

What @JenB said. Additional consideration: test that you found such a household.

breed [members member]
breed [households household]

members-own [mbzone abshid abspid absfid hhid]
households-own [hhzone abshid hhmemberlist]

to members-join-households
  ask members [
    let _pid abspid
    let _fid absfid
    set hhid one-of households with [
      hhzone = [mbzone] of myself 
      and abshid = [abshid] of myself 
      and not member? _pid ([abspid] of hhmemberlist)
      and not member? _fid ([absfid] of hhmemberlist)
    ]
    if (hhid != nobody) [ ;did such a household exist?
      ask hhid [
        set hhmemberlist (turtle-set hhmemberlist myself)
      ]
    ]
  ]
  output-print "member-join-households is done"
end

But keeping member lists this way is very clumsy compared to using directed links, so I recommend that you switch to that. If you have members form directed links to a households, then a household's members are just its in-link-neighbors, and a member's hhid is just the household it links to.

Upvotes: 2

Related Questions