Пример #1
0
def test_optimised_trajectories_groups(setup_param_groups_prime):
    """
    Tests that the optimisation problem gives
    the same answer as the brute force problem
    (for small values of `k_choices` and `N`)
    with groups
    """

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

    num_params = problem['num_vars']
    groups = compute_groups_matrix(problem['groups'], num_params)
    input_sample = _sample_groups(problem, N, num_levels)

    # From gurobi optimal trajectories
    strategy = GlobalOptimisation()
    actual = strategy.sample(input_sample,
                             N,
                             num_params,
                             k_choices,
                             groups)

    brute_strategy = BruteForce()
    desired = brute_strategy.sample(input_sample,
                                    N,
                                    num_params,
                                    k_choices,
                                    groups)
    assert_equal(actual, desired)
Пример #2
0
def test_optimal_sample_with_groups(setup_param_groups_prime):
    '''
    Tests that the combinatorial optimisation approach matches
    that of the brute force approach
    '''
    param_file = setup_param_groups_prime
    problem = read_param_file(param_file)

    N = 10
    num_levels = 8
    k_choices = 4
    num_params = problem['num_vars']

    sample = _sample_oat(problem,
                         N,
                         num_levels)

    strategy = GlobalOptimisation()
    actual = strategy.return_max_combo(sample,
                                       N,
                                       num_params,
                                       k_choices)

    brute_strategy = BruteForce()
    desired = brute_strategy.brute_force_most_distant(sample,
                                                      N,
                                                      num_params,
                                                      k_choices)

    assert_equal(actual, desired)
Пример #3
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)
Пример #4
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)
Пример #5
0
 def test_catch_combos_too_large(self):
     N = int(1e6)
     k_choices = 4
     num_params = 2
     input_sample = np.random.random_sample((N, num_params))
     strategy = BruteForce()
     with raises(ValueError):
         strategy.find_most_distant(input_sample, N, num_params, k_choices)
Пример #6
0
 def test_make_index_list_with_groups(self):
     N = 4
     num_params = 3
     groups = 2
     strategy = BruteForce()
     actual = strategy._make_index_list(N, num_params, groups)
     desired = [np.array([0, 1, 2]), np.array([3, 4, 5]),
                np.array([6, 7, 8]), np.array([9, 10, 11])]
     assert_equal(actual, desired)
Пример #7
0
def test_optimised_trajectories_without_groups(setup_function):
    """
    Tests that the optimisation problem gives
    the same answer as the brute force problem
    (for small values of `k_choices` and `N`),
    particularly when there are two or more identical
    trajectories
    """

    N = 6
    param_file = setup_function
    problem = read_param_file(param_file)
    k_choices = 4

    num_params = problem['num_vars']
    groups = problem['groups']

    # 6 trajectories, with 5th and 6th identical
    input_sample = np.array([[0.33333333,  0.66666667],
                             [1., 0.66666667],
                             [1., 0.],
                             [0., 0.33333333],
                             [0., 1.],
                             [0.66666667, 1.],
                             [0.66666667, 0.33333333],
                             [0.66666667, 1.],
                             [0., 1.],
                             [0.66666667, 1.],
                             [0.66666667, 0.33333333],
                             [0., 0.33333333],
                             [1., 1.],
                             [1., 0.33333333],
                             [0.33333333, 0.33333333],
                             [1., 1.],
                             [1., 0.33333333],
                             [0.33333333, 0.33333333]], dtype=np.float32)

    # print(input_sample)

    # From gurobi optimal trajectories
    strategy = GlobalOptimisation()
    actual = strategy.return_max_combo(input_sample,
                                       N,
                                       num_params,
                                       k_choices)

    local_strategy = BruteForce()
    desired = local_strategy.brute_force_most_distant(input_sample,
                                                      N,
                                                      num_params,
                                                      k_choices,
                                                      groups)

    assert_equal(actual, desired)
Пример #8
0
 def test_combo_from_find_most_distant(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 = BruteForce()
     scores = strategy.find_most_distant(sample_inputs, N, num_params,
                                         k_choices)
     output = strategy.find_maximum(scores, N, k_choices)
     expected = [0, 2, 3, 5]  # trajectories 1, 3, 4, 6
     assert_equal(output, 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_brute_force(self, setup_problem):

        (input_sample, num_samples, _,
         k_choices, groups, num_params, expected) = setup_problem

        strategy = BruteForce()
        context = SampleMorris(strategy)
        actual = context.sample(input_sample, num_samples, num_params,
                                k_choices, groups)

        np.testing.assert_equal(actual, expected)
Пример #11
0
def test_optimal_combinations(setup_function):

    N = 6
    param_file = setup_function
    problem = read_param_file(param_file)
    num_params = problem['num_vars']
    num_levels = 10
    grid_jump = num_levels / 2
    k_choices = 4

    morris_sample = _sample_oat(problem, N, num_levels, grid_jump)

    global_strategy = GlobalOptimisation()
    actual = global_strategy.return_max_combo(morris_sample, N, num_params,
                                              k_choices)

    brute_strategy = BruteForce()
    desired = brute_strategy.brute_force_most_distant(morris_sample, N,
                                                      num_params, k_choices)
    assert_equal(actual, desired)
Пример #12
0
    def test_scores_from_find_most_distant(self, setup_input):
        '''
        Checks whether array of scores from (6 4) is correct.

        Data is derived 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 = BruteForce()
        output = strategy.find_most_distant(sample_inputs, N, num_params,
                                            k_choices)
        expected = np.array([15.022, 13.871, 14.815, 14.582, 16.178, 14.912,
                             15.055, 16.410, 15.685, 16.098, 14.049, 15.146,
                             14.333, 14.807, 14.825],
                            dtype=np.float32)

        assert_allclose(output, expected, rtol=1e-1, atol=1e-2)
Пример #13
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)
Пример #14
0
def test_optimal_combinations(setup_function):

    N = 6
    param_file = setup_function
    problem = read_param_file(param_file)
    num_params = problem['num_vars']
    num_levels = 10
    k_choices = 4

    morris_sample = _sample_oat(problem, N, num_levels)

    global_strategy = GlobalOptimisation()
    actual = global_strategy.return_max_combo(morris_sample,
                                              N,
                                              num_params,
                                              k_choices)

    brute_strategy = BruteForce()
    desired = brute_strategy.brute_force_most_distant(morris_sample,
                                                      N,
                                                      num_params,
                                                      k_choices)
    assert_equal(actual, desired)
Пример #15
0
def strategy():
    return BruteForce()