-
Notifications
You must be signed in to change notification settings - Fork 87
/
errors.jl
194 lines (183 loc) · 8.71 KB
/
errors.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
@testset "Fallbacks for `set` methods" begin
model = DummyModel()
@testset "AddVariableNotAllowed" begin
@test_throws MOI.AddVariableNotAllowed MOI.add_variable(model)
try
MOI.add_variable(model)
catch err
@test sprint(showerror, err) == "MathOptInterface.AddVariableNotAllowed:" *
" Adding variables cannot be performed. You may want to use a" *
" `CachingOptimizer` in `AUTOMATIC` mode or you may need to call" *
" `resetoptimizer!` before doing this operation if the" *
" `CachingOptimizer` is in `MANUAL` mode."
end
@test_throws MOI.AddVariableNotAllowed MOI.add_variables(model, 2)
end
vi = MOI.VariableIndex(1)
func = MOI.SingleVariable(vi)
@testset "Inconsistent Vector/Scalar errors" begin
@test_throws MOI.ErrorException begin
MOI.add_constraint(model, MOI.VectorOfVariables([vi, vi]), MOI.EqualTo(0))
end
@test_throws MOI.ErrorException begin
MOI.add_constraint(model, func, MOI.Nonnegatives(2))
end
end
@testset "Unsupported constraint with $f" for f in (MOI.add_constraint,
MOIU.allocate_constraint,
MOIU.load_constraint)
@test_throws MOI.UnsupportedConstraint begin
MOI.add_constraint(model, func, MOI.EqualTo(0))
end
try
MOI.add_constraint(model, func, MOI.EqualTo(0))
catch err
@test sprint(showerror, err) == "$MOI.UnsupportedConstraint{$MOI.SingleVariable,$MOI.EqualTo{$Int}}:" *
" `$MOI.SingleVariable`-in-`$MOI.EqualTo{$Int}` constraints is" *
" not supported by the the model."
end
@test_throws MOI.UnsupportedConstraint begin
MOI.add_constraint(model, vi, MOI.EqualTo(0))
end
@test_throws MOI.UnsupportedConstraint begin
MOI.add_constraint(model, [vi, vi], MOI.Nonnegatives(2))
end
@test_throws MOI.UnsupportedConstraint begin
MOI.add_constraints(model, [vi, vi], [MOI.EqualTo(0),
MOI.EqualTo(0)])
end
end
@testset "add_constraint errors" begin
@test_throws MOI.AddConstraintNotAllowed begin
MOI.add_constraint(model, func, MOI.EqualTo(0.0))
end
try
MOI.add_constraint(model, func, MOI.EqualTo(0.0))
catch err
@test sprint(showerror, err) == "$MOI.AddConstraintNotAllowed{$MOI.SingleVariable,$MOI.EqualTo{Float64}}:" *
" Adding `$MOI.SingleVariable`-in-`$MOI.EqualTo{Float64}`" *
" constraints cannot be performed. You may want to use a" *
" `CachingOptimizer` in `AUTOMATIC` mode or you may need to call" *
" `resetoptimizer!` before doing this operation if the" *
" `CachingOptimizer` is in `MANUAL` mode."
end
@test_throws MOI.AddConstraintNotAllowed begin
MOI.add_constraint(model, vi, MOI.EqualTo(0.0))
end
@test_throws MOI.AddConstraintNotAllowed begin
MOI.add_constraint(model, [vi, vi], MOI.Zeros(2))
end
@test_throws MOI.AddConstraintNotAllowed begin
MOI.add_constraints(model, [vi, vi], [MOI.EqualTo(0.0),
MOI.EqualTo(0.0)])
end
end
@testset "$f errors" for f in (MOIU.allocate_constraint,
MOIU.load_constraint)
@test_throws MOI.ErrorException begin
f(model, func, MOI.EqualTo(0.0))
end
try
f(model, func, MOI.EqualTo(0.0))
catch err
@test err === MOIU.ALLOCATE_LOAD_NOT_IMPLEMENTED
end
end
ci = MOI.ConstraintIndex{MOI.SingleVariable, MOI.EqualTo{Float64}}(1)
@testset "DeleteNotAllowed" begin
@test_throws MOI.DeleteNotAllowed{typeof(vi)} MOI.delete(model, vi)
try
MOI.delete(model, vi)
catch err
@test sprint(showerror, err) == "MathOptInterface.DeleteNotAllowed{MathOptInterface.VariableIndex}:" *
" Deleting the index MathOptInterface.VariableIndex(1) cannot be" *
" performed. You may want to use a `CachingOptimizer` in" *
" `AUTOMATIC` mode or you may need to call `resetoptimizer!`" *
" before doing this operation if the `CachingOptimizer` is in" *
" `MANUAL` mode."
end
@test_throws MOI.DeleteNotAllowed{typeof(ci)} MOI.delete(model, ci)
try
MOI.delete(model, ci)
catch err
@test sprint(showerror, err) == "MathOptInterface.DeleteNotAllowed{MathOptInterface.ConstraintIndex{MathOptInterface.SingleVariable,MathOptInterface.EqualTo{Float64}}}:" *
" Deleting the index MathOptInterface.ConstraintIndex{MathOptInterface.SingleVariable,MathOptInterface.EqualTo{Float64}}(1)" *
" cannot be performed. You may want to use a `CachingOptimizer`" *
" in `AUTOMATIC` mode or you may need to call `resetoptimizer!`" *
" before doing this operation if the `CachingOptimizer` is in" *
" `MANUAL` mode."
end
end
@testset "Unsupported attribute with $f" for f in (MOI.set, MOIU.load,
MOIU.allocate)
@test_throws MOI.UnsupportedAttribute begin
f(model, MOI.ObjectiveFunction{MOI.SingleVariable}(),
MOI.SingleVariable(vi))
end
@test_throws MOI.UnsupportedAttribute begin
f(model, MOI.ConstraintDualStart(), ci, 0.0)
end
end
@testset "SetAttributeNotAllowed" begin
@test_throws MOI.SetAttributeNotAllowed begin
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
end
@test_throws MOI.SetAttributeNotAllowed begin
MOI.set(model, MOI.ConstraintPrimalStart(), ci, 0.0)
end
end
@testset "$f errors" for f in (MOIU.load, MOIU.allocate)
@test_throws MOI.ErrorException begin
f(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
end
try
f(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
catch err
@test err === MOIU.ALLOCATE_LOAD_NOT_IMPLEMENTED
end
@test_throws MOI.ErrorException begin
f(model, MOI.ConstraintPrimalStart(), ci, 0.0)
end
end
@testset "ConstraintFunction" begin
@test_throws MOI.SetAttributeNotAllowed begin
MOI.set(model, MOI.ConstraintFunction(), ci, func)
end
@test_throws ArgumentError begin
MOI.set(model, MOI.ConstraintFunction(), ci,
MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, vi)],
0.0))
end
end
@testset "ConstraintSet" begin
@test_throws MOI.SetAttributeNotAllowed begin
MOI.set(model, MOI.ConstraintSet(), ci, MOI.EqualTo(1.0))
end
@test_throws ArgumentError begin
MOI.set(model, MOI.ConstraintSet(), ci, MOI.EqualTo(1))
end
@test_throws ArgumentError begin
MOI.set(model, MOI.ConstraintSet(), ci, MOI.GreaterThan(1.0))
end
end
end
@testset "Error messages" begin
@test sprint(showerror, MOI.UnsupportedAttribute(MOI.Name())) == "$MOI.UnsupportedAttribute{$MOI.Name}:" *
" Attribute $MOI.Name() is not supported by the the model."
@test sprint(showerror, MOI.UnsupportedAttribute(MOI.Name(), "Message")) == "$MOI.UnsupportedAttribute{$MOI.Name}:" *
" Attribute $MOI.Name() is not supported by the the model: Message"
@test sprint(showerror, MOI.SetAttributeNotAllowed(MOI.Name())) == "$MOI.SetAttributeNotAllowed{$MOI.Name}:" *
" Setting attribute $MOI.Name() cannot be performed. You may want to use" *
" a `CachingOptimizer` in `AUTOMATIC` mode or you may need to call" *
" `resetoptimizer!` before doing this operation if the" *
" `CachingOptimizer` is in `MANUAL` mode."
@test sprint(showerror, MOI.SetAttributeNotAllowed(MOI.Name(), "Message")) == "$MOI.SetAttributeNotAllowed{$MOI.Name}:" *
" Setting attribute $MOI.Name() cannot be performed: Message You may want" *
" to use a `CachingOptimizer` in `AUTOMATIC` mode or you may need to call" *
" `resetoptimizer!` before doing this operation if the `CachingOptimizer`" *
" is in `MANUAL` mode." == "$MOI.SetAttributeNotAllowed{$MOI.Name}:" *
" Setting attribute $MOI.Name() cannot be performed: Message You may want" *
" to use a `CachingOptimizer` in `AUTOMATIC` mode or you may need to call" *
" `resetoptimizer!` before doing this operation if the `CachingOptimizer`" *
" is in `MANUAL` mode."
end