SimPy中的全局名称错误

0 投票
2 回答
748 浏览
提问于 2025-04-17 10:55

我正在尝试模拟在二维空间中移动的点,这些点在每一步都有可能消亡。我正在学习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

2 个回答

0

在移动函数中没有定义坐标。你没有把它作为参数传递,我觉得这样不太对。

2

我觉得你需要把

def Move(self):

换成:

def Move(self, coord):

然后在调用这个函数时,把新的坐标作为参数传进去,像这样:

obj.Move((10, 20))

在这个例子中,(10, 20) 是新的对象坐标(我不确定你的代码是否这样做,但我想这应该是一个叫“Move”的函数的正常行为)。

根据官方文档当一个名字完全找不到时,会抛出一个NameError异常。

在Python中,名字(在像Pythonista一样编写代码:惯用Python中解释得更清楚)就是在其他语言中你称之为变量的东西。所以:

NameError: global name 'coord' is not defined

基本上意味着编译器不知道'coord'是什么。

注意:你不应该把你的变量命名为'object',因为这会遮蔽内置的object类,它是所有类的基类。

另外,我也看不出这样做有什么意义:

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,所以这只是我随便的(而且不相关的)猜测。

看起来你在Move方法中没有定义host,但可能是通过from SimPy.SimulationTrace import *导入的。使用from ... import *是一种不好的做法,因为这会让别人不知道你从那个模块具体导入了什么(我猜这是在SimPy教程中为了快速入门而这样做的,但你应该只导入你需要的东西)。

撰写回答