LorenzoLearningCurve
LorenzoLearningCurve

Reputation: 71

Netlogo: Edit link attribute based on lists of linked agents

I have a model with banks. Each bank is linked to all other banks by a special breed of undirected link called "riskshares". Each bank also has links to other types of agents. Each bank has a list of countries where it operates. The list is called "op-countries-list", which does not have a fixed length, and both its length and countries are randomly picked. For example, one bank might have "op-countries-list" of length 5 = "Australia, Italy, Spain, Canada, Brazil", and another bank might have "op-countries-list" of length 2= "Italy, France". Now: I need to change to "TRUE" one boolean attribute of the "riskshare" link, depending on whether there is one common country in the "op-countries-list" of the two banks linked by the "riskshare".

I have (among other things):

banks-own [op-country-list]

riskshares-own [same-country-op?]

And I am trying this approach:

ask banks [ foreach op-country-list ifelse member? op-country-list of other-end [set same-country-op? of my-out-riskshare true] ]

With this, I get an "OF expected input to be a reporter" error. I think I should come up with a to-report procedure and then use it, but I am not sure how to do it in this case. In addition, I am not sure how to specify that I want other-end to look into the "riskshare" kind of link, and not all other links that the bank has.

Do you have any suggestions?

Upvotes: 0

Views: 42

Answers (1)

Luke C
Luke C

Reputation: 10291

There is definitely a way to do it with foreach as you're getting at, but you might find map a little more appropriate in this case. map will iterate over items in a list and do "something"- in this case, you could check for membership in the list of interest. More detail in comments below:

breed [ banks bank ]
undirected-link-breed [ riskshares riskshare ]

banks-own [ op-country-list ]
riskshares-own [ same-country-op? ]

to setup
  ca
  random-seed 1
  let possible-countries [ "CANADA" "USA" "FRANCE" "JAPAN" "AUSTRALIA" "SPAIN" "RUSSIA" ]
  create-banks 5 [
    ; Pick some random countries
    let n-countries random 3 + 1
    set op-country-list n-of n-countries possible-countries
  ]
  layout-circle turtles 8
  ask banks [
    set label op-country-list
    ; Build the links
    create-riskshares-with other banks [
      ; Determine if the banks share an operating country
      ; Pull the country lists from each bank associated with the link
      let bank-1-countries [op-country-list] of end1
      let bank-2-countries [op-country-list] of end2
      
      ; Use map to iterate over each country of one bank and check its
      ; membership in the op-country-list of the other bank. Check if 'true'
      ; is found in the list
      let any-true? member? true map [ cur_bank ->  member? cur_bank bank-2-countries ] bank-1-countries
      print member? true map [ cur_bank ->  member? cur_bank bank-2-countries ] bank-1-countries
      ; Set same-country-op? according to the any-true condition
      ifelse any-true? [
        set same-country-op? true        
        set color blue
      ] [
        set same-country-op? false
        set color red
      ]      
    ]
  ]    
  reset-ticks
end

This toy model (with the random-seed as is) gives an output like:

enter image description here

Which I think meets the conditions you outline. Hopefully that gets you pointed in the right direction!

Upvotes: 1

Related Questions