Skip to content

Commit 682bc27

Browse files
committed
Add tests
1 parent ecf99d3 commit 682bc27

5 files changed

+88
-2
lines changed

Project.toml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
88
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
99
MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
1010
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
11+
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1112
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1213
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1314

test/JuMPExtension.jl

+8-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ mutable struct MyModel <: JuMP.AbstractModel
3535
con_to_name::Dict{ConstraintIndex,String}
3636
name_to_con::Union{Dict{String,ConstraintIndex},Nothing}
3737
objective_sense::JuMP.MOI.OptimizationSense
38-
objective_function::JuMP.AbstractJuMPScalar
38+
objective_function::Union{
39+
JuMP.AbstractJuMPScalar,
40+
Vector{<:JuMP.AbstractJuMPScalar},
41+
}
3942
obj_dict::Dict{Symbol,Any}
4043
function MyModel()
4144
return new(
@@ -447,7 +450,10 @@ function JuMP.num_constraints(
447450
end
448451
end
449452

450-
function JuMP.set_objective_function(m::MyModel, f::JuMP.AbstractJuMPScalar)
453+
function JuMP.set_objective_function(
454+
m::MyModel,
455+
f::Union{JuMP.AbstractJuMPScalar,Vector{<:JuMP.AbstractJuMPScalar}},
456+
)
451457
m.objective_function = f
452458
return
453459
end

test/runtests.jl

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
# See https://github.com/jump-dev/JuMP.jl
99
#############################################################################
1010

11+
import Pkg
12+
Pkg.pkg"add MathOptInterface#od/vector-optimization"
13+
1114
import JuMP
1215
import Test
1316

test/test_generate_and_solve.jl

+38
Original file line numberDiff line numberDiff line change
@@ -552,4 +552,42 @@ c2: x + y <= 1.0
552552
return
553553
end
554554

555+
function test_generate_solve_vector_objective()
556+
model = Model() do
557+
return MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
558+
end
559+
@variable(model, x >= 0.0)
560+
@variable(model, y >= 1.0)
561+
@objective(model, Min, [x, y])
562+
optimize!(model)
563+
mock = unsafe_backend(model)
564+
MOI.set(mock, MOI.TerminationStatus(), MOI.OPTIMAL)
565+
MOI.set(mock, MOI.RawStatusString(), "Optimal")
566+
MOI.set(mock, MOI.ResultCount(), 1)
567+
MOI.set(mock, MOI.PrimalStatus(), MOI.FEASIBLE_POINT)
568+
MOI.set(mock, MOI.DualStatus(), MOI.NO_SOLUTION)
569+
# MOI.set(mock, MOI.ObjectiveValue(), [0.0, 1.0])
570+
MOI.set(mock, MOI.ObjectiveBound(), [0.0, 1.0])
571+
MOI.set(mock, MOI.VariablePrimal(), optimizer_index(x), 0.0)
572+
MOI.set(mock, MOI.VariablePrimal(), optimizer_index(y), 1.0)
573+
@test sprint(print, solution_summary(model)) == """
574+
* Solver : Mock
575+
576+
* Status
577+
Result count : 1
578+
Termination status : OPTIMAL
579+
Message from the solver:
580+
"Optimal"
581+
582+
* Candidate solution (result #1)
583+
Primal status : FEASIBLE_POINT
584+
Dual status : NO_SOLUTION
585+
Objective value : [0.00000e+00,1.00000e+00]
586+
Objective bound : [0.00000e+00,1.00000e+00]
587+
588+
* Work counters
589+
"""
590+
return
591+
end
592+
555593
end # module

test/test_objective.jl

+38
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,42 @@ function test_extension_objective_constant(
189189
return
190190
end
191191

192+
function test_extension_objective_vector_of_variables(
193+
ModelType = Model,
194+
VariableType = VariableRef,
195+
)
196+
model = ModelType()
197+
@variable(model, x[1:2])
198+
@objective(model, Min, x)
199+
@test isequal_canonical(objective_function(model), x)
200+
@test isequal_canonical(objective_function(model, typeof(x)), x)
201+
return
202+
end
203+
204+
function test_extension_objective_vector_affine_function(
205+
ModelType = Model,
206+
VariableType = VariableRef,
207+
)
208+
model = ModelType()
209+
@variable(model, x[1:2])
210+
f = 1.0 .* x .+ [2.0, 3.0]
211+
@objective(model, Min, f)
212+
@test isequal_canonical(objective_function(model), f)
213+
@test isequal_canonical(objective_function(model, typeof(f)), f)
214+
return
215+
end
216+
217+
function test_extension_objective_vector_quadratic_function(
218+
ModelType = Model,
219+
VariableType = VariableRef,
220+
)
221+
model = ModelType()
222+
@variable(model, x[1:2])
223+
f = 1.0 .* x .* x .+ [2.0, 3.0] .* x + [4.0, 5.0]
224+
@objective(model, Min, f)
225+
@test isequal_canonical(objective_function(model), f)
226+
@test isequal_canonical(objective_function(model, typeof(f)), f)
227+
return
228+
end
229+
192230
end # module

0 commit comments

Comments
 (0)