Jamie Joyce
Jamie Joyce

Reputation: 23

NetLogo test for shared turtles-own variable

I am trying to produce a NetLogo model to simulate competition for wood fuel. The model consists of two turtle breeds: "households" and "trees" distributed randomly over the world. Households-own [ fuel-store target radius ]. When fuel-store = 0, households "find" a new tree within the minimum radius 1, increasing radius by 1 if there are no trees until the maximum radius is reached, using the following procedure:

to FindFuelGo
  ask households [
    if fuel-store = 0 [
      set target min-one-of trees in-radius radius [ distance myself ]
      if not any? trees in-radius radius [
        if radius != max-radius [
          set radius radius + 1
       ]
     ]

However as this model is simulating competition, how do I test whether a household shares the same target ( which it will inevitably will as the model runs) as another household, and if it does assign the target to whichever household has the shortest distance to the target? I have tried:

ask households [
 let me self
 let others other households
 if target != nobody [
  if [ target ] of me = [ target ] of others  [

to first at least identify any households with the same variable however this doesn't work. Any thoughts would be greatly appreciated.

Upvotes: 2

Views: 255

Answers (1)

Mars
Mars

Reputation: 8854

[target] of me is returning a single value (you could just use target here), while [target] of others is returning a list of values. Here are some methods that I think would work:

ask households [
  let target1 target
  ask other households [
    if target = target1 [
      ...
    ]
  ]
]

The comparison in the if compares the target of the other household with target1--which is the target of the household that's doing the asking. You could also do it this way:

ask households [
  ask other households [
    if target = [target] of myself [
      ...
    ]
  ]
]

Again, the first target after the if refers to the target of the asked household. myself refers to the asking household, so [target] of myself refers to the same thing that target1 referred to in the previous example. (If you replaced myself with self, it would refer to the asked household.)

Another method would be to have the trees store the identification of any household that's targetting it. For example, if trees had a targetted variable that always referred to a household that's targetting the tree, you could do something like this:

ask households [
  if target != nobody [
    if ([targetted] of target != self) [
      ...
    ]
  ]
]

(self could also be replaced by me if you added let me self as in your original example.) I'm not sure whether that does what you want, but if not, you'll be able to figure out how to modify it.

One more tip that's relevant to the original formulation in the question. You could also replace the two ifs in my last example with:

if target != nobody and [targetted] of target != self [
  ...
]

The test after the and won't be evaluated unless the one before the and is.

Upvotes: 1

Related Questions