Python으로 배우는 이산 사건 시뮬레이션
Dr Diogo Costa
Adjunct Professor, University of Saskatchewan, Canada & CEO of ImpactBLUE-Scientific
현실 문제의 이벤트 발생은 여러 상황의 영향을 받습니다.
대표적 상황:
이러한 상황은 SimPy 이산 이벤트 모델로 표현할 수 있습니다.
예
고객이 의류 매장에 도착
def customer_arrivals(env, shop)
특정 상품을 찾음
env.timeout(search_time)
if shop.available[item]:
제너레이터: 코드 통합
def customer_arrivals(env, shop):while True: yield env.timeout(search_time)# Only buy item if available if shop.available[item]: env.process(shop_buyer(env, item, num_items, shop))
다음 이유로 이벤트가 중단될 수 있습니다:
메서드
simpy_process_1.interrupt()
예
기계가 주기적으로 고장 나 생산 라인을 중단합니다.
def break_machine(env):# Break machine every now and then. while True: # Time between machine fails yield env.timeout(fail_time())# Machine fail: interrupt process simpy_process_1.interrupt()
capacity=5simpy.Resource(env, capacity=5)
예: 주유소의 연료 비축은 정기 보충이 필요합니다
with gas_station.request() as request:# Request one of the gas pumps yield request# Once pump is available: pump fuel liters_request = 10 yield fuel_reserve.get(liters_request) # The "actual" refueling takes time refuel_duration = liters_request / refuel_speedyield env.timeout(refuel_duration)
비트 연산자
a & b, a >> b, a ^ bSimPy에서
SimPy에 유용한 비트 연산자
__and__() 또는 &__or__() 또는 |예: 관객은 자신의 차례가 오거나 매진될 때까지 대기
with counter.request() as my_turn:# Wait until turn or movie is sold out result = yield my_turn | counter.sold_out[movie]# If tickets available if my_turn in result: theater.num_moviegoers[movie] += 1 return# If tickets sold out if my_turn not in result: theater.num_renegers[movie] += 1 return
Python으로 배우는 이산 사건 시뮬레이션