first changes, topology09 running

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