Skip to content

Instantly share code, notes, and snippets.

@odow
Last active September 30, 2019 08:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odow/f1b4e37202274fb586284d895e60ee25 to your computer and use it in GitHub Desktop.
Save odow/f1b4e37202274fb586284d895e60ee25 to your computer and use it in GitHub Desktop.
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