from simpy.simulation import * ImportError: No module named simulation

1,283 views
Skip to first unread message

Vahid Nourbakhsh

unread,
Nov 25, 2015, 8:53:22 PM11/25/15
to python-simpy
Hi,


I am trying to run this code I found, but it gives me the following error. Any idea what the problem is and how I can fix it?

Error:
from simpy.simulation import *
ImportError: No module named simulation

Code:
#!/usr/bin/env python

# simulates NMachines machines, plus a queue of jobs waiting to use them 4
# usage: python MMk.py NMachines ArvRate SrvRate MaxSimtime

from random import Random,expovariate
from simpy.simulation import *

# globals
class G:
    Rnd = Random(12345)

class MachineClass(Process):
    SrvRate = None # reciprocal of mean service time
    Busy = [] # busy machines
    Idle = [] # idle machines
    Queue = [] # queue for the machines
    NDone = 0 # number of jobs done so far
    TotWait = 0.0 # total wait time of all jobs done so far, including
    # both queuing and service times
    def __init__(self):
        Process.__init__(self)
        MachineClass.Idle.append(self) # starts idle
    def Run(self):
        while 1:
            # "sleep" until this machine awakened
            yield passivate,self
            MachineClass.Idle.remove(self)
            MachineClass.Busy.append(self)
            # take jobs from the queue as long as there are some there
            while MachineClass.Queue != []:
                # get the job
                J = MachineClass.Queue.pop(0)
                # do the work
                yield hold,self,G.Rnd.expovariate(MachineClass.SrvRate)
                # bookkeeping
                MachineClass.NDone += 1
                MachineClass.TotWait += now() - J.ArrivalTime
            MachineClass.Busy.remove(self)
            MachineClass.Idle.append(self)

class JobClass:
    def __init__(self):
        self.ArrivalTime = now()

class ArrivalClass(Process):
    ArvRate = None
    def __init__(self):
        Process.__init__(self)
    def Run(self):
        while 1:
            # wait for arrival of next job
            yield hold,self,G.Rnd.expovariate(ArrivalClass.ArvRate)
            J = JobClass()
            MachineClass.Queue.append(J)
            # any machine ready?
            if MachineClass.Idle != []:
                reactivate(MachineClass.Idle[0])


def main():
    NMachines = 1 #int(sys.argv[1])
    ArrivalClass.ArvRate =2 #float(sys.argv[2])
    MachineClass.SrvRate =10 #float(sys.argv[3])
#    initialize()
    for I in range(NMachines):
        M = MachineClass()
        activate(M,M.Run())
    A = ArrivalClass()
    activate(A,A.Run())
    MaxSimtime =60 #float(sys.argv[4])
    simulate(until=MaxSimtime)
    print MachineClass.TotWait/MachineClass.NDone
    
if  __name__ == "__main__": main()

Stefan Scherfke

unread,
Nov 26, 2015, 2:42:31 AM11/26/15
to Vahid Nourbakhsh, python-simpy
Hi Vahid,

this is SimPy 2 code. It won’t run with SimPy 3.

Cheers,
Stefan
> --
> You received this message because you are subscribed to the Google Groups "python-simpy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to python-simpy...@googlegroups.com.
> To post to this group, send email to python...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/python-simpy/6e068c27-8b72-4fc4-8dd6-3dd8a5a72c60%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages