Vectornaut
b92be312e8
This PR adds code for a Julia-language prototype of a configuration solver, in the `engine-proto` folder. It uses Julia version 1.10.0. ### Approaches Development of this PR tried two broad approaches to the constraint geometry problem. Each one suggested various solution techniques. The Gram matrix approach, with the low-rank factorization technique, seems the most promising. - **Algebraic** *(In the `alg-test` subfolder).* Write the constraints as polynomials in the inversive coordinates of the elements, and use computational algebraic geometry techniques to solve the resulting system. We tried the following techniques. - **Gröbner bases** *(`Engine.Algebraic.jl`).* Symbolic. Find a Gröbner basis for the ideal generated by the constraint equations. Information about the solution variety, like its codimension, is then relatively easy to extract. - **Homotopy continuation** *(`Engine.Numerical.jl`).* Numerical. Cut the solution set along a random hyperplane to get a generic zero-dimensional slice, and then use a fancy homotopy technique to approximate the points in that slice. A few notes about our experiences can be found on the [engine prototype](wiki/Engine-prototype) wiki page. - **Gram matrix** *(in the `gram-test` subfolder).* A construction is described completely, up to conformal transformations, by the Gram matrix of the vectors representing its elements. Express the constraints as fixed entries of the Gram matrix, and use numerical linear algebra techniques to find a list of vectors whose Gram matrix fits the bill. We tried the following techniques. - **LDL decomposition** *(`gram-test.sage`, `gram-test.jl`, `overlap-test.jl`).* Find a cluster of up to five elements whose Gram matrix is completely filled in by the constraints. Use LDL decomposition to find a list of vectors with that Gram matrix. This technique can be made algebraic, as seen in `overlap-test.jl`. - **Low-rank factorization** *(source files listed in findings section).* Write down a quadratic loss function that says how far a set of vectors is from meeting the Gram matrix constraints. Use a smooth optimization technique like Newton's method or gradient descent to find a zero of the loss function. In addition to the polished prototype described in the results section, we have an early prototype using an off-the-shelf factorization package (`low-rank-test.jl`) and an visualization of the loss function landscape near global minima (`basin-shapes.jl`). The [Gram matrix parameterization](wiki/Gram-matrix-parameterization) wiki page contains detailed notes on this approach. ### Findings With the algebraic approach, we hit a performance wall pretty quickly as our constructions grew. It was often hard to find real solutions of the polynomial system, since the techniques we use work most naturally in the complex world. With the Gram matrix approach, on the other hand, we could solve interesting problems in acceptably short times using the low-rank factorization technique. We put the optimization routine in its own module (`Engine.jl`) and used it to solve five example problems: - `overlapping-pyramids.jl` - `circles-in-triangle.jl` - `sphere-in-tetrahedron.jl` - `tetrahedron-radius-ratio.jl` - `irisawa-hexlet.jl` We plan to use low-rank factorization of the Gram matrix in our first app prototype. ### Visualizations We used the visualizer in the `ganja-test` folder to visually check our low-rank factorization results. The visualizer runs [Ganja.js](https://enkimute.github.io/ganja.js/) in an Electron app, made with [Blink](https://github.com/JuliaGizmos/Blink.jl). Although Ganja.js makes beautiful pictures under most circumstances, we found two obstacles to using it in production. - It seems to have precision problems with low-curvature spheres. - We couldn't figure out how to customize its clipping and transparency settings, and the default settings often obscure construction details. Co-authored-by: Aaron Fenyes <aaron.fenyes@fareycircles.ooo> Co-authored-by: Glen Whitney <glen@studioinfinity.org> Reviewed-on: #13 Co-authored-by: Vectornaut <vectornaut@nobody@nowhere.net> Co-committed-by: Vectornaut <vectornaut@nobody@nowhere.net>
203 lines
5.9 KiB
Julia
203 lines
5.9 KiB
Julia
module Algebraic
|
|
|
|
export
|
|
codimension, dimension,
|
|
Construction, realize,
|
|
Element, Point, Sphere,
|
|
Relation, LiesOn, AlignsWithBy, mprod
|
|
|
|
import Subscripts
|
|
using LinearAlgebra
|
|
using AbstractAlgebra
|
|
using Groebner
|
|
using ...HittingSet
|
|
|
|
# --- commutative algebra ---
|
|
|
|
# as of version 0.36.6, AbstractAlgebra only supports ideals in multivariate
|
|
# polynomial rings when the coefficients are integers. we use Groebner to extend
|
|
# support to rationals and to finite fields of prime order
|
|
Generic.reduce_gens(I::Generic.Ideal{U}) where {T <: FieldElement, U <: MPolyRingElem{T}} =
|
|
Generic.Ideal{U}(base_ring(I), groebner(gens(I)))
|
|
|
|
function codimension(I::Generic.Ideal{U}, maxdepth = Inf) where {T <: RingElement, U <: MPolyRingElem{T}}
|
|
leading = [exponent_vector(f, 1) for f in gens(I)]
|
|
targets = [Set(findall(.!iszero.(exp_vec))) for exp_vec in leading]
|
|
length(HittingSet.solve(HittingSetProblem(targets), maxdepth))
|
|
end
|
|
|
|
dimension(I::Generic.Ideal{U}, maxdepth = Inf) where {T <: RingElement, U <: MPolyRingElem{T}} =
|
|
length(gens(base_ring(I))) - codimension(I, maxdepth)
|
|
|
|
# --- primitve elements ---
|
|
|
|
abstract type Element{T} end
|
|
|
|
mutable struct Point{T} <: Element{T}
|
|
coords::Vector{MPolyRingElem{T}}
|
|
vec::Union{Vector{MPolyRingElem{T}}, Nothing}
|
|
rel::Nothing
|
|
|
|
## [to do] constructor argument never needed?
|
|
Point{T}(
|
|
coords::Vector{MPolyRingElem{T}} = MPolyRingElem{T}[],
|
|
vec::Union{Vector{MPolyRingElem{T}}, Nothing} = nothing
|
|
) where T = new(coords, vec, nothing)
|
|
end
|
|
|
|
function buildvec!(pt::Point)
|
|
coordring = parent(pt.coords[1])
|
|
pt.vec = [one(coordring), dot(pt.coords, pt.coords), pt.coords...]
|
|
end
|
|
|
|
mutable struct Sphere{T} <: Element{T}
|
|
coords::Vector{MPolyRingElem{T}}
|
|
vec::Union{Vector{MPolyRingElem{T}}, Nothing}
|
|
rel::Union{MPolyRingElem{T}, Nothing}
|
|
|
|
## [to do] constructor argument never needed?
|
|
Sphere{T}(
|
|
coords::Vector{MPolyRingElem{T}} = MPolyRingElem{T}[],
|
|
vec::Union{Vector{MPolyRingElem{T}}, Nothing} = nothing,
|
|
rel::Union{MPolyRingElem{T}, Nothing} = nothing
|
|
) where T = new(coords, vec, rel)
|
|
end
|
|
|
|
function buildvec!(sph::Sphere)
|
|
coordring = parent(sph.coords[1])
|
|
sph.vec = sph.coords
|
|
sph.rel = mprod(sph.coords, sph.coords) + one(coordring)
|
|
end
|
|
|
|
const coordnames = IdDict{Symbol, Vector{Union{Symbol, Nothing}}}(
|
|
nameof(Point) => [nothing, nothing, :xₚ, :yₚ, :zₚ],
|
|
nameof(Sphere) => [:rₛ, :sₛ, :xₛ, :yₛ, :zₛ]
|
|
)
|
|
|
|
coordname(elt::Element, index) = coordnames[nameof(typeof(elt))][index]
|
|
|
|
function pushcoordname!(coordnamelist, indexed_elt::Tuple{Any, Element}, coordindex)
|
|
eltindex, elt = indexed_elt
|
|
name = coordname(elt, coordindex)
|
|
if !isnothing(name)
|
|
subscript = Subscripts.sub(string(eltindex))
|
|
push!(coordnamelist, Symbol(name, subscript))
|
|
end
|
|
end
|
|
|
|
function takecoord!(coordlist, indexed_elt::Tuple{Any, Element}, coordindex)
|
|
elt = indexed_elt[2]
|
|
if !isnothing(coordname(elt, coordindex))
|
|
push!(elt.coords, popfirst!(coordlist))
|
|
end
|
|
end
|
|
|
|
# --- primitive relations ---
|
|
|
|
abstract type Relation{T} end
|
|
|
|
mprod(v, w) = (v[1]*w[2] + w[1]*v[2]) / 2 - dot(v[3:end], w[3:end])
|
|
|
|
# elements: point, sphere
|
|
struct LiesOn{T} <: Relation{T}
|
|
elements::Vector{Element{T}}
|
|
|
|
LiesOn{T}(pt::Point{T}, sph::Sphere{T}) where T = new{T}([pt, sph])
|
|
end
|
|
|
|
equation(rel::LiesOn) = mprod(rel.elements[1].vec, rel.elements[2].vec)
|
|
|
|
# elements: sphere, sphere
|
|
struct AlignsWithBy{T} <: Relation{T}
|
|
elements::Vector{Element{T}}
|
|
cos_angle::T
|
|
|
|
AlignsWithBy{T}(sph1::Sphere{T}, sph2::Sphere{T}, cos_angle::T) where T = new{T}([sph1, sph2], cos_angle)
|
|
end
|
|
|
|
equation(rel::AlignsWithBy) = mprod(rel.elements[1].vec, rel.elements[2].vec) - rel.cos_angle
|
|
|
|
# --- constructions ---
|
|
|
|
mutable struct Construction{T}
|
|
points::Vector{Point{T}}
|
|
spheres::Vector{Sphere{T}}
|
|
relations::Vector{Relation{T}}
|
|
|
|
function Construction{T}(; elements = Vector{Element{T}}(), relations = Vector{Relation{T}}()) where T
|
|
allelements = union(elements, (rel.elements for rel in relations)...)
|
|
new{T}(
|
|
filter(elt -> isa(elt, Point), allelements),
|
|
filter(elt -> isa(elt, Sphere), allelements),
|
|
relations
|
|
)
|
|
end
|
|
end
|
|
|
|
function Base.push!(ctx::Construction{T}, elt::Point{T}) where T
|
|
push!(ctx.points, elt)
|
|
end
|
|
|
|
function Base.push!(ctx::Construction{T}, elt::Sphere{T}) where T
|
|
push!(ctx.spheres, elt)
|
|
end
|
|
|
|
function Base.push!(ctx::Construction{T}, rel::Relation{T}) where T
|
|
push!(ctx.relations, rel)
|
|
for elt in rel.elements
|
|
push!(ctx, elt)
|
|
end
|
|
end
|
|
|
|
function realize(ctx::Construction{T}) where T
|
|
# collect coordinate names
|
|
coordnamelist = Symbol[]
|
|
eltenum = enumerate(Iterators.flatten((ctx.spheres, ctx.points)))
|
|
for coordindex in 1:5
|
|
for indexed_elt in eltenum
|
|
pushcoordname!(coordnamelist, indexed_elt, coordindex)
|
|
end
|
|
end
|
|
|
|
# construct coordinate ring
|
|
coordring, coordqueue = polynomial_ring(parent_type(T)(), coordnamelist, ordering = :degrevlex)
|
|
|
|
# retrieve coordinates
|
|
for (_, elt) in eltenum
|
|
empty!(elt.coords)
|
|
end
|
|
for coordindex in 1:5
|
|
for indexed_elt in eltenum
|
|
takecoord!(coordqueue, indexed_elt, coordindex)
|
|
end
|
|
end
|
|
|
|
# construct coordinate vectors
|
|
for (_, elt) in eltenum
|
|
buildvec!(elt)
|
|
end
|
|
|
|
# turn relations into equations
|
|
eqns = vcat(
|
|
equation.(ctx.relations),
|
|
[elt.rel for (_, elt) in eltenum if !isnothing(elt.rel)]
|
|
)
|
|
|
|
# add relations to center, orient, and scale the construction
|
|
# [to do] the scaling constraint, as written, can be impossible to satisfy
|
|
# when all of the spheres have to go through the origin
|
|
if !isempty(ctx.points)
|
|
append!(eqns, [sum(pt.coords[k] for pt in ctx.points) for k in 1:3])
|
|
end
|
|
if !isempty(ctx.spheres)
|
|
append!(eqns, [sum(sph.coords[k] for sph in ctx.spheres) for k in 3:4])
|
|
end
|
|
n_elts = length(ctx.points) + length(ctx.spheres)
|
|
if n_elts > 0
|
|
push!(eqns, sum(elt.vec[2] for elt in Iterators.flatten((ctx.points, ctx.spheres))) - n_elts)
|
|
end
|
|
|
|
(Generic.Ideal(coordring, eqns), eqns)
|
|
end
|
|
|
|
end |