예제 #1
0
    def test_find_local_maximum_distance(self, setup_input):
        '''
        Test whether finding the local maximum distance equals the global
        maximum distance in a simple case for a defined random seed.
        From Saltelli et al. 2008, in the solution to exercise 3a,
        Chapter 3, page 134.

        Note that local and brute force methods are not guaranteed to produce
        the same results, even for simple problems,
        hence forcing the seed here.

        '''

        rd.seed(12345)

        local_strategy = LocalOptimisation()
        brute_strategy = BruteForce()

        sample_inputs = setup_input
        N = 6
        num_params = 2
        k_choices = 4
        output_global = brute_strategy.brute_force_most_distant(sample_inputs,
                                                                N, num_params,
                                                                k_choices)
        output_local = local_strategy.find_local_maximum(sample_inputs, N,
                                                         num_params, k_choices)
        assert_equal(output_global, output_local)
예제 #2
0
    def test_get_max_sum_index_raises_error(self):
        strategy = LocalOptimisation()
        indices = [(1, 2, 4), (3, 2, 1), (4, 2, 1)]
        distances_wrong = [20, 40]

        with raises(ValueError):
            strategy.get_max_sum_ind(indices, distances_wrong, 0, 0)
예제 #3
0
    def test_local_optimised_groups(self,
                                    setup_param_groups_prime):
        """
        Tests that the local optimisation problem gives
        the same answer as the brute force problem
        (for small values of `k_choices` and `N`)
        with groups
        """
        N = 8
        param_file = setup_param_groups_prime
        problem = read_param_file(param_file)
        num_levels = 4
        grid_jump = num_levels / 2
        k_choices = 4

        num_params = problem['num_vars']

        num_groups = len(set(problem['groups']))

        input_sample = _sample_groups(problem, N, num_levels, grid_jump)

        strategy = LocalOptimisation()

        # From local optimal trajectories
        actual = strategy.find_local_maximum(input_sample, N, num_params,
                                             k_choices, num_groups)

        brute = BruteForce()
        desired = brute.brute_force_most_distant(input_sample,
                                                 N,
                                                 num_params,
                                                 k_choices,
                                                 num_groups)
        assert_equal(actual, desired)
예제 #4
0
    def test_add_indices(self):
        '''
        Tests whether the right indices are added.
        '''
        strategy = LocalOptimisation()

        indices = (1, 3, 4)
        matr = np.zeros((6, 6), dtype=np.int16)
        ind_extra = strategy.add_indices(indices, matr)

        expected = [(1, 3, 4, 0), (1, 3, 4, 2), (1, 3, 4, 5)]

        assert_equal(ind_extra, expected)
예제 #5
0
    def test_get_max_sum_ind(self):
        '''
        Tests whether the right maximum indices are returned.
        '''
        strategy = LocalOptimisation()

        indices = np.array([(1, 2, 4), (3, 2, 1), (4, 2, 1)])
        distances = np.array([20, 40, 50])

        output = strategy.get_max_sum_ind(indices, distances, 0, 0)
        expected = (4, 2, 1)

        assert_equal(output, expected)
예제 #6
0
    def test_sum_distances(self, setup_input):
        '''
        Tests whether the combinations are summed correctly.
        '''
        strategy = LocalOptimisation()

        dist_matr = strategy.compute_distance_matrix(setup_input, 6, 2,
                                                     num_groups=None,
                                                     local_optimization=True)
        indices = (1, 3, 2)
        distance = strategy.sum_distances(indices, dist_matr)

        expected = 10.47
        assert_allclose(distance, expected, rtol=1e-2)
예제 #7
0
 def test_combo_from_locally_optimal_method(self, setup_input):
     '''
     Tests whether the correct combination is picked from the fixture drawn
     from Saltelli et al. 2008, in the solution to exercise 3a,
     Chapter 3, page 134.
     '''
     sample_inputs = setup_input
     N = 6
     num_params = 2
     k_choices = 4
     strategy = LocalOptimisation()
     output = strategy.find_local_maximum(sample_inputs, N,
                                          num_params, k_choices)
     expected = [0, 2, 3, 5]  # trajectories 1, 3, 4, 6
     assert_equal(output, expected)
예제 #8
0
    def test_local(self, setup_problem):
        (input_sample, num_samples, _,
         k_choices, groups, num_params, expected) = setup_problem

        local_strategy = LocalOptimisation()
        context = SampleMorris(local_strategy)
        actual = context.sample(input_sample, num_samples, num_params,
                                k_choices, groups)
        np.testing.assert_equal(actual, expected)
예제 #9
0
    def test_local_optimised_groups(self,
                                    setup_param_groups_prime,
                                    execution_number):
        """
        Tests that the local optimisation problem gives
        the same answer as the brute force problem
        (for small values of `k_choices` and `N`)
        with groups for a defined random seed.

        Note that local and brute force methods are not guaranteed to produce
        exact answers, even for small problems.
        """
        rd.seed(12345)

        N = 8
        param_file = setup_param_groups_prime
        problem = read_param_file(param_file)
        num_levels = 4
        k_choices = 4

        num_params = problem['num_vars']

        num_groups = len(set(problem['groups']))

        input_sample = _sample_groups(problem, N, num_levels)

        local = LocalOptimisation()

        # From local optimal trajectories
        actual = local.find_local_maximum(input_sample, N, num_params,
                                          k_choices, num_groups)

        brute = BruteForce()
        desired = brute.brute_force_most_distant(input_sample,
                                                 N,
                                                 num_params,
                                                 k_choices,
                                                 num_groups)

        print("Actual: {}\nDesired: {}\n".format(actual, desired))
        print(input_sample)
        assert_equal(actual, desired)
예제 #10
0
    def test_find_local_maximum_distance(self, setup_input):
        '''
        Test whether finding the local maximum distance equals the global
        maximum distance in a simple case.
        From Saltelli et al. 2008, in the solution to exercise 3a,
        Chapter 3, page 134.
        '''

        local_strategy = LocalOptimisation()
        brute_strategy = BruteForce()

        sample_inputs = setup_input
        N = 6
        num_params = 2
        k_choices = 4
        output_global = brute_strategy.brute_force_most_distant(sample_inputs,
                                                                N, num_params,
                                                                k_choices)
        output_local = local_strategy.find_local_maximum(sample_inputs, N,
                                                         num_params, k_choices)
        assert_equal(output_global, output_local)