2013-11-03 22 views
1

在我的模型中,我使用直接鏈接來保持每隻烏龜與其他烏龜的交互作用值,並且每個鏈接對鏈接的每一端都有不同的值,這正是我想要的,它確實是我想要的容易實現,但是,我有一個性能問題,我的模型工作速度不如我認爲它應該工作。使用無向鏈接,而不是定向鏈接

現在我正在嘗試不同的方法來減少計算需求。我想到的一件事是將所有定向鏈接集成到無向鏈接,並將end1和end2的交互值的值彼此作爲鏈接屬性,例如end1-end2-Relationship-Valueend2-end1 - 關係價值頻率1頻率2。這個實現會讓我的整個模型更難以調試,因爲鏈接的順序將難以跟蹤,我使用這些值的計算很多,所以我想知道是否有人有更好的方法來增加性能:)

我認爲這可能更好的原因是它會減少鏈接數量的一半,另一種方法是遺忘鏈接(殺死舊鏈接或關係不太重要的鏈接(無關緊要的價值和較低的頻率關係更新2),但是這個人是不是與我的模型設置完全兼容

agents-own [Belongs_to My-home popularity ] 
patches-own [label_ storage] 
links-own[Value-Of-The-Relationship frequency] 

to Update_link_Values [Self_Agent Other_Agent Value] 
ask Self_Agent 
    [  ifelse any? links with [end1 = Self_Agent and End2 = Other_Agent] 

     [ask out-link-to Other_Agent [ set Value-Of-The-Relationship Value-Of-The-Relationship + Value set Frequency Frequency + 1 ] set hidden? true] 
     [create-link-to Other_Agent [set Value-Of-The-Relationship Value-Of-The-Relationship + Value set Frequency Frequency + 1 ] set hidden? true ] 

    ] 

end 

to SeTPoPularity 
    set Popularity sum[Value-Of-The-Relationship] of links with [end2 = mySelf] 
end 

我想我已經找到了更好的方法(明顯的一個!我應該首先完成這項工作)來設置流行度,而不是每調用一次我都會更新它,我甚至認爲我可能不需要每次需要時都會調用「流行度」變量我只是叫我,在鏈接

* 更新3:*

to Update_link_Values [Self_Agent Other_Agent Value] 
    ask Self_Agent 
     [  ifelse out-link-neighbor? Other_Agent 

      [ask out-link-to Other_Agent [ set Value-Of-The-Relationship Value-Of-The-Relationship + Value set Frequency Frequency + 1 ] set hidden? true] 
      [create-link-to Other_Agent [set Value-Of-The-Relationship Value-Of-The-Relationship + Value set Frequency Frequency + 1 ] set hidden? true ] 

     ] 

end 

感謝塞特對他的評論

感謝。 Marzy。

+0

檢查當前的表現我已經通知後更好d檢查修補程序值是我模擬中的另一個主要性能問題,每個代理程序都有一個My-Home屬性,用於存儲修補程序地址,並且所有家庭成員具有相同的my-home值,每個修補程序存儲屬於全家的屬性值成員,當人口增長並且一個補丁中的人數超過7人時,如果所有家庭成員都死亡,那麼與他的伴侶(如果有的話)的大兒子將遷出並將他的份額轉移到新的家庭補丁中,家庭將被轉移到社區補丁。 – Marzy

+0

您的更新中的所有新問題都感到困惑 - 可能會打開一個或多個新的單獨問題? –

+0

好吧,我只是做了,我完全新到了Stackoverflow!感謝您的輸入:) – Marzy

回答

1

您的計劃對我來說聽起來並不像會有助於提升性能。這聽起來很可能讓事情變得越來越慢。

您是否嘗試過使用Profiler擴展來查看哪些程序是使用最多CPU的程序? http://ccl.northwestern.edu/netlogo/5.0/docs/profiler.html

UPDATE:(現在的代碼已經提供)

links with [end2 = mySelf]是緩慢的,因爲它必須檢查每一個環節,看看它是否滿足給定的條件。我想你的意思是[my-in-links] of myself;像my-in-links這樣的原語立即返回答案,而不必檢查每個鏈接。

