June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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), " €")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue