|
551 | 551 | (MOI.constant(f1) ≈ MOI.constant(f2)) &&
|
552 | 552 | (MOI.coefficient.(f1.terms) ≈ MOI.coefficient.(f2.terms)))
|
553 | 553 | 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}} |
555 | 562 | @test MOIU.is_canonical(expected)
|
556 | 563 | g = @inferred(MOIU.canonical(f))
|
557 | 564 | @test isapprox_ordered(g, expected)
|
|
665 | 672 | MOI.VectorAffineFunction(MOI.VectorAffineTerm.([2], MOI.ScalarAffineTerm.([1.0], MOI.VariableIndex.([1]))), [4.0, 5.0, 6.0]),
|
666 | 673 | )
|
667 | 674 | 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 |
668 | 725 | end
|
669 | 726 |
|
670 | 727 | @testset "Vector operate tests" begin
|
|
0 commit comments