Skip to content

Define convert #504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions src/Test/UnitTests/objectives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ function get_objective_function(model::MOI.ModelLike, config::TestConfig)
end
obj_fun = MOI.get(model, obj_attr)
@test obj_fun ≈ expected_obj_fun
quad_obj_attr = MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{Float64}}()
quad_obj_fun = MOI.get(model, quad_obj_attr)
@test convert(MOI.ScalarAffineFunction{Float64}, quad_obj_fun) ≈ expected_obj_fun
end
unittests["get_objective_function"] = get_objective_function

Expand Down
9 changes: 0 additions & 9 deletions src/Utilities/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,6 @@ end
constant(f::Union{SAF, SQF}) = [f.constant]
constant(f::Union{VAF, VQF}) = f.constants

# Define conversion SingleVariable -> ScalarAffineFunction and VectorOfVariables -> VectorAffineFunction{T}
function SAF{T}(f::SVF) where T
SAF([MOI.ScalarAffineTerm(one(T), f.variable)], zero(T))
end
function VAF{T}(f::VVF) where T
n = length(f.variables)
return VAF(map(i -> MOI.VectorAffineTerm(i, MOI.ScalarAffineTerm(one(T), f.variables[i])), 1:n), zeros(T, n))
end

# Implements iterator interface
"""
scalar_type(F::Type{<:MOI.AbstractVectorFunction})
Expand Down
9 changes: 1 addition & 8 deletions src/Utilities/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,7 @@ function MOI.get(model::AbstractModel, ::MOI.ObjectiveFunctionType)
return MOI.typeof(model.objective)
end
function MOI.get(model::AbstractModel, ::MOI.ObjectiveFunction{T})::T where T
if typeof(model.objective) != T
if VERSION >= v"0.7-"
throw(InexactError(:get, T, model.objective))
else
throw(InexactError())
end
end
model.objective
return model.objective
end
MOI.supports(model::AbstractModel, ::MOI.ObjectiveFunction) = true
function MOI.set(model::AbstractModel, ::MOI.ObjectiveFunction, f::MOI.AbstractFunction)
Expand Down
58 changes: 57 additions & 1 deletion src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ function MultirowChange(variable::VariableIndex, new_coefficients::Vector{Tuple{
MultirowChange(variable, [(convert(Int64, i), j) for (i,j) in new_coefficients])
end

# Implementation of comparison for MOI functions
# Implementation of comparison for functions
Base.:(==)(f::VectorOfVariables, g::VectorOfVariables) = f.variables == g.variables

Base.isapprox(f::Union{SingleVariable, VectorOfVariables}, g::Union{SingleVariable, VectorOfVariables}; kwargs...) = f == g
Expand Down Expand Up @@ -369,3 +369,59 @@ Return a new quadratic function with a shallow copy of the terms and constant(s)
from `func`.
"""
Base.copy(func::F) where {F <: Union{ScalarQuadraticFunction, VectorQuadraticFunction}} = F(copy(func.affine_terms), copy(func.quadratic_terms), copy(_constant(func)))

# Define shortcuts for
# SingleVariable -> ScalarAffineFunction
function ScalarAffineFunction{T}(f::SingleVariable) where T
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it still Julia convention (post-1.0) to add constructor methods for conversions like this?

Copy link
Member Author

@blegat blegat Aug 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, since constructors are the low level functions for building objects, convert should redirect to constructors not the opposite. See https://discourse.julialang.org/t/recommended-style-for-conversion-vs-constructors-in-v0-7/11561/5

ScalarAffineFunction([ScalarAffineTerm(one(T), f.variable)], zero(T))
end
# VectorOfVariables -> VectorAffineFunction
function VectorAffineFunction{T}(f::VectorOfVariables) where T
n = length(f.variables)
return VectorAffineFunction(map(i -> VectorAffineTerm(i, ScalarAffineTerm(one(T), f.variables[i])), 1:n), zeros(T, n))
end

# Conversion between scalar functions
# Conversion to SingleVariable
function Base.convert(::Type{SingleVariable}, f::ScalarAffineFunction)
if !iszero(f.constant) || !isone(length(f.terms)) || !isone(f.terms[1].coefficient)
if VERSION >= v"0.7-"
throw(InexactError(:convert, SingleVariable, f))
else
throw(InexactError())
end
end
return SingleVariable(f.terms[1].variable_index)
end
function Base.convert(::Type{SingleVariable},
f::ScalarQuadraticFunction{T}) where T
return convert(SingleVariable, convert(ScalarAffineFunction{T}, f))
end

# Conversion to ScalarAffineFunction
function Base.convert(::Type{ScalarAffineFunction{T}},
f::SingleVariable) where T
return ScalarAffineFunction{T}(f)
end
function Base.convert(::Type{ScalarAffineFunction{T}},
f::ScalarQuadraticFunction{T}) where T
if !Base.isempty(f.quadratic_terms)
if VERSION >= v"0.7-"
throw(InexactError(:convert, ScalarAffineFunction{T}, f))
else
throw(InexactError())
end
end
return ScalarAffineFunction{T}(f.affine_terms, f.constant)
end

# Conversion to ScalarQuadraticFunction
function Base.convert(::Type{ScalarQuadraticFunction{T}},
f::SingleVariable) where T
convert(ScalarQuadraticFunction{T}, convert(ScalarAffineFunction{T}, f))
end
function Base.convert(::Type{ScalarQuadraticFunction{T}},
f::ScalarAffineFunction{T}) where T
return ScalarQuadraticFunction{T}(f.terms, ScalarQuadraticTerm{T}[],
f.constant)
end
16 changes: 16 additions & 0 deletions test/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@
end
f = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([2, 4], [x, y]),
5)
@testset "convert" begin
@test_throws InexactError convert(MOI.SingleVariable, f)
quad_f = MOI.ScalarQuadraticFunction(f.terms,
MOI.ScalarQuadraticTerm{Int}[],
f.constant)
@test convert(MOI.ScalarQuadraticFunction{Int}, f) ≈ quad_f
g = convert(MOI.ScalarAffineFunction{Float64}, MOI.SingleVariable(x))
@test convert(MOI.SingleVariable, g) == MOI.SingleVariable(x)
end
@testset "operate" begin
f = MOIU.canonical(MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([1, 3, 1, 2, -3, 2],
[w, y, w, x, x, z]), 2) +
Expand Down Expand Up @@ -253,6 +262,13 @@
@test MOIU.isapprox_zero(g, 1e-5)
@test !MOIU.isapprox_zero(g, 1e-7)
end
@testset "convert" begin
@test_throws InexactError convert(MOI.SingleVariable, f)
@test_throws InexactError convert(MOI.ScalarAffineFunction{Int},
f)
g = convert(MOI.ScalarQuadraticFunction{Float64}, fx)
@test convert(MOI.SingleVariable, g) == fx
end
@testset "operate" begin
@test f ≈ 7 + (fx + 2fy) * (1fx + fy) + 3fx
@test f ≈ 7 + MOIU.operate(*, Int, fx, fx) + 3fx * (fy + 1) + 2fy * fy
Expand Down