Last active
September 30, 2019 08:47
-
-
Save odow/f1b4e37202274fb586284d895e60ee25 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using JuMP, Ipopt | |
function minimize( | |
f::Function, | |
A::Matrix, | |
b::Vector, | |
optimizer::JuMP.OptimizerFactory | |
) | |
M, N = size(A) | |
@assert length(b) == M | |
model = Model(optimizer) | |
@variable(model, x[1:N] >= 0) | |
@constraint(model, A * x .== b) | |
JuMP.register(model, :f, N, (x...) -> f(collect(x)), autodiff = true) | |
@NLobjective(model, Min, f(x...)) | |
optimize!(model) | |
return objective_value(model), value.(x) | |
end | |
# min x[1]^2 | |
# s.t. x[1] - x[2] == 2 | |
# x >= 0 | |
z, x = minimize(x -> x[1]^2, [1 -1], [2], with_optimizer(Ipopt.Optimizer)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment