Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,15 @@ function Base.isapprox(
return f == g
end

# For affine and quadratic functions, terms are compressed in a dictionary using `_dicts` and then the dictionaries are compared with `dict_compare`
# For affine and quadratic functions, terms are compressed in a dictionary using
# `_dicts` and then the dictionaries are compared with `dict_compare`
function dict_compare(d1::Dict, d2::Dict{<:Any,T}, compare::Function) where {T}
return all(kv -> compare(kv.second, Base.get(d2, kv.first, zero(T))), d1)
for key in union(keys(d1), keys(d2))
if !compare(Base.get(d1, key, zero(T)), Base.get(d2, key, zero(T)))
return false
end
end
return true
end

# Build a dictionary where the duplicate keys are summed
Expand Down
2 changes: 1 addition & 1 deletion test/Bridges/Variable/zeros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ function test_zeros()
s = """
variables: x
x >= 0.0
con1: x + 0.0 == 0.0
con1: 0.0 == 0.0
con2: x + 0.0 >= 1.0
minobjective: x
"""
Expand Down
4 changes: 2 additions & 2 deletions test/FileFormats/SDPA/SDPA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ const _EXAMPLE_MODELS = [
"""
variables: x, y
minobjective: 10x + 20y
c1: [x + -1, 0, x + -2] in PositiveSemidefiniteConeTriangle(2)
c1: [x + -1, 0, x + y + -2] in PositiveSemidefiniteConeTriangle(2)
c2: [5y + -3, 2y, 6y + -4] in PositiveSemidefiniteConeTriangle(2)
""",
),
Expand Down Expand Up @@ -298,7 +298,7 @@ end
function runtests()
for name in names(@__MODULE__, all = true)
if startswith("$(name)", "test_")
@testset "name" begin
@testset "$name" begin
getfield(@__MODULE__, name)()
end
end
Expand Down
44 changes: 44 additions & 0 deletions test/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function test_isapprox_SingleVariable()
y = MOI.VariableIndex(2)
@test MOI.SingleVariable(x) == MOI.SingleVariable(x)
@test MOI.SingleVariable(x) != MOI.SingleVariable(y)
return
end

function test_isapprox_VectorOfVariables()
Expand All @@ -119,6 +120,7 @@ function test_isapprox_VectorOfVariables()
@test MOI.VectorOfVariables([y, x]) != MOI.VectorOfVariables([x, y])
@test MOI.VectorOfVariables([x, x]) != MOI.VectorOfVariables([x])
@test MOI.VectorOfVariables([x]) != MOI.VectorOfVariables([y])
return
end

function test_isapprox_ScalarAffineFunction()
Expand All @@ -137,8 +139,11 @@ function test_isapprox_ScalarAffineFunction()
f = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([2, 4], [x, y]), 6)
g = deepcopy(f)
@test g ≈ f
@test f ≈ g
f.terms[2] = MOI.ScalarAffineTerm(3, y)
@test !(g ≈ f)
@test !(f ≈ g)
return
end

function test_isapprox_VectorAffineFunction()
Expand All @@ -155,8 +160,11 @@ function test_isapprox_VectorAffineFunction()
@test f ≈ g
f.terms[3] = MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(9, y))
@test !(f ≈ g)
@test !(g ≈ f)
push!(f.terms, MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(-6, y)))
@test f ≈ g
@test g ≈ f
return
end

function test_isapprox_ScalarQuadraticFunction()
Expand All @@ -175,8 +183,11 @@ function test_isapprox_ScalarQuadraticFunction()
g = deepcopy(f)
push!(f.quadratic_terms, MOI.ScalarQuadraticTerm(2, y, x))
@test !(f ≈ g)
@test !(g ≈ f)
push!(f.quadratic_terms, MOI.ScalarQuadraticTerm(-2, y, x))
@test f ≈ g
@test g ≈ f
return
end

function test_isapprox_VectorQuadraticFunction()
Expand All @@ -196,14 +207,47 @@ function test_isapprox_VectorQuadraticFunction()
)
g = deepcopy(f)
@test f ≈ g
@test g ≈ f
f.affine_terms[1] = MOI.VectorAffineTerm(3, MOI.ScalarAffineTerm(4, x))
@test !(f ≈ g)
@test !(g ≈ f)
push!(g.affine_terms, MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(-3, x)))
push!(g.affine_terms, MOI.VectorAffineTerm(3, MOI.ScalarAffineTerm(4, x)))
@test f ≈ g
@test g ≈ f
f.quadratic_terms[1] =
MOI.VectorQuadraticTerm(3, MOI.ScalarQuadraticTerm(1, x, x))
@test !(f ≈ g)
@test !(g ≈ f)
return
end

function test_isapprox_issue_1483()
x = MOI.ScalarQuadraticFunction(
MOI.ScalarAffineTerm{Float16}[],
MOI.ScalarQuadraticTerm{Float16}[],
Float16(0.0),
)
y = MOI.ScalarQuadraticFunction(
MOI.ScalarAffineTerm{Float16}[MOI.ScalarAffineTerm(
Float16(1.0),
MOI.VariableIndex(1234),
)],
MOI.ScalarQuadraticTerm{Float16}[],
Float16(0.0),
)
z = MOI.ScalarQuadraticFunction(
MOI.ScalarAffineTerm{Float16}[],
MOI.ScalarQuadraticTerm{Float16}[],
Float16(0.0),
)
@test !(x ≈ y)
@test !(y ≈ x)
@test x ≈ z
@test z ≈ x
@test !(y ≈ z)
@test !(z ≈ y)
return
end

function runtests()
Expand Down