示例#1
0
 def test_get_next_models(self):
     """Should retrieve the next models"""
     m1 = ModelMock(self.dynamic_system)
     m2 = ModelMock(self.dynamic_system)
     m3 = ModelMock(self.dynamic_system)
     self.dynamic_system.schedule(m1, Decimal(10))
     self.dynamic_system.schedule(m2, Decimal(10))
     self.dynamic_system.schedule(m3, Decimal(11))
     self.assertEqual({m1, m2}, self.dynamic_system.get_next_models())
示例#2
0
    def test_get_effective_paths_probability(self):
        """Should get the valid paths for each output probability"""
        m1 = ModelMock(self.dynamic_system)
        m2 = ModelMock(self.dynamic_system)
        m3 = ModelMock(self.dynamic_system)

        m11 = Path(m1, m1, ExpressionProperty(Value(0.1)))
        m12 = Path(m1, m2, ExpressionProperty(Value(0.3)))
        m13 = Path(m1, m3, ExpressionProperty(Value(0.6)))
        m23 = Path(m2, m3, ExpressionProperty(Value(1)))

        self.dynamic_system.link(m11)
        self.dynamic_system.link(m12)
        self.dynamic_system.link(m13)
        self.dynamic_system.link(m23)

        m1.get_output = lambda: 5
        m2.get_output = lambda: 15
        m3.get_output = lambda: 25

        self.dynamic_system.schedule(m1, Decimal(10))
        self.dynamic_system.schedule(m2, Decimal(10))
        self.dynamic_system.schedule(m3, Decimal(10))

        self.dynamic_system.get_output()

        self.assertEqual(1, len(self.dynamic_system._get_effective_paths(m1)))
        self.assertEqual({m23}, self.dynamic_system._get_effective_paths(m2))
    def test_save_and_load(self):
        m1 = ModelMock(self.experiment.dynamic_system)
        m2 = ModelMock(self.experiment.dynamic_system)

        m1.set_up_state(5)
        m2.set_up_state(10)

        self.experiment.dynamic_system.schedule(m1, Decimal(10))

        m12 = Path(m1, m2, ExpressionProperty(Value(1)))

        self.experiment.dynamic_system.link(m12)

        data = self.experiment.save()
        loaded_experiment = self.experiment.load(data)

        models_expected = set(
            [m.get_id() for m in self.experiment.dynamic_system._models]
        )
        models_actual = {m.get_id() for m in loaded_experiment.dynamic_system._models}

        paths_expected = {m.get_id() for m in self.experiment.dynamic_system._paths}
        paths_actual = {m.get_id() for m in loaded_experiment.dynamic_system._paths}

        self.assertEqual(models_expected, models_actual)
        self.assertEqual(paths_expected, paths_actual)
示例#4
0
 def test_get_output(self):
     """Should retrieve the next models"""
     m1 = ModelMock(self.dynamic_system)
     self.dynamic_system.schedule(m1, Decimal(10))
     m1.get_output = lambda: 5
     self.assertDictEqual({m1: 5}, self.dynamic_system.get_output())
示例#5
0
 def test_schedule(self):
     """Should schedule a model"""
     m = ModelMock(self.dynamic_system)
     self.dynamic_system.schedule(m, Decimal(10))
     self.assertEqual(10, self.dynamic_system.get_time_of_next_events())
示例#6
0
    def test_get_affected_models_and_its_inputs(self):
        """Should get the affected models and its inputs"""
        m1 = ModelMock(self.dynamic_system)
        m2 = ModelMock(self.dynamic_system)
        m3 = ModelMock(self.dynamic_system)

        m12 = Path(m1, m2, ExpressionProperty(Value(1)))
        m13 = Path(m1, m3, ExpressionProperty(Value(1)))
        m23 = Path(m2, m3, ExpressionProperty(Value(1)))

        np.random.seed(42)

        self.dynamic_system.link(m12)
        self.dynamic_system.link(m13)
        self.dynamic_system.link(m23)

        m1.get_output = lambda: 5
        m2.get_output = lambda: 15
        m3.get_output = lambda: 25

        self.dynamic_system.schedule(m1, Decimal(10))
        self.dynamic_system.schedule(m2, Decimal(10))
        self.dynamic_system.schedule(m3, Decimal(10))

        self.dynamic_system.get_output()

        (
            affected_models,
            inputs,
        ) = self.dynamic_system._get_affected_models_and_its_inputs()

        self.assertEqual({m2, m3}, affected_models)
        self.assertDictEqual(
            {
                m2: {
                    m1.get_id(): 5
                },
                m3: {
                    m1.get_id(): 5,
                    m2.get_id(): 15
                }
            }, inputs)
 def setUp(self) -> None:
     """Sets up tests"""
     self.dynamic_system = DynamicSystemMock(Scheduler())
     self.model = ModelMock(self.dynamic_system, "model")
class DiscreteEventModelTest(unittest.TestCase):
    """Discrete event model tests"""

    dynamic_system: DiscreteEventDynamicSystem
    model: DiscreteEventModel

    def setUp(self) -> None:
        """Sets up tests"""
        self.dynamic_system = DynamicSystemMock(Scheduler())
        self.model = ModelMock(self.dynamic_system, "model")

    def tearDown(self) -> None:
        """Remove changes of the tests."""
        static_entity_manager._saved_names = set()

    def test_schedule(self):
        """Should schedule an event."""
        self.model.schedule(Decimal(5))
        self.assertEqual(5, self.dynamic_system.get_time_of_next_events())

    def test_get_time(self):
        """Should retrieve the time evaluating the time advance function"""
        self.model._time_advance_function = lambda s: 10
        self.assertEqual(10, self.model.get_time())

    def test_get_output(self):
        """Should retrieve the output evaluating the time output function"""
        self.model._output_function = lambda s: 42
        self.assertEqual(42, self.model.get_output())

    def test_internal_transition(self):
        """Should execute autonomous, external and confluent transitions."""
        self.model.set_up_state(0)
        self.model._time_advance_function = lambda s: 10
        self.model._external_state_transition_function = lambda s, i, t: 1
        self.model._internal_state_transition_function = lambda s: s + 2
        self.model.state_transition(event_time=Decimal(0))
        self.assertEqual(2, self.model.get_state())

    def test_external_transition(self):
        """Should execute autonomous, external and confluent transitions."""
        self.model.set_up_state(0)
        self.model._time_advance_function = lambda s: 10
        self.model._external_state_transition_function = lambda s, i, t: s + i
        self.model._internal_state_transition_function = lambda s: s + 2
        self.model.state_transition(inputs=19, event_time=Decimal(2))
        self.assertEqual(19, self.model.get_state())

    def test_confluent_transition(self):
        """Should execute autonomous, external and confluent transitions."""
        self.model.set_up_state(0)
        self.model._time_advance_function = lambda s: 10
        self.model._external_state_transition_function = lambda s, i, t: s + i
        self.model._internal_state_transition_function = lambda s: s + 2
        self.model.state_transition(inputs=19, event_time=Decimal(0))
        self.assertEqual(21, self.model.get_state())