Skip to content

Commit 1eb96d3

Browse files
committed
Add support for getting objective
1 parent 5f5d2e5 commit 1eb96d3

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

src/MOI_wrapper/MOI_multi_objective.jl

+15
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,21 @@ function MOI.set(
138138
return
139139
end
140140

141+
function MOI.get(
142+
model::Optimizer,
143+
::MOI.ObjectiveFunction{MOI.VectorAffineFunction{Float64}},
144+
)
145+
env = GRBgetenv(model)
146+
F = MOI.ScalarAffineFunction{Float64}
147+
f = F[]
148+
for i in 1:MOI.get(model, NumberOfObjectives())
149+
ret = GRBsetintparam(env, "ObjNumber", i - 1)
150+
_check_ret(env, ret)
151+
push!(f, _get_affine_objective(model; is_multiobjective = true))
152+
end
153+
return MOI.Utilities.operate(vcat, Float64, f...)
154+
end
155+
141156
function MOI.supports(
142157
model::Optimizer,
143158
::MOI.ObjectiveFunction{MOI.VectorAffineFunction{Float64}},

src/MOI_wrapper/MOI_wrapper.jl

+18-12
Original file line numberDiff line numberDiff line change
@@ -1150,19 +1150,11 @@ function MOI.set(
11501150
return
11511151
end
11521152

1153-
function MOI.get(
1154-
model::Optimizer,
1155-
::MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}},
1156-
)
1157-
if model.objective_type == _SCALAR_QUADRATIC
1158-
error(
1159-
"Unable to get objective function. Currently: " *
1160-
"$(model.objective_type).",
1161-
)
1162-
end
1153+
function _get_affine_objective(model::Optimizer; is_multiobjective::Bool)
11631154
_update_if_necessary(model)
11641155
dest = zeros(length(model.variable_info))
1165-
ret = GRBgetdblattrarray(model, "Obj", 0, length(dest), dest)
1156+
name = is_multiobjective ? "ObjN" : "Obj"
1157+
ret = GRBgetdblattrarray(model, name, 0, length(dest), dest)
11661158
_check_ret(model, ret)
11671159
terms = MOI.ScalarAffineTerm{Float64}[]
11681160
for (index, info) in model.variable_info
@@ -1171,11 +1163,25 @@ function MOI.get(
11711163
push!(terms, MOI.ScalarAffineTerm(coefficient, index))
11721164
end
11731165
constant = Ref{Cdouble}()
1174-
ret = GRBgetdblattr(model, "ObjCon", constant)
1166+
name = is_multiobjective ? "ObjNCon" : "ObjCon"
1167+
ret = GRBgetdblattr(model, name, constant)
11751168
_check_ret(model, ret)
11761169
return MOI.ScalarAffineFunction(terms, constant[])
11771170
end
11781171

1172+
function MOI.get(
1173+
model::Optimizer,
1174+
::MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}},
1175+
)
1176+
if model.objective_type == _SCALAR_QUADRATIC
1177+
error(
1178+
"Unable to get objective function. Currently: " *
1179+
"$(model.objective_type).",
1180+
)
1181+
end
1182+
return _get_affine_objective(model; is_multiobjective = false)
1183+
end
1184+
11791185
function MOI.set(
11801186
model::Optimizer,
11811187
::MOI.ObjectiveFunction{F},

0 commit comments

Comments
 (0)