我试图模拟在每一步都有死亡概率的2D中移动的点。我正在尝试学习SimPy,这是我的第一个编程经验。为什么我会得到这个错误?以及如何解决它?谢谢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
IMO,不仅是定义,而且对Move函数的调用也需要在这里进行修改。 –
@AndreySobolev:谢谢,我现在明确表示了。 –
@ Rik Poggi非常感谢。不幸的是,这并没有解决我的代码。是的,我希望移动功能可以更改每个步骤中每个点的坐标。我在xrange(N)中添加了“for i”:“因为我想让这个过程在不同的100个点上执行,而不仅仅是一个。 – Aya