-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathvariable.jl
More file actions
203 lines (187 loc) · 5.6 KB
/
Copy pathvariable.jl
File metadata and controls
203 lines (187 loc) · 5.6 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
module TestVariable
using Test
import MutableArithmetics as MA
import StarAlgebras as SA
using MultivariatePolynomials
const MP = MultivariatePolynomials
import MultivariateBases as MB
using DynamicPolynomials
#using TypedPolynomials
using JuMP
using PolyJuMP
function test_variable_macro_with_Poly(var)
x = MP.similar_variable(var, Val{:x})
y = MP.similar_variable(var, Val{:y})
X = [1, y, x^2, x, y^2]
m = Model()
@variable(m, a)
var_poly = polynomial(a * x * y)
var_poly_x = polynomial(a * x)
aff_poly = polynomial((a + 1) * x * y)
aff_poly_x = polynomial((a + 1) * x)
@test_throws ErrorException @variable m p Poly(X) unknown_kw = 1
@test_throws ErrorException @variable m p == 1 Poly(X)
@test_throws ErrorException @variable m p Poly(X) start = 1
@test_throws ErrorException @variable m 0 <= p <= 1 Poly(X)
@test_throws ErrorException @variable m 0 >= p >= 1 Poly(X)
@test_throws ErrorException @variable m p <= 0 Poly(X)
@test_throws ErrorException @variable m p >= 1 Poly(X)
@test_throws ErrorException @variable m p == 1 Poly(X)
@test_throws ErrorException @variable m p >= 0 Poly(X)
@test_throws ErrorException @variable(m, p3[2:3] >= 0, Poly(X))
end
function _algebra_element_type(B, mono)
return MB.algebra_element_type(
Vector{JuMP.VariableRef},
MB.explicit_basis_type(MB.full_basis_type(B, typeof(mono))),
)
end
function _test_variable(
m,
p,
monos,
binary = false,
integer = false,
use_y = true,
B = MB.Monomial,
)
@test isa(
p,
_algebra_element_type(
B,
use_y ? first(monos) : first(variables(first(monos))),
),
)
@test MB.keys_as_monomials(SA.basis(p)) == monomial_vector(monos)
coeffs = SA.coeffs(p, MB.explicit_basis(p))
@test all(α -> JuMP.is_binary(α) == binary, coeffs)
@test all(α -> JuMP.is_integer(α) == integer, coeffs)
end
function test_MonomialBasis(var)
x = MP.similar_variable(var, Val{:x})
y = MP.similar_variable(var, Val{:y})
X = [1, y, x^2, x, y^2]
m = Model()
@variable m p1[1:3] Poly(X)
PT = _algebra_element_type(MB.Monomial, x * y)
@test isa(p1, Vector{PT})
_test_variable(m, p1[1], X)
@variable(m, p2, Poly(X), integer = true)
_test_variable(m, p2, X, false, true)
@variable(m, p3[2:3], Poly(X))
@test isa(p3, JuMP.Containers.DenseAxisArray{PT,1,Tuple{UnitRange{Int}}})
_test_variable(m, p3[2], X)
@variable(m, p4[i = 2:3, j = i:4], Poly(X), binary = true)
_test_variable(m, p4[2, 3], X, true)
X = [x^2, y^2]
@variable m p5[1:3] Poly(X)
@test isa(p5, Vector{PT})
_test_variable(m, p5[1], X)
@variable(m, p6, Poly(MB.SubBasis{MB.Monomial}(X)), integer = true)
return _test_variable(m, p6, X, false, true)
end
function test_ScaledMonomialBasis(var)
x = MP.similar_variable(var, Val{:x})
m = Model()
@variable(m, p1, Poly(MB.SubBasis{MB.ScaledMonomial}([1, x, x^2])), Int)
return _test_variable(
m,
p1,
monomial_vector([1, x, x^2]),
false,
true,
false,
MB.ScaledMonomial,
)
end
# TODO recover with other basis
#function test_FixedPolynomialBasis(var)
# x = MP.similar_variable(var, Val{:x})
# y = MP.similar_variable(var, Val{:y})
# m = Model()
# @variable(m, p1, Poly(MB.FixedPolynomialBasis([1 - x^2, x^2 + 2])), Bin)
# _test_variable(m, p1, monomial_vector([x^2, 1]), true, false, false, false)
# @variable(m, p2[1:2], Poly(MB.FixedPolynomialBasis([1 - x^2, x^2 + 2])))
# _test_variable(
# m,
# p2[1],
# monomial_vector([x^2, 1]),
# false,
# false,
# false,
# false,
# )
# # Elements of the basis have type monomial
# @variable(m, p3[2:3], Poly(MB.FixedPolynomialBasis([x, x^2])))
# _test_variable(
# m,
# p3[2],
# monomial_vector([x^2, x]),
# false,
# false,
# false,
# false,
# )
# # Elements of the basis have type term
# @variable(m, p4[1:2], Poly(MB.FixedPolynomialBasis([1, x, x^2])), Int)
# _test_variable(
# m,
# p4[1],
# monomial_vector([x^2, x, 1]),
# false,
# true,
# false,
# false,
# )
# # Elements of the basis have type variable
# @variable(
# m,
# p5[-1:1],
# Poly(MB.FixedPolynomialBasis([x, y])),
# integer = true
# )
# _test_variable(m, p5[0], monomial_vector([x, y]), false, true, false)
# @variable(m, p6[-1:1], Poly(MB.FixedPolynomialBasis([x])), integer = true)
# return _test_variable(
# m,
# p6[0],
# monomial_vector([x]),
# false,
# true,
# true,
# false,
# )
#end
function test_value_function(var)
x = MP.similar_variable(var, Val{:x})
y = MP.similar_variable(var, Val{:y})
m = Model()
@variable m α
@variable m β
p = α * x * y + β * x^2
JuMP.fix(α, 2)
JuMP.fix(β, 3)
expected = 2x * y + 3x^2
@test_broken JuMP.value(p) == expected
@test JuMP.value(fix_value, p) == expected
a = MB.algebra_element(p)
exp_a = MB.algebra_element(expected)
@test_broken JuMP.value(a) == exp_a
@test JuMP.value(fix_value, a) == exp_a
end
function runtests(var)
for name in names(@__MODULE__; all = true)
if startswith("$name", "test_")
@testset "$(name) $(typeof(var))" begin
getfield(@__MODULE__, name)(var)
end
end
end
end
end
import DynamicPolynomials
DynamicPolynomials.@polyvar(x)
TestVariable.runtests(x)
import TypedPolynomials
TypedPolynomials.@polyvar(y)
TestVariable.runtests(y)