ds4dm / Tulip.jl

Interior-point solver in pure Julia
Other
154 stars 20 forks source link

Writing user extension for `Tulip.KKT.AbstractKKTBackend`, `Tulip.KKT.AbstractKKTSolver` etc. #145

Closed pratyai closed 10 months ago

pratyai commented 10 months ago

Hi,

I am trying to write a custom KKT backend and solver for Tulip that could exploit the known structure of a certain class of problems. The problem details etc. are not particularly relevant, but I would like to write a module like:

module MyKKT

using Tulip

struct Backend <: AbstractKKTBackend    # custom backend class to pass to Tulip parameters
end

mutable struct Solver{Tv<:Number,Ti<:Integer} <: AbstractKKTSolver{Tv}   # custom solver class that knows the problem structure
  # problem specific data
end

backend(::Solver) = "CustomBackend"
linear_system(::Solver) = "Normal equations (K1)"

function setup(A::AbstractSparseMatrix{Tv,Ti}, ::K1, ::Backend) where {Tv<:Number,Ti<:Integer}
  # setup work
end

function update!(kkt::Solver{Tv,Ti}, θ::AbstractVector{Tv}, regP::AbstractVector{Tv}, regD::AbstractVector{Tv}) where {Tv<:Number,Ti<:Integer}
  # update work
end

function solve!(dx::AbstractVector{Tv}, dy::AbstractVector{Tv}, kkt::Solver{Tv,Ti}, ξp::AbstractVector{Tv}, ξd::AbstractVector{Tv}) where {Tv<:Number,Ti<:Integer}
  # solve work
end

end

The problem is that the rest of the Tulip framework heavily relies on the KKT module and its specific functions, e.g. this setup call.

I don't understand if it's possible to define a setup() function (as shown above) completely outside Tulip, and still be able to reuse the rest of the Tulip (i.e. I don't have to work on a fork). I couldn't find any example if that's possible to do so. If not right now, how difficult it could be to make the KKT module extendable (I'm happy to make an attempt if the maintainers are open to it)?

pratyai commented 10 months ago

I just realised that I could just write

function Tulip.KKT.setup(A::AbstractSparseMatrix{Tv,Ti}, ::K1, ::Backend) where {Tv<:Number,Ti<:Integer}
  # setup work
end

and so on, and that would be enough to extend those functions from KKT module. Didn't know I could do that in Julia before. Closing the issue, since it's a non-issue after all.