Search code examples
netlogoagent-based-modeling

Netlogo: variable sometimes list sometimes number, results in error


I have a reporter that works fine when I run it but mistakingly when I add a condition to it.

All my turtles have two three dimensional vectors called var_aand var_b. When I run this for my whole world there's no problem:

to-report turtle-bounds [p]
      let p-lower (([item 0 var_a] of p) - ([item 0 var_b] of p))
      let p-upper (([item 0 var_a] of p) + ([item 0 var_b] of p))
      let bounds list p-lower p-upper
      report bounds
end

But when I run it with a condition,

to condition
    let p1 turtles with-max [item 0 var_a]
    turtle-bounds p1
end

I get the following:

  • expected input to be a number but got the list [0.9967359117803329] instead.

Which is referencing a value of var_a, meaning that my restriction somehow makes the [item 0 var_a] of p give a list instead of a number.

Any thoughts?


Solution

  • turtle-bounds is written to take a single agent as its argument, but with-max returns an agentset. You can turn the agentset into an agent by using the one-of primitive before giving p1 to turtle-bounds.

    to condition
        let p1 turtles with-max [item 0 var_a]
        turtle-bounds one-of p1
    end
    

    Alternatively, you could check p in turtle-bounds to see if it is an agentset

    if is-agentset? p [set p one-of p]
    

    and make the conversion there, especially if there are other occasions where turtle-bounds might be fed an agentset rather than an agent.