This is the farthest I have been able to push my code. It is still a little bit slow. Any recommendation, again, is very very much appreciated.
Apart from speed considerations, I would also appreciate if someone could point me on how to run this code for high K (bigger than 13) because I get an out of memory error.
using Distributed
using BenchmarkTools
using ProgressMeter
using BitOperations
using Profile
using Traceur
J = 50
T = 20
Π = 200*rand(J,T)
Dᵢ = rand([0],J,1)
feat1 = rand([1,2,3,4,5,6,7,8],J,1)
feat2 = rand([1,2,3,4,5,6,7,8,9,10],J,1)
Φ1 = 50
Φ2 = 50
Sj = 200*ones(J)
Π=Π.-Sj
K = 11
Ind =([1,7,9,10,12,J-15,J-10,J-6,J-5,J-2,J])
function myfun1(Ind,K,D::Array{Int64},Φ1,Φ2,feat1,feat2)
X1c = zeros(Int8,size(D,1))
X2c = zeros(Int8,size(D,1))
X1l = zeros(Int8,size(D,1))
X2l = zeros(Int8,size(D,1))
countc = zeros(Float64,size(D,1))
countl = zeros(Float64,size(D,1))
feat1=feat1[Ind]
feat2=feat2[Ind]
for j=1:K
for l=1:K
X1c[j] = X1c[j] + (feat1[j]==feat1[l])*D[l]
X1l[j] = X1l[j] + (feat2[j]==feat2[l])*D[l]
countc[j] = countc[j] + (feat1[j]==feat1[l])
countl[j] = countl[j] + (feat2[j]==feat2[l])
end
end
X1c = Φ1.*(X1c.>0)./countc
X1l = Φ2.*(X1l.>0)./countl
fcc = sum(X1c)
fcl = sum(X1l)
return fcc, fcl
end
function myfun6(Ind,Π,Φ1,Φ2,Sj,Dᵢ,feat1,feat2,K,J,T)
β = 0.95
Π = Π[Ind,:]
Sj = Sj[Ind]
D = zeros(Int64,2^K,K,T)
Indices = ones(Int64,2^K,T)
ind = ones(Int64,T+1)
v = zeros(Float64,2^K,T)
Dstar = zeros(Int64,K,T+1)
Dstar[:,1]=Dᵢ[Ind]
Πstar=0
ChoiceSet = zeros(Int64,2^K,K)
GroupCosts = zeros(Float64,2^K,2)
let int_s = fill(false,K) #pre-allocate output
for j=0:2^K-1
@. int_s = bget(j,0:K-1)
ChoiceSet[j+1,:]=int_s
(GroupCosts[j+1,1],GroupCosts[j+1,2])=myfun1(Ind,K,ChoiceSet[j+1,:],Φ1,Φ2,feat1,feat2)
end
end
for t=0:T-1
vc = zeros(Float64,2^K,2^K)
for i=1:2^K
for ii=1:2^K
if t>0
vc[i,ii] = sum(@view(ChoiceSet[ii,:]).*(@view(Π[:,T-t]) .+ Sj.*@view(ChoiceSet[i,:]))) .- (GroupCosts[ii,1] .+ GroupCosts[ii,2])+β*@view(v[ii,T-t+1])
else
vc[i,ii] = sum(@view(ChoiceSet[ii,:]).*(@view(Π[:,T-t]) .+ Sj.*@view(ChoiceSet[i,:]))) .- (GroupCosts[ii,1] .+ GroupCosts[ii,2])
end
end
#v[i,T-t] = maximum(@view(vc[i,:,T-t]))
tempind = argmax(@view(vc[i,:]))
Indices[i,T-t] = tempind
v[i,T-t]=vc[i,tempind]
D[i,:,T-t] = ChoiceSet[tempind,:]
end
end
for t=2:T+1
ind[t] = Indices[ind[t-1],t-1]
Dstar[:,t]=D[ind[t],:,t-1]
end
Dstar = Dstar[:,2:end]
return Dstar
end
@profile myfun6(Ind,Π,Φ1,Φ2,Sj,Dᵢ,feat1,feat2,K,J,T)
@trace(myfun6(Ind,Π,Φ1,Φ2,Sj,Dᵢ,feat1,feat2,K,J,T), modules=[Main])
f6 = @btime myfun6(Ind,Π,Φ1,Φ2,Sj,Dᵢ,feat1,feat2,K,J,T)
10.207 s (335611985 allocations: 25.64 GiB)