-
Notifications
You must be signed in to change notification settings - Fork 63
/
MOI_wrapper.jl
208 lines (191 loc) · 6.06 KB
/
MOI_wrapper.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
module TestMOIwrapper
using CPLEX
using MathOptInterface
using Test
const MOI = MathOptInterface
function runtests()
for name in names(@__MODULE__; all = true)
if startswith("$(name)", "test_")
@testset "$(name)" begin
getfield(@__MODULE__, name)()
end
end
end
return
end
function test_runtests()
model = MOI.Bridges.full_bridge_optimizer(CPLEX.Optimizer(), Float64)
MOI.set(model, MOI.Silent(), true)
# Turn off presolve reductions so CPLEX will generate infeasibility
# certificates.
MOI.set(model, MOI.RawOptimizerAttribute("CPX_PARAM_REDUCE"), 0)
MOI.Test.runtests(
model,
MOI.Test.Config(atol = 1e-3, rtol = 1e-3),
exclude = String[
# CPLEX doesn't support nonconvex QCPs
"test_quadratic_nonconvex_",
# TODO(odow): fixed in MOI 0.10.1
"test_solve_SOS2_add_and_delete",
"test_objective_get_ObjectiveFunction_ScalarAffineFunction",
"test_conic_SecondOrderCone_negative_initial_bound",
"test_conic_SecondOrderCone_negative_post_bound",
"test_conic_SecondOrderCone_negative_post_bound_ii",
"test_conic_SecondOrderCone_no_initial_bound",
"test_conic_SecondOrderCone_nonnegative_initial_bound",
"test_conic_SecondOrderCone_nonnegative_post_bound",
],
)
return
end
"""
Test setting CPXPARAM_OptimalityTarget because it changes the problem type.
"""
function test_CPXPARAM_OptimalityTarget()
model = CPLEX.Optimizer()
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variable(model)
MOI.add_constraint(model, x, MOI.Interval(1.0, 4.0))
MOI.set(
model,
MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{Float64}}(),
MOI.ScalarQuadraticFunction(
[MOI.ScalarQuadraticTerm(2.0, x, x)],
MOI.ScalarAffineTerm{Float64}[],
0.0,
),
)
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
MOI.set(
model,
MOI.RawOptimizerAttribute("CPXPARAM_OptimalityTarget"),
CPX_OPTIMALITYTARGET_OPTIMALGLOBAL,
)
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == MOI.OPTIMAL
@test MOI.get(model, MOI.PrimalStatus()) == MOI.FEASIBLE_POINT
@test MOI.get(model, MOI.ObjectiveValue()) ≈ 16.0 atol = 1e-6
@test MOI.get(model, MOI.VariablePrimal(), x) ≈ 4.0 atol = 1e-6
return
end
function test_user_provided_env()
env = CPLEX.Env()
model_1 = CPLEX.Optimizer(env)
@test model_1.env === env
model_2 = CPLEX.Optimizer(env)
@test model_2.env === env
# Check that finalizer doesn't touch env when manually provided.
finalize(model_1)
@test env.ptr != C_NULL
return
end
function test_automatic_env()
model_1 = CPLEX.Optimizer()
model_2 = CPLEX.Optimizer()
@test model_1.env.ptr !== model_2.env.ptr
return
end
function test_user_provided_env_empty()
env = CPLEX.Env()
model = CPLEX.Optimizer(env)
@test model.env === env
@test env.ptr != C_NULL
MOI.empty!(model)
@test model.env === env
@test env.ptr != C_NULL
return
end
function test_automatic_env_empty()
model = CPLEX.Optimizer()
env = model.env
MOI.empty!(model)
@test model.env === env
@test env.ptr != C_NULL
return
end
function test_manual_env()
env = CPLEX.Env()
model = CPLEX.Optimizer(env)
finalize(env)
@test env.finalize_called
finalize(model)
@test env.ptr == C_NULL
return
end
function test_ZeroOne_NONE()
model = CPLEX.Optimizer()
x = MOI.add_variable(model)
c = MOI.add_constraint(model, x, MOI.ZeroOne())
tmp = Ref{Cdouble}()
CPLEX.CPXgetlb(model.env, model.lp, tmp, 0, 0)
@test tmp[] == 0.0
CPLEX.CPXgetub(model.env, model.lp, tmp, 0, 0)
@test tmp[] == 1.0
MOI.delete(model, c)
CPLEX.CPXgetlb(model.env, model.lp, tmp, 0, 0)
@test tmp[] == -CPLEX.CPX_INFBOUND
CPLEX.CPXgetub(model.env, model.lp, tmp, 0, 0)
@test tmp[] == CPLEX.CPX_INFBOUND
return
end
function test_ZeroOne_LESS_THAN()
model = CPLEX.Optimizer()
x = MOI.add_variable(model)
MOI.add_constraint(model, x, MOI.LessThan(2.0))
c = MOI.add_constraint(model, x, MOI.ZeroOne())
tmp = Ref{Cdouble}()
CPLEX.CPXgetlb(model.env, model.lp, tmp, 0, 0)
@test tmp[] == 0.0
CPLEX.CPXgetub(model.env, model.lp, tmp, 0, 0)
@test tmp[] == 2.0
MOI.delete(model, c)
CPLEX.CPXgetlb(model.env, model.lp, tmp, 0, 0)
@test tmp[] == -CPLEX.CPX_INFBOUND
CPLEX.CPXgetub(model.env, model.lp, tmp, 0, 0)
@test tmp[] == 2.0
return
end
function test_ZeroOne_GREATER_THAN()
model = CPLEX.Optimizer()
x = MOI.add_variable(model)
MOI.add_constraint(model, x, MOI.GreaterThan(-2.0))
c = MOI.add_constraint(model, x, MOI.ZeroOne())
tmp = Ref{Cdouble}()
CPLEX.CPXgetlb(model.env, model.lp, tmp, 0, 0)
@test tmp[] == -2.0
CPLEX.CPXgetub(model.env, model.lp, tmp, 0, 0)
@test tmp[] == 1.0
MOI.delete(model, c)
CPLEX.CPXgetlb(model.env, model.lp, tmp, 0, 0)
@test tmp[] == -2.0
CPLEX.CPXgetub(model.env, model.lp, tmp, 0, 0)
@test tmp[] == CPLEX.CPX_INFBOUND
return
end
function test_ZeroOne_INTERVAL()
model = CPLEX.Optimizer()
x = MOI.add_variable(model)
MOI.add_constraint(model, x, MOI.Interval(-2.0, 2.0))
c = MOI.add_constraint(model, x, MOI.ZeroOne())
tmp = Ref{Cdouble}()
CPLEX.CPXgetlb(model.env, model.lp, tmp, 0, 0)
@test tmp[] == -2.0
CPLEX.CPXgetub(model.env, model.lp, tmp, 0, 0)
@test tmp[] == 2.0
MOI.delete(model, c)
CPLEX.CPXgetlb(model.env, model.lp, tmp, 0, 0)
@test tmp[] == -2.0
CPLEX.CPXgetub(model.env, model.lp, tmp, 0, 0)
@test tmp[] == 2.0
return
end
function test_fake_status()
model = CPLEX.Optimizer()
model.ret_optimize = CPLEX.CPXERR_NO_MEMORY
@test MOI.get(model, MOI.TerminationStatus()) == MOI.MEMORY_LIMIT
@test MOI.get(model, MOI.RawStatusString()) ==
"CPLEX Error 1001: Out of memory.\n"
return
end
end # module TestMOIwrapper
TestMOIwrapper.runtests()