-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathutilities.jl
More file actions
189 lines (179 loc) · 5.92 KB
/
Copy pathutilities.jl
File metadata and controls
189 lines (179 loc) · 5.92 KB
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
import StarAlgebras as SA
using Test, JuMP
import StarAlgebras as SA
using SumOfSquares
function _optimize!(model, config)
JuMP.optimize!(model)
return inner_variable_value(model, config.atol + config.rtol)
end
function _model(optimizer::MOI.AbstractOptimizer)
MOI.empty!(optimizer)
return direct_model(optimizer)
end
function _model(factory)
return Model(factory)
end
#const SOSPolynomial{T, OT<:MOI.ModelLike} = MOI.Bridges.Constraint.SingleBridgeOptimizer{SumOfSquares.SOSPolynomialBridge{T}, OT}
#function _cheat_model(factory::OptimizerFactory)
# return Model(optimizer_with_attributes(() -> SOSPolynomial{Float64}(factory())))
#end
#"""
# @test_suite setname subsets
#
#Defines a function `setname_test(model, config, exclude)` that runs the tests
#defined in the dictionary `setname_tests` with the model `model` and config
#`config` except the tests whose dictionary key is in `exclude`. If `subsets` is
#`true` then each test runs in fact multiple tests hence the `exclude` argument
#is passed as it can also contains test to be excluded from these subsets of
#tests.
#"""
macro test_suite(setname, subsets = false)
testname = Symbol(string(setname) * "_test")
testdict = Symbol(string(testname) * "s")
if subsets
runtest = :(f(model, config; exclude))
else
runtest = :(f(model, config))
end
return esc(
:(
function $testname(
model, # could be ModelLike or an optimizer constructor
config::$MOI.Test.Config;
include::Vector{String} = collect(keys($testdict)),
exclude::Vector{String} = String[],
)
for name in include
f = $(testdict)[name]
if name in exclude
continue
end
@testset "$name" begin
$runtest
end
end
end
),
)
end
function test_noc(model, F, S, n)
@test MOI.get(model, MOI.NumberOfConstraints{F,S}()) == n
@test length(MOI.get(model, MOI.ListOfConstraintIndices{F,S}())) == n
@test ((F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent())) ==
!iszero(n)
end
# Test deletion of bridge
function test_delete_bridge(
model::Model,
cref::ConstraintRef{Model,MOI.ConstraintIndex{F,S}},
nvars::Int,
nocs::Tuple;
last_bridge = true,
) where {F,S}
@test num_variables(model) == nvars
@test length(all_variables(model)) == nvars
test_noc(model, F, S, 1)
for noc in nocs
test_noc(model, noc...)
end
@test is_valid(model, cref)
delete(model, cref)
@test_throws MOI.InvalidIndex(index(cref)) delete(model, cref)
@test !is_valid(model, cref)
test_noc(model, F, S, 0)
# As the bridge has been removed, if the constraints it has created where not removed, it wouldn't be there to decrease this counter anymore
@test num_variables(model) == nvars
@test length(all_variables(model)) == nvars
for noc in nocs
test_noc(model, noc...)
end
end
# Utilities for building the mock `optimize!` from the solution of a solver
_inner(model::MOI.Utilities.CachingOptimizer) = _inner(model.optimizer)
_inner(model::MOI.Bridges.LazyBridgeOptimizer) = model.model
_cheat_inner(model::MOI.ModelLike) = model
function _cheat_inner(model::MOI.Bridges.Constraint.SingleBridgeOptimizer)
return _cheat_inner(model.model)
end
# Variables primal values for inner bridged model
function print_value(v, atol)
i = round(v)
if isapprox(v, i, atol = atol)
print(float(i))
else
print(v)
end
end
function inner_variable_value(model, atol = 1e-4)
#inner = _cheat_inner(backend(model))
inner = _inner(backend(model))
values = MOI.get(
inner,
MOI.VariablePrimal(),
MOI.get(inner, MOI.ListOfVariableIndices()),
)
println("optimize!(mock) = MOI.Utilities.mock_optimize!(mock,")
println(JuMP.termination_status(model))
if JuMP.primal_status(model) != MOI.NO_SOLUTION
values = MOI.get(
inner,
MOI.VariablePrimal(),
MOI.get(inner, MOI.ListOfVariableIndices()),
)
print("(MOI.FEASIBLE_POINT, [")
for (i, v) in enumerate(values)
if i > 1
print(", ")
end
print_value(v, atol)
end
print("])")
else
print("tuple()")
end
println(",")
if JuMP.dual_status(model) != MOI.NO_SOLUTION
for (F, S) in MOI.get(inner, MOI.ListOfConstraintTypesPresent())
print("($F, $S) => [")
first = true
for ci in MOI.get(inner, MOI.ListOfConstraintIndices{F,S}())
if !first
print(", ")
end
first = false
print(MOI.get(inner, MOI.ConstraintDual(), ci))
end
println("],")
end
end
return println(")")
end
# Constraint dual values for inner bridged model
function inner_inspect(model, atol = 1e-4)
inner = _inner(backend(model))
@show MOI.get(inner, MOI.NumberOfVariables())
for (F, S) in MOI.get(inner, MOI.ListOfConstraintTypesPresent())
@show F
@show S
@show MOI.get(inner, MOI.NumberOfConstraints{F,S}())
if S <: MOI.AbstractVectorSet
for ci in MOI.get(inner, MOI.ListOfConstraintIndices{F,S}())
set = MOI.get(inner, MOI.ConstraintSet(), ci)
@show MOI.dimension(set)
end
end
end
end
function test_constraint_primal(cref, expected; atol, rtol)
# If there is a CachingOptimizer, it can use the fallback,
# otherwise, it should throw `ValueNotSupported`.
try
v = value(cref)
@test v isa SA.AlgebraElement
@test v ≈ expected atol = atol rtol = rtol
catch err
if !(err isa SumOfSquares.ValueNotSupported)
rethrow(err)
end
end
end