【问题标题】:Netlogo: "this code can't be run by a patch" when trying to call pcolor?Netlogo:尝试调用 pcolor 时“此代码不能由补丁运行”?
【发布时间】:2016-07-13 10:50:33
【问题描述】:

我目前正在通过 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' 

这是有问题的代码(特别是“如果 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

我想检查海龟(“潜在客户”)“领土”内的补丁是否为黑色,但海龟(电话推销员)只能拨打一定数量的电话。因此,如果其区域内的补丁数量超过 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 

【问题讨论】:

    标签: netlogo patch


    【解决方案1】:

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

    【讨论】:

    • 啊,谢谢!你是对的,我错过了这个我觉得很愚蠢!我想我是因为指向 if 语句的错误而被抛弃了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    相关资源
    最近更新 更多