基于代理的仿真:性能问题:Python与NetLogo和Repast

max*_*max 12 python simulation performance agent-based-modeling netlogo

我在Python 3中复制了一小块Sugarscape代理仿真模型.我发现我的代码的性能比NetLogo慢约3倍.它可能是我的代码的问题,还是它可能是Python的固有限制?

显然,这只是代码的一个片段,但是Python花费了三分之二的运行时间.我希望如果我写了一些非常低效的东西,它可能会出现在这个片段中:

UP = (0, -1)
RIGHT = (1, 0)
DOWN = (0, 1)
LEFT = (-1, 0)
all_directions = [UP, DOWN, RIGHT, LEFT]
# point is just a tuple (x, y)
def look_around(self):
    max_sugar_point = self.point
    max_sugar = self.world.sugar_map[self.point].level
    min_range = 0

    random.shuffle(self.all_directions)
    for r in range(1, self.vision+1):
        for d in self.all_directions:
            p = ((self.point[0] + r * d[0]) % self.world.surface.length,
                (self.point[1] + r * d[1]) % self.world.surface.height)
            if self.world.occupied(p): # checks if p is in a lookup table (dict)
                continue
            if self.world.sugar_map[p].level > max_sugar:
                max_sugar = self.world.sugar_map[p].level
                max_sugar_point = p
    if max_sugar_point is not self.point:
        self.move(max_sugar_point)
Run Code Online (Sandbox Code Playgroud)

NetLogo中的代码大致相当(这个片段比上面的Python函数更多):

; -- The SugarScape growth and motion procedures. --
to M    ; Motion rule (page 25)
    locals [ps p v d]
    set ps (patches at-points neighborhood) with [count turtles-here = 0]
    if (count ps > 0) [
        set v psugar-of max-one-of ps [psugar]              ; v is max sugar w/in vision
        set ps ps with [psugar = v]                         ; ps is legal sites w/ v sugar
        set d distance min-one-of ps [distance myself]      ; d is min dist from me to ps agents
        set p random-one-of ps with [distance myself = d]   ; p is one of the min dist patches
        if (psugar >= v and includeMyPatch?) [set p patch-here]
        setxy pxcor-of p pycor-of p                         ; jump to p
        set sugar sugar + psugar-of p                       ; consume its sugar
        ask p [setpsugar 0]                                 ; .. setting its sugar to 0
    ]
    set sugar sugar - metabolism    ; eat sugar (metabolism)
    set age age + 1
end
Run Code Online (Sandbox Code Playgroud)

在我的计算机上,Python代码需要15.5秒才能运行1000步; 在同一台笔记本电脑上,浏览器内部用Java运行的NetLogo模拟在不到6秒的时间内完成了1000步.

编辑:刚刚使用Java实现检查了Repast.它也与NetLogo大约相同,为5.4秒.Java和Python之间最近的比较表明Java没有优势,所以我想这只是我的代码应该归咎于什么?

编辑:我知道MASON应该比Repast更快,但它最终仍然运行Java.

sha*_*ang 11

这可能不会带来显着的加速,但你应该知道,与访问全局变量或属性相比,Python中的局部变量要快得多.因此,您可以尝试将内部循环中使用的一些值分配给本地,如下所示:

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)
Run Code Online (Sandbox Code Playgroud)

Python中的函数调用也具有相对较高的开销(与Java相比),因此您可以通过occupied使用直接字典查找替换函数来尝试进一步优化.

您还应该看看psyco.它是Python的即时编译器,可以在某些情况下显着提高速度.但是,它还不支持Python 3.x,因此您需要使用旧版本的Python.