Skip to content
Snippets Groups Projects
Commit 300d764f authored by Jayant Khatkar's avatar Jayant Khatkar
Browse files

add multi contour swap and swap validity check

parent e98e1304
Branches
No related merge requests found
......@@ -35,7 +35,7 @@ n_models = 10
max_iterations = 50
k = 150
i=7 # ith model
i=1 # ith model
obj = "M" * string(i)
# if obj in keys(results) || obj == "M4" || obj == "M9"
# if obj == "M4" || obj == "M9"
......@@ -48,7 +48,7 @@ f_name = Symbol("cost_" * obj)
println("Loading data...")
obj_add = add * obj
contours = clean_contour.(contour.(JSON.parse(open(obj_add * "contours.json"))))
cdata = contourdata(contours, 40, 10) # contour data
cdata = contourdata(contours, 40, 20) # contour data
try
cost_func = getfield(Main, f_name)
......@@ -73,10 +73,9 @@ plot(rl_g)
# 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 = greedish_rollout(cdata)
greedish_cost = cost_func(rl)
greedish_cost = cost_func(rl) # change to random
println("GREEDISH COST: " * string(greedish_cost))
update_result(results, obj, rl, greedish_cost, :greedish)
local_cost = local_search!(rl, max_iterations)
......
......@@ -551,7 +551,7 @@ function valid_swap(rollout::Vector{Int}, i::Int, j::Int, cdata::contourdata)
c_between = rollout[i+1:j-1]
for c in c_between
if c in c1_dependents || c in c2_dependson
if c c1_dependents || c c2_dependson
return false
end
end
......@@ -560,6 +560,24 @@ function valid_swap(rollout::Vector{Int}, i::Int, j::Int, cdata::contourdata)
end
function valid_swap(rollout::Vector{Int},
multirange::Tuple{Int, Int},
new_pos::Int,
cdata::contourdata)
s, e = multirange
c1s = rollout[s:e]
c2s = rollout[e+1:new_pos]
for c in c1s
c_dependents = outneighbors(cdata.G, c)
if any(c2s . c_dependents)
return false
end
end
return true
end
function check_validity(rollout::Vector{Int}, cdata::contourdata)
# make sure a given rollout is valid
done_contours = Set{Int}()
......@@ -583,6 +601,17 @@ function swap!(rollout::Vector{Int}, i::Int, j::Int)
end
function swap!(rollout::Vector{Int},
multirange::Tuple{Int, Int},
new_pos::Int
)
s, e = multirange
c1s = rollout[s:e]
c2s = rollout[e+1:new_pos]
rollout[s:new_pos] = [c2s;c1s]
end
function test_voxmap()
# create vox
vox = [0,0,0.5]
......@@ -718,6 +747,37 @@ function construct_best_neighbor(cdata::contourdata, cost_f::Function, k::Int=50
end
function construct_best_neighbor_multi(cdata::contourdata,
cost_f::Function,
k::Int=10,
multi_len::Int=5)
n_contours=length(cdata.contours)
bn_func = quote
function best_neighbor!(rl::Vector{Int}, current_cost::Number)
costs = Dict()
for i in 1:$n_contours-1
for j in i+$(multi_len+1):min(i+$(multi_len + k),$n_contours)
if valid_swap(rl, (i, i + $multi_len), j, $cdata)
swap!(rl, (i, i + $multi_len), j)
costs[i,j] = $cost_f(rl)
swap!(rl, (i, i + $multi_len), jblah) # TODO THIS DOESNT SWAP BACK
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, i + $multi_len), 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
......
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