Пример #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_random_seed(self, setup_param_groups_prime):
        """Setting the seed before generating a sample results in two
        identical samples
        """
        N = 8
        param_file = setup_param_groups_prime
        problem = read_param_file(param_file)
        num_levels = 4

        np.random.seed(12345)
        expected = _sample_groups(problem, N, num_levels)

        np.random.seed(12345)
        actual = _sample_groups(problem, N, num_levels)

        assert_equal(actual, expected)
Пример #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_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)
Пример #5
0
def test_size_of_trajectories_with_groups(setup_param_groups_prime):
    '''
    Tests that the number of trajectories produced is computed
    correctly (i.e. that the size of the trajectories is a function
    of the number of groups, rather than the number of variables
    when groups are used.

    There are seven variables and three groups.
    With N=10:
    1. the sample ignoring groups (i.e. the call to `sample_oat')
    should be of size N*(D+1)-by-D.
    2. the sample with groups should be of size N*(G+1)-by-D
    When k=4:
    3. the optimal sample ignoring groups should be of size k*(D+1)-by-D
    4. the optimal sample with groups should be of size k*(G+1)-by-D
    '''
    param_file = setup_param_groups_prime
    group_problem = read_param_file(param_file)
    no_group_problem = read_param_file(param_file)
    no_group_problem['groups'] = None

    N = 11
    num_levels = 8
    k_choices = 4
    num_params = group_problem['num_vars']

    num_groups = 3

    # Test 1. dimensions of sample ignoring groups
    sample = _sample_oat(no_group_problem,
                         N,
                         num_levels)

    size_x, size_y = sample.shape

    assert_equal(size_x, N * (num_params + 1))
    assert_equal(size_y, num_params)

    # Test 2. dimensions of sample with groups

    group_sample = _sample_groups(group_problem,
                                  N,
                                  num_levels)

    size_x, size_y = group_sample.shape

    assert_equal(size_x, N * (num_groups + 1))
    assert_equal(size_y, num_params)

    # Test 3. dimensions of optimal sample without groups

    optimal_sample_without_groups = \
        _compute_optimised_trajectories(no_group_problem,
                                        sample,
                                        N,
                                        k_choices)

    size_x, size_y = optimal_sample_without_groups.shape

    assert_equal(size_x, k_choices * (num_params + 1))
    assert_equal(size_y, num_params)

    # Test 4. dimensions of optimal sample with groups

    optimal_sample_with_groups = _compute_optimised_trajectories(group_problem,
                                                                 group_sample,
                                                                 N,
                                                                 k_choices)

    size_x, size_y = optimal_sample_with_groups.shape

    assert_equal(size_x, k_choices * (num_groups + 1))
    assert_equal(size_y, num_params)