Working with index positions on a list in Netlogo

Viewed 610

I'm struggling with the following problem using these variables:

set variablex .5
set threshhold-list [0 .3 .6]
set variable-list [0 0 1]

I have three agenttypes 0,1,2 that correspond to the index position of threshhold-list and variable-list. So Agent 0 has threshold 0 and variable 0, Agent 1 has threshold .3 and variable 0, and Agent 2 has threshold .6 and variable 1.

What I'd like to do is check if any agent has a threshold greater than zero and less than variablex. If so, update that agent's variable on the variable list to variablex. That is, for the variables above I'd like to run logic that produces a new variable-list like this one:

variable-list [0 .5 1]

But if variablex was .7, it would produce [0 .7 .7].

I've got some code I've been hacking away at but I feel like it's way more complicated than the problem and so I'm wondering if someone could point me in the right direction. Thanks so much!

1 Answers

There are a few different ways to approach the problem, but if I was in your situation, I would first write a small reporter that gives me the value that should be stored at each index:

to-report new-value [ i ]
  let t item i threshhold-list
  report ifelse-value (t > 0 and t < variablex)
    [ variablex ] ; the variable's new value should be variable x
    [ item i variable-list ] ; the variable's value should not change
end

Once you have that, you can use either foreach or map to change your variable list:

to update-variables-with-foreach  
  foreach range length variable-list [ i ->
    set variable-list replace-item i variable-list new-value i
  ]
end

to update-variables-with-map
  set variable-list map new-value range length variable-list
end

Here is a somewhat verbose test to check that both versions would give you the expected results:

globals [
  variablex
  threshhold-list
  variable-list
]

to test
  clear-all
  set threshhold-list [0 .3 .6]

  set variablex .5
  set variable-list [0 0 1]
  update-variables-with-foreach
  print variable-list

  set variablex .5
  set variable-list [0 0 1]
  update-variables-with-map
  print variable-list

  set variablex .7
  set variable-list [0 0 1]
  update-variables-with-foreach
  print variable-list

  set variablex .7
  set variable-list [0 0 1]
  update-variables-with-map
  print variable-list

end

That being said, as much as I think it is fun to play with lists, I think you are approaching your problem in a very unnetlogoish way.

NetLogo's world is a world of turtles and patches and links, not a world of arrays and indices and numbers.

You could do something along the lines of:

globals [
  variable-x
]

turtles-own [
  threshhold
  variable
]

to setup
  clear-all
  set variable-x .5  
  (foreach [0 .3 .6] [0 0 1] [ [t v] ->
    create-turtles 1 [
      set threshhold t
      set variable v
    ]
  ])
  ask turtles [ update-variable ]
  ask turtles [ show variable ]
end

to update-variable ; turtle procedure
  if threshhold > 0 and threshhold < variable-x [
    set variable variable-x
  ]
end

I don't know what you're ultimately trying to achieve, but if I could offer general advice, it would be to try to embrace to NetLogo mindset. Every time you're tempted to use an index of some kind in your code, take a step back and think again: there is probably a better (as in "more netlogoish") way to do it.

Related