MathProgBase.jl icon indicating copy to clipboard operation
MathProgBase.jl copied to clipboard

Using MathProgBase programmatically

Open gragusa opened this issue 8 years ago • 1 comments

I am trying to automate the construction of MathProgBase.

I am doing something along these lines:

matable struct MPB <: MathProgBase.AbstractNLPEvaluator end

function foo(f::Function, g!::Function, x0, lvar, uvar, sense, solver, args...)
MathProgBase.features_available(g::MPB) = [:Grad]
    function MathProgBase.initialize(d::MPB, requested_features::Vector{Symbol})
        for feat in requested_features
            if !(feat in [:Grad])
                error("Unsupported feature $feat")
            end
        end
    end

    MathProgBase.jac_structure(g::MPB) = Int[],Int[]
    MathProgBase.eval_jac_g(g::MPB, J, x) = nothing
    MathProgBase.eval_f(g::MPB, x) = f(x)
    MathProgBase.eval_grad_f(g::MPB, gr, x) = g!(gr, x)    
    m = MathProgBase.NonlinearModel(solver)
    MathProgBase.loadproblem!(m, length(x0), 0, lvar, uvar, Float64[], Float64[], sense, MPB())
    MathProgBase.setwarmstart!(m, x0)
    do_computations(m, args...)
end

The function works fails the first time with a ERROR: MethodError: no method matching features_available(::Genoud.Genoud_MPB) The applicable method may be too new: running in world age 21824, while current world is 21830.. After I run it twice everything works as expected. I know about the World count, but I was wondering how can I obviate this problem (I am running out of ideas). The alternative would be to pass the non-linear problem as an argument to foo but I would like to make this automatic.

gragusa avatar Oct 16 '17 21:10 gragusa

Make the functions fields of the MPB struct so that you can define all the relevant methods at the top level once.

mlubin avatar Oct 16 '17 22:10 mlubin