Compare commits
2 Commits
463a3b21e1
...
c29000d912
Author | SHA1 | Date | |
---|---|---|---|
|
c29000d912 | ||
|
86dbd9ea45 |
@ -12,46 +12,68 @@ using Groebner
|
|||||||
abstract type Element{T} end
|
abstract type Element{T} end
|
||||||
|
|
||||||
mutable struct Point{T} <: Element{T}
|
mutable struct Point{T} <: Element{T}
|
||||||
coords::Union{Vector{MPolyRingElem{T}}, Nothing}
|
coords::Vector{MPolyRingElem{T}}
|
||||||
vec::Union{Vector{MPolyRingElem{T}}, Nothing}
|
vec::Union{Vector{MPolyRingElem{T}}, Nothing}
|
||||||
rel::Nothing
|
rel::Nothing
|
||||||
|
|
||||||
## [to do] constructor argument never needed?
|
## [to do] constructor argument never needed?
|
||||||
Point{T}(
|
Point{T}(
|
||||||
coords::Union{Vector{MPolyRingElem{T}}, Nothing} = nothing,
|
coords::Vector{MPolyRingElem{T}} = MPolyRingElem{T}[],
|
||||||
vec::Union{Vector{MPolyRingElem{T}}, Nothing} = nothing
|
vec::Union{Vector{MPolyRingElem{T}}, Nothing} = nothing
|
||||||
) where T = new(coords, vec, nothing)
|
) where T = new(coords, vec, nothing)
|
||||||
end
|
end
|
||||||
|
|
||||||
coordnames(_::Point) = [:xₚ, :yₚ, :zₚ]
|
##coordnames(_::Point) = [:xₚ, :yₚ, :zₚ]
|
||||||
|
|
||||||
function buildvec(pt::Point, coordqueue)
|
function buildvec!(pt::Point)
|
||||||
coordring = parent(coordqueue[1])
|
coordring = parent(pt.coords[1])
|
||||||
pt.coords = splice!(coordqueue, 1:3)
|
|
||||||
pt.vec = [one(coordring), dot(pt.coords, pt.coords), pt.coords...]
|
pt.vec = [one(coordring), dot(pt.coords, pt.coords), pt.coords...]
|
||||||
end
|
end
|
||||||
|
|
||||||
mutable struct Sphere{T} <: Element{T}
|
mutable struct Sphere{T} <: Element{T}
|
||||||
coords::Union{Vector{MPolyRingElem{T}}, Nothing}
|
coords::Vector{MPolyRingElem{T}}
|
||||||
vec::Union{Vector{MPolyRingElem{T}}, Nothing}
|
vec::Union{Vector{MPolyRingElem{T}}, Nothing}
|
||||||
rel::Union{MPolyRingElem{T}, Nothing}
|
rel::Union{MPolyRingElem{T}, Nothing}
|
||||||
|
|
||||||
|
## [to do] constructor argument never needed?
|
||||||
Sphere{T}(
|
Sphere{T}(
|
||||||
coords::Union{Vector{MPolyRingElem{T}}, Nothing} = nothing,
|
coords::Vector{MPolyRingElem{T}} = MPolyRingElem{T}[],
|
||||||
vec::Union{Vector{MPolyRingElem{T}}, Nothing} = nothing,
|
vec::Union{Vector{MPolyRingElem{T}}, Nothing} = nothing,
|
||||||
rel::Union{MPolyRingElem{T}, Nothing} = nothing
|
rel::Union{MPolyRingElem{T}, Nothing} = nothing
|
||||||
) where T = new(coords, vec, rel)
|
) where T = new(coords, vec, rel)
|
||||||
end
|
end
|
||||||
|
|
||||||
coordnames(_::Sphere) = [:rₛ, :sₛ, :xₛ, :yₛ, :zₛ]
|
##coordnames(_::Sphere) = [:rₛ, :sₛ, :xₛ, :yₛ, :zₛ]
|
||||||
|
|
||||||
function buildvec(sph::Sphere, coordqueue)
|
function buildvec!(sph::Sphere)
|
||||||
coordring = parent(coordqueue[1])
|
coordring = parent(sph.coords[1])
|
||||||
sph.coords = splice!(coordqueue, 1:5)
|
|
||||||
sph.vec = sph.coords
|
sph.vec = sph.coords
|
||||||
sph.rel = mprod(sph.coords, sph.coords) + one(coordring)
|
sph.rel = mprod(sph.coords, sph.coords) + one(coordring)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
const coordnames = IdDict{Symbol, Vector{Union{Symbol, Nothing}}}(
|
||||||
|
nameof(Point) => [nothing, nothing, :xₚ, :yₚ, :zₚ],
|
||||||
|
nameof(Sphere) => [:rₛ, :sₛ, :xₛ, :yₛ, :zₛ]
|
||||||
|
)
|
||||||
|
|
||||||
|
coordname(elem::Element, index) = coordnames[nameof(typeof(elem))][index]
|
||||||
|
|
||||||
|
function pushcoordname!(coordnamelist, indexed_elem::Tuple{Any, Element}, coordindex)
|
||||||
|
elemindex, elem = indexed_elem
|
||||||
|
name = coordname(elem, coordindex)
|
||||||
|
if !isnothing(name)
|
||||||
|
subscript = Subscripts.sub(string(elemindex))
|
||||||
|
push!(coordnamelist, Symbol(name, subscript))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function takecoord!(coordlist, indexed_elem::Tuple{Any, Element}, coordindex)
|
||||||
|
elem = indexed_elem[2]
|
||||||
|
if !isnothing(coordname(elem, coordindex))
|
||||||
|
push!(elem.coords, popfirst!(coordlist))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# --- primitive relations ---
|
# --- primitive relations ---
|
||||||
|
|
||||||
abstract type Relation{T} end
|
abstract type Relation{T} end
|
||||||
@ -99,22 +121,38 @@ function Base.push!(ctx::Construction{T}, rel::Relation{T}) where T
|
|||||||
end
|
end
|
||||||
|
|
||||||
function realize(ctx::Construction{T}) where T
|
function realize(ctx::Construction{T}) where T
|
||||||
# collect variable names
|
# collect coordinate names
|
||||||
coordnamelist = Symbol[]
|
coordnamelist = Symbol[]
|
||||||
elemenum = enumerate(ctx.elements)
|
elemenum = enumerate(ctx.elements)
|
||||||
for (index, elem) in elemenum
|
for coordindex in 1:5
|
||||||
subscript = Subscripts.sub(string(index))
|
for indexed_elem in elemenum
|
||||||
append!(coordnamelist,
|
pushcoordname!(coordnamelist, indexed_elem, coordindex)
|
||||||
[Symbol(name, subscript) for name in coordnames(elem)]
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
display(collect(elemenum))
|
||||||
|
display(coordnamelist)
|
||||||
|
println()
|
||||||
|
|
||||||
# construct coordinate ring
|
# construct coordinate ring
|
||||||
coordring, coordqueue = polynomial_ring(parent_type(T)(), coordnamelist, ordering = :degrevlex)
|
coordring, coordqueue = polynomial_ring(parent_type(T)(), coordnamelist, ordering = :degrevlex)
|
||||||
|
|
||||||
|
# retrieve coordinates
|
||||||
|
for (_, elem) in elemenum
|
||||||
|
empty!(elem.coords)
|
||||||
|
end
|
||||||
|
for coordindex in 1:5
|
||||||
|
for indexed_elem in elemenum
|
||||||
|
takecoord!(coordqueue, indexed_elem, coordindex)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# construct coordinate vectors
|
# construct coordinate vectors
|
||||||
for (_, elem) in elemenum
|
for (_, elem) in elemenum
|
||||||
buildvec(elem, coordqueue)
|
buildvec!(elem)
|
||||||
|
display(elem.coords)
|
||||||
|
display(elem.vec)
|
||||||
|
println()
|
||||||
end
|
end
|
||||||
|
|
||||||
# turn relations into equations
|
# turn relations into equations
|
||||||
|
110
engine-proto/hitting-set.jl
Normal file
110
engine-proto/hitting-set.jl
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
module HittingSet
|
||||||
|
|
||||||
|
HittingSetProblem{T} = Pair{Set{T}, Vector{Pair{T, Set{Set{T}}}}}
|
||||||
|
|
||||||
|
# `subsets` should be a collection of Set objects
|
||||||
|
function HittingSetProblem(subsets, chosen = Set())
|
||||||
|
wholeset = union(subsets...)
|
||||||
|
T = eltype(wholeset)
|
||||||
|
unsorted_moves = [
|
||||||
|
elt => Set(filter(s -> elt ∉ s, subsets))
|
||||||
|
for elt in wholeset
|
||||||
|
]
|
||||||
|
moves = sort(unsorted_moves, by = pair -> length(pair.second))
|
||||||
|
Set{T}(chosen) => moves
|
||||||
|
end
|
||||||
|
|
||||||
|
function Base.display(problem::HittingSetProblem{T}) where T
|
||||||
|
println("HittingSetProblem{$T}")
|
||||||
|
|
||||||
|
chosen = problem.first
|
||||||
|
println(" {", join(string.(chosen), ", "), "}")
|
||||||
|
|
||||||
|
moves = problem.second
|
||||||
|
for (choice, missed) in moves
|
||||||
|
println(" | ", choice)
|
||||||
|
for s in missed
|
||||||
|
println(" | | {", join(string.(s), ", "), "}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
println()
|
||||||
|
end
|
||||||
|
|
||||||
|
function solve(pblm::HittingSetProblem{T}, maxdepth = Inf) where T
|
||||||
|
problems = Dict(pblm)
|
||||||
|
println(typeof(problems))
|
||||||
|
while length(first(problems).first) < maxdepth
|
||||||
|
subproblems = typeof(problems)()
|
||||||
|
for (chosen, moves) in problems
|
||||||
|
if isempty(moves)
|
||||||
|
return chosen
|
||||||
|
else
|
||||||
|
for (choice, missed) in moves
|
||||||
|
to_be_chosen = union(chosen, Set([choice]))
|
||||||
|
if isempty(missed)
|
||||||
|
return to_be_chosen
|
||||||
|
elseif !haskey(subproblems, to_be_chosen)
|
||||||
|
push!(subproblems, HittingSetProblem(missed, to_be_chosen))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
problems = subproblems
|
||||||
|
end
|
||||||
|
problems
|
||||||
|
end
|
||||||
|
|
||||||
|
function test(n = 1)
|
||||||
|
T = [Int64, Int64, Symbol, Symbol][n]
|
||||||
|
subsets = Set{T}.([
|
||||||
|
[
|
||||||
|
[1, 3, 5],
|
||||||
|
[2, 3, 4],
|
||||||
|
[1, 4],
|
||||||
|
[2, 3, 4, 5],
|
||||||
|
[4, 5]
|
||||||
|
],
|
||||||
|
# example from Amit Chakrabarti's graduate-level algorithms class (CS 105)
|
||||||
|
# notes by Valika K. Wan and Khanh Do Ba, Winter 2005
|
||||||
|
# https://www.cs.dartmouth.edu/~ac/Teach/CS105-Winter05/
|
||||||
|
[
|
||||||
|
[1, 3], [1, 4], [1, 5],
|
||||||
|
[1, 3], [1, 2, 4], [1, 2, 5],
|
||||||
|
[4, 3], [ 2, 4], [ 2, 5],
|
||||||
|
[6, 3], [6, 4], [ 5]
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[:w, :x, :y],
|
||||||
|
[:x, :y, :z],
|
||||||
|
[:w, :z],
|
||||||
|
[:x, :y]
|
||||||
|
],
|
||||||
|
# Wikipedia showcases this as an example of a problem where the greedy
|
||||||
|
# algorithm performs especially poorly
|
||||||
|
[
|
||||||
|
[:a, :x, :t1],
|
||||||
|
[:a, :y, :t2],
|
||||||
|
[:a, :y, :t3],
|
||||||
|
[:a, :z, :t4],
|
||||||
|
[:a, :z, :t5],
|
||||||
|
[:a, :z, :t6],
|
||||||
|
[:a, :z, :t7],
|
||||||
|
[:b, :x, :t8],
|
||||||
|
[:b, :y, :t9],
|
||||||
|
[:b, :y, :t10],
|
||||||
|
[:b, :z, :t11],
|
||||||
|
[:b, :z, :t12],
|
||||||
|
[:b, :z, :t13],
|
||||||
|
[:b, :z, :t14]
|
||||||
|
]
|
||||||
|
][n])
|
||||||
|
problem = HittingSetProblem(subsets)
|
||||||
|
if isa(problem, HittingSetProblem{T})
|
||||||
|
println("Correct type")
|
||||||
|
else
|
||||||
|
println("Wrong type: ", typeof(problem))
|
||||||
|
end
|
||||||
|
problem
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user