Reference · The Applied Higher-Order Functions catalogue
rainbow.algorithms.proofs.ahof.fractional-knapsack
AHoF Fractional Knapsack: unlike the 0/1 version (which needs DP), when items may be taken in fractional amounts the problem is solved OPTIMALLY by a simple greedy rule -- take items in decreasing order of value density (value / weight), filling the knapsack and splitting the last item that does not fully fit. The ordering decision between two items compares their value densities: prefer? density-a density-b = decide (density-b <= density-a) (take the higher-density item first), over exact-rational densities. Proved native and axiom-free: the priority test; worked instances -- density 3 is preferred over density 2; the certificate 2 <= 3 as a native nnrat/at-most; and density 2 is not preferred over 5. Optimality of the greedy order (the classical exchange argument) is not proved here. The value densities (a division) and the fractional fill are executable; what is proved is the greedy priority decision at these instances. Requires nnrat/at-most, audit/decide-nnrat-at-most, nnrat/decide-at-most-true-sound (all in the D63 checkpoint). Scope (measured by the kernel): this module has 3 worked instances (statements about specific values). None of its theorems is a guarantee about the algorithm for all inputs; `lein verify rainbow.algorithms.proofs.ahof.fractional-knapsack` prints this classification.
fractional-knapsack/prefer?
fractional-knapsack/prefer? : (x₁ x₂ : ℚ≥0) → data/Bool
ℚ≥0 = nnrat/NonnegativeRational
fractional-knapsack/prefers-higher-density
fractional-knapsack/prefers-higher-density : → bo/is-safe 3 2 = data/true
= = equality/Eq
fractional-knapsack/preferred-has-higher-density
fractional-knapsack/preferred-has-higher-density : → 2 ≤ 3
≤ = nnrat/at-most
fractional-knapsack/rejects-lower-density
fractional-knapsack/rejects-lower-density : → bo/is-safe 2 5 = data/false
= = equality/Eq