Esempio n. 1
0
    def test_policies(self):
        ensemble = ModelEnsemble()

        policy = Policy('test')
        ensemble.policies = policy

        ensemble = ModelEnsemble()

        policies = [Policy('test'), Policy('name')]
        ensemble.policies = policies
Esempio n. 2
0
    def test_model_structures(self):
        function = mock.Mock()
        model_a = Model("A", function)

        ensemble = ModelEnsemble()
        ensemble.model_structures = model_a
        self.assertEqual(ensemble.model_structures[0], model_a)

        model_a = Model("A", function)
        model_b = Model("B", function)
        ensemble = ModelEnsemble()
        ensemble.model_structures = [model_a, model_b]
        self.assertEqual(list(ensemble.model_structures), [model_a, model_b])
Esempio n. 3
0
    def test_perform_experiments(self):

        # everything shared
        model_a = Model("A", mock.Mock())
        model_b = Model("B", mock.Mock())
        model_c = Model("C", mock.Mock())
        models = [model_a, model_b, model_c]

        # let's add some uncertainties to this
        shared_abc_1 = RealParameter("shared abc 1", 0, 1)
        shared_abc_2 = RealParameter("shared abc 2", 0, 1)
        shared_ab_1 = RealParameter("shared ab 1", 0, 1)
        shared_bc_1 = RealParameter("shared bc 1", 0, 1)
        a_1 = RealParameter("a 1", 0, 1)
        b_1 = RealParameter("b 1", 0, 1)
        model_a.uncertainties = [shared_abc_1, shared_abc_2, shared_ab_1, a_1]
        model_b.uncertainties = [
            shared_abc_1, shared_abc_2, shared_ab_1, shared_bc_1, b_1
        ]
        model_c.uncertainties = [shared_abc_1, shared_abc_2, shared_bc_1]

        #let's add an outcome to this
        outcome_shared = TimeSeriesOutcome("test")
        model_a.outcomes = [outcome_shared]
        model_b.outcomes = [outcome_shared]
        model_c.outcomes = [outcome_shared]

        for model in models:
            model.function.return_value = {
                a: [0.1] * 10
                for a in outcome_shared.variable_name
            }

        ensemble = ModelEnsemble()
        ensemble.model_structures = [model_a, model_b, model_c]
        ensemble.policies = [Policy('None')]

        ensemble.perform_experiments(10,
                                     uncertainty_union=True,
                                     outcome_union=True,
                                     reporting_interval=1)
        #         for model in models:
        #             model.function.assert_has_calls() TODO::

        ensemble.perform_experiments(10,
                                     uncertainty_union=True,
                                     outcome_union=False,
                                     reporting_interval=1)

        ensemble.perform_experiments(10,
                                     uncertainty_union=False,
                                     outcome_union=True,
                                     reporting_interval=1)

        ensemble.perform_experiments(10,
                                     uncertainty_union=False,
                                     outcome_union=False,
                                     reporting_interval=1)
        #
        #         self.assertRaises(ValueError, ensemble.perform_experiments,
        #                          10, uncertainty_union=False,
        #                          union_outcomes='Label')

        with mock.patch(
                'ema_workbench.em_framework.ensemble.MultiprocessingPool'
        ) as MockPool:
            ensemble.parallel = True

            mockedCallback = mock.Mock(DefaultCallback)
            mockedCallback.configure_mock(**{'i': 30})
            mockedCallback.return_value = mockedCallback

            ensemble.perform_experiments(10,
                                         uncertainty_union=True,
                                         outcome_union=True,
                                         reporting_interval=1,
                                         callback=mockedCallback)

            self.assertEqual(2, len(MockPool.mock_calls))

            MockPool.reset_mock()
            mockedCallback = mock.Mock(DefaultCallback)
            mockedCallback.configure_mock(**{'i': 10})
            mockedCallback.return_value = mockedCallback

            self.assertRaises(EMAError,
                              ensemble.perform_experiments,
                              10,
                              uncertainty_union=True,
                              outcome_union=True,
                              reporting_interval=1,
                              callback=mockedCallback)