forked from jump-dev/MathOptInterface.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToScalarNonlinearBridge.jl
More file actions
160 lines (147 loc) · 4.78 KB
/
ToScalarNonlinearBridge.jl
File metadata and controls
160 lines (147 loc) · 4.78 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# 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 TestObjectiveToScalarNonlinear
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")
function test_solve_singlevariable_obj()
mock = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Objective.ToScalarNonlinear{Float64}(mock)
MOI.Utilities.set_mock_optimize!(
mock,
(mock::MOI.Utilities.MockOptimizer) ->
MOI.Utilities.mock_optimize!(mock, [1.0], MOI.FEASIBLE_POINT),
)
MOI.Test.test_objective_ObjectiveFunction_duplicate_terms(
model,
MOI.Test.Config(;
exclude = Any[MOI.DualObjectiveValue, MOI.ConstraintDual],
),
)
@test MOI.get(mock, MOI.ObjectiveFunctionType()) ==
MOI.ScalarNonlinearFunction
@test MOI.get(model, MOI.ObjectiveFunctionType()) ==
MOI.ScalarAffineFunction{Float64}
@test MOI.get(mock, MOI.ObjectiveSense()) == MOI.MIN_SENSE
@test MOI.get(model, MOI.ObjectiveSense()) == MOI.MIN_SENSE
vis = MOI.get(model, MOI.ListOfVariableIndices())
func = 3.0 * vis[1] + 0.0
@test MOI.get(
model,
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
) ≈ func
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
@test MOI.get(mock, MOI.ObjectiveSense()) == MOI.MAX_SENSE
@test MOI.get(model, MOI.ObjectiveSense()) == MOI.MAX_SENSE
_test_delete_objective(model, 1, tuple())
return
end
function test_solve_result_index()
mock = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Objective.ToScalarNonlinear{Float64}(mock)
MOI.Utilities.set_mock_optimize!(
mock,
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
mock,
MOI.OPTIMAL,
(MOI.FEASIBLE_POINT, [1.0]),
MOI.FEASIBLE_POINT,
(MOI.VariableIndex, MOI.GreaterThan{Float64}) => [1.0],
),
)
MOI.Test.test_solve_result_index(
model,
MOI.Test.Config(;
exclude = Any[MOI.DualObjectiveValue, MOI.ConstraintDual],
),
)
return
end
function test_runtests()
MOI.Bridges.runtests(
MOI.Bridges.Objective.ToScalarNonlinearBridge,
model -> begin
x = MOI.add_variable(model)
aff = MOI.ScalarAffineFunction(
MOI.ScalarAffineTerm.([2.0], [x]),
1.0,
)
MOI.set(
model,
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
aff,
)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
end,
model -> begin
x = MOI.add_variable(model)
exp = MOI.ScalarNonlinearFunction(
:+,
[
MOI.ScalarNonlinearFunction(
:*,
[2.0, MOI.VariableIndex(1)],
),
1.0,
],
)
MOI.set(
model,
MOI.ObjectiveFunction{MOI.ScalarNonlinearFunction}(),
exp,
)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
end,
)
MOI.Bridges.runtests(
MOI.Bridges.Objective.ToScalarNonlinearBridge,
model -> begin
x = MOI.add_variable(model)
aff = MOI.ScalarAffineFunction(
MOI.ScalarAffineTerm.([2.0], [x]),
1.0,
)
MOI.set(
model,
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
aff,
)
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
end,
model -> begin
x = MOI.add_variable(model)
exp = MOI.ScalarNonlinearFunction(
:+,
[
MOI.ScalarNonlinearFunction(
:*,
[2.0, MOI.VariableIndex(1)],
),
1.0,
],
)
MOI.set(
model,
MOI.ObjectiveFunction{MOI.ScalarNonlinearFunction}(),
exp,
)
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
end,
)
return
end
end # module
TestObjectiveToScalarNonlinear.runtests()