/
objective.jl
55 lines (43 loc) · 1.84 KB
/
objective.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function objectives_test(ModelType::Type{<:JuMP.AbstractModel}, VariableRefType::Type{<:JuMP.AbstractVariableRef})
AffExprType = JuMP.GenericAffExpr{Float64, VariableRefType}
QuadExprType = JuMP.GenericQuadExpr{Float64, VariableRefType}
@testset "objective_sense set and get" begin
model = ModelType()
JuMP.set_objective_sense(model, MOI.FeasibilitySense)
@test JuMP.objective_sense(model) == MOI.FeasibilitySense
end
@testset "SingleVariable objectives" begin
m = ModelType()
@variable(m, x)
@objective(m, Min, x)
@test JuMP.objective_sense(m) == MOI.MinSense
@test JuMP.objective_function(m, VariableRefType) == x
@objective(m, Max, x)
@test JuMP.objective_sense(m) == MOI.MaxSense
@test JuMP.objective_function(m, VariableRefType) == x
end
@testset "Linear objectives" begin
m = ModelType()
@variable(m, x)
@objective(m, Min, 2x)
@test JuMP.objective_sense(m) == MOI.MinSense
@test JuMP.isequal_canonical(JuMP.objective_function(m, AffExprType), 2x)
@objective(m, Max, x + 3x + 1)
@test JuMP.objective_sense(m) == MOI.MaxSense
@test JuMP.isequal_canonical(JuMP.objective_function(m, AffExprType), 4x + 1)
end
@testset "Quadratic objectives" begin
m = ModelType()
@variable(m, x)
@objective(m, Min, x^2 + 2x)
@test JuMP.objective_sense(m) == MOI.MinSense
@test JuMP.isequal_canonical(JuMP.objective_function(m, QuadExprType), x^2 + 2x)
@test_throws InexactError JuMP.objective_function(m, AffExprType)
end
end
@testset "Objectives for JuMP.Model" begin
objectives_test(Model, VariableRef)
end
@testset "Objectives for JuMPExtension.MyModel" begin
objectives_test(JuMPExtension.MyModel, JuMPExtension.MyVariableRef)
end