Limit the number of links an agent can make V2

2019-09-14 10:04发布

问题:

I know, here it is the same question. The thing is... I've been trying to implement it but it doesn't seem to work for me. My simulation is running but 0 links are made even though the agentes are facing the activities at sight-radius. Here is the code:

to participate
  ask activities [
    if count my-links < n-capacity [
      ask other agentes in-radius sight-radius with
      [shared-culture = [shared-culture] of other agentes] [
        create-participations-with n-of n-capacity agentes
        ]
        ask links [
          set color blue]
      ]
    ]
end

If the code isn't clear enough, I want the activities to: 1- Know how many agentes they can have. 2- Accept them if they have the same shared-culture and they are in-radius. 3- Represent this "acceptance" and "participation" with blue links.

I tried something similar with while but 0 results.

回答1:

It's hard to say without seeing more of your code, but there are a few issues that might be causing your problem. According to your summary, my understanding is that you essentially want activities agents to form links with agentes within sight-radius up to the number defined by n-capacity. However, in your code, you are having activities check if count my-links < n-capacity, have other agentes in that activity's sight radius to create participation links with other agents rather than with the original activity agent that I understand you want to have some links.

Assuming n-capacity is an activities-own variable, you may be able to get closer to what you want by just switching

ask other agentes in-radius sight-radius with
      [shared-culture = [shared-culture] of other agentes] [
        create-participations-with n-of n-capacity agentes
        ]

to

ask n-of n-capacity agentes in-radius sight-radius with [shared-culture = [shared-culture] myself] [create-participation-with myself]

Edit: forgot the of in original

ask n-of n-capacity agentes in-radius sight-radius with
  [shared-culture = [shared-culture] of myself] [
    create-participation-with myself
    ]

However, since I can't test that as I don't have your setup and other code, I'll show you a different example that I know works and might be what you're after. Below is all the code needed, make a button for setup and a forever button for go, and watch the wolves slowly build up to a maximum of three links with the sheep agents that have the same color:

breed [ wolves wolf ]
breed [ sheeps sheep ]
undirected-link-breed [ participations participation ]

to setup
  ca
  reset-ticks

  create-wolves 3 [
    set shape "wolf"
    setxy random 32 - 16 random 32 - 16
    set color one-of [ blue red ]
    set size 2
  ]

  create-sheeps 25 [
    set shape "sheep"
    setxy random 32 - 16 random 32 - 16
    set color one-of [ blue red ]
  ]

end


to go
  ask turtles [
    rt random 90 - 45 
    fd 0.1
  ]
  links-with-if
  tick  
end


to links-with-if
  ask wolves [
    if count my-links < 3 [
      ; Make sure the links a wolf tries to form
      ; does not exceed the max number of links it can make
      ; or the number of sheep available
      let n-to-link ( 3 - count my-links)      
      let n-sheep-in-radius count ( sheeps with [ color = [color] of myself ] in-radius 5 )
      if n-sheep-in-radius < n-to-link [
        set n-to-link n-sheep-in-radius
      ]

      ; Ask the appropriate sheeps to form a link with
      ; the asking wolf
      ask n-of n-to-link sheeps with [ color = [color] of myself ] in-radius 5  [
        create-participation-with myself [ set color red ]
      ]
    ]
  ]
end