2014-11-21 22 views
2

我想調試我的NetLogo代碼, 我想從鄰居的概率創建一個列表,然後使用'reduce'將列表中的所有這些值相乘。據我可以告訴這是工作,除非它運行它給運行時錯誤如下。使用減少在NetLogo中乘以一個列表

我也嘗試過使用'let P reduce [?1 *?2] prob-list',但它給出了相同的錯誤。

注意Prob_water_water_breaking,Prob_solute_solute_breaking被定義爲'globals',然後在設置時將它們分配給特定的品種。

to-report all-probabilities 
    let prob-list (list ([Prob_water_water_breaking ] of turtles-on neighbors4) 
     ([Prob_solute_solute_breaking ] of turtles-on neighbors4) 
    let P reduce * prob-list ;;this is the line that's causing the error 
    report P 
    end 

這是侏儒時錯誤:

* expected input to be a number but got the list [0.3 0.3 0.3] instead. 
error while solute 2 running * 
    called by procedure ALL-PROBABILITIES 
    called by procedure GO 
    called by Button 'go' 

任何幫助將是非常讚賞,非常感謝!

回答

2

啊!我喜歡涉及reduce的問題。但是,在你的情況下,問題是用線:

let prob-list (list ([Prob_water_water_breaking ] of turtles-on neighbors4) 
     ([Prob_solute_solute_breaking ] of turtles-on neighbors4) 

你(和reduce)期待號碼的單列表,但prob-list實際上是列出的數字的列表:每次調用of產生一個列表,並且這些列表中的每一個都成爲您使用list創建的列表中的一個項目。

既然你想連接你的列表放在一起,你可以使用sentence代替list

let prob-list (sentence ([Prob_water_water_breaking ] of turtles-on neighbors4) 
     ([Prob_solute_solute_breaking ] of turtles-on neighbors4)) 

另一個(可能更好)的方式來避免該問題將執行of中的第一個乘法:

let prob-list [ Prob_water_water_breaking * Prob_solute_solute_breaking ] 
    of turtles-on neighbors4 
+0

非常感謝Nicolas!這已經解決了問題!我非常感謝你的幫助! – 2014-11-22 00:52:21