使用无向链接而不是定向链接

时间:2013-11-03 03:54:28

标签: performance simulation netlogo multi-agent

在我的模型中,我使用直接链接来保持每只乌龟与其他海龟的交互价值,每个链接对链接的每一端都有不同的值,这正是我想要的,它实际上很容易实现,但是,我有一个性能问题,我的模型工作速度不如我认为应该有效。

现在我正在尝试不同的方法来减少计算需求。我想到的一件事是将所有有向链接集成到无向链接,并将end1和end2的交互值相互作为链接属性,例如 end1-end2-Relationship-Value end2-end1-Relationship-Value Frequency1 Frequency2 。这个实现将使我的整个模型更难以调试,因为链接的顺序将更难以跟踪,我使用这些值的计算很多,所以我想知道是否有人有更好的方法来增加表现:))

我认为这可能更好的原因是它会将链接数量减少一半,另一种方法是忘记链接(杀死旧链接或关系不太重要的链接(关系值无关紧要,关系频率较低)但这个一个与我的模型设置不完全兼容。

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 

更新2: 我想我已经找到了一个更好的方法(显而易见的一个!我应该首先做的)来设置流行度,而不是每次调用它我只是在它改变后才能更新它,我甚至认为我可能不会需要每次我需要它时称为“流行度”的变量我只需要调用 my-in-links

* 更新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

感谢赛斯的评论

谢谢。 马尔齐。

2 个答案:

答案 0 :(得分:1)

你的计划听起来不像是有助于表现。这听起来就像让事情变得更快一样慢。

您是否尝试过使用Profiler扩展程序来查看使用最多CPU的程序? http://ccl.northwestern.edu/netlogo/5.0/docs/profiler.html

更新:(现已提供代码)

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?更快。

答案 1 :(得分: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

通过Seth提出的更正,我认为拥有更多链接比使用更复杂的计算来查找正确的无向链接(此处称为友谊)更好