同樣,你有any? links with [end1 = Self_Agent and End2 = Other_Agent]。同樣,使用with表示如果滿足條件,則必須檢查每個鏈接。相反,請寫[out-link-neighbor? Other_Agent] of Self_Agentout-link-neighbor?可以直接檢查鏈接的存在,而無需掃描每個鏈接。

我有一個預感,消除不必要的使用with將解決您的性能問題。但是,一個額外的,不太重要的筆記:

爲什麼foreach sort sss?爲什麼不只是ask sss?是否有某些原因需要按排序順序運行? askforeachsort?更快。

+0

謝謝,是的,我已經使用這個擴展,目前我的模擬40000蜱(50-100代理人口的10生命),其中包括個人的社會互動需要15-20分鐘的win7, 32位,2GB RAM,2.4Ghz CPU)。最多的CPU被3個程序使用,一個用於檢查哪個補丁沒有任何代理在其中,並將該補丁的屬性移動到社區級別,即設置代理人氣度的那個,這是每個人的關係值之和-link-neighbor和一個檢查有多少代理人住在一個​​補丁中並要求最老的兒子搬出去的人。 – Marzy

+0

如果您發佈了使用大多數運行時的過程的代碼,我可能會發現加速機會。 (或不,不可能提前知道) –

+0

是的,你是對的:)我會改變代碼,現在改變[移出和設置人氣]功能,我提到的問題在很大程度上提高了性能:) – Marzy

1

只是爲了總結了我在改變定向鏈接,這讓很多之後的麻煩無向那些首先做了結果,所以我還是會使用定向鏈接:

這是代碼我已經使用:

to Update_link_Values [Self_Agent Other_Agent Value] 
ask Self_Agent 
    [  ifelse out-link-neighbor? Other_Agent 

     [ask out-link-to Other_Agent [ set Value-Of-The-Relationship Value-Of-The-Relationship + Value set Frequency Frequency + 1 ] set hidden? true] ;IF already has a link 
     [create-link-to Other_Agent [set Value-Of-The-Relationship Value-Of-The-Relationship + Value set Frequency Frequency + 1 ] set hidden? true ] ;If they meet for the first time 

    ] 

end 

;Update_Friendship_Values 
to Update_Friendship_Values [Self_Agent Other_Agent Value] 
    ask Self_Agent 
    [  
     ifelse any? Friendships with [end1 = Self_Agent and End2 = Other_Agent] 

     [ 
     ask Friendships with [end1 = Self_Agent and End2 = Other_Agent] 
     [ 
      set End1-End2-Value-Of-The-Relationship End1-End2-Value-Of-The-Relationship + Value 
      set End1-End2-Frequency End1-End2-Frequency + 1 
     ] 
     ; set hidden? true 
     ] ;IF already has a link and first agent is end1 
     [ 
     ifelse any? Friendships with [end2 = Self_Agent and End1 = Other_Agent] 

      [ 
      ask Friendships with [end2 = Self_Agent and End1 = Other_Agent] 
      [ 
       set End2-End1-Value-Of-The-Relationship End2-End1-Value-Of-The-Relationship + Value 
       set End2-End1-Frequency End2-End1-Frequency + 1 
      ] 
      ;set hidden? true 
      ] ;IF already has a link and first agent is end2 
      [ ifelse count Other_Agent = 1 
      [create-Friendship-with Other_Agent [ 
       set End1-End2-Value-Of-The-Relationship End1-End2-Value-Of-The-Relationship + Value 
       set End1-End2-Frequency End1-End2-Frequency + 1 
      ]] [ 
      create-Friendships-with Other_Agent [ 
       set End1-End2-Value-Of-The-Relationship End1-End2-Value-Of-The-Relationship + Value 
       set End1-End2-Frequency End1-End2-Frequency + 1] 
      ;set hidden? true 
      ] ] 
     ] 
    ] 

end 

隨着改正塞特建議我認爲有更多的聯繫比擁有更復雜的計算,以找到合適的無向鏈接(這裏稱爲友誼)