Commit 6f4c52dd authored by Jayant Khatkar's avatar Jayant Khatkar
Browse files

simplify interface

parent 6df6246d
include("utils.jl") include("utils.jl")
import Plots: histogram
### ABS material properties ### ABS material properties
α_ABS = 63e-6 α_ABS = 63e-6
...@@ -18,45 +19,26 @@ tdfast = tempdecay(215, 25, 0.08) ...@@ -18,45 +19,26 @@ tdfast = tempdecay(215, 25, 0.08)
add = "/Users/jayant/phd/tempaware/models/" add = "/Users/jayant/phd/tempaware/models/"
#stress_multiplier!(vd.voxels, 10)
#rl = random_rollout(cdata)
# single local search
#rl = random_rollout(cdata)
#cost_f(rl)
#@time local_search!(rl, max_iterations)
### MAIN LOOP ### MAIN LOOP
results = JSON.parse(open(add * "results.json")) results = JSON.parse(open(add * "results.json"))
n_local_searches = 10 n_local_searches = 100
n_models = 10 max_iterations = 250
max_iterations = 50
k = 150 k = 150
i=1 # ith model obj = "tempaware"
obj = "M" * string(i)
# if obj in keys(results) || obj == "M4" || obj == "M9"
# if obj == "M4" || obj == "M9"
# continue # skip is already done
# end
println("Starting " * obj)
# Load data # Load data
f_name = Symbol("cost_" * obj) f_name = Symbol("cost_" * obj)
println("Loading data...") println("Loading data...")
obj_add = add * obj obj_add = add * obj
contours = clean_contour.(contour.(JSON.parse(open(obj_add * "contours.json")))) contours = clean_contour.(contour.(JSON.parse(open(obj_add * "contours.json"))))
cdata = contourdata(contours, 40, 20) # contour data cdata = contourdata(contours, 40, 2) # contour data
@time vd = voxdata(obj_add * "_voxels.csv", cdata)
try println("Constructing Cost function...")
cost_func = getfield(Main, f_name) construct_cost(cdata, vd, ABS, td, f_name)
catch construct_cost_hist(cdata, vd, ABS, td)
vd = voxdata(obj_add * "_voxels.csv", cdata)
println("Constructing Cost function...")
construct_cost(cdata, vd, ABS, td, f_name)
end
cost_func = getfield(Main, f_name) cost_func = getfield(Main, f_name)
construct_best_neighbor(cdata, cost_func, k) construct_best_neighbor(cdata, cost_func, k)
...@@ -68,16 +50,17 @@ update_result(results, obj, rl_d, c, :default) ...@@ -68,16 +50,17 @@ update_result(results, obj, rl_d, c, :default)
# greedy rollout cost # greedy rollout cost
rl_g = greedy_rollout(cdata) rl_g = greedy_rollout(cdata)
c = cost_func(rl_g) c = cost_func(rl_g)
#update_result(results, obj, rl_g, c, :greedy) update_result(results, obj, rl_g, c, :greedy)
plot(rl_g) plot(rl_g)
# local search # local search
println("Doing local search " * string(n_local_searches) * " times") println("Doing local search " * string(n_local_searches) * " times")
Threads.@threads for i in 1:n_local_searches #Threads.@threads
rl = greedish_rollout(cdata) for i in 1:1#n_local_searches
greedish_cost = cost_func(rl) # change to random rl = greedy_rollout(cdata)
println("GREEDISH COST: " * string(greedish_cost)) random_cost = cost_func(rl) # change to random
update_result(results, obj, rl, greedish_cost, :greedish) println("RANDOM COST: " * string(random_cost))
update_result(results, obj, rl, random_cost, :random)
local_cost = local_search!(rl, max_iterations) local_cost = local_search!(rl, max_iterations)
println("LOCAL COST: " * string(local_cost)) println("LOCAL COST: " * string(local_cost))
update_result(results, obj, rl, local_cost, :local) update_result(results, obj, rl, local_cost, :local)
...@@ -91,10 +74,10 @@ data_stress = .√( vd.voxels.Sx.^2 + vd.voxels.Sy.^2 + vd.voxels.Sz.^2) ...@@ -91,10 +74,10 @@ data_stress = .√( vd.voxels.Sx.^2 + vd.voxels.Sy.^2 + vd.voxels.Sz.^2)
data_strain = .(vd.voxels.Txy.^2 + vd.voxels.Tyz.^2 + vd.voxels.Txz.^2) data_strain = .(vd.voxels.Txy.^2 + vd.voxels.Tyz.^2 + vd.voxels.Txz.^2)
data = data_stress data = data_stress
histogram(data) # vast majority of voxels near 0 stress - can ignore 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 histogram!(sort(data, rev=true)[1:1000]) # vast majority of voxels near 0 stress - can ignore
# voxmap sanity check # voxmap sanity check
plot(vd, 1500, cdata) plot(vd, 1000, cdata)
# rollouts # rollouts
rl_d = Vector(1:length(cdata.contours)) rl_d = Vector(1:length(cdata.contours))
...@@ -102,36 +85,32 @@ rl_r = random_rollout(cdata) ...@@ -102,36 +85,32 @@ rl_r = random_rollout(cdata)
rl_g = greedy_rollout(cdata) rl_g = greedy_rollout(cdata)
rl_l = Vector{Int}(results[obj]["best_rollout"]) rl_l = Vector{Int}(results[obj]["best_rollout"])
plot(rl_d) # plot rollout line graph plot(rl_l) # plot rollout line graph
plot(rl_d, cdata) # plot rollout 3d graph plot(rl_d, cdata) # plot rollout 3d graph
plot(rl_r, cdata) plot(rl_r, cdata)
plot(rl_g, cdata) plot(rl_g, cdata)
plot(rl_l, cdata) plot(rl_l, cdata)
# plot rollout animated # plot rollout animated
@time plot_animate(rl_g, cdata, rate=5) @time plot_animate(rl_l, cdata, rate=1)
# plot results # plot results
plot(results) plot(results)
# plot before and after histograms # plot before and after histograms
import Plots: histogram
i = 8
obj = "M" * string(i)
obj_add = add * obj
contours = clean_contour.(contour.(JSON.parse(open(obj_add * "contours.json"))))
cdata = contourdata(contours, 40, 20) # contour data
vd = voxdata(obj_add * "_voxels.csv", cdata)
construct_cost_hist(cdata, vd, ABS, td, :cost_hist)
histogram( histogram(
cost_hist(Vector(1:length(cdata.contours))), cost_hist(rl_d),
bins=50,
label="Default " * string(sum(cost_hist(rl_d))),
title=obj,
opacity=0.5)
histogram!(
cost_hist(rl_g),
bins=50, bins=50,
label="Default " * string(sum(cost_hist(Vector(1:length(cdata.contours))))), label="greedy " * string(sum(cost_hist(rl_g))),
opacity=0.5) opacity=0.5)
histogram!( histogram!(
cost_hist(Vector{Int}(results[obj]["best_rollout"])), cost_hist(rl_l),
bins=50, bins=50,
label="Opt " * string(sum(cost_hist(Vector{Int}(results[obj]["best_rollout"])))), label="local_search " * string(sum(cost_hist(rl_l))),
opacity=0.5) opacity=0.5)
\ No newline at end of file
...@@ -671,6 +671,7 @@ function construct_cost(cdata::contourdata, vd::voxdata, mat::material, td::temp ...@@ -671,6 +671,7 @@ function construct_cost(cdata::contourdata, vd::voxdata, mat::material, td::temp
relbelows = vd.below[valid_voxels] relbelows = vd.below[valid_voxels]
rel_voxels = vd.voxels[considered_voxels,:] rel_voxels = vd.voxels[considered_voxels,:]
relmaps = vd.maps relmaps = vd.maps
vox_area_scaling = min.(abs.(1 ./ rel_voxels.AreaRatio),1)
# voxtimes vectorize # voxtimes vectorize
max_contours_per_voxel = maximum([length(r.seglen) for r in relmaps]) max_contours_per_voxel = maximum([length(r.seglen) for r in relmaps])
...@@ -709,10 +710,10 @@ function construct_cost(cdata::contourdata, vd::voxdata, mat::material, td::temp ...@@ -709,10 +710,10 @@ function construct_cost(cdata::contourdata, vd::voxdata, mat::material, td::temp
σ12 = rel_v.Txy σ12 = rel_v.Txy
σ23 = rel_v.Tyz + $((cdata.layer_height/vd.width)*mat.E*mat.α)*ΔT σ23 = rel_v.Tyz + $((cdata.layer_height/vd.width)*mat.E*mat.α)*ΔT
σ31 = rel_v.Txy + $((cdata.layer_height/vd.width)*mat.E*mat.α)*ΔT σ31 = rel_v.Txy + $((cdata.layer_height/vd.width)*mat.E*mat.α)*ΔT
return sum($F * (σ11 - σ22).^2 + return sum($vox_area_scaling .* ($F * (σ11 - σ22).^2 +
$G * ((σ33 - σ11).^2 + (σ33 - σ22).^2) + $G * ((σ33 - σ11).^2 + (σ33 - σ22).^2) +
$(2 * L) * (σ12).^2 + $(2 * L) * (σ12).^2 +
$(2 * M) * (σ23 + σ31).^2) $(2 * M) * (σ23 + σ31).^2))
end end
end end
return eval(a) return eval(a)
...@@ -723,13 +724,19 @@ function construct_cost_hist(cdata::contourdata, vd::voxdata, mat::material, td: ...@@ -723,13 +724,19 @@ function construct_cost_hist(cdata::contourdata, vd::voxdata, mat::material, td:
contour_times = [cdata.contours[c].time[end] for c in 1:length(cdata.contours)] contour_times = [cdata.contours[c].time[end] for c in 1:length(cdata.contours)]
# considered voxels # considered voxels
println("n total voxels:")
println(length(vd.maps))
not_empty_voxels = length.([m.seglen for m in vd.maps]) .>0 not_empty_voxels = length.([m.seglen for m in vd.maps]) .>0
valid_voxels = (1:length(vd.below))[(vd.below.!=0) .& not_empty_voxels] valid_voxels = (1:length(vd.below))[(vd.below.!=0) .& not_empty_voxels]
valid_voxels = valid_voxels[not_empty_voxels[vd.below[valid_voxels]]] valid_voxels = valid_voxels[not_empty_voxels[vd.below[valid_voxels]]]
println("n empty voxels:")
println(sum(not_empty_voxels))
considered_voxels = valid_voxels considered_voxels = valid_voxels
relbelows = vd.below[valid_voxels] relbelows = vd.below[valid_voxels]
rel_voxels = vd.voxels[considered_voxels,:] rel_voxels = vd.voxels[considered_voxels,:]
println("n valid voxels:")
println(size(rel_voxels)[1])
relmaps = vd.maps relmaps = vd.maps
# voxtimes vectorize # voxtimes vectorize
...@@ -881,7 +888,7 @@ end ...@@ -881,7 +888,7 @@ end
function plot(rl::Vector{Int}, cdata::contourdata) function plot(rl::Vector{Int}, cdata::contourdata)
n = length(rl) n = length(rl)
cols = range(HSV(720,1,1), stop=HSV(-720,1,1), length=n) cols = range(HSV(180,1,1), stop=HSV(-180,1,1), length=n)
traces = Vector{GenericTrace}() traces = Vector{GenericTrace}()
layout = Layout( layout = Layout(
scene_aspect_ratio="data", scene_aspect_ratio="data",
......
...@@ -105,6 +105,7 @@ def check_data(mesh_file_name, csv_fname, from_inches=False): ...@@ -105,6 +105,7 @@ def check_data(mesh_file_name, csv_fname, from_inches=False):
tu.transform_to_z0(mesh) tu.transform_to_z0(mesh)
data = pd.read_csv(csv_fname, header=4) data = pd.read_csv(csv_fname, header=4)
locs = data.iloc[:,1:4].to_numpy() locs = data.iloc[:,1:4].to_numpy()
print(locs)
if from_inches: if from_inches:
locs = locs*25.4 locs = locs*25.4
locs_pv = pv.PolyData(locs) locs_pv = pv.PolyData(locs)
...@@ -194,19 +195,24 @@ def vox_section_area(voxels, mesh_file_name, vox_dim=3): ...@@ -194,19 +195,24 @@ def vox_section_area(voxels, mesh_file_name, vox_dim=3):
print("Fixed {} of those".format(fixed)) print("Fixed {} of those".format(fixed))
visualise_voxels(voxels, stresses=SecArea) visualise_voxels(voxels, stresses=SecArea)
return SecArea/(vox_dim**2)
if __name__ == '__main__': if __name__ == '__main__':
obj = 'models/M9' obj = 'models/tempaware'
#check_data(obj + '.stl', obj + '_raw.csv', from_inches=True) #check_data(obj + '.stl', obj + '_raw.csv', from_inches=False)
#out = voxelised_csv(obj + '.stl', obj + '_raw.csv', voxels=obj+'_vox_centers.csv', from_inches=True) #out = voxelised_csv(obj + '.stl', obj + '_raw.csv', voxels=obj+'_vox_centers.csv', from_inches=False)
#out.to_csv(obj + '_voxels.csv') #out.to_csv(obj + '_voxels.csv')
#visualise_voxels(obj + '_voxels.csv') #visualise_voxels(obj + '_voxels.csv')
voxels = pd.read_csv(obj + '_voxels.csv') #voxels = pd.read_csv(obj + '_voxels.csv')
vox_section_area(voxels, obj + '.stl') #area_ratio = vox_section_area(voxels, obj + '.stl')
#voxels["AreaRatio"] = area_ratio
#voxels.to_csv(obj + '_voxels.csv', index=False)
#contours = gc.decode_gcode(obj + '.gcode')
#outfile = open(obj + 'contours.json','w')
#json.dump([contour2dict(c) for c in contours], outfile) contours = gc.decode_gcode(obj + '.gcode')
#outfile.close() outfile = open(obj + 'contours.json','w')
json.dump([contour2dict(c) for c in contours], outfile)
outfile.close()
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