4

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 the JuMP code?

1
  • 2
    Does using @NLobjective(model, Min, abs(z1+5.0) + abs(z2-3.0)) meet your expectations? Commented Jul 30, 2017 at 4:18

2 Answers 2

3

My problem is solved by @rickhg12hs's 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))
Sign up to request clarification or add additional context in comments.

Comments

0

I did it on a diffrent way

AvgOperationtime = [1 2]#[2.0 2.0 2.0 3.3333333333333335 2.5 2.0 2.0 2.5 2.5 2.0 2.0]
    Operationsnumberremovecounter = [1 0;1 1]#[1.0 1.0 1.0 1.0 -0.0 1.0 1.0 1.0 -0.0 1.0 1.0; 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0; 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0; 1.0 1.0 1.0 -0.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0; 1.0 1.0 1.0 -0.0 1.0 1.0 1.0 -0.0 1.0 1.0 1.0]
    Modelnumber = 2
    Operationsnumber = 2
    Basecaseworkload = 2
    y = 0.1
    Highestnumber = 999
    Solver = GLPK.Optimizer
    #Operationtime[1,1 X;0,9 2]
    m = Model(with_optimizer(Solver)); 
    @variable(m, Operationtime[1:Modelnumber,1:Operationsnumber]>=0);
    @variable(m, Absoluttime[1:Modelnumber,1:Operationsnumber]>=0);
    @variable(m, Absolutchoice[1:Modelnumber,1:Operationsnumber,1:2], Bin);
    @objective(m, Max, sum(Absoluttime[M,O]*Operationsnumberremovecounter[M,O] for M=1:Modelnumber,O=1:Operationsnumber))
    #How much Time can differ
    @constraint(m, BorderOperationtime1[M=1:Modelnumber,O=1:Operationsnumber], AvgOperationtime[O]*(1-y) <= Operationtime[M,O]);
    @constraint(m, BorderOperationtime2[M=1:Modelnumber,O=1:Operationsnumber], AvgOperationtime[O]*(1+y) >= Operationtime[M,O]);
    #Workload
    @constraint(m, Worklimit[O=1:Operationsnumber],  sum(Operationtime[M,O]*Operationsnumberremovecounter[M,O] for M=1:Modelnumber) == Basecaseworkload);
    #Absolut
    @constraint(m, Absolutchoice1[M=1:Modelnumber,O=1:Operationsnumber],  sum(Absolutchoice[M,O,X] for X=1:2) == 1);
    @constraint(m, Absoluttime1[M=1:Modelnumber,O=1:Operationsnumber], Absoluttime[M,O] <= Operationtime[M,O]-AvgOperationtime[O]+Absolutchoice[M,O,1]*Highestnumber);
    @constraint(m, Absoluttime2[M=1:Modelnumber,O=1:Operationsnumber], Absoluttime[M,O] <= AvgOperationtime[O]-Operationtime[M,O]+Absolutchoice[M,O,2]*Highestnumber);

    optimize!(m);

println("Termination status: ", JuMP.termination_status(m));
println("Primal status: ", JuMP.primal_status(m));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.