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

ForwardDiff to calculate Gradient #199

Open
rt5592 opened this issue Sep 10, 2017 · 0 comments
Open

ForwardDiff to calculate Gradient #199

rt5592 opened this issue Sep 10, 2017 · 0 comments

Comments

@rt5592
Copy link

rt5592 commented Sep 10, 2017

Hi
Can I use ForwardDiff within MathProgBase NLPEvaluator to calculate Grad and Hess vectors?
For instance, I tried using it as shown below but the solution isn't correct

type HS071 <: MathProgBase.AbstractNLPEvaluator
end

# hs071
# min x1 * x4 * (x1 + x2 + x3) + x3
# st  x1 * x2 * x3 * x4 >= 25
#     x1^2 + x2^2 + x3^2 + x4^2 = 40
#     1 <= x1, x2, x3, x4 <= 5
# Start at (1,5,5,1)
# End at (1.000..., 4.743..., 3.821..., 1.379...)

function MathProgBase.initialize(d::HS071, requested_features::Vector{Symbol})
    for feat in requested_features
        if !(feat in [:Grad, :Jac, :Hess])
            error("Unsupported feature $feat")
            # TODO: implement Jac-vec and Hess-vec products
            # for solvers that need them
        end
    end
end

MathProgBase.features_available(d::HS071) = [:Grad, :Jac, :Hess]
MathProgBase.eval_f(d::HS071, x) = x[1] * x[4] * (x[1] + x[2] + x[3]) + x[3]

function MathProgBase.eval_grad_f(d::HS071, grad_f, x)
    
   grad_f= x -> ForwardDiff.gradient(MathProgBase.eval_f, x)       # would this work here? is the syntax correct?
   
   # grad_f[1] = x[1] * x[4] + x[4] * (x[1] + x[2] + x[3])
   # grad_f[2] = x[1] * x[4]
   # grad_f[3] = x[1] * x[4] + 1
   # grad_f[4] = x[1] * (x[1] + x[2] + x[3])
end

Any feedback is appreciated!

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

1 participant