Browse Source

updated getStats to be model independent

Snej69420 5 months ago
parent
commit
6015ceca8b
2 changed files with 48 additions and 43 deletions
  1. 46 41
      benchmarks/pythonpdevs/devstone.py
  2. 2 2
      benchmarks/pythonpdevs/main.py

+ 46 - 41
benchmarks/pythonpdevs/devstone.py

@@ -101,6 +101,26 @@ class DelayedAtomicStats(DelayedAtomic):
         self.ext_count += 1
         return super().extTransition(inputs)
 
+def getStats(model, display=True):
+    total_i = 0
+    total_e = 0
+    if isinstance(model.component_set[0], CoupledDEVS):
+        i, e = getStats(model.component_set[0], False)
+        total_i += i
+        total_e += e
+
+    for m in model.models:
+        if not (hasattr(m, "int_count") and hasattr(m, "ext_count")):
+            continue
+        total_i += m.int_count
+        total_e += m.ext_count
+    if display:
+        print(f"Total internal transitions during simulation: {total_i}")
+        print(f"Total external transitions during simulation: {total_e}")
+        print(f"Total Number of Transitions: {total_i + total_e}")
+
+    return total_i, total_e
+
 
 class DEVStoneWrapper(CoupledDEVS, ABC):
 
@@ -134,9 +154,9 @@ class DEVStoneWrapper(CoupledDEVS, ABC):
         if depth == 1:
             p_time = round(random.uniform(1.0, 2.0), 3)
             if self.stats:
-                atomic = DelayedAtomicStats("Atomic_0_0", int_delay, ext_delay, add_out_port=True, prep_time=p_time, mode=mode)
+                atomic = DelayedAtomicStats("A0", int_delay, ext_delay, add_out_port=True, prep_time=p_time, mode=mode)
             else:
-                atomic = DelayedAtomic("Atomic_0_0", int_delay, ext_delay, add_out_port=True, prep_time=p_time, mode=mode)
+                atomic = DelayedAtomic("A0", int_delay, ext_delay, add_out_port=True, prep_time=p_time, mode=mode)
 
             self.models.append(self.addSubModel(atomic))
 
@@ -155,11 +175,12 @@ class DEVStoneWrapper(CoupledDEVS, ABC):
             # => problem for Classic DEVS only it seems
             for idx in range(width - 1):
                 p_time = 1 - round(random.uniform(idx/self.width, (idx + 0.5)/self.width), DIGITS)
+                name = f"A{idx}"
                 if self.stats:
