ArgyL
ArgyL

Reputation: 5

Turtle variable registration not working as intended

I am working on a model that counts each interaction of all turtles and presents them in a monitor. My code works, except not as intended. I get way too many variable registrations.

I am somewhat inexperienced with Netlogo. I have already tested my code with as little and as many turtles as possible, yet the variable registration seems to happen on every tick for all turtles regardless of their location.

to interact

  ask citizens with [alerted?] [
    if any? citizens in-radius 3 with [panicked?]
        [ set mood mood - 20
          set total-contagions total-contagions + 1]

    if any? citizens in-radius 3 with [calm?]
        [ set mood mood + 10
          set total-contagions total-contagions + 1]

    if any? citizens in-radius 3 with [alerted?]
        [ set mood mood - 1
          set total-contagions total-contagions + 1]

    ]
    ask citizens with [calm?] [

    if any? citizens in-radius 3 with [alerted?]
      [set mood mood - 5
        set total-contagions total-contagions + 1 ]

    if any? citizens in-radius 3 with [panicked?]
      [set mood mood - 15
        set total-contagions total-contagions + 1 ]

    if any? citizens in-radius 3 with [calm?]
      [set mood mood + 5
        set total-contagions total-contagions + 1]
   ]

  ask citizens with [panicked?] [
    if any? citizens in-radius 3 with [panicked?]
      [ set mood mood - 3
        set total-contagions total-contagions + 1 ]

    if any? citizens in-radius 3 with [alerted?]
      [ set total-contagions total-contagions + 1 ]

    if any? citizens in-radius 3 with [calm?]
      [ set total-contagions total-contagions + 1 ]

   ]

   end

What I need to achieve is every interaction to be counted as one and not many times over the course of the simulation. Furthermore, I need every interaction to be counted only when turtles are in close proximity with each other.

Upvotes: 0

Views: 40

Answers (1)

JenB
JenB

Reputation: 17678

Okay, the problem is that you increase contagions in every block. Look at your example from the comments with 1 panicked, 1 calm and 1 alerted and the three meet. Take the perspective of the alerted turtle (the first part of your code):

  • first test: yes, there is a panicked nearby, so the mood changes and contagions increments
  • second test: yes, there is also a calm nearby, so the mood changes and contagions increases AGAIN
  • third test: yes, there is an alerted nearby (itself), so again contagions increments

The first correction is to change your tests to if any? other citizens ... because including other excludes itself from the test.

The second correction is really a logic issue rather than a coding issue, you need to work out what you really want to count.

Upvotes: 1

Related Questions