Coverage for moptipyapps/prodsched/random_simulation.py: 100%

45 statements  

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

1""" 

2A simulator for production scheduling that is more "randomized". 

3 

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

5>>> ri.n_products 

610 

7>>> ri.n_demands 

810038 

9>>> ri.station_product_unit_times 

10(((nan, nan), (nan, nan), (nan, nan), (nan, nan), (nan, nan), (nan, nan), \ 

11(nan, nan), (nan, nan), (nan, nan), (nan, nan)), ((nan, nan), (nan, nan), \ 

12(nan, nan), (nan, nan), (nan, nan), (nan, nan), (nan, nan), (nan, nan), \ 

13(nan, nan), (nan, nan)), ((), (), (), (), (), (), (), (nan, nan), \ 

14(nan, nan), (nan, nan)), ((nan, nan), (), (nan, nan), (nan, nan), \ 

15(nan, nan), (), (), (nan, nan), (nan, nan), (nan, nan)), ((), (nan, nan), \ 

16(), (), (), (nan, nan), (), (), (nan, nan), (nan, nan)), ((), (), \ 

17(nan, nan), (), (), (), (nan, nan), (nan, nan), (nan, nan), (nan, nan)), \ 

18((), (), (), (nan, nan), (), (nan, nan), (), (nan, nan), (), (nan, nan)), \ 

19((), (nan, nan), (), (), (), (), (nan, nan), (nan, nan), (nan, nan), ()), \ 

20((nan, nan), (nan, nan), (nan, nan), (nan, nan), (nan, nan), (nan, nan), \ 

21(), (nan, nan), (), (nan, nan)), ((nan, nan), (nan, nan), (), (nan, nan), \ 

22(), (), (), (), (nan, nan), ()), ((nan, nan), (nan, nan), (nan, nan), \ 

23(nan, nan), (), (), (), (), (), (nan, nan)), ((), (), (nan, nan), (), \ 

24(nan, nan), (nan, nan), (nan, nan), (nan, nan), (nan, nan), (nan, nan)), \ 

25((), (), (), (), (nan, nan), (nan, nan), (nan, nan), (nan, nan), \ 

26(nan, nan), (nan, nan))) 

27 

28>>> from moptipyapps.prodsched.mfc_generator import sample_mfc_instance 

29>>> di = sample_mfc_instance(seed=324) 

30 

31>>> di.demands == ri.demands 

32True 

33 

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

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

36>>> simulation = RandomSimulation(ri, PrintingListener(output=collector.append, 

37... print_time=False)) 

38>>> simulation.ctrl_run() 

39 

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

41... print(collector[i]) 

42start 

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

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

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

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

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

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

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

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

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

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

53T=2.981548103971826: product=0, amount=0, in_warehouse=0, in_production=0, \ 

541 pending demands 

55T=2.981548103971826: station=0, 1 jobs queued 

56T=2.981548103971826: start j(id: 0, p: 0, am: 1, ar: 2.981548103971826, \ 

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

58T=3.4193569376148965: finished j(id: 0, p: 0, am: 1, ar: 2.981548103971826, \ 

59me: F, c: F, st: 2.981548103971826, sp: 0) at station 0 

60T=3.4193569376148965: station=1, 1 jobs queued 

61T=3.4193569376148965: start j(id: 0, p: 0, am: 1, ar: 2.981548103971826, \ 

62me: F, c: F, st: 3.4193569376148965, sp: 1) at station 1 

63T=3.55779441043288: product=1, amount=0, in_warehouse=0, in_production=0, \ 

641 pending demands 

65T=3.55779441043288: station=0, 1 jobs queued 

66T=3.55779441043288: start j(id: 1, p: 1, am: 1, ar: 3.55779441043288, me: F, \ 

67c: F, st: 3.55779441043288, sp: 0) at station 0 

68 

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

70... print(collector[i]) 

71T=9999.323989064784! start j(id: 10035, p: 4, am: 1, ar: 9997.792977742796, \ 

72me: T, c: F, st: 9998.976626430733, sp: 1) at station 1 

73T=9999.360407487817! finished j(id: 9983, p: 7, am: 1, ar: 9949.406339628928, \ 

74me: T, c: T, st: 9989.970820097158, sp: 11) at station 12 

75T=9999.360407487817! station=12, 7 jobs queued 

76T=9999.360407487817! start j(id: 10003, p: 4, am: 1, ar: 9969.66567880894, \ 

77me: T, c: F, st: 9991.124064608839, sp: 7) at station 12 

78T=9999.360407487817! product=7, amount=1, in_warehouse=0, in_production=4, \ 

795 pending demands 

80T=9999.360407487817! d(id: 8000, p: 7, c: 8000, am: 1, ar: \ 

819949.406339628928, dl: 9949.406339628928, me: T) satisfied 

82T=9999.504763898161! finished j(id: 10035, p: 4, am: 1, ar: \ 

839997.792977742796, me: T, c: F, st: 9998.976626430733, sp: 1) \ 

84at station 1 

85T=9999.79811329168! product=6, amount=0, in_warehouse=0, \ 

86in_production=3, 4 pending demands 

87T=9999.953134938109! finished j(id: 10029, p: 6, am: 1, \ 

88ar: 9993.680935722501, me: T, c: F, st: 9995.014424332754, sp: 2) at station 5 

89T=9999.953134938109! station=5, 1 jobs queued 

90T=9999.953134938109! start j(id: 10030, p: 2, am: 1, ar: 9993.858569427597, \ 

91me: T, c: F, st: 9995.741612058786, sp: 2) at station 5 

92T=9999.984813470339! finished j(id: 10003, p: 4, am: 1, ar: \ 

939969.66567880894, me: T, c: T, st: 9991.124064608839, sp: 7) at station 12 

94T=9999.984813470339! station=12, 6 jobs queued 

95T=9999.984813470339! start j(id: 10002, p: 5, am: 1, ar: \ 

969964.392348080759, me: T, c: F, st: 9992.164222251737, sp: 7) at station 12 

97T=9999.984813470339! product=4, amount=1, in_warehouse=0, \ 

98in_production=3, 4 pending demands 

99T=9999.984813470339! d(id: 4992, p: 4, c: 4992, am: 1, ar: \ 

1009969.66567880894, dl: 9969.66567880894, me: T) satisfied 

101T=9999.992789452692! finished j(id: 10018, p: 0, am: 1, ar: \ 

1029978.69384408688, me: T, c: F, st: 9993.208961813833, sp: 4) at station 8 

103T=9999.992789452692! station=8, 4 jobs queued 

104T=9999.992789452692! start j(id: 10012, p: 2, am: 1, ar: \ 

1059975.265252124665, me: T, c: F, st: 9993.724592052344, sp: 5) at station 8 

106T=9999.992789452692 -- finished 

107""" 

