first changes, topology09 running

parent f9392dab
This diff is collapsed.
......@@ -25,7 +25,8 @@ Created on 6 Feb 2013
models the exit of the model
'''
from SimPy.Simulation import now, Process, Resource, infinity, waituntil, waitevent
# from SimPy.Simulation import now, Process, Resource, infinity, waituntil, waitevent
import simpy
import xlwt
from CoreObject import CoreObject
......@@ -54,7 +55,7 @@ class Exit(CoreObject):
CoreObject.initialize(self)
# initialize the internal Queue (type Resource) of the Exit
self.Res=Resource(capacity=infinity)
self.Res=simpy.Resource(self.env, capacity=10000)
# The number of resource that exited through this exit.
# XXX bug: cannot output as json when nothing has exited.
self.numOfExits=0
......@@ -68,7 +69,8 @@ class Exit(CoreObject):
def run(self):
while 1:
# wait until the Queue can accept an entity and one predecessor requests it
yield waitevent, self, self.isRequested
yield self.isRequested
self.isRequested=self.env.event()
# TODO: insert extra controls to check whether the self.giver attribute is correctly updated
self.getEntity()
......@@ -109,13 +111,13 @@ class Exit(CoreObject):
# if activeEntity in G.EntityList:
# G.EntityList.remove(activeEntity)
# self.clear(activeEntity)
self.totalLifespan+=now()-activeEntity.startTime #Add the entity's lifespan to the total one.
self.totalLifespan+=self.env.now-activeEntity.startTime #Add the entity's lifespan to the total one.
self.numOfExits+=1 # increase the exits by one
self.totalNumberOfUnitsExited+=activeEntity.numberOfUnits # add the number of units that xited
self.totalTaktTime+=now()-self.timeLastEntityLeft # add the takt time
self.timeLastEntityLeft=now() # update the time that the last entity left from the Exit
self.totalTaktTime+=self.env.now-self.timeLastEntityLeft # add the takt time
self.timeLastEntityLeft=self.env.now # update the time that the last entity left from the Exit
activeObjectQueue=self.getActiveObjectQueue()
del self.Res.activeQ[:]
del self.Res.users[:]
return activeEntity
@staticmethod
......
......@@ -40,7 +40,8 @@ import numpy
numpy.seterr(all='raise')
from SimPy.Simulation import activate, initialize, simulate, now, infinity
# from SimPy.Simulation import activate, initialize, simulate, now, infinity
import simpy
from Globals import G
from Source import Source
from Machine import Machine
......@@ -885,11 +886,14 @@ def initializeObjects():
# ===========================================================================
def activateObjects():
for element in G.ObjList:
activate(element, element.run())
# activate(element, element.run())
G.env.process(element.run())
for ev in G.EventGeneratorList:
activate(ev, ev.run())
# activate(ev, ev.run())
G.env.process(ev.run())
for oi in G.ObjectInterruptionList:
activate(oi, oi.run())
# activate(oi, oi.run())
G.env.process(oi.run())
# ===========================================================================
# reads the WIP of the stations
......@@ -1261,6 +1265,7 @@ def main(argv=[], input_data=None):
#read the input from the JSON file and create the line
G.JSONData=json.loads(G.InputData) # create the dictionary JSONData
readGeneralInput()
G.env=simpy.Environment() # initialize the environment
createObjects()
createObjectInterruptions()
setTopology()
......@@ -1272,7 +1277,7 @@ def main(argv=[], input_data=None):
G.Rnd=Random('%s%s' % (G.seed, i))
else:
G.Rnd=Random()
initialize() #initialize the simulation
# initialize() #initialize the simulation
createWIP()
initializeObjects()
Globals.setWIP(G.EntityList)
......@@ -1281,22 +1286,24 @@ def main(argv=[], input_data=None):
# if the simulation is ran until no more events are scheduled,
# then we have to find the end time as the time the last entity ended.
if G.maxSimTime==-1:
simulate(until=infinity) # simulate until there are no more events.
# If someone does it for a model that has always events, then it will run forever!
G.env.run(until=infinity)
# simulate(until=infinity) # simulate until there are no more events.
# # If someone does it for a model that has always events, then it will run forever!
# # identify from the exits what is the time that the last entity has ended.
# endList=[]
# for exit in G.ExitList:
# endList.append(exit.timeLastEntityLeft)
# G.maxSimTime=float(max(endList))
# identify the time of the last event
if now()!=0: #do not let G.maxSimTime=0 so that there will be no crash
G.maxSimTime=now()
if G.env.now!=0: #do not let G.maxSimTime=0 so that there will be no crash
G.maxSimTime=env.now
else:
print "simulation ran for 0 time, something may have gone wrong"
logger.info("simulation ran for 0 time, something may have gone wrong")
#else we simulate until the given maxSimTime
else:
simulate(until=G.maxSimTime) #simulate until the given maxSimTime
else:
G.env.run(until=G.maxSimTime)
# simulate(until=G.maxSimTime) #simulate until the given maxSimTime
#carry on the post processing operations for every object in the topology
for element in G.ObjList:
......
This diff is collapsed.
......@@ -26,7 +26,8 @@ Created on 8 Nov 2012
models the source object that generates the entities
'''
from SimPy.Simulation import now, Process, Resource, infinity, hold, SimEvent, activate, waitevent
# from SimPy.Simulation import now, Process, Resource, infinity, hold, SimEvent, activate, waitevent
import simpy
from RandomNumberGenerator import RandomNumberGenerator
from CoreObject import CoreObject
from Globals import G
......@@ -36,20 +37,20 @@ import Globals
#============================================================================
# the EntityGenerator object
#============================================================================
class EntityGenerator(Process):
class EntityGenerator(object):
#===========================================================================
# the __init__ method of the EntityGenerator
#===========================================================================
def __init__(self, victim=None):
Process.__init__(self)
self.env=G.env
self.type="EntityGenerator" #String that shows the type of object
self.victim=victim
#===========================================================================
# initialize method of the EntityGenerator
#===========================================================================
def initialize(self):
Process.__init__(self)
# #===========================================================================
# # initialize method of the EntityGenerator
# #===========================================================================
# def initialize(self):
# Process.__init__(self)
#===========================================================================
# the generator of the EntitiesGenerator
......@@ -59,8 +60,8 @@ class EntityGenerator(Process):
# if the Source is empty create the Entity
if len(self.victim.getActiveObjectQueue())==0:
entity=self.victim.createEntity() # create the Entity object and assign its name
entity.creationTime=now() # assign the current simulation time as the Entity's creation time
entity.startTime=now() # assign the current simulation time as the Entity's start time
entity.creationTime=self.env.now # assign the current simulation time as the Entity's creation time
entity.startTime=self.env.now # assign the current simulation time as the Entity's start time
entity.currentStation=self.victim # update the current station of the Entity
G.EntityList.append(entity)
self.victim.outputTrace(entity.name, "generated") # output the trace
......@@ -68,13 +69,13 @@ class EntityGenerator(Process):
self.victim.numberOfArrivals+=1 # we have one new arrival
G.numberOfEntities+=1
self.victim.appendEntity(entity)
self.victim.entityCreated.signal(entity)
self.victim.entityCreated.succeed(entity)
# else put it on the time list for scheduled Entities
else:
#print now(), 'appending to the list'
self.victim.scheduledEntities.append(now())
#print self.env.now, 'appending to the list'
self.victim.scheduledEntities.append(self.env.now)
self.victim.outputTrace(entity.name, "generated") # output the trace
yield hold,self,self.victim.calculateInterarrivalTime() # wait until the next arrival
yield self.env.timeout(self.victim.calculateInterarrivalTime()) # wait until the next arrival
#============================================================================
# The Source object is a Process
......@@ -104,9 +105,11 @@ class Source(CoreObject):
self.entityGenerator=EntityGenerator(victim=self) # the EntityGenerator of the Source
self.entityCreated=SimEvent('an entity is created')
# self.entityCreated=SimEvent('an entity is created')
self.entityCreated=self.env.event()
# event used by router
self.loadOperatorAvailable=SimEvent('loadOperatorAvailable')
# self.loadOperatorAvailable=SimEvent('loadOperatorAvailable')
self.loadOperatorAvailable=self.env.event()
self.scheduledEntities=[] # list of creations that are scheduled
......@@ -118,15 +121,19 @@ class Source(CoreObject):
CoreObject.initialize(self)
# initialize the internal Queue (type Resource) of the Source
self.Res=Resource(capacity=infinity)
self.Res.activeQ=[]
self.Res.waitQ=[]
# self.Res=Resource(capacity=infinity)
self.Res=simpy.Resource(self.env, capacity=10000)
self.Res.users=[]
# self.Res.waitQ=[]
self.numberOfArrivals = 0
self.entityGenerator.initialize()
activate(self.entityGenerator,self.entityGenerator.run())
self.entityCreated=SimEvent('an entity is created')
# self.entityGenerator.initialize()
# activate(self.entityGenerator,self.entityGenerator.run())
self.env.process(self.entityGenerator.run())
# self.entityCreated=SimEvent('an entity is created')
self.entityCreated=self.env.event()
# event used by router
self.loadOperatorAvailable=SimEvent('loadOperatorAvailable')
# self.loadOperatorAvailable=SimEvent('loadOperatorAvailable')
self.loadOperatorAvailable=self.env.event()
self.scheduledEntities=[] # list of creations that are scheduled
#===========================================================================
......@@ -138,16 +145,18 @@ class Source(CoreObject):
activeObjectQueue=self.getActiveObjectQueue()
while 1:
# wait for any event (entity creation or request for disposal of entity)
yield waitevent, self, [self.entityCreated, self.canDispose, self.loadOperatorAvailable]
receivedEvent=yield self.entityCreated | self.canDispose | self.loadOperatorAvailable
# if an entity is created try to signal the receiver and continue
if self.entityCreated.signalparam:
self.entityCreated.signalparam=None
if self.entityCreated in receivedEvent:
self.entityCreated=self.env.event()
if self.signalReceiver():
continue
# otherwise, if the receiver requests availability then try to signal him if there is anything to dispose of
if self.canDispose.signalparam or self.loadOperatorAvailable.signalparam:
self.canDispose.signalparam=None
self.loadOperatorAvailable.signalparam=None
if self.canDispose in receivedEvent or self.loadOperatorAvailable in receivedEvent:
# self.canDispose.signalparam=None
self.canDispose=self.env.event()
# self.loadOperatorAvailable.signalparam=None
self.loadOperatorAvailable=self.env.event()
if self.haveToDispose():
if self.signalReceiver():
continue
......@@ -188,7 +197,7 @@ class Source(CoreObject):
newEntity=self.createEntity() # create the Entity object and assign its name
newEntity.creationTime=self.scheduledEntities.pop(0) # assign the current simulation time as the Entity's creation time
newEntity.startTime=newEntity.creationTime # assign the current simulation time as the Entity's start time
#print now(), 'getting from the list. StartTime=',newEntity.startTime
#print self.env.now, 'getting from the list. StartTime=',newEntity.startTime
newEntity.currentStation=self # update the current station of the Entity
G.EntityList.append(newEntity)
self.getActiveObjectQueue().append(newEntity) # append the entity to the resource
......@@ -197,5 +206,5 @@ class Source(CoreObject):
self.appendEntity(newEntity)
activeEntity=CoreObject.removeEntity(self, entity) # run the default method
if len(self.getActiveObjectQueue())==1:
self.entityCreated.signal(newEntity)
self.entityCreated.succeed(newEntity)
return activeEntity
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment