JuliaOpt / MathProgBase.jl

DEPRECATED: Solver-independent functions (i.e. linprog and mixintprog) and low-level interface for Mathematical Programming
Other
80 stars 38 forks source link

Using MathProgBase programmatically #201

Open gragusa opened 6 years ago

gragusa commented 6 years ago

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.

mlubin commented 6 years ago

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