Netlogo:当试图调用pcolor时,“此代码无法通过补丁运行”?

时间:2016-07-13 10:50:33

标签: netlogo patch

我目前正在Grimm& amp;的电话推销员IBM工作。 Railsback的书。我确定这是非常明显的,但我无法弄清楚为什么我会收到错误:

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' 

这是有问题的代码(具体来说,“如果pcolor =黑色”)。

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

我想检查海龟“领土”内的补丁(“潜在客户”)是否为黑色,但海龟(电话推销员)只能拨打一定数量的电话。因此,如果其区域内的补丁数超过最大呼叫数,我会检查区域内多个补丁的颜色是否等于最大呼叫数。

任何帮助将不胜感激: - )

完整代码:

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 

1 个答案:

答案 0 :(得分:2)

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