From 20e47a59ac574a86920b131199e54b55a3be1eb3 Mon Sep 17 00:00:00 2001 From: odow Date: Wed, 24 Jan 2024 12:22:11 +1300 Subject: [PATCH] [FileFormats.MOF] fix affine and quadratic nodes in ScalarNonlinearFunction --- src/FileFormats/MOF/write.jl | 14 ++++++++ test/FileFormats/MOF/MOF.jl | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/src/FileFormats/MOF/write.jl b/src/FileFormats/MOF/write.jl index 50b18f50e3..99b8a16738 100644 --- a/src/FileFormats/MOF/write.jl +++ b/src/FileFormats/MOF/write.jl @@ -258,6 +258,20 @@ function _convert_nonlinear_to_mof( return name_map[variable] end +function _convert_nonlinear_to_mof( + ::Type{T}, + f::MOI.AbstractScalarFunction, + node_list::Vector{Any}, + name_map::Dict{MOI.VariableIndex,String}, +) where {T<:Object} + return _convert_nonlinear_to_mof( + T, + convert(MOI.ScalarNonlinearFunction, f), + node_list, + name_map, + ) +end + function _convert_nonlinear_to_mof( ::Type{T}, value::Real, diff --git a/test/FileFormats/MOF/MOF.jl b/test/FileFormats/MOF/MOF.jl index e0bb62d467..3905eb7c32 100644 --- a/test/FileFormats/MOF/MOF.jl +++ b/test/FileFormats/MOF/MOF.jl @@ -1511,6 +1511,68 @@ function test_nonlinear_variable_real_nodes() return end +function test_mof_scalaraffinefunction() + x = MOI.VariableIndex(1) + f = 1.0 * x + 2.0 + g = MOI.ScalarNonlinearFunction(:log, Any[f]) + name_map = Dict(x => "x") + object = MOF.moi_to_object(g, name_map) + object_dest = MOF.OrderedObject( + "type" => "ScalarNonlinearFunction", + "root" => MOF.OrderedObject("type" => "node", "index" => 3), + "node_list" => Any[ + MOF.OrderedObject("type" => "*", "args" => [1.0, "x"]), + MOF.OrderedObject( + "type" => "+", + "args" => [ + MOF.OrderedObject("type" => "node", "index" => 1), + 2.0, + ], + ), + MOF.OrderedObject( + "type" => "log", + "args" => Any[MOF.OrderedObject( + "type" => "node", + "index" => 2, + )], + ), + ], + ) + @test object == object_dest + return +end + +function test_mof_scalarquadraticfunction() + x = MOI.VariableIndex(1) + f = 1.0 * x * x + 2.0 + g = MOI.ScalarNonlinearFunction(:log, Any[f]) + name_map = Dict(x => "x") + object = MOF.moi_to_object(g, name_map) + object_dest = MOF.OrderedObject( + "type" => "ScalarNonlinearFunction", + "root" => MOF.OrderedObject("type" => "node", "index" => 3), + "node_list" => Any[ + MOF.OrderedObject("type" => "*", "args" => [1.0, "x", "x"]), + MOF.OrderedObject( + "type" => "+", + "args" => [ + MOF.OrderedObject("type" => "node", "index" => 1), + 2.0, + ], + ), + MOF.OrderedObject( + "type" => "log", + "args" => Any[MOF.OrderedObject( + "type" => "node", + "index" => 2, + )], + ), + ], + ) + @test object == object_dest + return +end + end TestMOF.runtests()