global name error in SimPy

761 views Asked by At

I am trying to simulate points moving in 2D which have a probability of dying at every step. I am trying to learn SimPy and this is my first programming experience. Why do I get this error? and how to fix it? Thank you

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()

Error

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

There are 2 answers

0
pod2metra On

Coord not defined in move function You don't give it as argument, in my view

6
Rik Poggi On

I think you need to replace

def Move(self):

with:

def Move(self, coord):

And after call this function passing the new coordinates as argument, something like:

obj.Move((10, 20))

Where in the example (10, 20) are the new object coordinates (I'm not sure that this is what your code does, but I guess it should be the natural behaviour of a function named 'Move').

From the official documentation: When a name is not found at all, a NameError exception is raised.

Names in python (as explained better in Code Like a Pythonista: Idiomatic Python) are what in other languages you call variables. So:

NameError: global name 'coord' is not defined

basically means that the compiler does not know what 'coord' is.

Note: You shouldn't call your variable 'object' shadowing the buil-in [object][3] that is the base class for every class.

Also I can't see the point in doing something like:

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

beacuse is the same as:

obj = Point(circular,r1,r2)

Update: Maybe what you're trying to do something like:

# 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)

I never used SimPy so this is just my wild (and off-topic) guess.

It also seems that you have host not defined in your Move method but maybe is imported with from SimPy.SimulationTrace import *. Using from ... import * is a bad practice because prevent others to know exactly what you're importing from that module (I presume that this was done in the SimPy tutorial for having a quick start, but you should import only what you need).