-
Notifications
You must be signed in to change notification settings - Fork 79
/
MOI_multi_objective.jl
165 lines (141 loc) · 4.54 KB
/
MOI_multi_objective.jl
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
161
162
163
164
165
# Copyright (c) 2015 Dahua Lin, Miles Lubin, Joey Huchette, Iain Dunning, and
# contributors
#
# 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.
struct NumberOfObjectives <: MOI.AbstractModelAttribute end
function MOI.set(model::Optimizer, ::NumberOfObjectives, n::Integer)
ret = GRBsetintattr(model, "NumObj", n)
_check_ret(model, ret)
return
end
function MOI.get(model::Optimizer, ::NumberOfObjectives)
_update_if_necessary(model)
n = Ref{Cint}()
ret = GRBgetintattr(model, "NumObj", n)
_check_ret(model, ret)
return n[]
end
struct MultiObjectiveFunction <: MOI.AbstractModelAttribute
index::Int
end
function MOI.set(
model::Optimizer,
attr::MultiObjectiveFunction,
f::MOI.ScalarAffineFunction,
)
num_vars = length(model.variable_info)
obj = zeros(Float64, num_vars)
for term in f.terms
column = _info(model, term.variable).column
obj[column] += term.coefficient
end
indices, coefficients = _indices_and_coefficients(model, f)
_update_if_necessary(model)
ret = GRBsetobjectiven(
model,
Cint(attr.index - 1),
Cint(0),
1.0,
0.0,
0.0,
"Objective $(attr.index)",
f.constant,
length(indices),
indices,
coefficients,
)
_check_ret(model, ret)
_require_update(model)
return
end
"""
MultiObjectiveAttribute(index::Int, name::String)
A Gurobi-specific attribute for interacting with multi-objective attributes.
`index` is the 1-based index of the objective to query. It corresponds to
setting `ObjNumber` to `index - 1`.
See also: `MultiObjectiveWeight`, `MultiObjectivePriority`, `MultiObjectiveValue`.
## Examples
```julia
MOI.get(model, MultiObjectiveAttribute(1, "ObjNPriority"))
MOI.get(model, MultiObjectiveAttribute(2, "ObjNVal"))
MOI.set(model, MultiObjectiveAttribute(2, "ObjNWeight"), 2.0)
```
"""
struct MultiObjectiveAttribute <: MOI.AbstractModelAttribute
index::Int
name::String
end
function MOI.set(model::Optimizer, attr::MultiObjectiveAttribute, value)
env = GRBgetenv(model)
ret = GRBsetintparam(env, "ObjNumber", attr.index - 1)
_check_ret(env, ret)
MOI.set(model, ModelAttribute(attr.name), value)
return
end
function MOI.get(model::Optimizer, attr::MultiObjectiveAttribute)
_update_if_necessary(model)
env = GRBgetenv(model)
ret = GRBsetintparam(env, "ObjNumber", attr.index - 1)
_check_ret(env, ret)
return MOI.get(model, ModelAttribute(attr.name))
end
struct MultiObjectivePriority <: MOI.AbstractModelAttribute
index::Int
end
function MOI.set(model::Optimizer, attr::MultiObjectivePriority, value::Int)
MOI.set(model, MultiObjectiveAttribute(attr.index, "ObjNPriority"), value)
return
end
function MOI.get(model::Optimizer, attr::MultiObjectivePriority)
return MOI.get(model, MultiObjectiveAttribute(attr.index, "ObjNPriority"))
end
struct MultiObjectiveWeight <: MOI.AbstractModelAttribute
index::Int
end
function MOI.set(model::Optimizer, attr::MultiObjectiveWeight, weight::Float64)
MOI.set(model, MultiObjectiveAttribute(attr.index, "ObjNWeight"), weight)
return
end
function MOI.get(model::Optimizer, attr::MultiObjectiveWeight)
return MOI.get(model, MultiObjectiveAttribute(attr.index, "ObjNWeight"))
end
struct MultiObjectiveValue <: MOI.AbstractModelAttribute
index::Int
end
function MOI.get(model::Optimizer, attr::MultiObjectiveValue)
return MOI.get(model, MultiObjectiveAttribute(attr.index, "ObjNVal"))
end
function MOI.set(
model::Optimizer,
::MOI.ObjectiveFunction{F},
f::F,
) where {F<:MOI.VectorAffineFunction{Float64}}
MOI.set(model, NumberOfObjectives(), MOI.output_dimension(f))
for (i, fi) in enumerate(MOI.Utilities.eachscalar(f))
MOI.set(model, MultiObjectiveFunction(i), fi)
end
model.objective_type = _VECTOR_AFFINE
model.is_objective_set = true
return
end
function MOI.get(
model::Optimizer,
::MOI.ObjectiveFunction{MOI.VectorAffineFunction{Float64}},
)
env = GRBgetenv(model)
F = MOI.ScalarAffineFunction{Float64}
f = F[]
for i in 1:MOI.get(model, NumberOfObjectives())
ret = GRBsetintparam(env, "ObjNumber", i - 1)
_check_ret(env, ret)
push!(f, _get_affine_objective(model; is_multiobjective = true))
end
return MOI.Utilities.operate(vcat, Float64, f...)
end
function MOI.supports(
model::Optimizer,
::MOI.ObjectiveFunction{MOI.VectorAffineFunction{Float64}},
)
return true
end