Home > OS >  Iterate through list to update particular items in the list for agents in Netlogo
Iterate through list to update particular items in the list for agents in Netlogo

Time:05-31

This is a follow-up question to my previous question which can be found here: Replace-item for replacing multiple items in a list that are equal to particular [who] in Netlogo I am creating a social intimacy relationship between agents in Netlogo. Each agent has an intimacy list which stores the intimacy value of the agent to every other agent. To do so, I make an agentset of related protesters. For this, I create in protesters-own:

protesters-own [
   partOfGroup         ;initially set to false for all agents
   myNRelatedProtesters
]

Then I create and fill the intimacy list which has the length of the total number of protesters:

to create-intimacyRelationship
  set numberOfProtesters count protesters 
  set intimacyVector []
  repeat numberOfProtesters [set intimacyVector lput 0.2 intimacyVector] 
end

How can I update the intimacy values to 0.8 for the indices that are equal to everyone in the selected group of ungrouped protesters in the following function?:

to updateIntimacy 
  let ProportionProtestersInSubgroup count protesters [partOfGroup = true])/ numberOfProtesters)  
  while [((count protesters with [partOfGroup = false])/ 
numberOfProtesters) > ProportionProtestersInSubgroup] [ 
     let nrUngroupedProtesters (count protesters with [partOfGroup = false])
     set myNRelatedProtesters n-of (random nrUngroupedProtesters) protesters 
     ask myNRelatedProtesters [
       foreach intimacy [ i ->
         set item i intimacy 0.8
       ]
       set partOfGroup true
     ]
   ]
end

to go
  updateIntimacy
  tick  
end

CodePudding user response:

Mirthe,

Here is a complete, minimal reproducible example of what I think you are looking for. Note that one can just paste it into NetLogo and it compiles and runs. I've made some assumptions here - in particular that intimacy is a protesters-own variable, which it was not in the code you provided, but which your textual description seemed to indicate. Again, using who numbers is not a good idea, but that is a different question and answer. If I have time tomorrow, I might be able to provide you with an example of how one might use agentsets, but if intimacy values can vary from agent-pair to agent-pair, then links is really the way to go.

breed [protesters protester]
globals [numberOfProtesters intimacyVector]

protesters-own [
  intimacy
  partOfGroup         ;initially set to false for all agents
  myNRelatedProtesters
]

to setup 
  clear-all
  create-protesters 10
  create-intimacyRelationship
  reset-ticks
end

to create-intimacyRelationship
  ask protesters [
    set numberOfProtesters count protesters 
    set intimacy []
    repeat numberOfProtesters [set intimacy lput 0.2 intimacy] 
    set partOfGroup false
  ]
end

to updateIntimacy 
  let nrUngroupedProtesters (count protesters with [partOfGroup = false])
  let NRelatedProtesters n-of (random nrUngroupedProtesters) protesters 
  ask NRelatedProtesters [
    foreach ([who] of NRelatedProtesters) [ i -> set intimacy replace-item i intimacy 0.8 ]
    set partOfGroup true
  ]
  ask NRelatedProtesters [ show intimacy ]
end

to go
  let ProportionProtestersInSubgroup (count protesters with [partOfGroup = true])/ numberOfProtesters  
  ifelse ((count protesters with [partOfGroup = false])/ 
    numberOfProtesters) > ProportionProtestersInSubgroup 
  [ 
    updateIntimacy
  ]
  [
    stop
  ]
  tick  
end

Hope this gets you started.

CodePudding user response:

You can use the replace-item reporter to update an item in a list.

Let this [ 1 2 3 4 ]
Let new-list replace-item 3 this “a”
Print new-list

Note that this does not affect the original list: it reports a new list with the specified item replaced with the given value.

Changing an item in a list of lists of similar.. but again, the entire list Of lists is created anew.

But maybe use links?

In the case of using a list for a turtle to track its relationship with other turtles, or of groups, links are useful, and simplify managing those relationships, and enable things that are very difficult with lists.

LINKS are just another kind of agent, specifically for recording a relationship between two turtles. They have a pair of built-in variables, end1 and end2 that refer to those two turtles. The -own variables of the link can be used to record properties of the relationship. Like “anniversary” or “affinity” or whatever! Links can be directional, so the “intimacy” value can be different depending on the “direction” of the relationship.

Directed-link-breed [ relationships relationship]
Relationships-own [ intimacy ]

to setup-all-relationships
  Ask protestors
  [ setup-relationship ]
End

To setup-relationship
  ;; link with everyone else 
  Create-relationships-to other protestor
  [ set intimacy .5 ]
End

The relationship between two turtles can be obtained in several ways.

(Link (this turtle) (that turtle))

Refers to the link from this turtle to that turtle.

Out-Link-neighbors is used to get the set of all turtles linked to from this turtle.

You can also use turtles to represent groups, and links to record membership in that group.

In that case, the members of the group are link-neighbors of the group.

While perhaps not a feature of your model, this opens up the possibility of multiple groups and of agents being members of more than one group, or of tracking things like former members.

  • Related