1
0
mirror of https://github.com/kalmarek/PropertyT.jl.git synced 2024-11-19 07:20:28 +01:00

parametrize Settings by {Symmetric, Naive}

This commit is contained in:
kalmarek 2018-09-09 13:21:33 +02:00
parent 23449ed3c0
commit 592f0bf7a5
2 changed files with 60 additions and 57 deletions

View File

@ -21,31 +21,36 @@ import MathProgBase.SolverInterface.AbstractMathProgSolver
struct Symmetrize end struct Symmetrize end
struct Naive end struct Naive end
mutable struct Settings{Gr<:Group, GEl<:GroupElem, Sol<:AbstractMathProgSolver} struct Settings{T} where T<:Union{Symmetrize, Naive}
name::String name::String
G::Gr
S::Vector{GEl} G::Group
S::Vector{GroupElem}
radius::Int radius::Int
solver::Sol solver::AbstractMathProgSolver
upper_bound::Float64 upper_bound::Float64
tol::Float64 tol::Float64
warmstart::Bool warmstart::Bool
autS::Group autS::Group
function Settings(name, G::Gr, S::Vector{GEl}, r::Int, function Settings(name::String,
sol::Sol, ub, tol, ws) where {Gr, GEl, Sol} G::Group, S::Vector{GEl}, r::Int,
return new{Gr, GEl, Sol}(name, G, S, r, sol, ub, tol, ws) sol::Sol, ub, tol, ws) where
{GEl<:GroupElem, Sol<:AbstractMathProgSolver}
return new{Naive}(name, G, S, r, sol, ub, tol, ws)
end end
function Settings(name, G::Gr, S::Vector{GEl}, r::Int, function Settings(name::String,
sol::Sol, ub, tol, ws, autS) where {Gr, GEl, Sol} G::Group, S::Vector{GEl}, r::Int,
return new{Gr, GEl, Sol}(name, G, S, r, sol, ub, tol, ws, autS) sol::Sol, ub, tol, ws, autS) where {Gr, GEl, Sol}
return new{Symmetrize}(name, G, S, r, sol, ub, tol, ws, autS)
end end
end end
prefix(s::Settings) = s.name prefix(s::Settings{Naive}) = s.name
prefix(s::Settings{Symmetrize}) = "o"*s.name
suffix(s::Settings) = "$(s.upper_bound)" suffix(s::Settings) = "$(s.upper_bound)"
prepath(s::Settings) = prefix(s) prepath(s::Settings) = prefix(s)
fullpath(s::Settings) = joinpath(prefix(s), suffix(s)) fullpath(s::Settings) = joinpath(prefix(s), suffix(s))
@ -72,57 +77,53 @@ filename(prefix, s::Symbol) = filename(prefix, Val{s})
end end
end end
for T in [:Naive, :Symmetrize] function check_property_T(sett::Settings{$T})
@eval begin
function check_property_T(::Type{$T}, sett::Settings)
if exists(filename(prepath(sett),:pm)) && if exists(filename(prepath(sett),:pm)) &&
exists(filename(prepath(sett),)) exists(filename(prepath(sett),))
# cached # cached
Δ = loadLaplacian(prepath(sett), parent(sett.S[1])) Δ = loadLaplacian(prepath(sett), parent(sett.S[1]))
else else
# compute # compute
Δ = computeLaplacian(sett.S, sett.radius) Δ = computeLaplacian(sett.S, sett.radius)
save(filename(prepath(sett), :pm), "pm", parent(Δ).pm) save(filename(prepath(sett), :pm), "pm", parent(Δ).pm)
save(filename(prepath(sett), ), "Δ", Δ.coeffs) save(filename(prepath(sett), ), "Δ", Δ.coeffs)
end end
files_exist = exists(filename(fullpath(sett), )) && files_exist = exists(filename(fullpath(sett), )) &&
exists(filename(fullpath(sett), :P)) exists(filename(fullpath(sett), :P))
if !sett.warmstart && files_exist if !sett.warmstart && files_exist
λ, P = loadλandP(fullpath(sett)) λ, P = loadλandP(fullpath(sett))
else else
warmfile = filename(fullpath(sett), :warm) warmfile = filename(fullpath(sett), :warm)
if sett.warmstart && exists(warmfile) if sett.warmstart && exists(warmfile)
ws = load(warmfile, "warmstart") ws = load(warmfile, "warmstart")
else else
ws = nothing ws = nothing
end end
λ, P, ws = computeλandP($T, Δ, sett, λ, P, ws = computeλandP(sett, Δ,
solverlog=filename(fullpath(sett), :solverlog)) solverlog=filename(fullpath(sett), :solverlog))
saveλandP(fullpath(sett), λ, P, ws) saveλandP(fullpath(sett), λ, P, ws)
if λ < 0 if λ < 0
warn("Solver did not produce a valid solution!") warn("Solver did not produce a valid solution!")
end
end
info("λ = ")
info("sum(P) = $(sum(P))")
info("maximum(P) = $(maximum(P))")
info("minimum(P) = $(minimum(P))")
isapprox(eigvals(P), abs.(eigvals(P)), atol=sett.tol) ||
warn("The solution matrix doesn't seem to be positive definite!")
@time Q = real(sqrtm((P+P')/2))
sgap = distance_to_cone(Δ, λ, Q, wlen=2*sett.radius)
return interpret_results(sett, sgap)
end end
end end
info("λ = ")
info("sum(P) = $(sum(P))")
info("maximum(P) = $(maximum(P))")
info("minimum(P) = $(minimum(P))")
isapprox(eigvals(P), abs.(eigvals(P)), atol=sett.tol) ||
warn("The solution matrix doesn't seem to be positive definite!")
@time Q = real(sqrtm((P+P')/2))
sgap = distance_to_cone(Δ, λ, Q, wlen=2*sett.radius)
return interpret_results(sett, sgap)
end end
Kazhdan(λ::Number, N::Integer) = sqrt(2*λ/N) Kazhdan(λ::Number, N::Integer) = sqrt(2*λ/N)

View File

@ -62,7 +62,8 @@ end
# #
############################################################################### ###############################################################################
function computeλandP(::Type{Naive}, Δ::GroupRingElem, sett::Settings, ws=nothing; solverlog=tempname()*".log") function computeλandP(sett::Settings{Naive},
Δ::GroupRingElem, ws=nothing; solverlog=tempname()*".log")
info("Creating SDP problem...") info("Creating SDP problem...")
SDP_problem, varλ, varP = SOS_problem(Δ^2, Δ, upper_bound=sett.upper_bound) SDP_problem, varλ, varP = SOS_problem(Δ^2, Δ, upper_bound=sett.upper_bound)
JuMP.setsolver(SDP_problem, sett.solver) JuMP.setsolver(SDP_problem, sett.solver)
@ -73,7 +74,8 @@ function computeλandP(::Type{Naive}, Δ::GroupRingElem, sett::Settings, ws=noth
return λ, P, ws return λ, P, ws
end end
function computeλandP(::Type{Symmetrize}, Δ::GroupRingElem, sett::Settings, ws=nothing; solverlog=tempname()*".log") function computeλandP(sett::Settings{Symmetrize},
Δ::GroupRingElem, ws=nothing; solverlog=tempname()*".log")
pdir = prepath(sett) pdir = prepath(sett)
files_exist = exists(filename(pdir,:Uπs)) && exists(filename(pdir,:orbits)) && exists(filename(pdir,:preps)) files_exist = exists(filename(pdir,:Uπs)) && exists(filename(pdir,:orbits)) && exists(filename(pdir,:preps))