Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -44,11 +44,11 @@ Adjust the supply and demand accordingly. If demand or supply becomes 0 for a gi
|
|||
|
||||
Repeat until all supply and demand is met:
|
||||
<pre>
|
||||
2 2 2 0 1 2 3 1 0 - C-W(50)
|
||||
3 5 5 7 4 35 - 1 0 - E-X(50)
|
||||
2 2 2 0 3 2 3 1 0 - C-W(50)
|
||||
3 5 5 7 4 35 - 1 0 - E-X(10)
|
||||
4 5 5 7 4 - - 1 0 - C-X(20)
|
||||
5 5 5 - 4 - - 0 0 - A-X(30)
|
||||
6 - 19 - 23 - - - 19 - D-Y(30)
|
||||
6 - 19 - 23 - - - 4 - D-Y(30)
|
||||
- - - - - - - - - B-Y(20)
|
||||
</pre>
|
||||
Finally calculate the cost of your solution. In the example given it is £3100:
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
vam=:1 :0
|
||||
:
|
||||
exceeding=. 0 <. -&(+/)
|
||||
D=. x,y exceeding x NB. x: demands
|
||||
S=. y,x exceeding y NB. y: sources
|
||||
C=. (m,.0),0 NB. m: costs
|
||||
B=. 1+>./,C NB. bigger than biggest cost
|
||||
mincost=. <./@-.&0 NB. smallest non-zero cost
|
||||
penalty=. |@(B * 2 -/@{. /:~ -. 0:)"1 - mincost"1
|
||||
R=. C*0
|
||||
while. 0 < +/D,S do.
|
||||
pS=. penalty C
|
||||
pD=. penalty |:C
|
||||
if. pS >&(>./) pD do.
|
||||
row=. (i. >./) pS
|
||||
col=. (i. mincost) row { C
|
||||
else.
|
||||
col=. (i. >./) pD
|
||||
row=. (i. mincost) col {"1 C
|
||||
end.
|
||||
n=. (row{S) <. col{D
|
||||
S=. (n-~row{S) row} S
|
||||
D=. (n-~col{D) col} D
|
||||
C=. C * S *&*/ D
|
||||
R=. n (<row,col)} R
|
||||
end.
|
||||
_1 _1 }. R
|
||||
)
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
demand=: 30 20 70 30 60
|
||||
src=: 50 60 50 50
|
||||
cost=: 16 16 13 22 17,14 14 13 19 15,19 19 20 23 50,:50 12 50 15 11
|
||||
|
||||
demand cost vam src
|
||||
0 0 50 0 0
|
||||
30 0 20 0 10
|
||||
0 20 0 30 0
|
||||
0 0 0 0 50
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
immutable TProblem{T<:Integer,U<:String}
|
||||
sd::Array{Array{T,1},1}
|
||||
toc::Array{T,2}
|
||||
labels::Array{Array{U,1},1}
|
||||
tsort::Array{Array{T,2}, 1}
|
||||
end
|
||||
|
||||
function TProblem{T<:Integer,U<:String}(s::Array{T,1},
|
||||
d::Array{T,1},
|
||||
toc::Array{T,2},
|
||||
slab::Array{U,1},
|
||||
dlab::Array{U,1})
|
||||
scnt = length(s)
|
||||
dcnt = length(d)
|
||||
size(toc) = (scnt,dcnt) || error("Supply, Demand, TOC Size Mismatch")
|
||||
length(slab) == scnt || error("Supply Label Size Labels")
|
||||
length(dlab) == dcnt || error("Demand Label Size Labels")
|
||||
0 <= minimum(s) || error("Negative Supply Value")
|
||||
0 <= minimum(d) || error("Negative Demand Value")
|
||||
sd = Array{T,1}[]
|
||||
push!(sd, s)
|
||||
push!(sd, d)
|
||||
labels = Array{U,1}[]
|
||||
push!(labels, slab)
|
||||
push!(labels, dlab)
|
||||
tsort = Array{T,2}[]
|
||||
push!(tsort, mapslices(sortperm, toc, 2))
|
||||
push!(tsort, mapslices(sortperm, toc, 1))
|
||||
TProblem(sd, toc, labels, tsort)
|
||||
end
|
||||
isbalanced(tp::TProblem) = sum(tp.sd[1]) == sum(tp.sd[2])
|
||||
|
||||
type Resource{T<:Integer}
|
||||
dim::T
|
||||
i::T
|
||||
quant::T
|
||||
l::T
|
||||
m::T
|
||||
p::T
|
||||
q::T
|
||||
end
|
||||
function Resource{T<:Integer}(dim::T, i::T, quant::T)
|
||||
zed = zero(T)
|
||||
Resource(dim, i, quant, zed, zed, zed, zed)
|
||||
end
|
||||
|
||||
isavailable(r::Resource) = 0 < r.quant
|
||||
Base.isless(a::Resource, b::Resource) = a.p < b.p || (a.p == b.p && b.q < a.q)
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
function penalize!{T<:Integer,U<:String}(sd::Array{Array{Resource{T},1},1},
|
||||
tp::TProblem{T,U})
|
||||
avail = BitArray{1}[]
|
||||
for dim in 2:-1:1
|
||||
push!(avail, bitpack(map(isavailable, sd[dim])))
|
||||
end
|
||||
for dim in 1:2, r in sd[dim]
|
||||
if r.quant == 0
|
||||
r.l = r.m = r.p = r.q = 0
|
||||
continue
|
||||
end
|
||||
r.l == 0 || !avail[dim][r.l] || !avail[dim][r.m] || continue
|
||||
rsort = filter(x->avail[dim][x], vec(slicedim(tp.tsort[dim],dim,r.i)))
|
||||
rcost = vec(slicedim(tp.toc, dim, r.i))[rsort]
|
||||
if length(rsort) == 1
|
||||
r.l = r.m = rsort[1]
|
||||
r.p = r.q = rcost[1]
|
||||
else
|
||||
r.l, r.m = rsort[1:2]
|
||||
r.p = rcost[2] - rcost[1]
|
||||
r.q = rcost[1]
|
||||
end
|
||||
end
|
||||
nothing
|
||||
end
|
||||
|
||||
function vogel{T<:Integer,U<:String}(tp::TProblem{T,U})
|
||||
sdcnt = collect(size(tp.toc))
|
||||
sol = spzeros(T, sdcnt[1], sdcnt[2])
|
||||
sd = Array{Resource{T},1}[]
|
||||
for dim in 1:2
|
||||
push!(sd, [Resource(dim, i, tp.sd[dim][i]) for i in 1:sdcnt[dim]])
|
||||
end
|
||||
while any(map(isavailable, sd[1])) && any(map(isavailable, sd[2]))
|
||||
penalize!(sd, tp)
|
||||
a = maximum([sd[1], sd[2]])
|
||||
b = sd[rem1(a.dim+1,2)][a.l]
|
||||
if a.dim == 2 # swap to make a supply and b demand
|
||||
a, b = b, a
|
||||
end
|
||||
expend = min(a.quant, b.quant)
|
||||
sol[a.i, b.i] = expend
|
||||
a.quant -= expend
|
||||
b.quant -= expend
|
||||
end
|
||||
return sol
|
||||
end
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
sup = [50, 60, 50, 50]
|
||||
slab = ["W", "X", "Y", "Z"]
|
||||
dem = [30, 20, 70, 30, 60]
|
||||
dlab = ["A", "B", "C", "D", "E"]
|
||||
c = [16 16 13 22 17;
|
||||
14 14 13 19 15;
|
||||
19 19 20 23 50;
|
||||
50 12 50 15 11]
|
||||
|
||||
tp = TProblem(sup, dem, c, slab, dlab)
|
||||
sol = vogel(tp)
|
||||
cost = sum(tp.toc .* sol)
|
||||
|
||||
println("The solution is:")
|
||||
print(" ")
|
||||
for s in tp.labels[2]
|
||||
print(@sprintf "%4s" s)
|
||||
end
|
||||
println()
|
||||
for i in 1:size(tp.toc)[1]
|
||||
print(@sprintf " %4s" tp.labels[1][i])
|
||||
for j in 1:size(tp.toc)[2]
|
||||
print(@sprintf "%4d" sol[i,j])
|
||||
end
|
||||
println()
|
||||
end
|
||||
println("The total cost is: ", cost)
|
||||
Loading…
Add table
Add a link
Reference in a new issue