2016-07-13 36 views
1

我目前正在通过Grimm & Railsback的电话推销员IBM的书。我敢肯定,这东西真的很明显,但我不明白,为什么我得到的错误:Netlogo:尝试调用pcolor时,此代码无法通过修补程序运行?

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 

回答

2

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

+0

啊谢谢!你是对的,我错过了这个愚蠢的想法!我想我因为指向if语句的错误而被抛弃了。 – alexg

相关问题