@@ -8,7 +8,7 @@ module TestNonlinearRead
88
99using Test
1010import MathOptInterface as MOI
11- const NL = MOI . FileFormats. NL
11+ import MathOptInterface . FileFormats: NL
1212
1313function runtests ()
1414 for name in names (@__MODULE__ ; all = true )
@@ -720,10 +720,10 @@ function test_hs071_free_constraint_nlexpr()
720720 open (joinpath (@__DIR__ , " data" , " hs071_free_constraint.nl" ), " r" ) do io
721721 return read! (io, model)
722722 end
723- @test MOI. get (model, MOI. ListOfConstraintTypesPresent ()) == [
724- (MOI . ScalarNonlinearFunction, MOI . GreaterThan{Float64}),
725- (MOI. ScalarNonlinearFunction , MOI. Interval {Float64}),
726- ]
723+ types = MOI. get (model, MOI. ListOfConstraintTypesPresent ())
724+ @test length (types) == 2
725+ @test (MOI. ScalarAffineFunction{Float64} , MOI. GreaterThan {Float64}) in types
726+ @test (MOI . ScalarNonlinearFunction, MOI . Interval{Float64}) in types
727727 for (F, S) in MOI. get (model, MOI. ListOfConstraintTypesPresent ())
728728 @test MOI. get (model, MOI. NumberOfConstraints {F,S} ()) == 1
729729 end
@@ -783,6 +783,36 @@ function test_mac_minlp()
783783 return
784784end
785785
786+ function test_nl_read_scalar_affine_function ()
787+ for obj_fn in (x -> x, x -> 1.0 * x, x -> 2.0 * x + 3.0 )
788+ src = MOI. Utilities. Model {Float64} ()
789+ x = MOI. add_variable (src)
790+ MOI. set (src, MOI. ObjectiveSense (), MOI. MIN_SENSE)
791+ f = obj_fn (x)
792+ MOI. set (src, MOI. ObjectiveFunction {typeof(f)} (), f)
793+ MOI. add_constraint (src, f, MOI. LessThan (1.0 ))
794+ dest = MOI. FileFormats. NL. Model ()
795+ MOI. copy_to (dest, src)
796+ io = IOBuffer ()
797+ write (io, dest)
798+ input = MOI. FileFormats. NL. Model (; use_nlp_block = false )
799+ seekstart (io)
800+ read! (io, input)
801+ model = MOI. Utilities. Model {Float64} ()
802+ MOI. copy_to (model, input)
803+ y = only (MOI. get (model, MOI. ListOfVariableIndices ()))
804+ g = MOI. Utilities. substitute_variables (
805+ _ -> y,
806+ convert (MOI. ScalarAffineFunction{Float64}, f),
807+ )
808+ obj = MOI. get (model, MOI. ObjectiveFunction {typeof(g)} ())
809+ @test ≈ (obj, g)
810+ @test MOI. get (model, MOI. ListOfConstraintTypesPresent ()) ==
811+ [(typeof (f), MOI. LessThan{Float64})]
812+ end
813+ return
814+ end
815+
786816end
787817
788818TestNonlinearRead. runtests ()
0 commit comments