June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,20 +1,17 @@
using MathProgBase
immutable KPDSupply{S<:String, T<:Integer}
item::S
struct KPDSupply{T<:Integer}
item::String
weight::T
value::T
quant::T
end
function KPDSupply{S<:String, T<:Integer}(item::S, weight::T, value::T)
KPDSupply(item, weight, value, one(T))
end
function solve{S<:String, T<:Integer}(gear::Array{KPDSupply{S,T},1},
capacity::T)
w = map(x->x.weight, gear)
v = map(x->x.value, gear)
sol = mixintprog(-v, w', '<', capacity, :Bin, 0, 1)
sol.status == :Optimal || error("This Problem could not be solved")
gear[sol.sol .== 1.0]
KPDSupply{T<:Integer}(itm::AbstractString, w::T, v::T, q::T=one(T)) = KPDSupply(itm, w, v, q)
Base.show(io::IO, kdps::KPDSupply) = print(io, kdps.quant, " ", kdps.item, " ($(kdps.weight) kg, $(kdps.value) €)")
using MathProgBase, Cbc
function solve(gear::Vector{<:KPDSupply}, capacity::Integer)
w = getfield.(gear, :weight)
v = getfield.(gear, :value)
sol = mixintprog(-v, w', '<', capacity, :Bin, 0, 1, CbcSolver())
gear[sol.sol .≈ 1]
end

View file

@ -22,11 +22,6 @@ gear = [KPDSupply("map", 9, 150),
KPDSupply("book", 30, 10)]
pack = solve(gear, 400)
println("The hiker should pack:")
for s in pack
println(" ", s.item)
end
println()
println("Packed Weight: ", mapreduce(x->x.weight, +, pack))
println("Packed Value: ", mapreduce(x->x.value, +, pack))
println("The hicker should pack: \n - ", join(pack, "\n - "))
println("\nPacked weight: ", mapreduce(x -> x.weight, +, pack), " kg")
println("Packed value: ", mapreduce(x -> x.value, +, pack), " €")

View file

@ -0,0 +1,33 @@
#lang racket
(define items '((map 9 150) (compass 13 35) (water 153 200) (sandwich 50 160)
(glucose 15 60) (tin 68 45)(banana 27 60) (apple 39 40)
(cheese 23 30) (beer 52 10) (cream 11 70) (camera 32 30)
(T-shirt 24 15) (trousers 48 10) (umbrella 73 40)
(trousers 42 70) (overclothes 43 75) (notecase 22 80)
(glasses 7 20) (towel 18 12) (socks 4 50) (book 30 10)))
(define max-weight 400)
(define (item-value item)
(caddr item))
(define (item-weight item)
(cadr item))
(define (pack-weight pack)
(apply + (map item-weight pack)))
(define (pack-value pack)
(apply + (map item-value pack)))
(define (max-pack-value pack-with pack-without max-weight)
(if (and
(not (> (pack-weight pack-with) max-weight))
(> (pack-value pack-with) (pack-value pack-without)))
pack-with pack-without))
(define (display-solution pack)
(displayln (list 'weight: (pack-weight pack)
'value: (pack-value pack)
'items: (map car pack))))

View file

@ -0,0 +1,17 @@
(define (show-brute)
(define empty-accumulator '())
(define (knapsack-brute included items)
(cond
((null? items) included)
(else
(max-pack-value
(knapsack-brute (cons (car items) included) (cdr items))
(knapsack-brute included (cdr items))
max-weight
))))
(display-solution (reverse (knapsack-brute empty-accumulator items))))
(show-brute); takes around five seconds on my machine

View file

@ -0,0 +1,24 @@
(define (show-memoized)
(define (memoize func)
(let ([result-ht (make-hash)])
(lambda args ; this is the rest-id pattern
(when (not (hash-has-key? result-ht args))
(hash-set! result-ht args (apply func args)))
(hash-ref result-ht args))))
(define knapsack
(memoize
(lambda (max-weight items)
(cond
((null? items) '())
(else
(let ([item (car items)] [items (cdr items)])
(max-pack-value
(cons item (knapsack (- max-weight (item-weight item)) items))
(knapsack max-weight items)
max-weight)))))))
(display-solution (knapsack max-weight items)))
(show-memoized)

View file

@ -0,0 +1 @@
(weight: 396 value: 1030 items: (map compass water sandwich glucose banana cream trousers overclothes notecase glasses socks))

View file

@ -0,0 +1,88 @@
# Project : Knapsack problem/0-1
# Date : 2018/01/27
# Author : Gal Zsolt [~ CalmoSoft ~]
# Email : <calmosoft@gmail.com>
knap = [["map",9,150],
["compass",13,35],
["water",153,20],
["sandwich",50,160],
["glucose",15,60],
["tin",68,45],
["banana",27,60],
["apple",39,40],
["cheese",23,30],
["beer",52,10],
["suntan cream",11,70],
["camera",32,30],
["T-shirt",24,15],
["trousers",48,10],
["umbrella",73,40],
["waterproof trousers",42,70],
["waterproof overclothes",43,75],
["note-case",22,80],
["sunglasses",7,20],
["towel",18,12],
["socks",4,50],
["book",30,10]]
knapsack = createDimList([pow(2, len(knap)),len(knap)+2])
lenknap = list(pow(2, len(knap)))
sacksize = 400
powerset(knap)
for n = 1 to pow(2, len(knap))-2
for m = n + 1 to pow(2, len(knap))-1
if knapsack[m][lenknap[m]-1] <= sacksize and
knapsack[m][lenknap[m]] > knapsack[n][lenknap[n]]
temp = knapsack[n]
lentemp = lenknap[n]
knapsack[n] = knapsack[m]
knapsack[n+1] = temp
lenknap[n] = lenknap[m]
lenknap[n+1] = lentemp
ok
next
next
for n = 1 to lenknap[1] - 2
see knapsack[1][n] + nl
next
see "Total weight = " + knapsack[1][lenknap[1]-1] + nl
see "Total value = " + knapsack[1][lenknap[1]] + nl
func powerset(list)
n1 = 0
for i = 2 to (2 << len(list)) - 1 step 2
n2 = 0
n1 = n1 + 1
weight = 0
value = 0
for j = 1 to len(list)
if i & (1 << j)
n2 = n2 + 1
knapsack[n1][n2] = list[j][1]
weight = weight + list[j][2]
value = value + list[j][3]
knapsack[n1][n2+1] = weight
knapsack[n1][n2+2] = value
ok
next
lenknap[n1] = n2+2
next
func createDimList(dimArray)
sizeList = len(dimArray)
newParms = []
for i = 2 to sizeList
Add(newParms, dimArray[i])
next
alist = list(dimArray[1])
if sizeList = 1
return aList
ok
for t in alist
t = createDimList(newParms)
next
return alist