Use undirected links instead of Directed links

a 夏天 提交于 2019-12-11 06:36:24

问题


In my model I use direct links to keep interaction value of each turtle to other turtles and each link has a different value for each end of the links which is exactly what I want and it was really easy to implement, However, I have a performance issue and my model is not working as fast as I think it should work.

Now I am trying different methods to decrease the computation needs. One of the things that crossed my mind is to integrate all directed links to undirected links and put the value of interaction value of end1 and end2 to each other as links attributes for example end1-end2-Relationship-Value and end2-end1-Relationship-Value and Frequency1 Frequency2. This implementation will make my whole model a bit more difficult to debug since the order of links will be much more difficult to keep track of and I use the calculation of these values a lot, so I was wondering if anyone has a better way to increase the performance :)

The reason I thought this might be better is that It will reduce the number of links to half, another method is forgetting links (kill old links or links with less significant relationship (insignificant relationship value and lower frequency of relationship) but this one is not totally compatible with my model setting.

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 

Update 2 : I think I already found a better way (obvious one! Which I should have done in first place) to set popularity, instead of calling it every tick I just can update it only if it has changed, I even think I might don't need The variable called "popularity" every time I need it I just call the my-in-links

*Update 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

Thank to Seth for his comments

Thanks . Marzy.


回答1:


Your plan doesn't sound to me like it would help performance. It sounds just as likely to make things slower as faster.

Have you tried using the profiler extension to see which procedures are the ones using the most CPU? http://ccl.northwestern.edu/netlogo/5.0/docs/profiler.html

UPDATE: (now that code has been provided)

links with [end2 = mySelf] is slow because it has to check every link to see whether it satisfies the given condition. I think you mean [my-in-links] of myself; primitives like my-in-links return answers immediately, without having to check every link.

Similarly, you have any? links with [end1 = Self_Agent and End2 = Other_Agent]. Again, using with means every link has to be checked if satisfies the condition. Instead, write [out-link-neighbor? Other_Agent] of Self_Agent. out-link-neighbor? can check for the existence of a link directly, without having to scan every link.

I have a hunch that eliminating unnecessary uses of with will fix your performance problems. But, one additional, less important note:

Why foreach sort sss? Why not just ask sss? Is there some reason it needs to run in sorted order? ask is faster than foreach plus sort plus ?.




回答2:


Just for summarizing the result of what I have done in the first place to change Directed links to undirected ones which made lots of trouble after that, so I will still use Directed links :

This is the code that I have used:

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

With corrections that Seth suggested I think having more links is better than having more complex computation to find right undirected links (here called Friendships)



来源:https://stackoverflow.com/questions/19749657/use-undirected-links-instead-of-directed-links

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!