Coverage for moptipyapps/prodsched/random_rop_simulation.py: 92%

39 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-13 02:48 +0000

1""" 

2A randomized re-order-point-based simulation. 

3 

4>>> ri = RandomInstance(seed=324) 

5>>> collector: list[str] = [] 

6>>> from moptipyapps.prodsched.simulation import PrintingListener 

7>>> simulation = RandomROPSimulation(ri, PrintingListener( 

8... output=collector.append, print_time=False)) 

9>>> simulation.set_rop([1] * ri.n_products) 

10>>> simulation.ctrl_run() 

11 

12>>> for i in range(20): 

13... print(collector[i]) 

14start 

15T=0.0: product=0, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

16T=0.0: station=0, 1 jobs queued 

17T=0.0: start j(id: 0, p: 0, am: 2, ar: 0, me: F, c: F, st: 0, sp: 0) at \ 

18station 0 

19T=0.0: product=2, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

20T=0.0: product=6, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

21T=0.0: product=5, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

22T=0.0: product=1, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

23T=0.0: product=4, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

24T=0.0: product=9, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

25T=0.0: product=3, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

26T=0.0: product=8, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

27T=0.0: product=7, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

28T=1.700981296962859: finished j(id: 0, p: 0, am: 2, ar: 0, me: F, c: F, st: \ 

290, sp: 0) at station 0 

30T=1.700981296962859: station=0, 9 jobs queued 

31T=1.700981296962859: start j(id: 1, p: 2, am: 2, ar: 0, me: F, c: F, st: 0, \ 

32sp: 0) at station 0 

33T=1.700981296962859: station=1, 1 jobs queued 

34T=1.700981296962859: start j(id: 0, p: 0, am: 2, ar: 0, me: F, c: F, st: \ 

351.700981296962859, sp: 1) at station 1 

36T=2.217634752949551: finished j(id: 0, p: 0, am: 2, ar: 0, me: F, c: F, \ 

37st: 1.700981296962859, sp: 1) at station 1 

38T=2.217634752949551: station=3, 1 jobs queued 

39 

40>>> for i in range(len(collector) - 20, len(collector)): 

41... print(collector[i]) 

42T=9999.33328342021! start j(id: 10014, p: 2, am: 1, ar: 9970.157310086975, \ 

43me: T, c: F, st: 9996.158106819432, sp: 7) at station 10 

44T=9999.33328342021! product=3, amount=1, in_warehouse=0, in_production=2, \ 

451 pending demands 

46T=9999.33328342021! d(id: 4008, p: 3, c: 4008, am: 1, ar: 9989.806382347304, \ 

47dl: 9989.806382347304, me: T) satisfied 

48T=9999.522691001024! finished j(id: 10021, p: 4, am: 1, ar: 9974.764243825606\ 

49, me: T, c: F, st: 9995.187765657156, sp: 6) at station 1 

50T=9999.522691001024! station=1, 9 jobs queued 

51T=9999.522691001024! start j(id: 10017, p: 6, am: 1, ar: 9971.454782810877, \ 

52me: T, c: F, st: 9995.510736597318, sp: 6) at station 1 

53T=9999.66344376004! finished j(id: 10044, p: 5, am: 1, ar: 9997.291327122433\ 

54, me: T, c: F, st: 9997.291327122433, sp: 0) at station 0 

55T=9999.66344376004! station=0, 2 jobs queued 

56T=9999.66344376004! start j(id: 10045, p: 4, am: 1, ar: 9997.792977742796, \ 

57me: T, c: F, st: 9997.792977742796, sp: 0) at station 0 

58T=9999.669678312202! finished j(id: 10039, p: 6, am: 1, ar: \ 

599993.680935722501, me: T, c: F, st: 9998.800471480217, sp: 2) at station 5 

60T=9999.79811329168! product=6, amount=0, in_warehouse=0, in_production=4, \ 

613 pending demands 

62T=9999.93543391247! finished j(id: 10014, p: 2, am: 1, ar: 9970.157310086975,\ 

63 me: T, c: T, st: 9996.158106819432, sp: 7) at station 10 

64T=9999.93543391247! station=10, 2 jobs queued 

65T=9999.93543391247! start j(id: 9982, p: 9, am: 1, ar: 9933.380445487133, \ 

66me: T, c: F, st: 9997.379811520846, sp: 10) at station 10 

67T=9999.93543391247! product=2, amount=1, in_warehouse=0, in_production=3, \ 

682 pending demands 

69T=9999.93543391247! d(id: 2989, p: 2, c: 2989, am: 1, ar: 9983.827600477664\ 

70, dl: 9983.827600477664, me: T) satisfied 

71T=9999.995308785708! finished j(id: 10024, p: 8, am: 1, ar: 9976.199007067482\ 

72, me: T, c: F, st: 9997.64169250468, sp: 3) at station 4 

73T=9999.995308785708! station=4, 1 jobs queued 

74T=9999.995308785708! start j(id: 10038, p: 5, am: 1, ar: 9993.649532104819\ 

75, me: T, c: F, st: 9998.100880797963, sp: 2) at station 4 

76T=9999.995308785708 -- finished 

77 

78>>> collector.clear() 

79>>> simulation.ctrl_reset() 

80>>> simulation.ctrl_run() 

81 

82>>> for i in range(20): 

83... print(collector[i]) 

84start 

85T=0.0: product=0, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

86T=0.0: station=0, 1 jobs queued 

87T=0.0: start j(id: 0, p: 0, am: 2, ar: 0, me: F, c: F, st: 0, sp: 0) at \ 

88station 0 

89T=0.0: product=2, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

90T=0.0: product=6, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

91T=0.0: product=5, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

92T=0.0: product=1, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

93T=0.0: product=4, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

94T=0.0: product=9, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

95T=0.0: product=3, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

96T=0.0: product=8, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

97T=0.0: product=7, amount=0, in_warehouse=0, in_production=0, 0 pending demands 

98T=1.700981296962859: finished j(id: 0, p: 0, am: 2, ar: 0, me: F, c: F, st: \ 

990, sp: 0) at station 0 

100T=1.700981296962859: station=0, 9 jobs queued 

101T=1.700981296962859: start j(id: 1, p: 2, am: 2, ar: 0, me: F, c: F, st: 0, \ 

102sp: 0) at station 0 

103T=1.700981296962859: station=1, 1 jobs queued 

104T=1.700981296962859: start j(id: 0, p: 0, am: 2, ar: 0, me: F, c: F, st: \ 

1051.700981296962859, sp: 1) at station 1 

106T=2.217634752949551: finished j(id: 0, p: 0, am: 2, ar: 0, me: F, c: F, \ 

107st: 1.700981296962859, sp: 1) at station 1 

108T=2.217634752949551: station=3, 1 jobs queued 

109 

110>>> for i in range(len(collector) - 20, len(collector)): 

111... print(collector[i]) 

112T=9999.33328342021! start j(id: 10014, p: 2, am: 1, ar: 9970.157310086975, \ 

113me: T, c: F, st: 9996.158106819432, sp: 7) at station 10 

114T=9999.33328342021! product=3, amount=1, in_warehouse=0, in_production=2, \ 

1151 pending demands 

116T=9999.33328342021! d(id: 4008, p: 3, c: 4008, am: 1, ar: 9989.806382347304, \ 

117dl: 9989.806382347304, me: T) satisfied 

118T=9999.522691001024! finished j(id: 10021, p: 4, am: 1, ar: 9974.764243825606\ 

119, me: T, c: F, st: 9995.187765657156, sp: 6) at station 1 

120T=9999.522691001024! station=1, 9 jobs queued 

121T=9999.522691001024! start j(id: 10017, p: 6, am: 1, ar: 9971.454782810877, \ 

122me: T, c: F, st: 9995.510736597318, sp: 6) at station 1 

123T=9999.66344376004! finished j(id: 10044, p: 5, am: 1, ar: 9997.291327122433\ 

124, me: T, c: F, st: 9997.291327122433, sp: 0) at station 0 

125T=9999.66344376004! station=0, 2 jobs queued 

126T=9999.66344376004! start j(id: 10045, p: 4, am: 1, ar: 9997.792977742796, \ 

127me: T, c: F, st: 9997.792977742796, sp: 0) at station 0 

128T=9999.669678312202! finished j(id: 10039, p: 6, am: 1, ar: \ 

1299993.680935722501, me: T, c: F, st: 9998.800471480217, sp: 2) at station 5 

130T=9999.79811329168! product=6, amount=0, in_warehouse=0, in_production=4, \ 

1313 pending demands 

132T=9999.93543391247! finished j(id: 10014, p: 2, am: 1, ar: 9970.157310086975,\ 

133 me: T, c: T, st: 9996.158106819432, sp: 7) at station 10 

134T=9999.93543391247! station=10, 2 jobs queued 

135T=9999.93543391247! start j(id: 9982, p: 9, am: 1, ar: 9933.380445487133, \ 

136me: T, c: F, st: 9997.379811520846, sp: 10) at station 10 

137T=9999.93543391247! product=2, amount=1, in_warehouse=0, in_production=3, \ 

1382 pending demands 

139T=9999.93543391247! d(id: 2989, p: 2, c: 2989, am: 1, ar: 9983.827600477664\ 

140, dl: 9983.827600477664, me: T) satisfied 

141T=9999.995308785708! finished j(id: 10024, p: 8, am: 1, ar: 9976.199007067482\ 

142, me: T, c: F, st: 9997.64169250468, sp: 3) at station 4 

143T=9999.995308785708! station=4, 1 jobs queued 

144T=9999.995308785708! start j(id: 10038, p: 5, am: 1, ar: 9993.649532104819\ 

145, me: T, c: F, st: 9998.100880797963, sp: 2) at station 4 

146T=9999.995308785708 -- finished 

147""" 