-                    atomic = DelayedAtomicStats("Atomic_%d_%d" % (depth - 1, idx), int_delay, ext_delay,
+                    atomic = DelayedAtomicStats(name, int_delay, ext_delay,
                                                 add_out_port=add_atomic_out_ports, prep_time=p_time, mode=mode)
                 else:
-                    atomic = DelayedAtomic("Atomic_%d_%d" % (depth - 1, idx), int_delay, ext_delay,
+                    atomic = DelayedAtomic(name, int_delay, ext_delay,
                                            add_out_port=add_atomic_out_ports, prep_time=p_time, mode=mode)
                 self.models.append(self.addSubModel(atomic))
 
@@ -188,25 +209,6 @@ class DEVStoneWrapper(CoupledDEVS, ABC):
         src = Source(result)
         src.view(filename=f'{name}_{self.depth}')
 
-    def getStats(self, display=True):
-        total_i = 0
-        total_e = 0
-        if isinstance(self.component_set[0], CoupledDEVS):
-            i, e = self.component_set[0].getStats(False)
-            total_i += i
-            total_e += e
-
-        for m in self.models:
-            if not (hasattr(m, "int_count") and hasattr(m, "ext_count")):
-                continue
-            total_i += m.int_count
-            total_e += m.ext_count
-        if display:
-            print(f"Total internal transitions during simulation: {total_i}")
-            print(f"Total external transitions during simulation: {total_e}")
-            print(f"Total Number of Transitions: {total_i + total_e}")
-        return total_i, total_e
-
     @abstractmethod
     def gen_coupled(self):
         """ :return a coupled method with i_in and o_out ports"""
@@ -224,7 +226,7 @@ class LI(DEVStoneWrapper):
             self.connectPorts(self.i_in, self.component_set[idx].i_in)
 
     def gen_coupled(self):
-        return LI("Coupled_%d" % (self.depth - 1), self.depth - 1, self.width, self.int_delay, self.ext_delay,
+        return LI(f"C{self.depth -1}", self.depth - 1, self.width, self.int_delay, self.ext_delay,
                   prep_time=self.prep_time, stats=self.stats)
 
 
@@ -244,7 +246,7 @@ class HI(DEVStoneWrapper):
             self.connectPorts(self.i_in, self.component_set[idx].i_in)
 
     def gen_coupled(self):
-        return HI("Coupled_%d" % (self.depth - 1), self.depth - 1, self.width, self.int_delay, self.ext_delay,
+        return HI(f"C{self.depth -1}", self.depth - 1, self.width, self.int_delay, self.ext_delay,
                   prep_time=self.prep_time, stats=self.stats)
 
 
@@ -273,7 +275,7 @@ class HO(DEVStoneWrapper):
             self.connectPorts(self.component_set[idx].o_out, self.o_out2)
 
     def gen_coupled(self):
-        return HO("Coupled_%d" % (self.depth - 1), self.depth - 1, self.width, self.int_delay, self.ext_delay,
+        return HO(f"C{self.depth -1}", self.depth - 1, self.width, self.int_delay, self.ext_delay,
                   prep_time=self.prep_time, stats=self.stats)
 
 
@@ -281,6 +283,7 @@ class HOmod(CoupledDEVS):
 
     def __init__(self, name: str, depth: int, width: int, int_delay: float, ext_delay: float, prep_time=0, stats=False):
         super().__init__(name)
+        self.models = []
 
         self.depth = depth
         self.width = width
@@ -304,15 +307,16 @@ class HOmod(CoupledDEVS):
 
         if depth == 1:
             if self.stats:
-                atomic = DelayedAtomicStats("Atomic_0_0", int_delay, ext_delay, add_out_port=True, prep_time=prep_time)
+                atomic = DelayedAtomicStats("A0", int_delay, ext_delay, add_out_port=True, prep_time=prep_time)
             else:
-                atomic = DelayedAtomic("Atomic_0_0", int_delay, ext_delay, add_out_port=True, prep_time=prep_time)
-            self.addSubModel(atomic)
+                atomic = DelayedAtomic("A0", int_delay, ext_delay, add_out_port=True, prep_time=prep_time)
+            self.models.append(self.addSubModel(atomic))
 
             self.connectPorts(self.i_in, atomic.i_in)
             self.connectPorts(atomic.o_out, self.o_out)
         else:
-            coupled = HOmod("Coupled_%d" % (self.depth - 1), self.depth - 1, self.width, self.int_delay, self.ext_delay, prep_time=prep_time, stats=stats)
+            c_name = f"C{depth-1}"
+            coupled = HOmod(c_name, self.depth - 1, self.width, self.int_delay, self.ext_delay, prep_time=prep_time, stats=stats)
             self.addSubModel(coupled)
             self.connectPorts(self.i_in, coupled.i_in)
             self.connectPorts(coupled.o_out, self.o_out)
@@ -324,13 +328,14 @@ class HOmod(CoupledDEVS):
                 for i in range(width):
                     min_row_idx = 0 if i < 2 else i - 1
                     for j in range(min_row_idx, width - 1):
+                        name = f"A{i}_{j}"
                         if self.stats:
-                            atomic = DelayedAtomicStats("Atomic_%d_%d_%d" % (depth - 1, i, j), int_delay, ext_delay,
+                            atomic = DelayedAtomicStats(name, int_delay, ext_delay,
                                                    add_out_port=True, prep_time=prep_time)
                         else:
-                            atomic = DelayedAtomic("Atomic_%d_%d_%d" % (depth - 1, i, j), int_delay, ext_delay,
+                            atomic = DelayedAtomic(name, int_delay, ext_delay,
                                                    add_out_port=True, prep_time=prep_time)
-                        self.addSubModel(atomic)
+                        self.models.append(self.addSubModel(atomic))
                         atomics[i].append(atomic)
 
                 # Connect EIC
@@ -366,7 +371,7 @@ class LI2HI(DEVStoneWrapper):
             self.connectPorts(self.i_in, self.component_set[idx].i_in)
 
     def gen_coupled(self):
-        return LI2HI("Coupled_%d" % (self.depth - 1), self.depth - 1, self.width, self.int_delay, self.ext_delay,
+        return LI2HI(f"C{self.depth-1}", self.depth - 1, self.width, self.int_delay, self.ext_delay,
                   prep_time=self.prep_time, stats=self.stats)
 
     def modelTransition(self, state):
@@ -399,7 +404,7 @@ class HI2LI(DEVStoneWrapper):
             self.connectPorts(self.i_in, self.component_set[idx].i_in)
 
     def gen_coupled(self):
-        return HI2LI("Coupled_%d" % (self.depth - 1), self.depth - 1, self.width, self.int_delay, self.ext_delay,
+        return HI2LI(f"C{self.depth -1}", self.depth - 1, self.width, self.int_delay, self.ext_delay,
                   prep_time=self.prep_time, stats=self.stats)
 
 
@@ -554,7 +559,7 @@ class dLI(DynamicDEVStoneWrapper):
         super().__init__(name, depth, width, int_delay, ext_delay, False, prep_time, stats, mode="LI-Dynamic", root=root, number=number, gen_mode=gen_mode)
 
     def gen_coupled(self):
-        return dLI("Coupled_%d" % (self.depth - 1), self.depth - 1, self.width, self.int_delay, self.ext_delay,
+        return dLI(f"C{self.depth -1}", self.depth - 1, self.width, self.int_delay, self.ext_delay,
                    prep_time=self.prep_time, stats=self.stats, root=False, number=self.number)
 
 
@@ -573,7 +578,7 @@ class mdLI(DynamicDEVStoneWrapper):
 
 
     def gen_coupled(self):
-        return mdLI("Coupled_%d" % (self.depth - 1), self.depth - 1, self.width, self.int_delay, self.ext_delay,
+        return mdLI(f"C{self.depth -1}", self.depth - 1, self.width, self.int_delay, self.ext_delay,
                    prep_time=self.prep_time, stats=self.stats, root=False, number=self.number)
 
 
@@ -585,7 +590,7 @@ class dHI(DynamicDEVStoneWrapper):
         super().__init__(name, depth, width, int_delay, ext_delay, True, prep_time, stats, mode="HI-Dynamic", root=root, number=number, gen_mode=gen_mode)
 
     def gen_coupled(self):
-        return dHI("Coupled_%d" % (self.depth - 1), self.depth - 1, self.width, self.int_delay, self.ext_delay,
+        return dHI(f"C{self.depth -1}", self.depth - 1, self.width, self.int_delay, self.ext_delay,
                    prep_time=self.prep_time, stats=self.stats, root=False, number=self.number)
 
 
@@ -602,7 +607,7 @@ class mdHI(DynamicDEVStoneWrapper):
         self.connector = intermediate.o_out
 
     def gen_coupled(self):
-        return mdHI("Coupled_%d" % (self.depth - 1), self.depth - 1, self.width, self.int_delay, self.ext_delay,
+        return mdHI(f"C{self.depth -1}", self.depth - 1, self.width, self.int_delay, self.ext_delay,
                    prep_time=self.prep_time, stats=self.stats, root=False, number=self.number)
 
 
@@ -619,7 +624,7 @@ class dHO(DynamicDEVStoneWrapper):
         self.connector = self.i_in2
 
     def gen_coupled(self):
-        return dHO("Coupled_%d" % (self.depth - 1), self.depth - 1, self.width, self.int_delay, self.ext_delay, self.prep_time, self.stats, root=False, number=self.number)
+        return dHO(f"C{self.depth -1}", self.depth - 1, self.width, self.int_delay, self.ext_delay, self.prep_time, self.stats, root=False, number=self.number)
 
 
 class mdHO(DynamicDEVStoneWrapper):
@@ -639,7 +644,7 @@ class mdHO(DynamicDEVStoneWrapper):
         self.connector = intermediate.o_out
 
     def gen_coupled(self):
-        return mdHO("Coupled_%d" % (self.depth - 1), self.depth - 1, self.width, self.int_delay, self.ext_delay, self.prep_time, self.stats, root=False, number=self.number)
+        return mdHO(f"C{self.depth -1}", self.depth - 1, self.width, self.int_delay, self.ext_delay, self.prep_time, self.stats, root=False, number=self.number)
 
 
 class dynamicTest(CoupledDEVS):

+ 2 - 2
benchmarks/pythonpdevs/main.py

@@ -6,7 +6,7 @@ from functools import partial
 from pypdevs.DEVS import CoupledDEVS
 from pypdevs.simulator import Simulator
 
-from devstone import LI, HI, HO, HOmod, LI2HI, HI2LI, dLI, mdLI, dHI, mdHI, dHO, mdHO, dynamicTest
+from devstone import LI, HI, HO, HOmod, LI2HI, HI2LI, dLI, mdLI, dHI, mdHI, dHO, mdHO, dynamicTest, getStats
 from generator import Generator
 from benchmarks.model_properties import model_properties
 
@@ -144,7 +144,7 @@ if __name__ == '__main__':
     print("Simulation time: {}".format(sim_time - engine_setup_time))
     if args.stats:
         print("-"*50)
-        devstone_model.getStats()
+        getStats(devstone_model)
         print(f"Theoretical Events: {model_properties(args.model_type, args.width, args.depth)['Events']}")
         print("-"*50)