Search code examples
netlogoagent-based-modeling

Limit the number of links an agent can make


I have turtles linking if they have an equal value for var1 (this works fine). I want to limit the number of links to just three. I added an IF statement before the linking part of the code (If count my-links < 3), but it does not work; the agents continue to link past the max value I set. I read the other question How to limit the number of links an agent can make in a model but that doesn't seem to quite do what I am attempting here. What am I doing wrong?

to communicate
  If count my-links < 3
  [
  ask other xagents in-radius 5 with [var1 = [var1] of myself]
  [create-links-with yagents in-radius 5 with [var1 = [var1] of myself]
    [
      set color white
      set thickness 0.1
    ]
  ]
  ]
end

Solution

  • Limit number of links for the turtles before letting them create new ones:

    By looking at your complete module, as @JenB mentioned that, it seems that there's no condition to limit number of links that the targeted turtle has for making a link.

    This would be the first step:

    to communicate
      If count my-links < 3
      [
      ask other xagents in-radius 5 with [(var1 = [var1] of myself) and (count my-links < 3)]
      [create-links-with yagents in-radius 5 with [(var1 = [var1] of myself) and (count my-links < 3)]
        [
          set color white
          set thickness 0.1
        ]
      ]
      ]
    end
    

    But what if there's no agent like that? (in radius of 5, with the same val1 and links less than 3) Probably an if-statement is needed.

    I also think you need to use one-of in your code to make only one link in each step.


    Kill links after each tick to limit the number of links for turtles:

    You can have this at the end of your communicate sub-procedure to kill the extra links. It has a down-side of random removing links and also may remove the link from turtles with fewer link instead of the ones that may also have extra links.

    ask turtles with [count my-links > LIMIT] [ if count my-links > LIMIT [ask n-of (count my-links - LIMIT) my-links [die]] ]