相关疑难解决方法(0)

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

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

python simulation performance agent-based-modeling netlogo

12
推荐指数
1
解决办法
5002
查看次数