Search code examples
netlogo

Creating (& updating count) of link-breed based on presence of other link-breed


I have 6 members who are on one of two teams (so 2 x 3 member teams). Two team members randomly get selected from each team to work on a team task -- the two members simultaneously form a task-link with their respective team task. My goal is to create and then update the count (links-own variable n-interaction) of the number of times the two members on each team interact (interact-link). I have attempted to use ifelse statements calling who numbers but cannot get n-interaction to update. Any ideas would be appreciated.

breed [members member]
breed [team-tasks team-task]

members-own [
  teamID
  tasklink?
  teammembers       
  interactwith ]

team-tasks-own [
  teamID
]

undirected-link-breed [team-links team-link]
undirected-link-breed [task-links task-link]
undirected-link-breed [interact-links interact-link]
interact-links-own [ n-interaction ]


to go
  create-initialform-task         
  dyads-work-task                 
  update-interaction             
  tick
  clear-taskwork
end 

to dyads-work-task                             
  ask n-of 2 members with [teamID = 1]
  [ create-task-link-with one-of team-tasks with [teamID = 1 and completed? = false]
    set tasklink? true
  ]

  ask n-of 2 members with [teamID = 2]
  [ create-task-link-with one-of team-tasks with [teamID = 2 and completed? = false]
    set tasklink? true
  ]
end

to update-interaction                               
  ask members with [ teamID = 1 and tasklink? ]
   [ let my-work-partner (other teammembers with [ teamID = 1 and tasklink? ])
      set interactwith (sentence interactwith my-work-partner)    
  ]
  
  ask members with [ teamID = 2 and tasklink? ]
   [ 
    let my-work-partner (other teammembers with [ teamID = 2 and tasklink? ])
    set interactwith (sentence interactwith [ who ] of my-work-partner) 
   ]
end

to clear-taskwork        
 ask task-links [ die ]   
  ask members
  [ set tasklink? false ]
end


Solution

  • I couldn't get your code to work without create-initialform-task so I created a quick example using simplified code, where I ask two turtles to set their color to the same random color and ask the link between them to update the counter by 1 and set to the same color (color was easy for quick visualisation).

    go-1 starts by choosing the two turtles who will do the task and putting them in a local variable using let, since I will be calling them more than once. Then it executes the task (changes their color). Finally it asks one of the turtles to check their link with the other turtle and ask that link to update its color and counter. The one-of I use in ask link-with one-of other actors feels a bit redundant but link-with only works with a turtle whereas other actors is a turtleset containing one turtle, which is not the same. Hence one-of other actors.

    go-2 stars from the links perspective instead. To me it feels a bit more elegant but it is probably less useful for your purposes. Instead of choosing two turtles, I choose a random link and let it update the color of the turtles at both of its ends.

    links-own [counter]
    
    
    to setup
      
      ca
      
      crt 4 [
        setxy random-xcor random-ycor
        create-links-with other turtles [set counter 0]
      ]
      
    end
    
    to go-1
      
      let random-color random 14 * 10 + 5
      let actors n-of 2 turtles 
      ask actors [ set color random-color ]
      ask one-of actors [
        ask link-with one-of other actors [
          set counter counter + 1
          set color random-color
        ]
      ]
    
    end
    
    
    to go-2
      
      let random-color random 14 * 10 + 5
      ask one-of links [ 
        set counter counter + 1
        set color random-color
        ask both-ends [
          set color random-color
        ]
      ]
      
    end
    

    Two more things I noticed: In your dyads-work-task, you let both turtles of a team create a link with a random task, but nothing in your code ensures that both create a link with the same random task. This is where local variables come in handy.

    to dyads-work-task  
    
      let current-task one-of team-tasks with [teamID = 1 and completed? = false]                     
      ask n-of 2 members with [teamID = 1]
      [ create-task-link-with current-task
        set tasklink? true
      ]
    
    end
    

    In update-interaction you treat team 1 and team 2 differently. Team 1 creates a list of turtles while Team 2 creates a list of numbers (whos).

    Finally, is it necessary to have both an interact-link and a team-link? Can't you just add the interaction counter to the team-link instead since you already have that link in place?