Example #1
0
func PreferenceOverLotteryExample(t *testing.T) {
	s := set.WithElements(0, 1, 2, 3, 4)

	l := NewUniformLottery(s)

	l2 := NewLottery(s)
	l.AddOutcome(2, .5)
	l.AddOutcome(3, .5)

	l3 := NewDegenerateLottery(s, 4)

	// E(l)  => .2*0 + .2*10 + .2*20 + .2*30 + .2*30 = 18
	// E(l2) => .5*20 + .5*30 = 35
	// E(l3) => 1*40 = 40

	lotteries := set.WithElements(l, l2, l3)

	b := NewUtilityBinaryRelationOn(lotteries, func(a Alternative) Utility {
		return ExpectedUtility(a.(Lottery), func(a Alternative) Utility {
			return Utility(a.(int) * 10)
		})
	})

	if !Rational(b) {
		t.Errorf("expected the relation to be rational")
	}

	if !b.ContainsRelation(l3, l2) {
		t.Errorf("expected to prefer l3 to l2")
	}

	if !b.ContainsRelation(l2, l) {
		t.Errorf("expected to prefer l2 to l1")
	}
}
Example #2
0
func TestSetUnion(t *testing.T) {
	t.Parallel()

	A := set.WithElements(1, 2, 3)
	B := set.WithElements(4, 5, 6)
	U := set.Union(A, B)

	for i := 1; i < 7; i++ {
		if !U.Contains(i) {
			t.Fatalf("Union of %s and %s should contain %d", A, B, i)
		}
	}

	if U.Cardinality() != 6 {
		t.Fatalf("Expected cardinality of union of %s and %s to be 6", A, B)
	}

	U.Remove(1)
	U.Remove(6)

	if !A.Contains(1) || !B.Contains(6) {
		t.Fatalf("Modifying the union set should not change the original set")
	}

}
Example #3
0
func TestPowerSet(t *testing.T) {
	t.Parallel()

	A := set.WithElements(1, 2, 3)
	P := set.PowerSet(A)

	one := set.WithElements(1)

	if !P.Contains(one) {
		t.Fatalf("Power set of %s should contains %s", A, one)
	}
}
Example #4
0
func TestSetRelations(t *testing.T) {
	t.Parallel()

	elements := []set.Element{"A", "B", "C", "D", "E", "F"}
	A := set.WithElements(elements...)
	B := set.WithElements(elements...)

	if !set.IsSubset(A, B) {
		t.Fatalf("%s should be a subset of %s", A, B)
	}

	if set.IsProperSubset(A, B) {
		t.Fatalf("%s should be a not proper subset of %s", A, B)
	}

	if !set.IsSuperset(A, B) {
		t.Fatalf("%s should be a superset of %s", A, B)
	}

	if !set.IsSubset(A, A) {
		t.Fatalf("%s should be a subset of itself")
	}

	if set.IsProperSubset(A, A) {
		t.Fatalf("%s should not be a proper subset of itself")
	}

	if !set.IsSuperset(A, A) {
		t.Fatalf("%s should be a superset of itself")
	}

	A.Remove("A")

	if !set.IsSubset(A, B) {
		t.Fatal("%s should be a subset of %s", A, B)
	}

	if set.IsSubset(B, A) {
		t.Fatalf("%s should not be a subset of %s", B, A)
	}

	if !set.IsSuperset(B, A) {
		t.Fatal("%s should be a superset of %s", B, A)
	}

	if set.IsSuperset(A, B) {
		t.Fatal("%s should not be a superset of %s", A, B)
	}

	if !set.IsProperSubset(A, B) {
		t.Fatal("%s should be a proper subset of %s", A, B)
	}
}
Example #5
0
func TestSetComplement(t *testing.T) {
	t.Parallel()

	A := set.WithElements(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
	B := set.WithElements(2, 4, 6, 8, 10)
	Odds := set.Complement(A, B)

	expectedOdds := set.WithElements(1, 3, 5, 7, 9)

	if !set.Equivalent(Odds, expectedOdds) {
		t.Fatalf("Expected %s\\%s to be %s, but got %s", A, B, expectedOdds, Odds)
	}
}
Example #6
0
func TestSetIntersection(t *testing.T) {
	t.Parallel()

	A := set.WithElements("one", "two")
	B := set.WithElements("two", "three")
	I := set.Intersection(A, B)

	if I.Cardinality() != 1 {
		t.Fatalf("Intersection of %s and %s should have cardinality 1", A, B)
	}

	expected := set.WithElements("two")

	if !set.Equivalent(I, expected) {
		t.Fatalf("Expected %s intersect %s to be %s, but got %s", A, B, expected, I)
	}
}
Example #7
0
func TestEquivalent(t *testing.T) {
	t.Parallel()

	elements := []set.Element{"A", "B", "C", "D", "E", "F"}
	A := set.WithElements(elements...)
	B := set.WithElements(elements...)

	if !set.Equivalent(A, B) {
		t.Fatalf("%s and %s should be equivalent", A, B)
	}

	if set.Equivalent(A, set.Empty) {
		t.Fatalf("%s and %s should not be equivalent", A, set.Empty)
	}

	if !set.Equivalent(set.Empty, set.Empty) {
		t.Fatalf("%s and %s should be equivalent", set.Empty, set.Empty)
	}
}
Example #8
0
func TestString(t *testing.T) {
	t.Parallel()

	A := set.WithElements(1, 2, 3)
	B := set.Clone(A)

	if !set.Equivalent(A, B) {
		t.Fatalf("Clone of %s: %s should be equivalent", A, B)
	}

	B.Remove(1)

	if set.Equivalent(A, B) {
		t.Fatalf("%s should no longer be equivalent to %s", B, A)
	}

	if !A.Contains(1) {
		t.Fatalf("%s should still contain 1", A)
	}
}
Example #9
0
func TestConstructors(t *testing.T) {
	t.Parallel()

	s := make([]set.Element, 10)

	for i := 0; i < 10; i++ {
		s[i] = i
	}

	A := set.With(s)
	B := set.WithElements(s...)

	for _, v := range s {
		if !A.Contains(v) {
			log.Fatal("A should contain %d (created by set.With)", v)
		}
		if !B.Contains(v) {
			log.Fatal("B should contain %d (created with set.WithElements", v)
		}
	}
}
Example #10
0
func TestBinaryRelationBasicUsage(t *testing.T) {
	s := set.WithElements(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)

	b := relation.New(s)

	if b.Universe() != s {
		t.Errorf("Expected b's universe to be the set given in constructoon")
	}

	// Let's define the greater than binary relation
	b.AddRelation(2, 1)
	b.AddRelation(3, 2)
	b.AddRelation(4, 3)
	b.AddRelation(5, 4)
	b.AddRelation(6, 5)
	b.AddRelation(7, 6)
	b.AddRelation(8, 7)
	b.AddRelation(9, 8)
	b.AddRelation(1, 0)

	if !b.ContainsRelation(3, 2) {
		t.Error("Expected the binary relation to contain (3, 2), as we defined it")
	}

	if relation.Complete(b) {
		t.Error("Expected b to be incomplete, consider: (9, 0)")
	}

	if !b.ContainsRelation(1, 0) {
		t.Errorf("Expected binary relation to contain (1, 0")
	}

	b.RemoveRelation(1, 0)

	if b.ContainsRelation(1, 0) {
		t.Errorf("Expected binary relation to no longer contain (1, 0), as we removed it")
	}
}