Skip to content

Commit 58df878

Browse files
authored
Merge pull request #1089 from JuliaOpt/bl/quad_is_canon_test
Add tests for is_canonical on quadratic functions
2 parents f07cb1c + 8e8ac62 commit 58df878

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

test/Utilities/functions.jl

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,14 @@ end
551551
(MOI.constant(f1) MOI.constant(f2)) &&
552552
(MOI.coefficient.(f1.terms) MOI.coefficient.(f2.terms)))
553553
end
554-
function test_canonicalization(f::T, expected::T) where {T <: Union{MOI.ScalarAffineFunction, MOI.VectorAffineFunction}}
554+
function isapprox_ordered(f1::T, f2::T) where {T <: Union{MOI.ScalarQuadraticFunction, MOI.VectorQuadraticFunction}}
555+
((MOI.term_indices.(f1.affine_terms) == MOI.term_indices.(f2.affine_terms)) &&
556+
(MOI.term_indices.(f1.quadratic_terms) == MOI.term_indices.(f2.quadratic_terms)) &&
557+
(MOI.constant(f1) MOI.constant(f2)) &&
558+
(MOI.coefficient.(f1.affine_terms) MOI.coefficient.(f2.affine_terms)) &&
559+
(MOI.coefficient.(f1.quadratic_terms) MOI.coefficient.(f2.quadratic_terms)))
560+
end
561+
function test_canonicalization(f::T, expected::T) where {T <: Union{MOI.ScalarAffineFunction, MOI.VectorAffineFunction, MOI.ScalarQuadraticFunction, MOI.VectorQuadraticFunction}}
555562
@test MOIU.is_canonical(expected)
556563
g = @inferred(MOIU.canonical(f))
557564
@test isapprox_ordered(g, expected)
@@ -665,6 +672,56 @@ end
665672
MOI.VectorAffineFunction(MOI.VectorAffineTerm.([2], MOI.ScalarAffineTerm.([1.0], MOI.VariableIndex.([1]))), [4.0, 5.0, 6.0]),
666673
)
667674
end
675+
@testset "ScalarQuadratic" begin
676+
x = MOI.SingleVariable(MOI.VariableIndex(1))
677+
y = MOI.SingleVariable(MOI.VariableIndex(2))
678+
@test MOIU.is_canonical(convert(MOI.ScalarQuadraticFunction{Float64}, 1.0))
679+
@test !MOIU.is_canonical(1.0x*y + 2.0x*x + 2.0)
680+
@test !MOIU.is_canonical(1.0x*x + 0.0x*y + 2.0)
681+
682+
test_canonicalization(
683+
convert(MOI.ScalarQuadraticFunction{Float64}, 1.5),
684+
convert(MOI.ScalarQuadraticFunction{Float64}, 1.5)
685+
)
686+
test_canonicalization(
687+
0.0x*y - 2.0,
688+
convert(MOI.ScalarQuadraticFunction{Float64}, -2.0)
689+
)
690+
test_canonicalization(
691+
3.0x*y + 4.0x*x + 0.0,
692+
4.0x*x + 3.0x*y + 0.0
693+
)
694+
test_canonicalization(
695+
1.0x*y + 0.1x*y + 5.0,
696+
1.1x*y + 5.0
697+
)
698+
end
699+
@testset "VectorQuadratic" begin
700+
x = MOI.SingleVariable(MOI.VariableIndex(1))
701+
y = MOI.SingleVariable(MOI.VariableIndex(2))
702+
@test MOIU.is_canonical(MOIU.operate(vcat, Float64, convert(MOI.ScalarQuadraticFunction{Float64}, 1.0)))
703+
@test !MOIU.is_canonical(MOIU.operate(vcat, Float64, 1.0x*y + 2.0x*x + 2.0))
704+
@test !MOIU.is_canonical(MOIU.operate(vcat, Float64, 1.0x*x + 0.0x*y + 2.0))
705+
706+
test_canonicalization(
707+
MOIU.operate(
708+
vcat,
709+
Float64,
710+
1.5,
711+
0.0x*y - 2.0,
712+
3.0x*y + 4.0x*x + 0.0,
713+
1.0x*y + 0.1x*y + 5.0
714+
),
715+
MOIU.operate(
716+
vcat,
717+
Float64,
718+
1.5,
719+
-2.0,
720+
4.0x*x + 3.0x*y + 0.0,
721+
1.1x*y + 5.0
722+
)
723+
)
724+
end
668725
end
669726

670727
@testset "Vector operate tests" begin

0 commit comments

Comments
 (0)