Paul
Paul

Reputation: 189

I get an error in this code using "of" in NetLogo

I am writing a code and i get an error while running the simulation. The error is this:

 "You can only use the operator > on two numbers, two strings, or two agents
    of the same type, but can not be used on a list and a number"

And the code that i am using is this:

   globals
 [
   colors
   ]

  turtles-own
  [
 payoff
  ]

  to setup
  clear-all
  set colors [red green blue]
  create-turtles number
  [
  set color item (who mod 3) (colors)
 setxy random-xcor random-ycor
   ]
  reset-ticks
   end

     to go
    move
    play-piedra
   play-papel
   play-tijeras
    ask turtles [rock]
   ask turtles [papel]
   ask turtles [tijeras]
    end

    to rock
     if [payoff] of turtles with [color = red] > 0
     [set color red]
     if [payoff] of turtles with [color = red] < 0
      [set color green]
      end

There is more code but its too long. I think that the problem is that payoff must be in a reporter context but im not sure.

Upvotes: 0

Views: 80

Answers (1)

Nicolas Payette
Nicolas Payette

Reputation: 14972

[payoff] of turtles with [color = red] reports a list of payoffs: the payoffs of all the red turtles. You're trying to compare that list with the number 0, and that makes NetLogo confused.

The of primitive is admittedly a bit confusing because it can work with either a single agent (in which case it reports a value) or an agentset (in which case it reports a list).

The way to fix this will vary depending on what you're trying to do.

  • Do you want a sum of the red turtle payoffs?

    if sum [payoff] of turtles with [color = red] > 0

  • Do you want to check if at least one red turtle has a payoff greater than 0? In that case, use any?:

    if any? turtles with [ color = red and payoff > 0 ]

  • Do you want to check if all red turtles have a payoff greater than 0? In that case, use all?:

    if all? turtles with [ color = red ] [ payoff > 0 ]

  • Are you sure that there is only one red turtle and you only want the payoff for that one? Use one-of:

    if [ payoff ] of one-of turtles with [color = red] > 0

In any case, you will need to make sure you compare values of the same type (e.g., two numbers...)

Upvotes: 4

Related Questions