108 

109from dataclasses import dataclass 

110from math import nan 

111from typing import Final, Iterable, cast 

112 

113from moptipy.utils.nputils import rand_generator 

114from numpy.random import Generator 

115from pycommons.ds.immutable_map import immutable_mapping 

116 

117from moptipyapps.prodsched.instance import ( 

118 Instance, 

119) 

120from moptipyapps.prodsched.mfc_generator import ( 

121 Product, 

122 Station, 

123 _sample_demands, 

124 default_products, 

125 default_stations, 

126) 

127from moptipyapps.prodsched.simulation import ( 

128 Job, 

129 Listener, 

130 Simulation, 

131) 

132 

133 

134@dataclass(order=True, frozen=True) 

135class TimeJob(Job): 

136 """The record for a production job.""" 

137 

138 #: the work times 

139 worktimes: tuple[float, ...] = () 

140 

141 

142class RandomInstance(Instance): 

143 """An instance of the Production Scheduling Problem.""" 

144 

145 def __init__( 

146 self, 

147 seed: int, 

148 products: Iterable[Product] = default_products(), 

149 stations: Iterable[Station] = default_stations(), 

150 time_end_warmup: int | float = 3000, 

151 time_end_measure: int | float = 10000) -> None: 

152 """ 

153 Create an instance of the production scheduling time. 

154 

155 :param seed: the instance seed 

156 :param products: the list of products 

157 :param stations: the list of stations 

158 :param time_end_warmup: the time unit when the warmup time ends and the 

159 actual measurement begins 

160 :param time_end_measure: the time unit when the actual measure time 

161 ends 

162 """ 

