0

我正在尝试模拟在 2D 中移动的点,这些点在每一步都有可能死亡。我正在尝试学习 SimPy,这是我的第一次编程体验。为什么我会收到此错误?以及如何解决?谢谢

from SimPy.SimulationTrace import *
import random as RD
import scipy as SP
import math
import matplotlib.pyplot as plt

N=100
r1=0.02
r2=0.03
maxTime=100


class Point(Process):
    def __init__(self,coord,rate1,rate2):
          Process.__init__(self)
          self.x=coord[0]
          self.y=coord[1]
          self.rate1=r1
          self.rate2=r2

    def Move(self):
        RD.uniform(coord[0]-0.1,coord[0]+0.1)
        RD.uniform(coord[1]-0.1,coord[1]+0.1)
        yield hold,self,0.5
        self.x=coord[0]
        self.y=coord[1]
        yield hold,self,0.5

     #   reactivate(self,now())

    def die(self):
        if RD.random() < self.rate2:
          N-=1
          m.observe(N)
          yield cancel,self


initialize()
m=Monitor()
circular=[RD.uniform(0,100),RD.uniform(0,100)]
for object in xrange(N):
   object=Point(circular,r1,r2)   
activate(object,object.Move())
simulate(until=maxTime)
activate(object,object.die())
simulate(until=maxTime)

h=m.histogram(low=0.0,high=100,nbins=100)
g=m.yseries()
plt.plot(g)
plt.show()

错误

Traceback (most recent call last):
  File "C:\Users\dell\Desktop\ask.py", line 46, in <module>
    simulate(until=maxTime)
  File "C:\Python27\lib\site-packages\SimPy\Globals.py", line 61, in simulate
    return sim.simulate(until = until)
  File "C:\Python27\lib\site-packages\SimPy\SimulationTrace.py", line 96, in simulate
    return Simulation.simulate(self, until)
 File "C:\Python27\lib\site-packages\SimPy\Simulation.py", line 581, in simulate
  step()
  File "C:\Python27\lib\site-packages\SimPy\Simulation.py", line 525, in step
  resultTuple = proc._nextpoint.next()
  File "C:\Users\dell\Desktop\ask.py", line 23, in Move
    RD.uniform(coord[0]-0.3,coord[0]+0.3)
NameError: global name 'coord' is not defined
4

2 回答 2

2

我认为你需要更换

def Move(self):

和:

def Move(self, coord):

在调用此函数后,将新坐标作为参数传递,类似于:

obj.Move((10, 20))

示例(10, 20)中的新对象坐标在哪里(我不确定这是您的代码所做的,但我想这应该是名为“移动”的函数的自然行为)。

来自官方文档当根本找不到名称时,会引发 NameError 异常。

python 中的名称(如Pythonista 中的代码:Idiomatic Python中更好地解释)在其他语言中称为变量。所以:

NameError: global name 'coord' is not defined

基本上意味着编译器不知道“坐标”是什么。

注意:object你不应该调用你的变量'object'来隐藏作为每个类的基类的内置[ ][3]。

我也看不出这样做的意义:

for i in xrange(N):   # Notice that I also used a different name here: i
    obj = Point(circular,r1,r2) 

因为是一样的:

obj = Point(circular,r1,r2)

更新:也许您正在尝试执行以下操作:

# maybe you want to put this inside a function so every time you get
# different random numbers
def circular():
     return RD.uniform(0,100), RD.uniform(0,100)

points = []
for i in xrange(N):
    p = Point(circular(), r1, r2)
    points.append(p)
    activate(p, p.Move(circular())

simulate(until=maxTime)

for p in points:
    activate(p, p.die())

simulate(until=maxTime)

我从未使用过 SimPy,所以这只是我的疯狂(和离题)猜测。

似乎您还host没有在方法中定义,Move但可能是使用from SimPy.SimulationTrace import *. 使用from ... import *是一种不好的做法,因为会阻止其他人确切知道您从该模块导入了什么(我认为这是在 SimPy 教程中完成的,以便快速入门,但您应该只导入您需要的内容)。

于 2012-01-23T20:23:10.150 回答
0

在移动函数中未定义坐标在我看来,您不会将其作为参数给出

于 2012-01-23T20:20:02.467 回答