Netlogo:"this code can't be run by a patch" 尝试调用 pcolor 时?

Netlogo: "this code can't be run by a patch" when trying to call pcolor?

我目前正在通过 Grimm & Railsback 书中的电话推销员 IBM 工作。我确定这很明显,但我不明白为什么会出现错误:

this code can't be run by a patch
error while patch -38 75 running IF
  called by procedure MAKE-CALLS
  called by procedure GO
  called by Button 'step' 

这是有问题的代码(具体来说,"if pcolor = black")。

to make-calls
  ask turtles [
    let territory ( 10 * sqrt size )
    let max-calls floor ( 100 * size )
    let potential-customers patches in-radius territory
    set successful-sales 0
    ifelse count potential-customers <= max-calls
    [
      ask potential-customers[ ;call all customers
        if pcolor = black[
          set pcolor red
          set successful-sales successful-sales + 1
          ]]
      ]  
    [
      ask n-of max-calls potential-customers[ ;call max-calls customers
        if pcolor = black[
          set pcolor red
          set successful-sales successful-sales + 1
          ]]
      ] 
    set total-sales total-sales + successful-sales
  ]
end

我想查看海龟('potential customers')'territory'内的补丁是否为黑色,但海龟(电话推销员)只能拨打一定数量的电话。因此,如果其领土内的补丁数量超过 max-calls,我将检查领土内等于 max-calls 的多个补丁的颜色。​​

如有任何帮助,我们将不胜感激:-)

完整代码:

globals[
  sim-length
  money-size-ratio
  total-sales
  ]

patches-own[
  ;potential customers coloured black, unavailable customers coloured red
  ]

turtles-own[
  ;telemarketers
  funds
  successful-sales
  ]


to setup
  ca
  set sim-length 200
  set money-size-ratio 0.001
  set total-sales 0

  crt initial-num-marketers [
    set size 1.0
    set funds 0.0
    set successful-sales 0
    setxy random-xcor random-ycor
    set shape "circle"
  ]


  ask patches [ set pcolor black ]

end


to go
  reset-phones
  make-calls
  do-accounting
  update-observer
  tick
  if ticks = sim-length [stop]  
end


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



to reset-phones
  ask patches [ set pcolor black ]
end


to make-calls
  ask turtles [
    let territory ( 10 * sqrt size )
    let max-calls floor ( 100 * size )
    let potential-customers patches in-radius territory
    set successful-sales 0
    ifelse count potential-customers <= max-calls
    [
      ask potential-customers[ ;call all customers
        if pcolor = black[
          set pcolor red
          set successful-sales successful-sales + 1
          ]]
      ]  
    [
      ask n-of max-calls potential-customers[ ;call max-calls customers
        if pcolor = black[
          set pcolor red
          set successful-sales successful-sales + 1
          ]]
      ] 
    set total-sales total-sales + successful-sales
  ]
end


to do-accounting
  ask turtles [
    let costs ( size * 50 )
    let income successful-sales * 2
    set funds funds + income - costs
    if funds > growth-param
    [
      let growth floor ( funds - growth-param )
      set size size + ( size * growth * money-size-ratio )
    ]

    if funds < 0 [ die ]
  ]
end


to update-observer
  set-current-plot "number of businesses"
  plot count turtles

  set-current-plot "business size distribution"
  histogram [size] of turtles

  set-current-plot "total sales"
  plot total-sales

end 

问题是successful-sales:它是一个海龟属性,但你要求补丁来设置它。把它到处改成_sales,然后把set _sales 0改成let _sales 0。这引入了一个新的局部变量。现在你的代码应该可以工作了。但是,您不再使用海龟的 successful-sales 属性。摆脱它。如果由于某种原因无法删除它,可以在更新 total-sales 之前将其设置为 _sales