3

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
M--
  • 25,431
  • 8
  • 61
  • 93
  • probably what's happening is that the turtles are not initiating links when they get to 3, but you don't appear to have any code that stops other turtles creating links with them. So A is close to B and A has reached 3. A doesn't create a link with B, but B will create a link with A if B has fewer than 3 links. But I am not clear what xagents and yagents are in your code so this could be incorrect. – JenB May 24 '17 at 13:53
  • Thanks. The whole code is listed here (minus the if count my-links... comment [link] (https://stackoverflow.com/questions/44122905/netlogo-getting-agents-to-link-if-variables-values-are-the-same) Would I need to have separate IF statements with link max for xagents and yagents? – Umpherous Vermillion May 24 '17 at 16:12

1 Answers1

3

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]] ]
M--
  • 25,431
  • 8
  • 61
  • 93
  • Masoud-I added your code suggestion: still no luck. – Umpherous Vermillion May 25 '17 at 03:13
  • @UmpherousVermillion What's xagent and yagent? – M-- May 25 '17 at 13:19
  • Not sure what you are asking about agents--conceptually or within netlogo? Conceptually, xagents are people moving about randomly and when they encounter yagents with the same value, they link (like forming friendship). But I don't want all xagents to form links with yagents with the same value to link, hence the max link limit. Within Netlogo, they are two breeds of agents that share the same variable var1. – Umpherous Vermillion May 25 '17 at 14:37
  • I applied a change to yagents part. but still this is too intimidating for me as everyone has the same variable name (the one that create the link to the one that gets linked to from different breed). I would suggest using local variables and use `let` like this: `let X_link_creator count my-links` or `let Y_var1_connected [var1] of myself`. this way your code will be more informative and you'll know which parameter and where should be limited to avoid making extra links. In general, you need to limit both xagents and yagents from having links more than 3 in your sub-procedure `to communicate` – M-- May 25 '17 at 14:45
  • I tried to play with changing up the IF statement in the to communicate procedure, but no luck. What do you think of this solution? I kill all links after the first one in the 'to go' procedure. Not terribly eloquent, but seems to be working. ask xagents [ if count my-links > 1 [die] – Umpherous Vermillion May 26 '17 at 19:43
  • @UmpherousVermillion I have seen your complete code in your last question. – M-- May 26 '17 at 19:44
  • @UmpherousVermillion What about this: `ask turtles with [count my-links > LIMIT] [ if count my-links > LIMIT [ask n-of (count my-links - LIMIT) my-links [die]] ] ` – M-- May 26 '17 at 19:50
  • @UmpherousVermillion Also use `Create-Link` to make one link at a time. – M-- May 26 '17 at 19:52
  • Masoud - looks like our comments crossed each other. We must be linked ; ) My kill links procedure is similar to yours but simpler in the go procedure: ask xagents [ if count my-links > 1 [die] ] rather than ...[ask n-of (count my-links - LIMIT my-links [die]]... Is there any specific reason to include more information than simply [die]? So far that scheme seems to be working; xagents report only 1 link per. What do you mean "Also use Create-Link to make one link at a time" -- Do you mean replace "[create-links-with.." in the "to communicate" procedure? – Umpherous Vermillion May 26 '17 at 20:56
  • @UmpherousVermillion Yes. What you have is a kill procedure. What I am proposing is about limiting number of links. If the time order of links is not important (you just want the links to be limited to 3 and it's not important which one has been created first) then what I proposed randomly removes links to limit them to 3. – M-- May 26 '17 at 21:00
  • @UmpherousVermillion With that you don't need any of the changes I proposed before. You can just have this at the end of your communicate procedure and no turtle will have more than 3. however, that has some downsides. Turtle will remove a link from another one that has less than 3, while it can remove the one from another agent which surpasses the number of allowed links. – M-- May 26 '17 at 21:03
  • 1
    @UmpherousVermillion BTW, I checked my answer and it works. What exactly happens when you apply my updated **answer**. AFAIK if your first condition (i.e. `(var1 = [var1] of myself)`) is fine (I haven't changed that, that's copied/paste from the question) then the second criterion (i.e. `and (count my-links < 3)`) works fine. **Let me know what's the problem with *my updated answer*!** It has been edited yesterday, as you can see. – M-- May 26 '17 at 21:07
  • 1
    Got it! Thanks, Masoud! You have been very helpful and patient. Also, I realized in my simple kill procedure, I was killing turtles and not links. I should have put ask my-links [die] I will explore your idea further. – Umpherous Vermillion May 26 '17 at 21:08