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,26 +1,27 @@
using MathProgBase
using MathProgBase, Cbc
immutable KPDSupply{S<:String, T<:Integer}
item::S
struct KPDSupply{T<:Integer}
item::String
weight::T
value::T
quant::T
end
Base.show(io::IO, kdps::KPDSupply) = print(io, kdps.quant, " ", kdps.item, " ($(kdps.weight) kg, $(kdps.value) €)")
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)
q = map(x->x.quant, gear)
sol = mixintprog(-v, w', '<', capacity, :Int, 0, q)
sol.status == :Optimal || error("This Problem could not be solved")
if all(q .== 1)
return gear[sol.sol .== 1.0]
function solve(gear::Vector{KPDSupply{T}}, capacity::Integer) where T<:Integer
w = getfield.(gear, :weight)
v = getfield.(gear, :value)
q = getfield.(gear, :quant)
sol = mixintprog(-v, w', '<', capacity, :Int, 0, q, CbcSolver())
sol.status == :Optimal || error("this problem could not be solved")
if all(q .== 1) # simpler case
return gear[sol.sol == 1.0]
else
pack = KPDSupply[]
s = int(sol.sol)
pack = similar(gear, 0)
s = round.(Int, sol.sol)
for (i, g) in enumerate(gear)
s[i] != 0 || continue
iszero(s[i]) && continue
push!(pack, KPDSupply(g.item, g.weight, g.value, s[i]))
end
return pack

View file

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