예제 #1
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
예제 #2
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
예제 #3
0
    def test_constraint_heuristic_with_one_constraint(self):
        item1 = Item(weight1=100, value=1)
        item2 = Item(weight1=5, value=4)
        item3 = Item(weight1=5, value=5)

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

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

        expect(backpack.items) == [item3, item2, item3]
예제 #4
0
    def test_passes_lower_bound(self):
        starting_results = AllTestResults()
        starting_results.update_constraint(
            WeightedAverageTargetResults("WAT1", True, 94.46, 9351.54, 99))

        backpack = Backpack(items=range(0, 99),
                            test_results=starting_results,
                            target_item_count=200)
        backpack.add_item(Item(weight10=100))
        target = WeightedAverageTarget("WAT1", lambda x: x.weight10, 100, 20,
                                       2)
        results = target.test(backpack)
        expect(results.passes) is True
        expect(float("{0:.4f}".format(results.result))) == 94.5154
예제 #5
0
    def test_fails_upper_bound(self):
        starting_results = AllTestResults()
        starting_results.update_constraint(
            WeightedAverageTargetResults("WAT1", True, 105.545, 10448.955, 99))

        backpack = Backpack(items=range(0, 99),
                            test_results=starting_results,
                            target_item_count=200)
        backpack.add_item(Item(weight10=101))
        target = WeightedAverageTarget("WAT1", lambda x: x.weight10, 100, 20,
                                       2)
        results = target.test(backpack)
        expect(results.passes) is False
        expect(float("{0:.4f}".format(results.result))) == 105.4995
예제 #6
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
예제 #7
0
    def test_demand_heuristic_with_one_demand(self):
        item1 = Item(weight1=100, value=1)
        item2 = Item(weight1=5, value=4)
        item3 = Item(weight1=5, value=5)

        demands = [
            FastMinTotalValue(lambda x: x.weight1, 20, "Min Item Weight 1"),
        ]

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

        expect(backpack.items) == [
            item1,  # Top quartile progress towards demand, passes the demand minimum
            item3,  # No longer constrained - most bang for buck
            item3
        ]  # No longer constrained - most bang for buck
예제 #8
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
예제 #9
0
 def test_progress_to_demand(self):
     backpack = Backpack()
     backpack.add_item(Item(weight1=2))
     demand = FastMinTotalValue(lambda x: x.weight1, 5, "Test Rule")
     results = demand.test(backpack)
     expect(results.progress_to_demand) == 0.4
예제 #10
0
 def test_fails(self):
     backpack = Backpack()
     backpack.add_item(Item(weight1=9))
     demand = FastMinTotalValue(lambda x: x.weight1, 10, "Test Rule")
     results = demand.test(backpack)
     expect(results.passes) == False
예제 #11
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
예제 #12
0
 def test_fails(self):
     item2 = Item(weight1=25.1)
     backpack = Backpack(items=[Item(weight1=50), item2, item2])
     constraint = MaxItemValue(lambda x: x.weight1, 50)
     results = constraint.test(backpack)
     expect(results.passes) == False
예제 #13
0
 def test_passes(self):
     backpack = Backpack(items=[Item(weight1=10)])
     constraint = MaxItemValue(lambda x: x.weight1, 10)
     results = constraint.test(backpack)
     expect(results.passes) == True