Пример #1
0
 def test_bang_for_buck(self):
     item = Item(weight1=10, value=2)
     backpack = Backpack()
     backpack.add_item(item)
     constraint = FastMaxItemValue(lambda x: x.weight1, 50, "Test Rule")
     results = constraint.test(backpack)
     expect(results.bang_for_buck) == 10
Пример #2
0
 def test_fit_multiple(self):
     item = Item(weight1=10)
     backpack = Backpack()
     backpack.add_item(item)
     constraint = FastMaxItemValue(lambda x: x.weight1, 50, "Test Rule")
     results = constraint.test(backpack)
     expect(results.fit_multiple) == 5
Пример #3
0
    def test_constraint_heuristic_with_two_constraints(self):
        item1 = Item(weight1=100, value=1)
        item2 = Item(weight1=5, value=5)
        item3 = Item(weight2=5, value=4)

        constraints = [
            FastMaxItemValue(lambda x: x.weight1, 20, "Max Item Weight 1"),
            FastMaxItemValue(lambda x: x.weight2, 20, "Max Item Weight 2"),
        ]

        backpack = Backpack(constraints)
        backpack.pack(3, [item1, item2, item3])

        expect(backpack.items) == [item2, item3, item2]
Пример #4
0
    def test_fails(self):
        item = Item(weight1=25.1)
        max_value_results = MaxValueResult(
            True, {item: {
                'pass': True,
                'total': 25.1
            }}, "Test Rule")
        results = AllTestResults()
        results.update_constraint(max_value_results)

        backpack = Backpack(test_results=results)
        backpack.add_item(item)

        constraint = FastMaxItemValue(lambda x: x.weight1, 50, "Test Rule")
        new_max_value_results = constraint.test(backpack)
        expect(new_max_value_results.passes) == False
Пример #5
0
    def test_pack(self):
        target_number_of_items = 500
        items = create_random_items(500)

        constraints = [
            FastMaxItemValue(lambda x: x.weight1, 50, "Max Item Weight 1"),
            FastMaxItemValue(lambda x: x.weight2, 20, "Max Item Weight 2"),
            FastMaxItemValue(lambda x: x.weight3, 20, "Max Item Weight 3"),
            FastMaxItemValue(lambda x: x.weight4, 200, "Max Item Weight 4"),
            FastMaxItemValue(lambda x: x.weight5, 500, "Max Item Weight 5"),
            WeightedAverageTarget("Weighted Average Target 10",
                                  lambda x: x.weight10, 98.25, 1, 0.1)
        ]

        demands = [
            FastMinTotalValue(lambda x: x.weight6, 100, "Min Total Weight 6"),
            FastMinTotalValue(lambda x: x.weight7, 150, "Min Total Weight 7"),
            FastMinTotalValue(lambda x: x.weight8, 200, "Min Total Weight 8"),
            FastMinTotalValue(lambda x: x.weight9, 1000, "Min Total Weight 9"),
        ]

        backpack = Backpack(constraints, demands=demands)
        backpack.pack(target_number_of_items, items)

        expect(len(backpack.items)) == target_number_of_items

        expect(backpack.test_results.for_test("Max Item Weight 1").total) <= 50
        expect(backpack.test_results.for_test("Max Item Weight 2").total) <= 20
        expect(backpack.test_results.for_test("Max Item Weight 3").total) <= 20
        expect(
            backpack.test_results.for_test("Max Item Weight 4").total) <= 200
        expect(
            backpack.test_results.for_test("Max Item Weight 5").total) <= 500
        98.15 <= expect(
            backpack.test_results.for_test(
                "Weighted Average Target 10").result) <= 98.35

        expect(
            backpack.test_results.for_test("Min Total Weight 6").total) >= 500
        expect(
            backpack.test_results.for_test("Min Total Weight 7").total) >= 1000
        expect(
            backpack.test_results.for_test("Min Total Weight 8").total) >= 2000
        expect(
            backpack.test_results.for_test("Min Total Weight 9").total) >= 50
Пример #6
0
 def test_passes(self):
     backpack = Backpack()
     backpack.add_item(Item(weight1=10))
     constraint = FastMaxItemValue(lambda x: x.weight1, 10, "Test Rule")
     results = constraint.test(backpack)
     expect(results.passes) == True