-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathFunctionConversionBridge.jl
More file actions
69 lines (57 loc) · 1.64 KB
/
FunctionConversionBridge.jl
File metadata and controls
69 lines (57 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Copyright (c) 2017: Miles Lubin and contributors
# Copyright (c) 2017: Google Inc.
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
module TestObjectiveConversion
using Test
import MathOptInterface as MOI
function runtests()
for name in names(@__MODULE__; all = true)
if startswith("$(name)", "test_")
@testset "$(name)" begin
getfield(@__MODULE__, name)()
end
end
end
return
end
include("../utilities.jl")
struct VariableDifference <: MOI.AbstractScalarFunction
x::MOI.VariableIndex
y::MOI.VariableIndex
end
function MOI.Utilities.is_coefficient_type(
::Type{VariableDifference},
::Type{T},
) where {T}
return true
end
function MOI.Bridges.Constraint.conversion_cost(
::Type{<:MOI.ScalarAffineFunction},
::Type{VariableDifference},
)
return 1.0
end
function MOI.convert(
::Type{MOI.ScalarAffineFunction{T}},
f::VariableDifference,
) where {T}
return one(T) * f.x - one(T) * f.y
end
function test_variable_difference(T = Float64)
F = MOI.ScalarAffineFunction{T}
B = MOI.Bridges.Objective.FunctionConversionBridge{T,F}
inner = MOI.Utilities.Model{T}()
model = MOI.Bridges.Objective.SingleBridgeOptimizer{B}(inner)
x = MOI.add_variable(model)
y = MOI.add_variable(model)
g = VariableDifference(x, y)
G = typeof(g)
MOI.set(model, MOI.ObjectiveFunction{G}(), g)
f = one(T) * x - one(T) * y
@test MOI.get(inner, MOI.ObjectiveFunction{F}()) ≈ f
return
end
end # module
TestObjectiveConversion.runtests()