def test_request(): pm = ProcessManager() dummy_resource = ['test1'] pm.create_resource(dummy_resource) class TestProcess1(Process): def definition(self, user, **kwargs): assert user.requests('test1')[0].triggered == True yield user.waits(1) pm.attach_process(TestProcess1) em = EventManager(pm) em.create_user('user_test') em.run()
def test_release(): pm = ProcessManager() dummy_resource = ['test1'] pm.create_resource(dummy_resource) class TestProcess1(Process): def definition(self, user, **kwargs): user.requests('test1') yield user.waits(1.2) user.releases('test1') pm.attach_process(TestProcess1) em = EventManager(pm) em.create_user('user_test') em.run()
def test_waits_unlimited_patience(): pm = ProcessManager() dummy_resource = ['test1', 'test2'] pm.create_resource(dummy_resource) class TestProcess1(Process): def definition(self, user, **kwargs): require = 'any' arrived = self.env.now yield user.waits(dummy_resource, require=require) now = self.env.now assert now - arrived == 0 pm.attach_process(TestProcess1) em = EventManager(pm) em.create_user('user_test') em.run()
def test_create_resource(): pm = ProcessManager() dummy_resource = ['test1', 'test2'] pm.create_resource(dummy_resource) assert len(pm.rm._store) == 2
RANDOM_SEED = 42 NUM_MACHINES = 2 # Number of machines in the carwash WASHTIME = 5 # Minutes it takes to clean a car T_INTER = 3 # Create a car every ~T_INTER minutes SIM_TIME = 25 # Simulation time in minutes N_INITIAL = 1 # Number of cars in the car wash at the start # Instantiate Process Manager random.seed(RANDOM_SEED) pm = ProcessManager() # Create car wash with NUM_MACHINE machines and attach car wash process pm.create_resource('machines', capacity=NUM_MACHINES) class CarWash(Process): def definition(self, user): # Car arrives and requests the use of a machine yield user.waits('machines') # Car enters the car wash and spends WASHTIME minutes inside yield user.waits(timedelta(minutes=WASHTIME)) # Car leaves the car wash and releases the machine for the next car to use user.set_checkpoint(f'Left the carwash. {random.randint(50, 99)}% of dirt removed.') user.releases('machines') pm.attach_process(CarWash)
# Custom Resource class for bikes class BikeResource(Resource): @property def station(self): # Bike-user and bike-resource have the same name dock = self.rm.get_resources(by_user=self.name) if len(dock) == 0: return None else: return dock[0].station # Creating station docks as resources docks = [random.randint(2, 4) for s in STATIONS] for index, [name, capacity] in enumerate(zip(STATIONS, docks)): for d in range(capacity): pm.create_resource(f'dock_{index}{d}', type='dock', station=name) # Creating bikes as resources (for customers) and users (of docks) bid = 0 dock_resources = pm.get_resources(by_properties={'type': 'dock'}) for dock in dock_resources: if random.uniform(0, 1) < TARGET_OCCUPATION: pm.create_resource( f'bike_{bid}', custom_resource=BikeResource, type='bike' ) em.create_user( f'bike_{bid}', initial_process='IncludeBike', initial_dock=dock
import random from chronon import ProcessManager, EventManager, Process # RANDOM_SEED = 42 NEW_CUSTOMERS = 7 # Total number of customers NUMBER_OF_COUNTERS = 1 # Total number of counters INTERVAL_CUSTOMERS = 3.0 # Generate new customers roughly every x seconds MIN_PATIENCE = 1 # Min. customer patience MAX_PATIENCE = 5 # Max. customer patience # create process manager pm = ProcessManager() # create resource pm.create_resource('counters', capacity=NUMBER_OF_COUNTERS) # process definition class CustomerWaitCounter(Process): def definition(self, user): # time when customer arrives arrive = self.env.now user.set_checkpoint('Arrived') # amount of patience a customer can have patience = random.uniform(MIN_PATIENCE, MAX_PATIENCE) # waiting... yield user.waits('counters', patience=patience) # calculate time waited since customer arrived wait = self.env.now - arrive if wait < patience: # clients go to the counter