コード例 #1
0
ファイル: test_cquad.py プロジェクト: basnijholt/adaptive
def test_approximating_intervals():
    import random
    learner = IntegratorLearner(f24, bounds=(0, 3), tol=1e-10)

    xs, _ = learner.ask(10000)
    random.shuffle(xs)
    for x in xs:
        learner.tell(x, f24(x))

    ivals = sorted(learner.approximating_intervals, key=lambda l: l.a)
    for i in range(len(ivals) - 1):
        assert ivals[i].b == ivals[i + 1].a, (ivals[i], ivals[i + 1])
コード例 #2
0
ファイル: test_cquad.py プロジェクト: jhoofwijk/adaptive
def test_tell_in_random_order(first_add_33=False):
    from operator import attrgetter
    import random
    tol = 1e-10
    for f, a, b in (
        [f0, 0, 3],
        [f21, 0, 1],
        [f24, 0, 3],
        [f7, 0, 1],
    ):
        learners = []

        for shuffle in [True, False]:
            l = IntegratorLearner(f, bounds=(a, b), tol=tol)

            if first_add_33:
                xs, _ = l.ask(33)
                for x in xs:
                    l.tell(x, f(x))

            xs, _ = l.ask(10000)

            if shuffle:
                random.shuffle(xs)
            for x in xs:
                l.tell(x, f(x))

            learners.append(l)

        # Check whether the points of the learners are identical
        assert set(learners[0].done_points) == set(learners[1].done_points)

        # Test whether approximating_intervals gives a complete set of intervals
        for l in learners:
            ivals = sorted(l.approximating_intervals, key=lambda l: l.a)
            for i in range(len(ivals) - 1):
                assert ivals[i].b == ivals[i + 1].a, (ivals[i], ivals[i + 1])

        # Test if approximating_intervals is the same for random order of adding the point
        ivals = [
            sorted(ival, key=attrgetter('a'))
            for ival in [l.approximating_intervals for l in learners]
        ]
        assert all(ival.a == other_ival.a for ival, other_ival in zip(*ivals))

        # Test if the approximating_intervals are the same
        ivals = [{(i.a, i.b)
                  for i in l.approximating_intervals} for l in learners]
        assert ivals[0] == ivals[1]

        # Test whether the igral is identical
        assert np.allclose(learners[0].igral, learners[1].igral), f

        # Compare if the errors are in line with the sequential case
        igral, err, *_ = algorithm_4(f, a, b, tol=tol)
        assert all((l.err + err >= abs(l.igral - igral)) for l in learners)

        # Check that the errors are finite
        for l in learners:
            assert np.isfinite(l.err)
コード例 #3
0
ファイル: test_cquad.py プロジェクト: basnijholt/adaptive
def test_removed_ask_one_by_one():
    with pytest.raises(RuntimeError):
        # This test should raise because integrating np.exp should be done
        # after the 33th point
        learner = IntegratorLearner(np.exp, bounds=(0, 1), tol=1e-15)
        n = ns[-1] + 2 * (ns[0] - 2)  # first + two children (33+6=39)
        for _ in range(n):
            xs, _ = learner.ask(1)
            for x in xs:
                learner.tell(x, learner.function(x))
コード例 #4
0
ファイル: test_cquad.py プロジェクト: basnijholt/adaptive
def test_removed_choose_mutiple_points_at_once():
    """Given that a high-precision interval that was split into 2 low-precision ones,
       we should use the high-precision interval.
    """
    learner = IntegratorLearner(np.exp, bounds=(0, 1), tol=1e-15)
    n = ns[-1] + 2 * (ns[0] - 2)  # first + two children (33+6=39)
    xs, _ = learner.ask(n)
    for x in xs:
        learner.tell(x, learner.function(x))
    assert list(learner.approximating_intervals)[0] == learner.first_ival
コード例 #5
0
ファイル: test_cquad.py プロジェクト: basnijholt/adaptive
def run_integrator_learner(f, a, b, tol, n):
    learner = IntegratorLearner(f, bounds=(a, b), tol=tol)
    for _ in range(n):
        points, _ = learner.ask(1)
        learner.tell_many(points, map(learner.function, points))
    return learner
コード例 #6
0
ファイル: test_cquad.py プロジェクト: basnijholt/adaptive
def test_adding_points_and_skip_one_point():
    learner = IntegratorLearner(f24, bounds=(0, 3), tol=1e-10)
    xs, _ = learner.ask(17)
    skip_x = xs[1]

    for x in xs:
        if x != skip_x:
            learner.tell(x, learner.function(x))

    for i in range(1000):
        xs, _ = learner.ask(1)
        for x in xs:
            if x != skip_x:
                learner.tell(x, learner.function(x))

    # Now add the point that was skipped
    learner.tell(skip_x, learner.function(skip_x))

    # Create a learner with the same number of points, which should
    # give an identical igral value.
    learner2 = IntegratorLearner(f24, bounds=(0, 3), tol=1e-10)
    for i in range(1017):
        xs, _ = learner2.ask(1)
        for x in xs:
            learner2.tell(x, learner2.function(x))

    np.testing.assert_almost_equal(learner.igral, learner2.igral)
コード例 #7
0
ファイル: test_cquad.py プロジェクト: basnijholt/adaptive
def test_choosing_and_adding_multiple_points_at_once():
    learner = IntegratorLearner(f24, bounds=(0, 3), tol=1e-10)
    xs, _ = learner.ask(100)
    for x in xs:
        learner.tell(x, learner.function(x))
コード例 #8
0
ファイル: test_cquad.py プロジェクト: basnijholt/adaptive
def test_choosing_and_adding_points_one_by_one():
    learner = IntegratorLearner(f24, bounds=(0, 3), tol=1e-10)
    for _ in range(1000):
        xs, _ = learner.ask(1)
        for x in xs:
            learner.tell(x, learner.function(x))