Exemplo n.º 1
0
 def test_eq(self, low, high, res):
     """This also tests Set.array().
     This test can massively slow down hypothesis with even
     reasonably large/small values.
     """
     assume(low < high)
     # to avoid MemoryError and runs that take forever..
     assume(high - low <= 10)
     D1 = Domain("1", low, high, res=res)
     D1.s1 = fun.bounded_linear(low, high)
     D2 = Domain("2", low, high, res=res)
     D2.s2 = Set(fun.bounded_linear(low, high))
     assert D1.s1 == D2.s2
Exemplo n.º 2
0
def test_rating():
    """Tom is surveying restaurants.
    He doesn't need fancy logic but rather uses a simple approach
    with weights.
    He went into a small, dirty bar that served some
    really good drink and food that wasn't nicely arranged but still
    yummmy. He rates the different factors on a scale from 1 to 10,
    uses a bounded_linear function to normalize over [0,1] and
    passes both the weights (how much each aspect should weigh in total)
    and the domain as parameters into weighted_sum.
    However, he can't just use Domain(value) because that would return
    a dict of memberships, instead he uses Domain.min(value) which
    returns the minimum of all memberships no matter how many sets
    there are. He creates a dict of membership values corresponding to
    the weights and passes that into the parametrized weighted_sum func
    as argument to get the final rating for this restaurant.
    """
    R = Domain("rating", 1, 10, res=0.1)
    R.norm = bounded_linear(1, 10)
    weights = {"beverage": 0.3, "atmosphere": 0.2, "looks": 0.2, "taste": 0.3}
    w_func = weighted_sum(weights=weights, target_d=R)

    ratings = {
        "beverage": R.min(9),
        "atmosphere": R.min(5),
        "looks": R.min(4),
        "taste": R.min(8),
    }
    assert w_func(ratings) == 6.9
Exemplo n.º 3
0
 def test_complement(self):
     D = Domain("d", 0, 10, res=0.1)
     D.s1 = Set(fun.bounded_linear(3, 12))
     D.s2 = ~~D.s1
     assert all(np.isclose(D.s1.array(), D.s2.array()))
Exemplo n.º 4
0
 def test_sub_super_set(self):
     D = Domain("d", 0, 10, res=0.1)
     D.s = Set(fun.bounded_linear(3, 12))
     D.x = D.s.normalized()
     assert D.x >= D.s
     assert D.s <= D.x
Exemplo n.º 5
0
 def test_normalized(self):
     D = Domain("d", 0, 10, res=0.1)
     D.s = Set(fun.bounded_linear(3, 12))
     D.x = D.s.normalized()
     D.y = D.x.normalized()
     assert D.x == D.y
Exemplo n.º 6
0
 def test_bounded_linear(self, x, low, high, c_m, no_m):
     assume(low < high)
     assume(c_m > no_m)
     f = fun.bounded_linear(low, high, c_m=c_m, no_m=no_m)
     assert 0 <= f(x) <= 1