From c24d92212b58fc166db11a7956021312a1bc7724 Mon Sep 17 00:00:00 2001 From: Naimeh Sadeghi Date: Thu, 16 Dec 2021 15:49:28 +0330 Subject: [PATCH] fixed bugs in priority queue and canceling resoure requests --- .../Earth Moving 2-plus breakdown (1).py | 89 ++++++++++++++ src/des_tests/example.py | 24 ---- src/des_tests/pr_cpm (1).py | 114 ++++++++++++++++++ src/des_tests/test_repair_earthmoving.py | 4 +- src/pmpy/des.py | 17 ++- 5 files changed, 217 insertions(+), 31 deletions(-) create mode 100644 src/des_tests/Earth Moving 2-plus breakdown (1).py delete mode 100644 src/des_tests/example.py create mode 100644 src/des_tests/pr_cpm (1).py diff --git a/src/des_tests/Earth Moving 2-plus breakdown (1).py b/src/des_tests/Earth Moving 2-plus breakdown (1).py new file mode 100644 index 0000000..341d115 --- /dev/null +++ b/src/des_tests/Earth Moving 2-plus breakdown (1).py @@ -0,0 +1,89 @@ +#******Earth moving simulation(plus breakdown)******* (meeting #17) +import os,sys,inspect +currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) +parentdir = os.path.dirname(currentdir) +sys.path.insert(0,parentdir) + +import pmpy.des as des +import numpy as np +def truck_process(truck: des.entity,loader_1,loader_2,dumped_dirt): + while True: + yield truck.get(loader_1,1,2)|truck.get(loader_2,1,2) + yield truck.do("wait",0) + if truck.not_pending(loader_1): + L=loader_1 + truck.cancel(loader_2,1) + elif truck.not_pending(loader_2): + L=loader_2 + truck.cancel(loader_1,1) + if truck.env.now>500: + print("here") + start_load_list.append(env.now) + yield truck.do("loading",truck.attr["load_dur"]) + if truck.env.now>500: + print("here") + if L not in truck.using_resources: + print("why") + yield truck.put(L,1) + if dumped_dirt.level()>10000: + break + yield truck.add(dumped_dirt,truck.attr["capacity"]) + start_haul_list.append(env.now) + yield truck.do("hauling",truck.attr["haul_dur"]) + yield truck.do("dumping",truck.attr["dump_dur"]) + yield truck.do("returning",truck.attr["return_dur"]) + +def loader_breakdown_proc(repair_man,loader): + while True: + yield repair_man.do("wait",loader.attr['time_betwin_break']) + yield repair_man.get(loader,1,1) + yield repair_man.do("repairing",loader.attr['time_to_fix']) + yield repair_man.put(loader,1) + + + +env=des.environment() +dumped_dirt=des.resource(env,"dumped_dirt",init=0,capacity=150000) +start_load_list=[] +start_haul_list=[] +large_trucks=[] +small_trucks=[] +loader_1=des.priority_resource(env,"Loader1",1,print_actions=True) +loader_1.attr['time_betwin_break']=500 +loader_1.attr['time_to_fix']=20 +loader_2=des.priority_resource(env,"Loader2",1,print_actions=True) +loader_2.attr['time_betwin_break']=600 +loader_2.attr['time_to_fix']=25 +for i in range(4): + large_trucks.append(des.entity(env,"truck")) + large_trucks[i].attr["capacity"]=20 + large_trucks[i].attr["load_dur"]=5 + large_trucks[i].attr["haul_dur"]=35 + large_trucks[i].attr["dump_dur"]=2 + large_trucks[i].attr["return_dur"]=20 + p=env.process(truck_process(large_trucks[i],loader_1,loader_2,dumped_dirt)) +for i in range(3): + small_trucks.append(des.entity(env,"truck")) + small_trucks[i].attr["capacity"]=15 + small_trucks[i].attr["load_dur"]=3 + small_trucks[i].attr["haul_dur"]=25 + small_trucks[i].attr["dump_dur"]=1.5 + small_trucks[i].attr["return_dur"]=13 + env.process(truck_process(small_trucks[i],loader_1,loader_2,dumped_dirt)) + + +repair_man=des.entity(env,"repair_man",print_actions=True) +env.process(loader_breakdown_proc(repair_man,loader_1)) +env.process(loader_breakdown_proc(repair_man,loader_2)) +env.run(until=p) +production_rate=(dumped_dirt.level()/env.now) +print("Production Rate is:",production_rate,"m3/minute") +l2=np.array(start_load_list) +l1=np.array(start_haul_list) +#loader_utilization_time=l1-l2 +#loader_utilization_sum=(sum(loader_utilization_time)) +#loader_utilization=(loader_utilization_sum/env.now) +#print("Loader Utilization is:",loader_utilization) +#loader_idleness=1-loader_utilization +#print("Loader Idleness is:",loader_idleness) +print("simulation time: ",env.now) diff --git a/src/des_tests/example.py b/src/des_tests/example.py deleted file mode 100644 index 74a5da5..0000000 --- a/src/des_tests/example.py +++ /dev/null @@ -1,24 +0,0 @@ -import pmpy.simulation.discrete_event as des - -def earthmoving(truck,res): - global dirt - while dirt>100: - yield truck.get(res,1) - yield truck.do('loading',5) - truck.put(res,1) - yield truck.do('hauling',30) - yield truck.do('dumping',5) - yield truck.do('returning',20) - dirt-=100 - print(dirt) - -dirt=500 - -env=des.Environment() -loader = des.Resource(env,'loader', capacity=1) - -truck= des.Entity(env,'truck',print_actions=True) -truck2= des.Entity(env,'truck2',print_actions=False) -env.process(earthmoving(truck,loader)) -env.process(earthmoving(truck2,loader)) -env.run() diff --git a/src/des_tests/pr_cpm (1).py b/src/des_tests/pr_cpm (1).py new file mode 100644 index 0000000..32a78bc --- /dev/null +++ b/src/des_tests/pr_cpm (1).py @@ -0,0 +1,114 @@ +import os,sys,inspect +currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) +parentdir = os.path.dirname(currentdir) +sys.path.insert(0,parentdir) + +import pmpy.des as des + +def prcpm(list_priority): + def a0(e,r): + yield e.get(r, 1,list_pr[0]) + yield e.do("activity0",3) + yield e.put(r,1) + e.attr["finish_time"]=env.now + + + + def a1(e,r): + yield p0 + if en[0].attr["finish_time"]==env.now: + en[0].attr["critical"]=True + else: + en[0].attr["critical"]=False + yield e.get(r, 3,list_pr[1]) + yield e.do("activity1",6) + yield e.put(r,3) + e.attr["finish_time"]=env.now + + def a2(e,r): + yield p0 + if en[0].attr["finish_time"]==env.now: + en[0].attr["critical"]=True + else: + en[0].attr["critical"]=False + yield e.get(r, 2,list_pr[2]) + yield e.do("activity2",5) + yield e.put(r,2) + e.attr["finish_time"]=env.now + def a3(e,r): + yield p1&p2 + if en[1].attr["finish_time"]==env.now: + en[1].attr["critical"]=True + else: + en[1].attr["critical"]=False + if en[2].attr["finish_time"]==env.now: + en[2].attr["critical"]=True + else: + en[2].attr["critical"]=False + yield e.get(r, 3,list_pr[3]) + yield e.do("activity3",1) + yield e.put(r,3) + e.attr["finish_time"]=env.now + + def a4(e,r): + yield p1 + if en[1].attr["finish_time"]==env.now: + en[1].attr["critical"]=True + else: + en[1].attr["critical"]=False + yield e.get(r, 1,list_pr[4]) + yield e.do("activity4",3) + yield e.put(r,1) + e.attr["finish_time"]=env.now + + def a5(e,r): + yield p2 + if en[2].attr["finish_time"]==env.now: + en[2].attr["critical"]=True + else: + en[2].attr["critical"]=False + yield e.get(r, 2,list_pr[5]) + yield e.do("activity5",4) + yield e.put(r,2) + e.attr["finish_time"]=env.now + + + def a6(e,r): + yield p4&p5&p3 + if en[3].attr["finish_time"]==env.now: + en[3].attr["critical"]=True + else: + en[3].attr["critical"]=False + if en[4].attr["finish_time"]==env.now: + en[4].attr["critical"]=True + else: + en[4].attr["critical"]=False + if en[5].attr["finish_time"]==env.now: + en[5].attr["critical"]=True + else: + en[5].attr["critical"]=False + yield e.get(r, 2,list_pr[6]) + yield e.do("activity6",2.5) + yield e.put(r,2) + e.attr["finish_time"]=env.now + e.attr["critical"]=True + env=des.environment() + en=env.create_entities("e",7,True,True) + + + res=des.priority_resource(env,"pr_resourse",init=4) + + p0=env.process(a0(en[0],res)) + p1=env.process(a1(en[1],res)) + p2=env.process(a2(en[2],res)) + p3=env.process(a3(en[3],res)) + p4=env.process(a4(en[4],res)) + p5=env.process(a5(en[5],res)) + p6=env.process(a6(en[6],res)) + + env.run() + return(env.now) + + +list_pr=[1,2,1,1,3,1,1] +print(prcpm(list_pr)) \ No newline at end of file diff --git a/src/des_tests/test_repair_earthmoving.py b/src/des_tests/test_repair_earthmoving.py index ff5dca0..0dd3ebe 100644 --- a/src/des_tests/test_repair_earthmoving.py +++ b/src/des_tests/test_repair_earthmoving.py @@ -37,13 +37,13 @@ def repairPorcess(e,loader,workedHours): workedHours=d.resource(env,'workedHours',init=0,capacity=2000,print_actions=True) truckent=d.entity(env,'smallTruck',print_actions=True) -truckent.attr['loadingDur']=dist.norm(5,4) +truckent.attr['loadingDur']=dist.uniform(4,5) truckent.attr['haulingDur']=dist.uniform(10,14) truckent.attr['DumpingDur']=4 truckent.attr['capacity']=80 truckent2=d.entity(env,'bigTruck',print_actions=True) -truckent2.attr['loadingDur']=dist.norm(7,4) +truckent2.attr['loadingDur']=dist.uniform(4,7) truckent2.attr['haulingDur']=dist.uniform(12,16) truckent2.attr['DumpingDur']=5 truckent2.attr['capacity']=100 diff --git a/src/pmpy/des.py b/src/pmpy/des.py index ed6523b..7179f43 100644 --- a/src/pmpy/des.py +++ b/src/pmpy/des.py @@ -697,7 +697,7 @@ def _check_all_requests(self): r=self.request_list.pop(0) #remove the first element from the list simpy_request=self.container.get(r.amount) yield simpy_request - r.entity.pending_requests.remove(r) + r.entity.pending_requests.remove(r) r.flag.put(1) super()._get(r.entity,r.amount) if self.log: @@ -841,10 +841,17 @@ def _check_all_requests(self): r.entity._waiting_log=append(r.entity._waiting_log,[[self.id,r.time,self.env.now,r.amount]],axis=0) def cancel(self,priority_request): - if priority_request in self.request_list: - self.request_list.remove(priority_request) - else: - print("warning: the priority request can not be cancled as it is not in the request list") + #***the followig code did not work*** + #if priority_request in self.request_list: + #self.request_list.remove(priority_request) + #remove does not work since it uses equal defined in the priority resource class + for i in range(len(self.request_list)): + pr=self.request_list[i] + if pr.entity==priority_request.entity and pr.amount==priority_request.amount: + del self.request_list[i] + return + + print("warning: the priority request can not be cancled as it is not in the request list") def add(self,entity,amount):