Commit 1b7d5fec authored by Jayant Khatkar's avatar Jayant Khatkar
Browse files

10x faster cost func, dated code removed

parent 3c05268b
......@@ -33,7 +33,6 @@ end
struct voxmap
seglen::Vector{Float64}
segoffset::Vector{Float64}
segcontours::Vector{Int}
c::Number
end
......@@ -172,7 +171,7 @@ function voxmap(vox::Vector{Float64}, vox_d::Number, cdata::contourdata)
t_start = 0
if l > length(cdata.layers)
return voxmap(seglen, segoffset, segcontours, 0)
return voxmap(seglen, segcontours, 0)
end
for cid in cdata.layers[l]
......@@ -330,7 +329,16 @@ function voxmap(vox::Vector{Float64}, vox_d::Number, cdata::contourdata)
# for those contours find exact segments
end
return voxmap(seglen./sum(seglen), segoffset, segcontours, Float64(segoffset seglen)/sum(seglen))
c = Float64(segoffset seglen)/sum(seglen) # constant used for cost calc
new_seglen = Vector{Float64}()
new_segcontours = Vector{Int64}()
for i in 1:length(segcontours)
if !(segcontours[i] in new_segcontours)
push!(new_segcontours, segcontours[i])
push!(new_seglen, sum(seglen[segcontours.==segcontours[i]]))
end
end
return voxmap(new_seglen./sum(new_seglen), new_segcontours, c)
end
......@@ -471,11 +479,6 @@ function test_voxmap()
end
function rollout2time(rollout::Vector{Int}, cdata::contourdata)
# start time of each contour, assuming no travel time
return cumsum([cdata.contours[c].time[end] for c in rollout])
end
# Temperature function
T0 = 215 # extrusion temp
Tc = 25 # room temp
......@@ -487,42 +490,6 @@ Temp(t::Number) = Tc + (T0-Tc)*ℯ^(-k*t)
#y = Temp.(x)
#plot(x,y)
function calc_cost(rollout::Vector{Int}, cdata::contourdata, vd::voxdata, mat::material)
# go from rollout to timestart for each contour
timestart = rollout2time(rollout, cdata)
# calculate time at each voxel
voxtimes = [sum(v.seglen.*(v.segoffset + timestart[v.segcontours]))/sum(v.seglen) for v in vd.maps]
# calculate temp difference from voxel below it
considered_voxels = (1:length(vd.below))[(vd.below.!=0) .& (.!isnan.(voxtimes))] # cannot calculate cost is no voxel underneath
considered_voxels = considered_voxels[.!isnan.(voxtimes[vd.below[considered_voxels]])]
Δt = voxtimes[considered_voxels] - voxtimes[vd.below[considered_voxels]]
ΔT = Tcutoff .- Temp.(Δt)
replace!(x-> x<0 ? 0 : x, ΔT)
# calculate stresses at each voxel
F = (2/mat.σ̄[1,1]^2 - 1/mat.σ̄[3,3]^2)/2
G = 1/(2*mat.σ̄[3,3]^2)
L = 1/(2*mat.σ̄[1,2]^2)
M = 1/(2*mat.σ̄[1,3]^2)
rel_voxels = vd.voxels[considered_voxels,:]
# calculate cost func at each voxel
σ11 = rel_voxels.Sx + mat.E*mat.α*ΔT
σ22 = rel_voxels.Sy + mat.E*mat.α*ΔT
σ33 = rel_voxels.Sz
σ12 = rel_voxels.Txy
σ23 = rel_voxels.Tyz + (cdata.layer_height/vd.width)*mat.E*mat.α*ΔT
σ31 = rel_voxels.Txy + (cdata.layer_height/vd.width)*mat.E*mat.α*ΔT
cost = F * (σ11 - σ22).^2 +
G * ((σ33 - σ11).^2 + (σ33 - σ22).^2) +
2 * L * (σ12).^2 +
2 * M * (σ23 + σ31).^2 .- 1.0
return sum(cost)
end
function clean_contour(c::contour)
# remove first element of array if second element is the same
......@@ -562,7 +529,6 @@ contours = clean_contour.(contour.(JSON.parse(open(obj * "contours.json"))))
cdata = contourdata(contours, 5, 5) # contour data
@time vd = voxdata(obj * "_voxels.csv", cdata)
rl = random_rollout(cdata)
@time calc_cost(rl, cdata, vd, ABS) # 500/second
#####################################
###### CONSTRUCT COST FUNCTION
......@@ -588,6 +554,18 @@ relbelows = vd.below[valid_voxels]
rel_voxels = vd.voxels[considered_voxels,:]
relmaps = vd.maps
# voxtimes vectorize
max_contours_per_voxel = maximum([length(r.seglen) for r in relmaps])
vox_contour_id = ones(Int64, length(relmaps), max_contours_per_voxel)
for i in 1:length(relmaps)
vox_contour_id[i, 1:length(relmaps[i].segcontours)] = relmaps[i].segcontours
end
vox_c = [Float64(v.c) for v in relmaps]
vox_seglen = zeros(length(relmaps), max_contours_per_voxel)
for i in 1:length(relmaps)
vox_seglen[i, 1:length(relmaps[i].seglen)] = relmaps[i].seglen
end
# calculate stresses at each voxel
F = (2/mat.σ̄[1,1]^2 - 1/mat.σ̄[3,3]^2)/2
G = 1/(2*mat.σ̄[3,3]^2)
......@@ -596,19 +574,19 @@ M = 1/(2*mat.σ̄[1,3]^2)
a = quote
function cost_f(rl::Vector{Int})
# println("Cumulative Sum") # 100k
# println("Cumulative Sum") # 800k
timestart = cumsum([$contour_times[c] for c in rl])
#println("Vox times") # 600
voxtimes = [v.seglen timestart[v.segcontours] + v.c for v in relmaps]
#println("Vox times") # 18k
voxtimes = sum(vox_seglen .* timestart[vox_contour_id], dims=2) .+ vox_c
# println("time diff") # 28k
# println("time diff") # 100k
Δt = voxtimes[considered_voxels] - voxtimes[relbelows]
#println("Temp diff") # 6k
ΔT = Tcutoff .- (Tc .+ $(T0-Tc).*.^(-k.*Δt))
#println("Temp diff") # 30k
ΔT = Tcutoff .- ($Tc .+ $(T0-Tc).*.^(-k.*Δt))
# println("Temp diff clean") # 82k
# println("Temp diff clean") # 500k
replace!(x-> x<0 ? 0 : x, ΔT)
# println("Stresses") # 6k
......@@ -632,7 +610,7 @@ cost_f(rl)
k = 5 # TODO k is hardcoded in function below (uses global variable)
function best_neighbor!(rl::Vector{Int}, current_cost::Number)
costs = Dict()
for i in 1:length(rl)-1
for i in 1:1:length(rl)-1
for j in i+1:min(i+k,length(rl)) # TODO length rl is constant
if valid_swap(rl, i, j, cdata)
swap!(rl, i, j)
......@@ -675,16 +653,16 @@ threads = zeros(n_starts)
@time Threads.@threads for i in 1:n_starts
my_costs[i] = local_search!(random_rollout(cdata), 5)
threads[i] = Threads.threadid()
end
end # 12 seconds
@time for i in 1:n_starts
my_costs[i] = local_search!(random_rollout(cdata), 5)
threads[i] = Threads.threadid()
end
end # 22 seconds
### Visualise distribution of stresses
data = .( vd.voxels.Sx.^2 + vd.voxels.Sy.^2 + vd.voxels.Sz.^2)
data = .(vd.voxels.Txy.^2 + vd.voxels.Tyz.^2 + vd.voxels.Txz.^2)
histogram(data) # vast majority of voxels near 0 stress - can ignore
histogram!(sort(data, rev=true)[1:4000]) # vast majority of voxels near 0 stress - can ignore
\ No newline at end of file
#data = .√( vd.voxels.Sx.^2 + vd.voxels.Sy.^2 + vd.voxels.Sz.^2)
#data = .√(vd.voxels.Txy.^2 + vd.voxels.Tyz.^2 + vd.voxels.Txz.^2)
#histogram(data) # vast majority of voxels near 0 stress - can ignore
#histogram!(sort(data, rev=true)[1:4000]) # vast majority of voxels near 0 stress - can ignore
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment