[docs]classOrderAlgorithm(IntEnum):""" Defines the possible order algorithm. * NONE: does not change anything * RANDOM: random order """NONE=0RANDOM=1
[docs]classOrderOptimization:""" Optimizes the order of computation. :param builder: GraphBuilder holding the model :param algorithm: to apply :param verbose: verbosity """def__init__(self,builder:"GraphBuilder",# noqa: F821algorithm:OrderAlgorithm=OrderAlgorithm.NONE,verbose:int=0,):self.builder=builderself.algorithm=algorithmself.verbose=verbosedef__repr__(self)->str:"usual"returnf"{self.__class__.__name__}(..., {self.algorithm})"
[docs]defoptimize(self)->List[Dict[str,Any]]:""" Optimizes the model inplace. It optimizes the model in the builder itself by switching nodes. """begin=time.perf_counter()ifself.algorithm==OrderAlgorithm.NONE:ifself.verbose:print(f"[OrderOptimization.optimize] {self.algorithm}: does nothing")duration=time.perf_counter()-beginifself.verbose:print(f"[OrderOptimization.optimize] done in {duration}")return[dict(pattern="order",time_in=duration)]ifself.algorithm==OrderAlgorithm.RANDOM:ifself.verbose:print(f"[OrderOptimization.optimize] {self.algorithm}: does nothing")duration=time.perf_counter()-beginstats=self.random_order()ifself.verbose:print(f"[OrderOptimization.optimize] done in {duration}")stats.append(dict(pattern="order",algo=str(self.algorithm),time_in=duration))returnstatsraiseAssertionError(f"Unsupported algorithm {self.algorithm}.")