我使用""在此代码中出错在NetLogo中

时间:2015-02-02 21:29:43

标签: netlogo

我正在编写代码,运行模拟时出错。错误是这样的:

 "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"

我正在使用的代码是:

   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

代码越来越多,但它太长了。我认为问题在于收益必须在记者的背景下,但我不确定。

1 个答案:

答案 0 :(得分:4)

[payoff] of turtles with [color = red]报告了列表的收益:所有红海龟的收益。您试图将该列表与数字0进行比较,这会使NetLogo感到困惑。

of原语无疑是有点混乱,因为它可以与单个代理(在这种情况下报告值)或代理集(在这种情况下报告列表)一起使用。

解决此问题的方法取决于您要做的事情。

  • 你想要红鳍鱼的sum收益吗?

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

  • 您想检查至少有一只红龟是否有超过0的收益?在这种情况下,请使用any?

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

  • 您想检查所有红龟是否有超过0的收益?在这种情况下,请使用all?

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

  • 你确定只有一只红海龟而你只想获得那只红海龟吗?使用one-of

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

在任何情况下,您都需要确保比较相同类型的值(例如,两个数字......)