Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using MathProgBase programmatically #201

Open
gragusa opened this issue Oct 16, 2017 · 1 comment
Open

Using MathProgBase programmatically #201

gragusa opened this issue Oct 16, 2017 · 1 comment

Comments

@gragusa
Copy link

gragusa commented Oct 16, 2017

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
Copy link
Member

mlubin commented Oct 16, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants