How to use abs function in JuMP + Julia objective function
I would like to solve a simple linear optimization problem with JuMP and Julia. This is my code:
using JuMP
using Mosek
model = Model(solver=MosekSolver())
@variable(model, 2.5 <= z1 <= 5.0)
@variable(model, -1.0 <= z2 <= 1.0)
@objective(model, Min, abs(z1+5.0) + abs(z2-3.0))
status = solve(model)
println("Objective value: ", getobjectivevalue(model))
println("z1:",getvalue(z1))
println("z2:",getvalue(z2))
However, I got this error message.
> ERROR: LoadError: MethodError: no method matching
> abs(::JuMP.GenericAffExpr{Float64,JuMP.Variable}) Closest candidates
> are: abs(!Matched::Bool) at bool.jl:77 abs(!Matched::Float16) at
> float.jl:512 abs(!Matched::Float32) at float.jl:513
How can I use abs function in JuMP code?
+3
source to share
1 answer
My problem was solved by @ rickhg12hs commnet. If I use @NLobjective instead of @objective it works. This is the final code.
using JuMP
using Mosek
model = Model(solver=MosekSolver())
@variable(model, 2.5 <= z1 <= 5.0)
@variable(model, -1.0 <= z2 <= 1.0)
@NLobjective(model, Min, abs(z1+5.0) + abs(z2-3.0))
status = solve(model)
println("Objective value: ", getobjectivevalue(model))
println("z1:",getvalue(z1))
println("z2:",getvalue(z2))
+3
source to share