1

in my netlogo code I have a network with companies (that is my breed). I want to ask the companies to share information with their neighbors and their neighbors and so on, this works (see code below, the agentsets are b, c and d).

However when I ask for information on the third level neighbors my agentset also includes the first level neighbors (obviously since it takes all neighbors into acount), so I want to remove these first level neighbors from the third level neighbors agentset. In the code this means I want to remove agents present in D which are also present in B

But I cant find the way to do it, other doesnt work since it is not the agent asking which has to be removed. And remove also doesnt seem to do the job. I also tried != not equal to the first level but this reports a true or false and I just want to remove these agents from the third level agentset so I dont double count them.

ask companies [
  let i who
  let b link-neighbors
    ask b [ let c link-neighbors
     ask c [ let d link-neighbors

      ask companies with [who = i] [ 
        set iburen [who] of b
        set iiburen [who] of other c
        set iiiburen [who] of d
     ]
   ]
 ]
]

can somebody help me with this?

Wessel K
  • 55
  • 6

2 Answers2

2

I think what you want is the member? primitive. If D and B are agentsets, the following should give you the members of D that are not members of B.

let DminusB D with [not member? self B]
Charles
  • 3,888
  • 11
  • 13
2

Many things to say here:

  1. Charles' answer is technically correct.

If a and b are agentsets, a with [ not member? self b ] will give you agents from a that are not already in b.

But I think there are better ways to accomplish what you are trying to do. I will come back to that, but first, a general piece of advice:

  1. Don't use who!

The who primitive has some (very few) legitimate usages, but it's mostly evil. It tends to lead to brittle, inefficient code. For example, when you do:

let i who
; ...
ask companies with [who = i] [ ... ]

NetLogo has to scan all companies to find the one with that specific who number.

NetLogo can store agent references directly. Use that instead! For example:

let this-company self
; ...
ask this-company [ ... ]
  1. Especially don't use lists of who numbers!

NetLogo is adequate for manipulating lists, but its awesome for manipulating agentsets. If you do something like this:

set iburen [who] of b
set iiburen [who] of other c
set iiiburen [who] of d

You are forfeiting the power of agentsets. I don't know why you want to store the three different levels separately, but supposing it's OK to store all your neighbors together, you could do:

set my-neighbors other (turtle-set b c d)

The use of other will exclude the original company and turtle-set will make sure that each agent in the set is unique (as agentsets can only contain unique agents anyway).

If you really want three separate variables, use Charles' answer, but make sure to store agentsets, not lists of who numbers!

If you don't need separate variables, however, I think the best solution would be to:

  1. Use nw:turtles-in-radius.

NetLogo's Networks extension has a primitive that does exactly what I think you want to do:

ask companies [ set my-neighbors nw:turtles-in-radius 3 ]

That's it.

Nicolas Payette
  • 14,847
  • 1
  • 27
  • 37
  • Hi Nicolas, yeah you are right about the who numbers. But I wanted each agent to report their set of link-neighbors on the three different levels. But you are right about the who. however if I jsut used ask companies, all the companies report the same agentset. But I want them to report their neighbors and neighbors etc. I will use the three different agentsets because I weigh there information different. the further away in the network. so first. second and third neighbors the less your information is valued. I will take your suggestion about the who numbers and will chang this. – Wessel K Dec 18 '17 at 20:08
  • I got it to work without the who numbers, however I want the second and third layer to add all their different neighbors into one `agentset`. Now the original agent gets the agentset from the last agent in the `agentset` since he overrides the `let` instead of adding to it. I thought of `foreach` however this doesnt work with agentsets. do you know a way to do this or do I need to ask a new question? – Wessel K Dec 18 '17 at 21:21
  • How about a different approach: instead of using three different agentsets, just use one, and then use [`nw:distance-to`](http://ccl.northwestern.edu/netlogo/docs/nw.html#nw:distance-to) to do the weighting? – Nicolas Payette Dec 19 '17 at 09:32
  • 1
    Another general comment: any time you have three different variables to store more or less the same thing, it's probably an indication that you are not approaching the problem from the best angle. Ideally, your code should be general enough that you could add a fourth level just by changing `3` to `4` somewhere in your code. If you have to add a new variable and duplicate blocks of code instead, it's usually a bad sign. Even if you don't intend to ever add a fourth level, more general code is usually shorter and (more importantly) easier to prove correct. – Nicolas Payette Dec 19 '17 at 09:38