Skip to content

Commit 6906080

Browse files
authored
Fix nesting structs in generators. (#176)
* nesting * fixed nesting * pretty print nodes * add tests * remove string / show
1 parent f85560f commit 6906080

File tree

5 files changed

+31
-26
lines changed

5 files changed

+31
-26
lines changed

src/graph.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ end
117117
@inline Base.getindex(n::ParSource, i) = ParIndexed(n, i)
118118
@inline Base.indexed_iterate(n::P, idx, start = 1) where P <: Union{ParSource, ParIndexed} = (ParIndexed(n, idx), idx + 1)
119119

120+
@inline Base.getproperty(v::ParIndexed{I, n}, s::Symbol) where {I, n} = ParIndexed(v, s)
121+
@inline Base.getindex(v::ParIndexed{I, n}, i) where {I, n} = ParIndexed(v, i)
122+
@inline Base.indexed_iterate(v::ParIndexed{I, n}, idx, start = 1) where {I, n} = (ParIndexed(v, idx), idx + 1)
123+
120124

121125
@inline Base.getindex(n::VarSource, i) = Var(i)
122126
@inline Base.getindex(::ParameterSource, i) = ParameterNode(i)
@@ -140,7 +144,7 @@ struct Identity end
140144
@inline (v::ParameterNode{I})(::Identity, x, ::Nothing) where {I<:AbstractNode} = NaN
141145

142146
@inline (v::ParSource)(i, x, θ) = i
143-
@inline (v::ParIndexed{I,n})(i, x, θ) where {I,n} = @inbounds getfield(v.inner(i, x, θ), n)
147+
@inline (v::ParIndexed{I,n})(i, x, θ) where {I,n} = @inbounds getfield(getfield(v, :inner)(i, x, θ), n)
144148

145149
(v::ParIndexed)(i::Identity, x, θ) = NaN # despecialized
146150
(v::ParSource)(i::Identity, x, θ) = NaN # despecialized

test/NLPTest/NLPTest.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ using JuMP, PowerModels, MadNLP, Percival
88
import ..BACKENDS
99

1010
const NLP_TEST_ARGUMENTS = [
11+
("luksan_struct", 3),
12+
("luksan_struct", 20),
1113
("luksan_vlcek", 3),
1214
("luksan_vlcek", 20),
1315
("ac_power", "pglib_opf_case3_lmbd.m"),
1416
("ac_power", "pglib_opf_case14_ieee.m"),
15-
("struct_ac_power", "pglib_opf_case3_lmbd.m"),
16-
("struct_ac_power", "pglib_opf_case14_ieee.m"),
1717
]
1818

1919
const SOLVERS = [
@@ -36,8 +36,8 @@ end
3636

3737
include("luksan.jl")
3838
include("power.jl")
39+
include("luksan_struct.jl")
3940
include("parameter_test.jl")
40-
include("power_struct.jl")
4141

4242
function test_nlp(m1, m2; full = false)
4343
@testset "NLP meta tests" begin

test/NLPTest/luksan_struct.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
struct I2
2+
i::Tuple{Int, Int}
3+
end
4+
5+
struct I1
6+
i::I2
7+
end
8+
9+
function _exa_luksan_struct_model(backend, N; M = 1)
10+
11+
c = ExaCore(backend = backend)
12+
data = [I1(I2((i,j))) for i = 1:N, j = 1:M]
13+
x = variable(c, N, M; start = [luksan_vlcek_x0(i.i.i[1]) for i in data])
14+
s = constraint(c, luksan_vlcek_con1(x, i.i.i[1], i.i.i[2]) for i in data[1:end-2,:])
15+
constraint!(c, s, (i.i.i[1], i.i.i[2]) => luksan_vlcek_con2(x, i.i.i[1], i.i.i[2]) for i in data[1:end-2,:])
16+
objective(c, luksan_vlcek_obj(x, i, j) for i = 2:N, j = 1:M)
17+
18+
return ExaModel(c; prod = true), (x,), (s,)
19+
end
20+
21+
_jump_luksan_struct_model(backend, N; M = 1) = _jump_luksan_vlcek_model(backend, N; M)

test/NLPTest/power_struct.jl

Lines changed: 0 additions & 20 deletions
This file was deleted.

test/Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ NLPModels = "a4795742-8479-5a88-8948-cc11e1c8c1a6"
1111
NLPModelsIpopt = "f4238b75-b362-5c4c-b852-0801c9a21d71"
1212
NLPModelsJuMP = "792afdf1-32c1-5681-94e0-d7bf7a5df49e"
1313
NLPModelsTest = "7998695d-6960-4d3a-85c4-e1bceb8cd856"
14+
OpenCL = "08131aa3-fb12-5dee-8b74-c09406e224a2"
1415
Percival = "01435c0c-c90d-11e9-3788-63660f8fbccc"
1516
PowerModels = "c36e90e8-916a-50a6-bd94-075b64ef4655"
1617
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1718
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1819
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
1920
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2021
oneAPI = "8f75cd03-7ff8-4ecb-9b8f-daf728133b1b"
21-
OpenCL = "08131aa3-fb12-5dee-8b74-c09406e224a2"
22-
pocl_jll = "627d6b7a-bbe6-5189-83e7-98cc0a5aeadd"
22+
pocl_jll = "627d6b7a-bbe6-5189-83e7-98cc0a5aeadd"

0 commit comments

Comments
 (0)