def test_knapsack_empty(self):
        bag = ()
        size = 50
        ks = Knapsack(bag, size)
        result = ks.solve()

        self.assertEqual((), result)
    def test_knapsack_values(self):
        bag = (
            (10, 60),
            (20, 100),
            (30, 195),
            (40, 120),
            (50, 120),
            (5, 120),
            (60, 120),
            (10, 50),
            (10, 200),
            (20, 120),
        )
        size = 50
        ks = Knapsack(bag, size)
        result = ks.solve()

        self.assertEqual(((30, 195), (5, 120), (10, 200)), result)
Exemple #3
0
    media7 = Curve(664196.7, 0.662257, 0.92, 1.275716e-06 / 87.98509)

    # adding media to budget
    # in second argument we pass minimum and maximum allowed for media
    budget.add_curve(media1, 10600000, maxb)
    budget.add_curve(media2, 5700000, maxb)
    budget.add_curve(media3, 1923077, maxb)
    budget.add_curve(media4, 8307692, maxb)
    budget.add_curve(media5, 3246154, maxb)
    budget.add_curve(media6, 9791667, maxb)
    budget.add_curve(media7, 15786389, maxb)

    # call to optimize spends between media
    # last line represents optimal budget for every media, e.g. first number
    # stands for media1, second for media2, etc.
    budget.solve()
    """
    Optimization terminated successfully.(Exit mode 0)
    Current function value: -1.0779430709417468
    Iterations: 2
    Function evaluations: 2
    Gradient evaluations: 2
    Out[37]:
    array([7917025.28571427, 4140102.28571439, 10524717.28571427,
           5463179.28571427, 12008692.28571427, 12817025.28571427,
           18003414.28571427])
    """
    print(budget.solution)
    # Out[38]: 1.0779430709417468
    # Old solution found with old optimization method
    print(