agent-based simulation: performance issue: Python vs NetLogo & Repast

后端 未结 4 664
滥情空心
滥情空心 2021-02-03 10:17

I\'m replicating a small piece of Sugarscape agent simulation model in Python 3. I found the performance of my code is ~3 times slower than that of NetLogo. Is it likely the pro

相关标签:
4条回答
  • 2021-02-03 10:56

    This probably won't give dramatic speedups, but you should be aware that local variables are quite a bit faster in Python compared to accessing globals or attributes. So you could try assigning some values that are used in the inner loop into locals, like this:

    def look_around(self):
        max_sugar_point = self.point
        max_sugar = self.world.sugar_map[self.point].level
        min_range = 0
    
        selfx = self.point[0]
        selfy = self.point[1]
        wlength = self.world.surface.length
        wheight = self.world.surface.height
        occupied = self.world.occupied
        sugar_map = self.world.sugar_map
        all_directions = self.all_directions
    
        random.shuffle(all_directions)
        for r in range(1, self.vision+1):
            for dx,dy in all_directions:
                p = ((selfx + r * dx) % wlength,
                    (selfy + r * dy) % wheight)
                if occupied(p): # checks if p is in a lookup table (dict)
                    continue
                if sugar_map[p].level > max_sugar:
                    max_sugar = sugar_map[p].level
                    max_sugar_point = p
        if max_sugar_point is not self.point:
            self.move(max_sugar_point)
    

    Function calls in Python also have a relatively high overhead (compared to Java), so you can try to further optimize by replacing the occupied function with a direct dictionary lookup.

    You should also take a look at psyco. It's a just-in-time compiler for Python that can give dramatic speed improvements in some cases. However, it doesn't support Python 3.x yet, so you would need to use an older version of Python.

    0 讨论(0)
  • 2021-02-03 10:57

    I'm going to guess that the way that neighborhood is implemented in NetLogo is different from the double loop you have. Specifically, I think they pre-calculate a neighborhood vector like

    n = [ [0,1],[0,-1],[1,0],[-1,0]....]
    

    (you would need a different one for vision=1,2,...) and then use just one loop over n instead of a nested loop like you are doing. This eliminates the need for the multiplications.

    I don't think this will get you 3X speedup.

    0 讨论(0)
  • 2021-02-03 10:59

    Here is a relatively up to date comparison of NetLogo and one version of Repast. I would not necessarily assumed Repast is faster. NetLogo seems to contain some very smart algorithms that can make up for whatever costs it has. http://condor.depaul.edu/slytinen/abm/Lytinen-Railsback-EMCSR_2012-02-17.pdf

    0 讨论(0)
  • 2021-02-03 11:05

    This is an old question, but I suggest you look into using NumPy for speeding up your operations. Places where you use dicts and lists which are logically organized (1-, 2-, 3-, or N-dimensional grid) homogenous data object (all integers, or all floats, etc) will have less overhead when represented and accessed as Numpy arrays.

    http://numpy.org

    0 讨论(0)
提交回复
热议问题