0

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
    One thing you might try in an attempt to isolate the problem is to build the code in steps. So start by `ask other agentes in-radius sight-radius with [shared-culture = [shared-culture] of other agentes] [ set color red]` to see if there are `agentes` to create links with. Then create links with all of them. Then do the `n-of`. Steps like this help you work out which bit of your code is broken. – JenB Jun 22 '17 at 15:36

1 Answers1

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
Luke C
  • 10,081
  • 1
  • 14
  • 21
  • I'm sorry if I wasn't clear enough. You understood correctly what I need and what I have. I see what your point with the wolfs and sheeps example but since you understood correctly... I tried with your suggestion but it prints a new error: "Expected a literal value", for the `[shared-culture = [shared-culture] myself] [` part. For better understanding, the setup of shared-culture: `ask agentes [ set culture-tags n-values 17 [random 2] set shared-culture (filter [ i -> i = 0 ] culture-tags)` I thought it could be a problem of `shared-culture` being a list but... I don't know. – Jorge Martínez Jun 22 '17 at 14:31
  • Oh, I'm very sorry- I made a typo. I meant to say `[shared-culture = [shared-culture] of myself ]`. I dropped the **of**. Does it print the same error with that change? – Luke C Jun 22 '17 at 15:26
  • No problem, of course. I think I already wrote this piece of code once, trying to figure the problem out. But it gave me the next runtime error when I did and it gave me the same error now: Requested 19 random agents (the `n-capacity` slider I set) from a set of only 0 agents. error while activity 162 running N-OF called by procedure PARTICIPATE called by procedure GO called by Button 'go' Could it be that when I press `go` no "agentes" with that characteristics are near? – Jorge Martínez Jun 22 '17 at 20:34
  • Yep, that sounds correct to me! If you check in the wolves/sheep code, I included a modifier to make sure that if there were no sheep in the wolf's radius, it wouldn't try to make links with them (the `if n-sheep-in-radius < n-to-link` bit). – Luke C Jun 22 '17 at 21:00
  • Thank you a lot! It's finally working. The final code: `to participate ask activities [ if count my-links < n-capacity [ let n-to-link (n-capacity - count my-links) let n-agentes-in-radius count ( agentes with [shared-culture = [shared-culture] of myself] in-radius sight-radius) if n-agentes-in-radius < n-to-link [ set n-to-link n-agentes-in-radius ] ask n-of n-to-link agentes with [shared-culture = [shared-culture] of myself] in-radius sight-radius [ create-participation-with myself [set color red] ] ] ] end` – Jorge Martínez Jun 23 '17 at 09:11