dyna3/engine-proto/gram-test/circles-in-triangle.jl

76 lines
2.3 KiB
Julia
Raw Permalink Normal View History

include("Engine.jl")
using SparseArrays
2024-07-18 07:43:44 +00:00
using Random
# initialize the partial gram matrix for a sphere inscribed in a regular
# tetrahedron
J = Int64[]
K = Int64[]
values = BigFloat[]
2024-07-09 21:10:23 +00:00
for j in 1:9
for k in 1:9
filled = false
if j == 9
2024-07-18 08:48:05 +00:00
if k <= 5 && k != 2
2024-07-09 21:10:23 +00:00
push!(values, 0)
filled = true
end
elseif k == 9
2024-07-18 08:48:05 +00:00
if j <= 5 && j != 2
2024-07-09 21:10:23 +00:00
push!(values, 0)
filled = true
end
elseif j == k
push!(values, 1)
filled = true
2024-07-18 08:48:05 +00:00
elseif j == 1 || k == 1
push!(values, 0)
filled = true
2024-07-18 08:48:05 +00:00
elseif j == 2 || k == 2
push!(values, -1)
filled = true
end
if filled
push!(J, j)
push!(K, k)
end
end
end
append!(J, [6, 4, 6, 5, 7, 5, 7, 3, 8, 3, 8, 4])
append!(K, [4, 6, 5, 6, 5, 7, 3, 7, 3, 8, 4, 8])
append!(values, fill(-1, 12))
#= make construction rigid
append!(J, [3, 4, 4, 5])
append!(K, [4, 3, 5, 4])
append!(values, fill(-0.5, 4))
=#
gram = sparse(J, K, values)
# set initial guess
2024-07-18 07:43:44 +00:00
Random.seed!(58271)
guess = hcat(
Engine.plane(BigFloat[0, 0, 1], BigFloat(0)),
2024-07-18 07:43:44 +00:00
Engine.sphere(BigFloat[0, 0, 0], BigFloat(1//2)) + 0.1*Engine.rand_on_shell([BigFloat(-1)]),
Engine.plane(-BigFloat[1, 0, 0], BigFloat(-1)) + 0.1*Engine.rand_on_shell([BigFloat(-1)]),
Engine.plane(-BigFloat[cos(2pi/3), sin(2pi/3), 0], BigFloat(-1)) + 0.1*Engine.rand_on_shell([BigFloat(-1)]),
Engine.plane(-BigFloat[cos(-2pi/3), sin(-2pi/3), 0], BigFloat(-1)) + 0.1*Engine.rand_on_shell([BigFloat(-1)]),
Engine.sphere(BigFloat[-1, 0, 0], BigFloat(1//5)) + 0.1*Engine.rand_on_shell([BigFloat(-1)]),
Engine.sphere(BigFloat[cos(-pi/3), sin(-pi/3), 0], BigFloat(1//5)) + 0.1*Engine.rand_on_shell([BigFloat(-1)]),
Engine.sphere(BigFloat[cos(pi/3), sin(pi/3), 0], BigFloat(1//5)) + 0.1*Engine.rand_on_shell([BigFloat(-1)]),
BigFloat[0, 0, 0, 0, 1]
)
frozen = [CartesianIndex(j, 9) for j in 1:5]
2024-07-18 07:50:48 +00:00
# complete the gram matrix using Newton's method with backtracking
2024-07-16 05:11:54 +00:00
L, success, history = Engine.realize_gram(gram, guess, frozen)
completed_gram = L'*Engine.Q*L
println("Completed Gram matrix:\n")
display(completed_gram)
if success
println("\nTarget accuracy achieved!")
else
println("\nFailed to reach target accuracy")
end
println("Steps: ", size(history.scaled_loss, 1))
println("Loss: ", history.scaled_loss[end], "\n")