163 name: Final[str] = f"randomInstance_0x{seed:x}" 

164 

165 prods: Final[list[Product]] = sorted(products) 

166 n_products: Final[int] = list.__len__(prods) 

167 gen: Final[Generator] = rand_generator(seed) 

168 demands, _ = _sample_demands( 

169 gen, prods, time_end_warmup, time_end_measure) 

170 n_demands: Final[int] = tuple.__len__(demands) 

171 n_customers: Final[int] = set.__len__({ 

172 dem.customer_id for dem in demands}) 

173 

174 #: the stations 

175 self.stations: Final[tuple[Station, ...]] = tuple(sorted(stations)) 

176 n_stations: Final[int] = tuple.__len__(self.stations) 

177 routes: Final[tuple[tuple[int, ...], ...]] = tuple( 

178 prod.routing for prod in prods) 

179 

180 # the work times, will later be replaced 

181 worktimes: Final[tuple[tuple[tuple[float, ...], ...], ...]] = tuple( 

182 tuple((1.0, 1.0) if sid in prd.routing else () for prd in prods) 

183 for sid in range(n_stations)) 

184 

185 #: the random seed to use 

186 self.seed: Final[int] = seed 

187 #: the internal state where a random generator should pick up 

188 self.state: Final = immutable_mapping(dict(gen.bit_generator.state)) 

189 

190 super().__init__(name=name, 

191 n_products=n_products, 

192 n_customers=n_customers, 

193 n_stations=n_stations, 

194 n_demands=n_demands, 

195 time_end_warmup=time_end_warmup, 

196 time_end_measure=time_end_measure, 

197 routes=routes, 

198 demands=demands, 

199 warehous_at_t0=(0, ) * n_products, 

200 station_product_unit_times=worktimes) 

201 

202 # the invalid work times, will later be replaced 

203 nan_worktimes: Final[tuple[tuple[tuple[ 

204 float, ...], ...], ...]] = tuple( 

205 tuple((nan, nan) if sid in prd.routing else () for prd in prods) 

206 for sid in range(n_stations)) 

207 

208 # reset the production times NAN to cause trouble if they are 

209 # accidentally used 

210 object.__setattr__(self, "station_product_unit_times", nan_worktimes) 

211 

212 

213class RandomSimulation(Simulation): 

214 """A randomized simulation.""" 

215 

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

217 """ 

218 Create the randomized simulation. 

219 

220 :param instance: the instance 

221 :param listener: the listener 

222 """ 

223 super().__init__(instance, listener) 

224 

225 #: the random seed 

226 self.seed: Final[int] = instance.seed 

227 #: the stations 

228 self.__stations: Final[tuple[Station, ...]] = instance.stations 

229 #: the internal generator 

230 self.__rnd: Final[Generator] = rand_generator(self.seed) 

231 

232 def ctrl_run(self) -> None: 

233 """Begin the simulation.""" 

234 self.__rnd.bit_generator.state = ( # type: ignore 

235 dict(self.instance.state)) # type: ignore 

236 super().ctrl_run() 

237 

238 def _create_job(self, jid: int, product_id: int, amount: int, 

239 time: float, measure: bool) -> Job: 

240 """ 

241 Create the job. 

242 

243 :param jid: the job id 

244 :param product_id: the product id 

245 :param amount: the amount of product 

246 :param time: the start time 

247 :param measure: will this be measured 

248 :return: the job 

249 """ 

250 gen: Final[Generator] = self.__rnd 

251 stat: Final[tuple[Station, ...]] = self.__stations 

252 return TimeJob(jid, product_id, amount, time, self.warmup <= time, 

253 worktimes=tuple( 

254 sum(stat[i].processing_time.sample(gen) 

255 for _ in range(amount)) 

256 for i in self.routes[product_id])) 

257 

258 def _get_job_finish_time( 

259 self, time: float, job: Job, 

260 step: int, station_id: int, product_id: int) -> float: 

261 """ 

262 Get the finish time of a job. 

263 

264 :param time: the start time 

265 :param job: the job 

266 :param step: the job step 

267 :param station_id: the station ID 

268 :param product_id: the product ID 

269 :return: the finish time 

270 """ 

271 return time + cast("TimeJob", job).worktimes[step]