148 

149from typing import Final, Iterable 

150 

151import numpy as np 

152from pycommons.types import check_int_range, type_error 

153 

154from moptipyapps.prodsched.instance import ( 

155 Demand, 

156) 

157from moptipyapps.prodsched.random_simulation import ( 

158 RandomInstance, 

159 RandomSimulation, 

160) 

161from moptipyapps.prodsched.simulation import Listener 

162 

163 

164class RandomROPSimulation(RandomSimulation): 

165 """Create the randomized re-order point-based simulation.""" 

166 

167 def __init__(self, instance: RandomInstance, listener: Listener) -> None: 

168 """ 

169 Initialize this simulation. 

170 

171 :param instance: the instance 

172 :param listener: the listener 

173 """ 

174 super().__init__(instance, listener) 

175 #: the re-order point 

176 self.__rop: Final[list[int]] = [0] * instance.n_products 

177 

178 def set_rop(self, rop: Iterable[int] | np.ndarray) -> None: 

179 """ 

180 Set the re-order point. 

181 

182 :param rop: the re-order point 

183 """ 

184 if isinstance(rop, np.ndarray): 

185 rop = map(int, rop) # type: ignore 

186 if not isinstance(rop, Iterable): 

187 raise type_error(rop, "rop", Iterable) 

