Search code examples
netlogoletagentset

adding agentset from different agents togother into a let


my model is a network of agents connected to each other with links. I try to create a agentset from the neighbors of an agents and their neigbors and so on (I need this to assign different values to it).

However when I create a let with the agentset in it. the agents asked to make this agentset all have their own, this is so far so good. But when I want the original agent to ask him his second line neighbors he just returns an agentset from one of this neighbors instead of the combined agentsets of all his second line neighbors I want the neighbors to store their own neighbors into a agentset with all the neighbors from the different agents in that set.

I cant ask the let agentset to simple do turtleset current-agentset new-agentset since in a let you cant ask to let variable. So a code which would normally be set second-neighbors (turtle-set second-neighbors other-nieghbors doesnt work since I cant ask second-neighbors already in a let

I also cant make this a global or somethins since it is agent specific.

the code I have so far looks like this

    ask companies [
      let this-company self
      let b link-neighbors
        ask b [ let c link-neighbors with [self != this-company]

          ask c [ let d  link-neighbors with [not member? self b]
            ask this-company [
              set iburen b
              set iiburen c
              set iiiburen d
        ]
      ]
    ]
  ]

so what I want is that all the agents in the agentset c report their link-neighbors like they do now. But also store these link-neighbors into a new agentset which has all the link-neighbors of all the agents in c. like a simple i i + 1. but than with turtle-set (what I have) (what is new from the next agent asked)

the same goes for d

If I run the model now agents report different agentset almost every tick. They just pick one agentset from any of these agents instead of combining them all togother.


Solution

  • Here is what I think you need:

    extensions [ nw ]
    breed [ companies company ]
    companies-own [
      buren ; a list of agentsets, with one item for each "level" of neighbors
    ]
    
    to setup
      clear-all
      ; create a random network and lay it out:
      create-companies 20 [ create-links-with n-of 3 other companies ]
      repeat 30 [ layout-spring turtles links 0.2 5 1 ]
    
      let num-levels 3
    
      ask companies [
        let all-neighbors other nw:turtles-in-radius num-levels    
        set buren (list) ; initialize to empty list
        foreach range num-levels [ i ->
          let neighbors-at-this-level all-neighbors with [
            nw:distance-to myself = i + 1
          ]
          set buren lput neighbors-at-this-level buren
        ]
      ]
    
      ; demonstrate how to access the levels (sorted only for display purposes)
      ask one-of companies [
        show sort item 0 buren ; first level neighbors
        show sort item 1 buren ; second level neighbors
        show sort item 2 buren ; third level neighbors
      ]
    
    end
    

    This might not be the most efficient code possible, because it goes through the list of all neighbors once for each level, but unless you have a humongous network, you should not notice.

    If you really wanted to use variables like iburen, iiburen and iiiburen, you could always alias the items of the list:

    set iburen   item 0 buren
    set iiburen  item 1 buren
    set iiiburen item 2 buren
    

    ...but I don't recommend it. Having your agentsets in a list should encourage you to think of your levels in a more general way.