changeset 15:be208ce0ffaf

Some more standard DP
author Lewin Bormann <lbo@spheniscida.de>
date Tue, 28 Feb 2023 12:52:34 +0100
parents c6bc1d41657c
children a4a917da5365
files julia/dp.jl
diffstat 1 files changed, 83 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/julia/dp.jl	Fri Feb 24 23:31:00 2023 +0100
+++ b/julia/dp.jl	Tue Feb 28 12:52:34 2023 +0100
@@ -96,6 +96,22 @@
     c
 end
 
+function recursive_liss(v::AbstractVector{<:Integer}, last=-1000, i=1, l=0)::Int
+    if i > length(v)
+        return l
+    else
+        if v[i] >= last
+            incl = recursive_liss(v, v[i], i+1, l+1)
+            excl = recursive_liss(v, last, i+1, l)
+            max(incl, excl)
+        else # v[i] < last
+            incl = recursive_liss(v, v[i], i+1, 1)
+            excl = recursive_liss(v, last, i+1, l)
+            max(incl, excl)
+        end
+    end
+end
+
 function number_of_pattern_times(hs::AbstractString, pat::AbstractString)::Int
     if isempty(pat) || isempty(hs)
         0
@@ -112,3 +128,70 @@
         end
     end
 end
+
+function binpack_simple(items::AbstractVector{<:Pair}, caps::AbstractVector{<:Integer}=Int[], i=1, bincap=5, bincost=1)
+    @show (i, caps)
+    if i > length(items)
+        return 0
+    end
+    size, profit = items[i]
+
+    bin_candidates = zeros(Int, length(caps))
+    for (b, c) = enumerate(caps)
+        if c >= size
+            caps[b] -= size
+            bin_candidates[b] = profit + binpack_simple(items, caps, i+1, bincap, bincost)
+            caps[b] += size
+        else
+            continue
+        end
+    end
+
+    maxbincand = isempty(bin_candidates) ? 0 : maximum(bin_candidates)
+    newbin = profit + binpack_simple(items, vcat(caps, [bincap-size]), i+1, bincap, bincost) - bincost
+
+    if maxbincand > newbin
+        maxbincand
+    else
+        newbin
+    end
+
+end
+
+function knapsack_one_track(cap::Integer, items::AbstractVector{<:Pair}, i=1, map::Vector{Bool}=zeros(Bool, length(items)))
+    if i > length(items) || cap <= 0
+        return zeros(Bool, length(items)), 0
+    end
+
+    current_size, current_profit = items[i]
+    incl_map, incl_item = if current_size > cap
+        zeros(Bool, length(items)), 0
+    else
+        map2, nxp = knapsack_one_track(cap - current_size, items, i+1, map)
+        map2, current_profit + nxp
+    end
+    excl_map, excl_item = knapsack_one_track(cap, items, i+1, map)
+
+    if incl_item > excl_item
+        incl_map[i] = true
+        (incl_map, incl_item)
+    else
+        (excl_map, excl_item)
+    end
+end
+
+function knapsack_one(cap::Integer, sizes::AbstractVector{<:Integer}, profits::AbstractVector{<:Integer})::Int
+    if isempty(sizes) || cap <= 0
+        return 0
+    end
+    # Include first item:
+    with_current = if cap >= sizes[begin]
+            profits[begin] + knapsack_one(cap - sizes[begin], (@view sizes[2:end]), (@view profits[2:end]))
+        else
+            0
+        end
+    # or don't
+    without_current = knapsack_one(cap, (@view sizes[2:end]), (@view profits[2:end]))
+    max(without_current, with_current)
+end
+