forked from jump-dev/JuMP.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjective.jl
94 lines (75 loc) · 3.29 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
struct DummyOptimizer <: MOI.AbstractOptimizer end
MOI.is_empty(::DummyOptimizer) = true
@testset "Unsupported objective_function" begin
model = Model(with_optimizer(DummyOptimizer))
func = MOI.SingleVariable(MOI.VariableIndex(1))
@test_throws ErrorException JuMP.set_objective_function(model, func)
end
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.FEASIBILITY_SENSE)
@test JuMP.objective_sense(model) == MOI.FEASIBILITY_SENSE
end
@testset "SingleVariable objectives" begin
m = ModelType()
@variable(m, x)
@objective(m, Min, x)
@test JuMP.objective_sense(m) == MOI.MIN_SENSE
@test JuMP.objective_function_type(m) == VariableRefType
@test JuMP.objective_function(m) == x
@test JuMP.objective_function(m, VariableRefType) == x
@objective(m, Max, x)
@test JuMP.objective_sense(m) == MOI.MAX_SENSE
@test JuMP.objective_function_type(m) == VariableRefType
@test JuMP.objective_function(m) == x
@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.MIN_SENSE
@test JuMP.objective_function_type(m) == AffExprType
@test JuMP.isequal_canonical(JuMP.objective_function(m), 2x)
@test JuMP.isequal_canonical(JuMP.objective_function(m, AffExprType), 2x)
@objective(m, Max, x + 3x + 1)
@test JuMP.objective_sense(m) == MOI.MAX_SENSE
@test JuMP.objective_function_type(m) == AffExprType
@test JuMP.isequal_canonical(JuMP.objective_function(m), 4x + 1)
@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.MIN_SENSE
@test JuMP.objective_function_type(m) == QuadExprType
@test JuMP.isequal_canonical(JuMP.objective_function(m), x^2 + 2x)
@test JuMP.isequal_canonical(JuMP.objective_function(m, QuadExprType), x^2 + 2x)
@test_throws InexactError JuMP.objective_function(m, AffExprType)
end
@testset "Sense as symbol" begin
m = ModelType()
@variable(m, x)
@test_throws ErrorException @objective(m, :Min, 2x)
end
@testset "Sense in variable" begin
m = ModelType()
@variable(m, x)
sense = MOI.MIN_SENSE
@objective(m, sense, 2x)
@test JuMP.objective_sense(m) == MOI.MIN_SENSE
@test JuMP.isequal_canonical(JuMP.objective_function(m, AffExprType), 2x)
sense = :Min
@test_throws ErrorException @objective(m, sense, 2x)
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