Пример #1
0
    def knapsack(self, max_weight_percent, stateCount):
        state, weights, values = self.generate_test_state(stateCount, "KS")
        initial = Knapsack(weights, values, max_weight_percent)
        problem = DiscreteOpt(length=stateCount,
                              fitness_fn=initial,
                              maximize=True)

        problem.set_state(state)
        return problem
Пример #2
0
    def test_knapsack_weight_gt_max():
        """Test Knapsack fitness function for case where total weight is
        greater than the maximum"""
        weights = [10, 5, 2, 8, 15]
        values = [1, 2, 3, 4, 5]
        max_weight_pct = 0.4

        state = np.array([1, 0, 2, 1, 0])
        assert Knapsack(weights, values, max_weight_pct).evaluate(state) == 0
Пример #3
0
    def test_knapsack_weight_lt_max():
        """Test Knapsack fitness function for case where total weight is less
        than the maximum"""
        weights = [10, 5, 2, 8, 15]
        values = [1, 2, 3, 4, 5]
        max_weight_pct = 0.6

        state = np.array([1, 0, 2, 1, 0])
        calculated_weights = Knapsack(weights, values, max_weight_pct).evaluate(state)
        assert calculated_weights == 11