Skip to content

Commit 145e7bc

Browse files
authored
[FileFormats.MOF] fix affine and quadratic nodes in ScalarNonlinearFunction (#2409)
1 parent 474ffe8 commit 145e7bc

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/FileFormats/MOF/write.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,20 @@ function _convert_nonlinear_to_mof(
258258
return name_map[variable]
259259
end
260260

261+
function _convert_nonlinear_to_mof(
262+
::Type{T},
263+
f::MOI.AbstractScalarFunction,
264+
node_list::Vector{Any},
265+
name_map::Dict{MOI.VariableIndex,String},
266+
) where {T<:Object}
267+
return _convert_nonlinear_to_mof(
268+
T,
269+
convert(MOI.ScalarNonlinearFunction, f),
270+
node_list,
271+
name_map,
272+
)
273+
end
274+
261275
function _convert_nonlinear_to_mof(
262276
::Type{T},
263277
value::Real,

test/FileFormats/MOF/MOF.jl

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,68 @@ function test_nonlinear_variable_real_nodes()
15111511
return
15121512
end
15131513

1514+
function test_mof_scalaraffinefunction()
1515+
x = MOI.VariableIndex(1)
1516+
f = 1.0 * x + 2.0
1517+
g = MOI.ScalarNonlinearFunction(:log, Any[f])
1518+
name_map = Dict(x => "x")
1519+
object = MOF.moi_to_object(g, name_map)
1520+
object_dest = MOF.OrderedObject(
1521+
"type" => "ScalarNonlinearFunction",
1522+
"root" => MOF.OrderedObject("type" => "node", "index" => 3),
1523+
"node_list" => Any[
1524+
MOF.OrderedObject("type" => "*", "args" => [1.0, "x"]),
1525+
MOF.OrderedObject(
1526+
"type" => "+",
1527+
"args" => [
1528+
MOF.OrderedObject("type" => "node", "index" => 1),
1529+
2.0,
1530+
],
1531+
),
1532+
MOF.OrderedObject(
1533+
"type" => "log",
1534+
"args" => Any[MOF.OrderedObject(
1535+
"type" => "node",
1536+
"index" => 2,
1537+
)],
1538+
),
1539+
],
1540+
)
1541+
@test object == object_dest
1542+
return
1543+
end
1544+
1545+
function test_mof_scalarquadraticfunction()
1546+
x = MOI.VariableIndex(1)
1547+
f = 1.0 * x * x + 2.0
1548+
g = MOI.ScalarNonlinearFunction(:log, Any[f])
1549+
name_map = Dict(x => "x")
1550+
object = MOF.moi_to_object(g, name_map)
1551+
object_dest = MOF.OrderedObject(
1552+
"type" => "ScalarNonlinearFunction",
1553+
"root" => MOF.OrderedObject("type" => "node", "index" => 3),
1554+
"node_list" => Any[
1555+
MOF.OrderedObject("type" => "*", "args" => [1.0, "x", "x"]),
1556+
MOF.OrderedObject(
1557+
"type" => "+",
1558+
"args" => [
1559+
MOF.OrderedObject("type" => "node", "index" => 1),
1560+
2.0,
1561+
],
1562+
),
1563+
MOF.OrderedObject(
1564+
"type" => "log",
1565+
"args" => Any[MOF.OrderedObject(
1566+
"type" => "node",
1567+
"index" => 2,
1568+
)],
1569+
),
1570+
],
1571+
)
1572+
@test object == object_dest
1573+
return
1574+
end
1575+
15141576
end
15151577

15161578
TestMOF.runtests()

0 commit comments

Comments
 (0)