DailyReader
DailyReader

Reputation: 45

How to ask links or turtles or variables that are formed at a certain time?

Thank you for the help you provide on the community. So I am trying to create links between agents that come within a radius of 1 of each other. If the turtles do not come within a radius of 1 of each other for 16 ticks, the link automatically dies. So technically, one link might start at tick 200 and has to die at tick 216 unless it is "renewed" however another link can start at tick 250 and has to die at link 266. However, if I use ask links it changes for all the links.

So my question is how to say something like : ask the link that was formed at tick xx set die time at xx+16 if ticks = xx+16 [die].

Also how do I renew the links so when two agents with a link come in contact again they set a new start and die time for the link between them?

Thank you very much. I know I should only ask one question at a time but I guess they might be related and can be solved together. Thanks in advance.

Upvotes: 1

Views: 139

Answers (1)

Matteo
Matteo

Reputation: 2926

Two equivalent ways come to my mind.

One way mimics the logic that you expressed in your question:

links-own [
  time-to-die
]


to create-links
  ask one-of turtles [
    create-links-with one-of other turtles [
      set time-to-die (ticks + 16)
    ]
  ]
end


to check-condition
  ask links [
    if (link-length <= 1) [
      set time-to-die (ticks + 16)
    ]

    if (time-to-die = ticks) [die]
  ]
end

The other way creates a counter where each link keeps track of how long its two ends have been apart:

links-own [
  time-apart
]


to check-condition
  ask links [
    ifelse (link-length > 1)
      [set time-apart time-apart + 1]
      [set time-apart 0]

    if (time-apart = 16) [die]
  ]
end

If we want to be precise, the first approach will be a bit lighter because links will not have to set the value of time-to-die at every tick (while, in the second approach, links set the value of time-apart at every tick), but I guess this advantage might be so small that you should probably just prefer the one that best fits the overall logic of your model.

In any case, the key point is that there is no problem in asking all links to update & check their condition: each link will perform its calculations and checks based on its own situation, so there is no problem in handling different links being born and having to die at different points in time.

Upvotes: 3

Related Questions