2

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.

Wessel K
  • 55
  • 6
  • 1
    Please explain what you mean by "since in a let you cant ask to let variable". What is the error you are getting? I would be doing this as `let newset turtle-set current-agentset new-agentset`. – JenB Dec 18 '17 at 23:49
  • Hi jenb, yeah I got it working. I meant that when you make a let you first have to define it before you can say `set newset (turtle-set newset currentset)`. I do this now by defining it as a agentset which I later filter out again. I hoped that you can define the let as an empty agentset on which I can add agents later like the way you suggested. however I didin't figure this out yet. – Wessel K Dec 19 '17 at 20:33
  • The primitive `let` means create the agentset and set its contents to some value. That is, the `let` does the defining for you. Just replace the word `set` with `let` in your code and it will work fine. However, you can only `let` once for each variable, you will need to use `set` to change the contents later. – JenB Dec 20 '17 at 08:07

1 Answers1

3

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.

Nicolas Payette
  • 14,847
  • 1
  • 27
  • 37
  • I got it working the way I wanted. It looks like this now: `ask companies [ let b link-neighbors let c self let d b ask b [ set c (turtle-set c link-neighbors) set c c with [self != c] ask c [set d (turtle-set d link-neighbors) set d d with [not member? self b] ask this-company [ set iburen b set iiburen c set iiiburen d ] ] ] ]` But I will try your way too, it looks better since I have a network of 4000 companies. – Wessel K Dec 19 '17 at 20:26
  • to be clear, what you code does is the same but more efficient and it stores the 3 different layers of agentsets in a list where each item in the list is an agentset, going from closest to most far away? And the nw:turtles-in-radius is used to get the level the turtles are away measuring it in number of links or steps? – Wessel K Dec 19 '17 at 20:42
  • I tried your code, and it does indeed what I want and what I did with my code. however it is more efficient and I can add layers whenever I want. thanks for the help – Wessel K Dec 19 '17 at 20:56