2017-06-22 14:12:35 +02:00
|
|
|
using JuMP
|
|
|
|
using SCS
|
|
|
|
|
2017-06-22 15:15:43 +02:00
|
|
|
export Settings, OrbitData
|
2017-06-22 14:12:35 +02:00
|
|
|
|
2017-11-13 10:57:02 +01:00
|
|
|
immutable Settings{T<:AbstractMathProgSolver}
|
2018-01-01 14:06:33 +01:00
|
|
|
name::String
|
|
|
|
N::Int
|
|
|
|
G::Group
|
|
|
|
S::Vector
|
|
|
|
autS::Group
|
|
|
|
radius::Int
|
|
|
|
solver::T
|
|
|
|
upper_bound::Float64
|
|
|
|
tol::Float64
|
|
|
|
warmstart::Bool
|
2018-01-02 03:17:44 +01:00
|
|
|
logger
|
2017-06-22 14:12:35 +02:00
|
|
|
end
|
|
|
|
|
2017-10-09 19:45:28 +02:00
|
|
|
prefix(s::Settings) = s.name
|
|
|
|
suffix(s::Settings) = "$(s.upper_bound)"
|
|
|
|
prepath(s::Settings) = prefix(s)
|
|
|
|
fullpath(s::Settings) = joinpath(prefix(s), suffix(s))
|
|
|
|
|
2017-11-08 09:58:37 +01:00
|
|
|
immutable OrbitData{T<:AbstractArray{Float64, 2}, LapType <:AbstractVector{Float64}}
|
2018-01-01 14:06:33 +01:00
|
|
|
name::String
|
|
|
|
Us::Vector{T}
|
|
|
|
Ps::Vector{Array{JuMP.Variable,2}}
|
|
|
|
cnstr::Vector{SparseMatrixCSC{Float64, Int}}
|
|
|
|
laplacian::LapType
|
|
|
|
laplacianSq::LapType
|
|
|
|
dims::Vector{Int}
|
2017-06-22 14:12:35 +02:00
|
|
|
end
|
|
|
|
|
2017-10-09 19:46:08 +02:00
|
|
|
function OrbitData(sett::Settings)
|
2018-01-02 02:55:53 +01:00
|
|
|
splap = load(filename(prepath(sett), :Δ), "Δ");
|
|
|
|
pm = load(filename(prepath(sett), :pm), "pm");
|
2018-01-01 14:06:33 +01:00
|
|
|
cnstr = PropertyT.constraints(pm);
|
|
|
|
splap² = similar(splap)
|
|
|
|
splap² = GroupRings.mul!(splap², splap, splap, pm);
|
2017-06-22 15:09:46 +02:00
|
|
|
|
2018-01-02 02:55:53 +01:00
|
|
|
Uπs = load(filename(prepath(sett), :Uπs), "Uπs")
|
2018-01-01 14:06:33 +01:00
|
|
|
nzros = [i for i in 1:length(Uπs) if size(Uπs[i],2) !=0]
|
|
|
|
Uπs = Uπs[nzros]
|
|
|
|
Uπs = sparsify!.(Uπs, sett.tol, check=true, verbose=true)
|
2017-11-13 10:57:57 +01:00
|
|
|
|
2018-01-01 14:06:33 +01:00
|
|
|
#dimensions of the corresponding πs:
|
2018-01-02 02:55:53 +01:00
|
|
|
dims = load(filename(prepath(sett), :Uπs), "dims")[nzros]
|
2017-06-22 15:09:46 +02:00
|
|
|
|
2018-01-01 14:06:33 +01:00
|
|
|
m, P = init_model(size(Uπs,1), [size(U,2) for U in Uπs]);
|
2017-06-22 15:09:46 +02:00
|
|
|
|
2018-01-02 02:55:53 +01:00
|
|
|
orbits = load(filename(prepath(sett), :orb), "orbits");
|
2018-01-01 14:06:33 +01:00
|
|
|
n = size(Uπs[1],1)
|
|
|
|
orb_spcnstrm = [orbit_constraint(cnstr[collect(orb)], n) for orb in orbits]
|
|
|
|
orb_splap = orbit_spvector(splap, orbits)
|
|
|
|
orb_splap² = orbit_spvector(splap², orbits)
|
2017-06-22 15:09:46 +02:00
|
|
|
|
2018-01-01 14:06:33 +01:00
|
|
|
orbData = OrbitData(fullpath(sett), Uπs, P, orb_spcnstrm, orb_splap, orb_splap², dims);
|
2017-06-22 15:09:46 +02:00
|
|
|
|
2018-01-01 14:06:33 +01:00
|
|
|
# orbData = OrbitData(name, Uπs, P, orb_spcnstrm, splap, splap², dims);
|
2017-06-22 15:09:46 +02:00
|
|
|
|
2018-01-01 14:06:33 +01:00
|
|
|
return m, orbData
|
2017-06-22 15:09:46 +02:00
|
|
|
end
|
|
|
|
|
2017-06-22 14:12:35 +02:00
|
|
|
include("OrbitDecomposition.jl")
|
|
|
|
|
2017-07-26 10:29:11 +02:00
|
|
|
dens(M::SparseMatrixCSC) = length(M.nzval)/length(M)
|
2017-08-27 18:57:27 +02:00
|
|
|
dens(M::AbstractArray) = length(findn(M)[1])/length(M)
|
2017-07-26 10:29:11 +02:00
|
|
|
|
2017-08-27 21:23:42 +02:00
|
|
|
function sparsify!{Tv,Ti}(M::SparseMatrixCSC{Tv,Ti}, eps=eps(Tv); verbose=false)
|
2018-01-01 14:06:33 +01:00
|
|
|
n = nnz(M)
|
|
|
|
|
|
|
|
densM = dens(M)
|
|
|
|
for i in eachindex(M.nzval)
|
|
|
|
if abs(M.nzval[i]) < eps
|
|
|
|
M.nzval[i] = zero(Tv)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
dropzeros!(M)
|
|
|
|
m = nnz(M)
|
|
|
|
|
|
|
|
if verbose
|
2018-01-02 03:32:24 +01:00
|
|
|
info("Sparsified density:", rpad(densM, 20), " → ", rpad(dens(M), 20))
|
2018-01-01 14:06:33 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|
2017-06-22 14:12:35 +02:00
|
|
|
end
|
|
|
|
|
2017-08-27 21:23:14 +02:00
|
|
|
function sparsify!{T}(M::AbstractArray{T}, eps=eps(T); check=false, verbose=false)
|
2018-01-01 14:06:33 +01:00
|
|
|
densM = dens(M)
|
|
|
|
rankM = rank(M)
|
|
|
|
M[abs.(M) .< eps] .= zero(T)
|
2017-08-27 18:58:01 +02:00
|
|
|
|
2018-01-01 14:06:33 +01:00
|
|
|
if check && rankM != rank(M)
|
2018-01-02 03:32:24 +01:00
|
|
|
warn("Sparsification decreased the rank!")
|
2018-01-01 14:06:33 +01:00
|
|
|
end
|
2017-08-27 18:58:01 +02:00
|
|
|
|
2018-01-01 14:06:33 +01:00
|
|
|
if verbose
|
2018-01-02 03:32:24 +01:00
|
|
|
info("Sparsified density:", rpad(densM, 20), " → ", rpad(dens(M),20))
|
2018-01-01 14:06:33 +01:00
|
|
|
end
|
2017-08-27 21:23:14 +02:00
|
|
|
|
2018-01-01 14:06:33 +01:00
|
|
|
return sparse(M)
|
2017-08-27 18:58:01 +02:00
|
|
|
end
|
|
|
|
|
2017-11-08 10:00:34 +01:00
|
|
|
sparsify{T}(U::AbstractArray{T}, tol=eps(T); check=true, verbose=false) = sparsify!(deepcopy(U), tol, check=check, verbose=verbose)
|
2017-08-27 18:58:01 +02:00
|
|
|
|
2017-07-26 12:15:31 +02:00
|
|
|
function transform(U::AbstractArray, V::AbstractArray; sparse=true)
|
2018-01-01 14:06:33 +01:00
|
|
|
if sparse
|
|
|
|
return sparsify!(U'*V*U)
|
|
|
|
else
|
|
|
|
return U'*V*U
|
|
|
|
end
|
2017-06-22 14:12:35 +02:00
|
|
|
end
|
|
|
|
|
2017-07-26 18:02:21 +02:00
|
|
|
A(data::OrbitData, π, t) = data.dims[π].*transform(data.Us[π], data.cnstr[t])
|
2017-06-22 14:12:35 +02:00
|
|
|
|
|
|
|
function constrLHS(m::JuMP.Model, data::OrbitData, t)
|
2018-01-01 14:06:33 +01:00
|
|
|
l = endof(data.Us)
|
|
|
|
lhs = @expression(m, sum(vecdot(A(data, π, t), data.Ps[π]) for π in 1:l))
|
|
|
|
return lhs
|
2017-06-22 14:12:35 +02:00
|
|
|
end
|
|
|
|
|
2017-08-27 18:59:16 +02:00
|
|
|
function constrLHS(m::JuMP.Model, cnstr, Us, Ust, dims, vars, eps=100*eps(1.0))
|
2018-01-01 14:06:33 +01:00
|
|
|
M = [PropertyT.sparsify!(dims[π].*Ust[π]*cnstr*Us[π], eps) for π in 1:endof(Us)]
|
|
|
|
return @expression(m, sum(vecdot(M[π], vars[π]) for π in 1:endof(Us)))
|
2017-07-26 18:02:21 +02:00
|
|
|
end
|
|
|
|
|
2017-08-27 19:00:49 +02:00
|
|
|
function addconstraints!(m::JuMP.Model, data::OrbitData, l::Int=length(data.laplacian); var::Symbol=:λ)
|
2018-01-01 14:06:33 +01:00
|
|
|
λ = m[var]
|
|
|
|
Ust = [U' for U in data.Us]
|
|
|
|
idx = [π for π in 1:endof(data.Us) if size(data.Us[π],2) != 0]
|
|
|
|
|
|
|
|
for t in 1:l
|
|
|
|
if t % 100 == 0
|
|
|
|
print(t, ", ")
|
|
|
|
end
|
|
|
|
# lhs = constrLHS(m, data, t)
|
|
|
|
lhs = constrLHS(m, data.cnstr[t], data.Us[idx], Ust[idx], data.dims[idx], data.Ps[idx])
|
|
|
|
|
|
|
|
d, d² = data.laplacian[t], data.laplacianSq[t]
|
|
|
|
# if lhs == zero(lhs)
|
|
|
|
# if d == 0 && d² == 0
|
|
|
|
# info("Detected empty constraint")
|
|
|
|
# continue
|
|
|
|
# else
|
|
|
|
# warn("Adding unsatisfiable constraint!")
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
JuMP.@constraint(m, lhs == d² - λ*d)
|
|
|
|
end
|
|
|
|
println("")
|
2017-06-22 14:12:35 +02:00
|
|
|
end
|
|
|
|
|
2017-11-08 10:02:38 +01:00
|
|
|
function init_model(n, sizes)
|
2018-01-01 14:06:33 +01:00
|
|
|
m = JuMP.Model();
|
|
|
|
P = Vector{Array{JuMP.Variable,2}}(n)
|
2017-11-05 20:55:53 +01:00
|
|
|
|
2018-01-01 14:06:33 +01:00
|
|
|
for (k,s) in enumerate(sizes)
|
|
|
|
P[k] = JuMP.@variable(m, [i=1:s, j=1:s])
|
|
|
|
JuMP.@SDconstraint(m, P[k] >= 0.0)
|
|
|
|
end
|
2017-11-05 20:55:53 +01:00
|
|
|
|
2018-01-01 14:06:33 +01:00
|
|
|
JuMP.@variable(m, λ >= 0.0)
|
|
|
|
JuMP.@objective(m, Max, λ)
|
|
|
|
return m, P
|
2017-06-22 15:11:14 +02:00
|
|
|
end
|
|
|
|
|
2017-10-09 19:46:08 +02:00
|
|
|
function create_SDP_problem(sett::Settings)
|
2018-01-02 03:17:44 +01:00
|
|
|
info(sett.logger, "Loading orbit data....")
|
|
|
|
@logtime sett.logger SDP_problem, orb_data = OrbitData(sett);
|
2017-06-22 14:12:35 +02:00
|
|
|
|
2018-01-01 14:06:33 +01:00
|
|
|
if sett.upper_bound < Inf
|
|
|
|
λ = JuMP.getvariable(SDP_problem, :λ)
|
|
|
|
JuMP.@constraint(SDP_problem, λ <= sett.upper_bound)
|
|
|
|
end
|
2017-06-22 14:12:35 +02:00
|
|
|
|
2018-01-01 14:06:33 +01:00
|
|
|
t = length(orb_data.laplacian)
|
2018-01-02 03:17:44 +01:00
|
|
|
info(sett.logger, "Adding $t constraints ... ")
|
|
|
|
@logtime sett.logger addconstraints!(SDP_problem, orb_data)
|
2017-06-22 14:12:35 +02:00
|
|
|
|
2018-01-01 14:06:33 +01:00
|
|
|
return SDP_problem, orb_data
|
2017-06-22 14:12:35 +02:00
|
|
|
end
|
|
|
|
|
2017-12-01 18:26:08 +01:00
|
|
|
function λandP(m::JuMP.Model, data::OrbitData, warmstart=true)
|
2018-01-01 14:06:33 +01:00
|
|
|
varλ = m[:λ]
|
|
|
|
varP = data.Ps
|
|
|
|
λ, Ps = PropertyT.λandP(data.name, m, varλ, varP, warmstart)
|
|
|
|
return λ, Ps
|
2017-06-22 14:12:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function λandP(m::JuMP.Model, data::OrbitData, sett::Settings)
|
2018-01-02 03:17:44 +01:00
|
|
|
info(sett.logger, "Solving SDP problem...")
|
2018-01-02 03:52:24 +01:00
|
|
|
@logtime sett.logger λ, Ps = λandP(m, data, sett.warmstart)
|
2017-06-22 14:12:35 +02:00
|
|
|
|
2018-01-02 03:17:44 +01:00
|
|
|
info(sett.logger, "Reconstructing P...")
|
2017-06-22 14:12:35 +02:00
|
|
|
|
2018-01-02 02:55:53 +01:00
|
|
|
preps = load_preps(filename(prepath(sett), :preps), sett.autS)
|
2017-08-27 19:06:17 +02:00
|
|
|
|
2018-01-02 03:17:44 +01:00
|
|
|
@logtime sett.logger recP = reconstruct_sol(preps, data.Us, Ps, data.dims)
|
2017-06-22 14:12:35 +02:00
|
|
|
|
2018-01-01 23:57:03 +01:00
|
|
|
fname = filename(fullpath(sett), :P)
|
2018-01-01 14:06:33 +01:00
|
|
|
save(fname, "origP", Ps, "P", recP)
|
|
|
|
return λ, recP
|
2017-06-22 14:12:35 +02:00
|
|
|
end
|
|
|
|
|
2017-11-08 09:33:00 +01:00
|
|
|
function load_preps(fname::String, G::Nemo.Group)
|
|
|
|
lded_preps = load(fname, "perms_d")
|
|
|
|
permG = PermutationGroup(length(first(lded_preps)))
|
|
|
|
@assert length(lded_preps) == order(G)
|
|
|
|
return Dict(k=>permG(v) for (k,v) in zip(elements(G), lded_preps))
|
|
|
|
end
|
|
|
|
|
|
|
|
function save_preps(fname::String, preps)
|
|
|
|
autS = parent(first(keys(preps)))
|
|
|
|
JLD.save(fname, "perms_d", [preps[elt].d for elt in elements(autS)])
|
|
|
|
end
|
|
|
|
|
2017-06-22 15:11:39 +02:00
|
|
|
function check_property_T(sett::Settings)
|
2017-06-22 14:12:35 +02:00
|
|
|
|
2018-01-02 03:19:57 +01:00
|
|
|
ex(s) = exists(filename(prepath(sett), s))
|
|
|
|
|
|
|
|
files_exists = ex.([:pm, :Δ, :Uπs, :orb, :preps])
|
|
|
|
|
|
|
|
if !all(files_exists)
|
2018-01-02 03:28:34 +01:00
|
|
|
compute_orbit_data(sett.logger, prepath(sett), sett.S, sett.autS, radius=sett.radius)
|
2018-01-02 03:19:57 +01:00
|
|
|
end
|
2018-01-01 14:06:33 +01:00
|
|
|
|
2018-01-01 23:57:03 +01:00
|
|
|
cond1 = exists(filename(fullpath(sett), :λ))
|
|
|
|
cond2 = exists(filename(fullpath(sett), :P))
|
|
|
|
|
|
|
|
if !sett.warmstart && cond1 && cond2
|
2018-01-25 21:39:06 +01:00
|
|
|
λ, P = λandP(fullpath(sett))
|
2018-01-01 14:06:33 +01:00
|
|
|
else
|
2018-01-02 03:17:44 +01:00
|
|
|
info(sett.logger, "Creating SDP problem...")
|
2018-01-01 14:06:33 +01:00
|
|
|
SDP_problem, orb_data = create_SDP_problem(sett)
|
|
|
|
JuMP.setsolver(SDP_problem, sett.solver)
|
2018-01-02 03:52:24 +01:00
|
|
|
info(sett.logger, Base.repr(SDP_problem))
|
2018-01-01 14:06:33 +01:00
|
|
|
|
|
|
|
λ, P = λandP(SDP_problem, orb_data, sett)
|
|
|
|
end
|
|
|
|
|
2018-01-02 03:17:44 +01:00
|
|
|
info(sett.logger, "λ = $λ")
|
|
|
|
info(sett.logger, "sum(P) = $(sum(P))")
|
|
|
|
info(sett.logger, "maximum(P) = $(maximum(P))")
|
|
|
|
info(sett.logger, "minimum(P) = $(minimum(P))")
|
2018-01-01 14:06:33 +01:00
|
|
|
|
2018-01-02 03:22:46 +01:00
|
|
|
isapprox(eigvals(P), abs.(eigvals(P)), atol=sett.tol) ||
|
2018-01-01 14:06:33 +01:00
|
|
|
warn("The solution matrix doesn't seem to be positive definite!")
|
|
|
|
|
2018-01-02 03:22:46 +01:00
|
|
|
if λ > 0
|
|
|
|
return check_λ(sett.name, sett.S, λ, P, sett.radius, sett.logger)
|
2018-01-01 14:06:33 +01:00
|
|
|
end
|
2018-01-02 03:17:44 +01:00
|
|
|
info(sett.logger, "κ($(sett.name), S) ≥ $λ < 0: Tells us nothing about property (T)")
|
2018-01-01 14:06:33 +01:00
|
|
|
return false
|
2017-06-22 14:12:35 +02:00
|
|
|
end
|