From 65c75f39cdbdd7ccad08f72dc8e2face5d5d9052 Mon Sep 17 00:00:00 2001 From: harshangrjn Date: Fri, 22 Oct 2021 17:57:28 -0600 Subject: [PATCH] dropping experimental functions --- CHANGELOG.md | 7 ++- src/multi.jl | 172 ++++++++++++++++++++++++++------------------------- 2 files changed, 93 insertions(+), 86 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4ddeeb2..86457d4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,12 @@ ## v0.2.7 - Support for module shortcut `Alp` in function calls - Solver options clean up in `examples` folder -- Dropped support for unsued functions `_bound_sense`, `update_boundstop_options` +- Dropped support for unsued functions: + `_bound_sense` + `update_boundstop_options` + `amp_post_inequalities_int` + `amp_pick_ratevec` + `amp_collect_tight_regions` - Improved code coverage - Docs cleanup diff --git a/src/multi.jl b/src/multi.jl index 7b5cd561..65280f28 100644 --- a/src/multi.jl +++ b/src/multi.jl @@ -12,15 +12,15 @@ function amp_post_convhull(m::Optimizer; kwargs...) for k in keys(m.nonconvex_terms) nl_type = m.nonconvex_terms[k][:nonlinear_type] if ((nl_type == :MULTILINEAR) || (nl_type == :BILINEAR)) && (m.nonconvex_terms[k][:convexified] == false) - λ, α = amp_convexify_multilinear(m, k, λ, α, d) + λ, α = Alp.amp_convexify_multilinear(m, k, λ, α, d) elseif nl_type == :MONOMIAL && !m.nonconvex_terms[k][:convexified] - λ, α = amp_convexify_monomial(m, k, λ, α, d) + λ, α = Alp.amp_convexify_monomial(m, k, λ, α, d) elseif nl_type == :BINLIN && !m.nonconvex_terms[k][:convexified] - β = amp_convexify_binlin(m, k, β) + β = Alp.amp_convexify_binlin(m, k, β) elseif nl_type == :BINPROD && !m.nonconvex_terms[k][:convexified] - β = amp_convexify_binprod(m, k, β) + β = Alp.amp_convexify_binprod(m, k, β) elseif nl_type == :BININT && !m.nonconvex_terms[k][:convexified] - β = amp_convexify_binint(m, k, β) + β = Alp.amp_convexify_binint(m, k, β) elseif nl_type == :INTPROD && !m.nonconvex_terms[k][:convexified] error("Integer features are OFF. No support for INTPROD at this moment.") elseif nl_type == :INTLIN && !m.nonconvex_terms[k][:convexified] @@ -31,7 +31,7 @@ function amp_post_convhull(m::Optimizer; kwargs...) end # Experimental code for Warm starting - Alp.get_option(m, :convhull_warmstart) && !isempty(m.best_bound_sol) && amp_warmstart_α(m, α) + Alp.get_option(m, :convhull_warmstart) && !isempty(m.best_bound_sol) && Alp.amp_warmstart_α(m, α) return end @@ -40,11 +40,11 @@ function amp_convexify_multilinear(m::Optimizer, k::Any, λ::Dict, α::Dict, dis m.nonconvex_terms[k][:convexified] = true # Bookeeping the convexified terms - ml_indices, dim, extreme_point_cnt = amp_convhull_prepare(m, discretization, k) # convert key to easy read mode - λ = amp_convhull_λ(m, k, ml_indices, λ, extreme_point_cnt, dim) - λ = populate_convhull_extreme_values(m, discretization, ml_indices, λ, dim, ones(Int,length(dim))) - α = amp_convhull_α(m, ml_indices, α, dim, discretization) - amp_post_convhull_constrs(m, λ, α, ml_indices, dim, extreme_point_cnt, discretization) + ml_indices, dim, extreme_point_cnt = Alp.amp_convhull_prepare(m, discretization, k) # convert key to easy read mode + λ = Alp.amp_convhull_λ(m, k, ml_indices, λ, extreme_point_cnt, dim) + λ = Alp.populate_convhull_extreme_values(m, discretization, ml_indices, λ, dim, ones(Int,length(dim))) + α = Alp.amp_convhull_α(m, ml_indices, α, dim, discretization) + Alp.amp_post_convhull_constrs(m, λ, α, ml_indices, dim, extreme_point_cnt, discretization) return λ, α end @@ -53,11 +53,11 @@ function amp_convexify_monomial(m::Optimizer, k::Any, λ::Dict, α::Dict, discre m.nonconvex_terms[k][:convexified] = true # Bookeeping the convexified terms - monomial_index, dim, extreme_point_cnt = amp_convhull_prepare(m, discretization, k, monomial=true) - λ = amp_convhull_λ(m, k, monomial_index, λ, extreme_point_cnt, dim) - λ = populate_convhull_extreme_values(m, discretization, monomial_index, λ, 2) - α = amp_convhull_α(m, [monomial_index], α, dim, discretization) - amp_post_convhull_constrs(m, λ, α, monomial_index, dim, discretization) + monomial_index, dim, extreme_point_cnt = Alp.amp_convhull_prepare(m, discretization, k, monomial=true) + λ = Alp.amp_convhull_λ(m, k, monomial_index, λ, extreme_point_cnt, dim) + λ = Alp.populate_convhull_extreme_values(m, discretization, monomial_index, λ, 2) + α = Alp.amp_convhull_α(m, [monomial_index], α, dim, discretization) + Alp.amp_post_convhull_constrs(m, λ, α, monomial_index, dim, discretization) return λ, α end @@ -84,7 +84,7 @@ function amp_convexify_binlin(m::Optimizer, k::Any, β::Dict) bin_idx = bin_idx[1] cont_idx = cont_idx[1] - mccormick_binlin(m.model_mip, _index_to_variable_ref(m.model_mip, lift_idx), + Alp.mccormick_binlin(m.model_mip, _index_to_variable_ref(m.model_mip, lift_idx), _index_to_variable_ref(m.model_mip, bin_idx), _index_to_variable_ref(m.model_mip, cont_idx), m.l_var_tight[cont_idx], m.u_var_tight[cont_idx]) @@ -200,7 +200,7 @@ function populate_convhull_extreme_values(m::Optimizer, discretization::Dict, in else for i in 1:dim[level] locator[level] = i - λ = populate_convhull_extreme_values(m, discretization, indices, λ, dim, locator, level+1) + λ = Alp.populate_convhull_extreme_values(m, discretization, indices, λ, dim, locator, level+1) end end @@ -272,7 +272,7 @@ function amp_warmstart_α(m::Optimizer, α::Dict) if ws_idx > 0 # If a warm starter is found for v in m.bound_sol_pool[:vars] partition_cnt = length(d[v])-1 - active_j = get_active_partition_idx(d, m.bound_sol_pool[:sol][ws_idx][v], v) + active_j = Alp.get_active_partition_idx(d, m.bound_sol_pool[:sol][ws_idx][v], v) for j = 1:partition_cnt j == active_j ? set_start_value(α[v][j], 1.0) : set_start_value(α[v][j], 0.0) end @@ -298,7 +298,7 @@ function amp_post_convhull_constrs(m::Optimizer, λ::Dict, α::Dict, indices::An for (cnt, i) in enumerate(indices) l_cnt = length(d[i]) if m.var_type[i] == :Cont - amp_post_inequalities_cont(m, d, λ, α, indices, dim, i, cnt) # Add links between λ and α + Alp.amp_post_inequalities_cont(m, d, λ, α, indices, dim, i, cnt) # Add links between λ and α else error("EXCEPTION: unexpected variable type during integer related realxation") end @@ -366,12 +366,12 @@ function amp_post_inequalities_cont(m::Optimizer, discretization::Dict, λ::Dict YCnt = Int(ebd_map[:L]) @assert YCnt == length(α[var_ind]) for i in 1:YCnt - p_sliced_indices = collect_indices(λ[ml_indices][:indices], cnt, collect(ebd_map[i]), dim) - n_sliced_indices = collect_indices(λ[ml_indices][:indices], cnt, collect(ebd_map[i+YCnt]), dim) + p_sliced_indices = Alp.collect_indices(λ[ml_indices][:indices], cnt, collect(ebd_map[i]), dim) + n_sliced_indices = Alp.collect_indices(λ[ml_indices][:indices], cnt, collect(ebd_map[i+YCnt]), dim) JuMP.@constraint(m.model_mip, sum(λ[ml_indices][:vars][p_sliced_indices]) <= α[var_ind][i]) JuMP.@constraint(m.model_mip, sum(λ[ml_indices][:vars][n_sliced_indices]) <= 1-α[var_ind][i]) end - Alp.get_option(m, :convhull_ebd_link) && ebd_link_xα(m, α[var_ind], lambda_cnt, discretization[var_ind], ebd_map[:H_orig], var_ind) + Alp.get_option(m, :convhull_ebd_link) && Alp.ebd_link_xα(m, α[var_ind], lambda_cnt, discretization[var_ind], ebd_map[:H_orig], var_ind) return end @@ -405,6 +405,69 @@ function amp_post_inequalities_cont(m::Optimizer, discretization::Dict, λ::Dict return end +function amp_post_λ_upperbound(m::Optimizer, λ::Dict, indices::Any, dim::Tuple, d::Dict, tregions::Vector, reg=[], level=0) + + if level == length(indices) + isempty(tregions[level]) && return + sliced_indices = Set(collect_indices(λ[indices][:indices], 1, [reg[1]; reg[1]+1], dim)) + for i in 2:length(reg) + sliced_indices = intersect(sliced_indices, Set(collect_indices(λ[indices][:indices], i, [reg[i],reg[i]+1], dim))) + end + for i in sliced_indices + JuMP.set_upper_bound(λ[indices][:vars][i], (1/2)^level) + end + return + end + + for i in 1:length(tregions[level+1]) + push!(reg, tregions[level+1][i]) + Alp.amp_post_λ_upperbound(m, λ, indices, dim, d, tregions, reg, level+1) + length(reg) < level && error("Something is wrong") + length(reg) > level && pop!(reg) + end + + return +end + +function amp_post_λ_upperbound(m::Optimizer, λ::Dict, indices::Any, ub::Float64) + + for i in λ[indices][:vars] JuMP.set_upper_bound(i, ub) end + + return +end + +function collect_indices(l::Array, fixed_dim::Int, fixed_partition::Array, dim::Tuple) + + k = 0 + indices = Vector{Int}(undef, Int(prod(dim)/dim[fixed_dim]*length(fixed_partition))) + for i in 1:prod(dim) + ind = Tuple(CartesianIndices(l)[i]) + if ind[fixed_dim] in fixed_partition + k += 1 + indices[k] = i + end + end + + return indices +end + +#= +function collect_indices(l::Array, locator::Tuple, dim::Tuple) + + k = 0 + indices = Vector{Int}(2^length(dim)) + for i in 1:prod(dim) + ind = Tuple(CartesianIndices(l)[i]) + diff = [((ind[i] - locator[i] == 0) || (ind[i] - locator[i] == 1)) for i in 1:length(dim)] + if prod(diff) + k +=1 + indices[k] = i + end + end + + return indices +end + """ [Experimental Function] Method for multilinear terms with discrete variables @@ -486,65 +549,4 @@ function amp_collect_tight_regions(partvec::Vector) return tight_regions end - -function amp_post_λ_upperbound(m::Optimizer, λ::Dict, indices::Any, dim::Tuple, d::Dict, tregions::Vector, reg=[], level=0) - - if level == length(indices) - isempty(tregions[level]) && return - sliced_indices = Set(collect_indices(λ[indices][:indices], 1, [reg[1]; reg[1]+1], dim)) - for i in 2:length(reg) - sliced_indices = intersect(sliced_indices, Set(collect_indices(λ[indices][:indices], i, [reg[i],reg[i]+1], dim))) - end - for i in sliced_indices - JuMP.set_upper_bound(λ[indices][:vars][i], (1/2)^level) - end - return - end - - for i in 1:length(tregions[level+1]) - push!(reg, tregions[level+1][i]) - amp_post_λ_upperbound(m, λ, indices, dim, d, tregions, reg, level+1) - length(reg) < level && error("Something is wrong") - length(reg) > level && pop!(reg) - end - - return -end - -function amp_post_λ_upperbound(m::Optimizer, λ::Dict, indices::Any, ub::Float64) - - for i in λ[indices][:vars] JuMP.set_upper_bound(i, ub) end - - return -end - -function collect_indices(l::Array, fixed_dim::Int, fixed_partition::Array, dim::Tuple) - - k = 0 - indices = Vector{Int}(undef, Int(prod(dim)/dim[fixed_dim]*length(fixed_partition))) - for i in 1:prod(dim) - ind = Tuple(CartesianIndices(l)[i]) - if ind[fixed_dim] in fixed_partition - k += 1 - indices[k] = i - end - end - - return indices -end - -# function collect_indices(l::Array, locator::Tuple, dim::Tuple) - -# k = 0 -# indices = Vector{Int}(2^length(dim)) -# for i in 1:prod(dim) -# ind = Tuple(CartesianIndices(l)[i]) -# diff = [((ind[i] - locator[i] == 0) || (ind[i] - locator[i] == 1)) for i in 1:length(dim)] -# if prod(diff) -# k +=1 -# indices[k] = i -# end -# end - -# return indices -# end +=# \ No newline at end of file