Commit 42cbea0c authored by Jayant Khatkar's avatar Jayant Khatkar
Browse files

save results and run main loop over all models

parent b061b0d9
......@@ -17,91 +17,75 @@ tdfast = tempdecay(215, 25, 0.08)
# visualise_tempdecay(tdfast)
### LOAD IN DATA
obj = "/Users/jayant/phd/tempaware/models/" * "M5"
contours = clean_contour.(contour.(JSON.parse(open(obj * "contours.json"))))
cdata = contourdata(contours, 40, 5) # contour data
@time vd = voxdata(obj * "_voxels.csv", cdata)
add = "/Users/jayant/phd/tempaware/models/"
#stress_multiplier!(vd.voxels, 10)
rl = random_rollout(cdata)
#rl = random_rollout(cdata)
### CONSTRUCT COST FUNCTION GIVEN MODEL ADN MATERIAL
construct_cost(cdata, vd, ABS, td, :cost_f)
c = cost_f(rl)
# local search loop
k = 150 # 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: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)
costs[i,j] = cost_f(rl) # TODO cost function global vairable being used here
swap!(rl, i, j)
end
end
end
v, (i,j) = findmin(costs)
if abs(v - current_cost) < current_cost/1e6
return 0
elseif v < current_cost
swap!(rl, i, j)
return v
end
return 0
end
# single local search
#rl = random_rollout(cdata)
#cost_f(rl)
#@time local_search!(rl, max_iterations)
function local_search!(rl::Vector{Int}, max_iter::Int)
cost_val = Inf
for l in 1:max_iter
c = best_neighbor!(rl, cost_val)
if c 0
cost_val = c
else
break
end
### MAIN LOOP
results = JSON.parse(open(add * "results.json"))
n_local_searches = 5
n_models = 10
max_iterations = 20
k = 50
for i in 1:n_models
s = time()
obj = "M" * string(i)
if obj in keys(results) || obj == "M4" || obj == "M9"
continue # skip is already done
end
return cost_val
end
println("Starting " * obj)
# Load data
println("Loading data...")
obj_add = add * obj
contours = clean_contour.(contour.(JSON.parse(open(obj_add * "contours.json"))))
cdata = contourdata(contours, 40, 5) # contour data
vd = voxdata(obj_add * "_voxels.csv", cdata)
construct_cost(cdata, vd, ABS, td, :cost_f)
construct_best_neighbor(cdata, cost_f, k)
# single local search
rl = random_rollout(cdata)
cost_f(rl)
max_iterations = 50
@time local_search!(rl, max_iterations)
# run many times
n_starts=10
opt_costs = zeros(n_starts)
start_costs = zeros(n_starts)
threads = zeros(n_starts)
@time Threads.@threads for i in 1:n_starts
rl = random_rollout(cdata)
start_costs[i] = cost_f(rl)
opt_costs[i] = local_search!(rl, max_iterations)
threads[i] = Threads.threadid()
end
# default rollout cost
rl = Vector(1:length(cdata.contours))
c = cost_f(rl)
update_result(results, obj, rl, c, :default)
@time for i in 1:n_starts
my_costs[i] = local_search!(random_rollout(cdata), 5)
threads[i] = Threads.threadid()
# local search
println("Doing local search " * string(n_local_searches) * " times")
opt_costs = zeros(n_local_searches)
Threads.@threads for i in 1:n_local_searches
rl = random_rollout(cdata)
random_cost = cost_f(rl)
update_result(results, obj, rl, random_cost, :random)
local_cost = local_search!(rl, max_iterations)
update_result(results, obj, rl, local_cost, :local)
end
println("Completed in " * string(time()-s) * " seconds")
end
save_result(results, add * "results.json")
### Visualise distribution of stresses
### Visualisations
# distribution of stresses
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 = data_stress
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
### Visualise voxmap
# voxmap sanity check
plot(vd, 46, cdata)
# rollout order 3d
plot(Vector(1:length(rl)), cdata)
plot(rl, cdata)
# rollout order animated
rl = random_rollout(cdata)
rl = Vector(1:length(rl))
@time plot_animate(rl, cdata)
\ No newline at end of file
......@@ -576,6 +576,48 @@ function construct_cost(cdata::contourdata, vd::voxdata, mat::material, td::temp
end
function construct_best_neighbor(cdata::contourdata, cost_f::Function, k::Int=50)
n_contours=length(cdata.contours)
bn_func = quote
function best_neighbor!(rl::Vector{Int}, current_cost::Number)
costs = Dict()
for i in 1:1:$n_contours-1
for j in i+1:min(i+$k,$n_contours)
if valid_swap(rl, i, j, $cdata)
swap!(rl, i, j)
costs[i,j] = $cost_f(rl)
swap!(rl, i, j)
end
end
end
v, (i,j) = findmin(costs)
if abs(v - current_cost) < current_cost/1e6
return 0
elseif v < current_cost
swap!(rl, i, j)
return v
end
return 0
end
end
return eval(bn_func)
end
function local_search!(rl::Vector{Int}, max_iter::Int)
cost_val = Inf
for l in 1:max_iter
c = best_neighbor!(rl, cost_val)
if c 0
cost_val = c
else
break
end
end
return cost_val
end
function plot(vd::voxdata, i::Int, cdata::contourdata)
vm = vd.maps[i]
loc = Array(vd.voxels[i, ["x", "y", "z"]])
......@@ -626,11 +668,12 @@ function plot(rl::Vector{Int}, cdata::contourdata)
end
function plot_animate(rl::Vector{Int}, cdata::contourdata)
function plot_animate(rl::Vector{Int}, cdata::contourdata; cam=(45,45), rate=1)
i=1
p=Plots.plot3d(cdata.contours[rl[i]].pos[:,1],
cdata.contours[rl[i]].pos[:,2],
cdata.contours[rl[i]].pos[:,3]
cdata.contours[rl[i]].pos[:,3],
camera=cam
)
return @gif for i in 2:length(rl)
......@@ -638,7 +681,45 @@ function plot_animate(rl::Vector{Int}, cdata::contourdata)
cdata.contours[rl[i]].pos[:,1],
cdata.contours[rl[i]].pos[:,2],
cdata.contours[rl[i]].pos[:,3],
legend=false
legend=false,
camera=cam
)
end every rate
end
function update_result(
results::Dict,
obj::String,
rl::Vector{Int},
cost::Number,
type::Symbol
)
if obj keys(results)
results[obj] = Dict(
"best_rollout" => Vector{Number}(),
"cost_default"=>Inf,
"cost_random"=>Inf,
"cost_local"=>Inf
# TODO: also save parameters, save all costs
)
end
if type==:default && results[obj]["cost_default"]>cost
results[obj]["cost_default"] = cost
elseif type==:random && results[obj]["cost_random"]>cost
results[obj]["cost_random"] = cost
elseif type==:local && results[obj]["cost_local"]>cost
results[obj]["cost_local"] = cost
results[obj]["best_rollout"] = rl
end
end
function save_result(results::Dict, fname::String)
stringdata = JSON.json(results)
open(fname, "w") do f
write(f, stringdata)
end
end
\ 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