188 i: int = -1 

189 for i, rv in enumerate(rop): 

190 self.__rop[i] = check_int_range( 

191 rv, "rop-value", 0, 1_000_000_000) 

192 i += 1 

193 n_prod: Final[int] = list.__len__(self.__rop) 

194 if i != n_prod: 

195 raise ValueError(f"Invalid rop length {i}, must be {n_prod}!") 

196 

197 def event_product(self, time: float, # pylint: disable=W0613,R0913,R0917 

198 product_id: int, amount: int, 

199 in_warehouse: int, 

200 in_production: int, # pylint: disable=W0613 

201 pending_demands: tuple[Demand, ...]) -> None: 

202 """Perform a simulation step based on a product.""" 

203 # satisfy demands 

204 available: int = in_warehouse + amount 

205 unsatisfied: int = 0 

206 for demand in pending_demands: 

207 request: int = demand.amount 

208 if request <= available: 

209 self.act_demand_satisfied(demand) 

210 available -= request 

211 else: 

212 unsatisfied += request 

213 

214 # update warehouse 

215 if available < in_warehouse: 

216 self.act_take_from_warehouse(product_id, in_warehouse - available) 

217 elif available > in_warehouse: 

218 self.act_store_in_warehouse(product_id, available - in_warehouse) 

219 

220 # check if re-order point needs to be triggered 

221 rop: Final[int] = self.__rop[product_id] 

222 available = available - unsatisfied + in_production 

223 if available <= rop: 

224 self.act_produce(product_id, rop - available + 1)