Skip to content

Commit 8c6195c

Browse files
authored
[docs] clarify behavior of FEASIBILITY_SENSE in documentation (#3966)
1 parent a817247 commit 8c6195c

File tree

3 files changed

+103
-22
lines changed

3 files changed

+103
-22
lines changed

docs/src/manual/objective.md

+24
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,30 @@ julia> @objective(model, Max, objective_function(model))
231231
2 x
232232
```
233233

234+
## Remove an objective
235+
236+
To remove an objective function use [`set_objective_sense`](@ref) to set the
237+
sense to [`FEASIBILITY_SENSE`](@ref):
238+
239+
```jldoctest
240+
julia> model = Model();
241+
242+
julia> @variable(model, x);
243+
244+
julia> @objective(model, Min, 2x);
245+
246+
julia> objective_function(model)
247+
2 x
248+
249+
julia> set_objective_sense(model, FEASIBILITY_SENSE)
250+
251+
julia> objective_sense(model)
252+
FEASIBILITY_SENSE::OptimizationSense = 2
253+
254+
julia> objective_function(model)
255+
0
256+
```
257+
234258
## Set a vector-valued objective
235259

236260
Define a multi-objective optimization problem by passing a vector of objectives:

src/macros/@objective.jl

+41-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,24 @@
88
99
Set the objective sense to `sense` and objective function to `func`.
1010
11-
The objective sense can be either `Min`, `Max`, `MOI.MIN_SENSE`, `MOI.MAX_SENSE`
12-
or `MOI.FEASIBILITY_SENSE`. In order to set the sense programmatically, that is,
13-
when `sense` is a variable whose value is the sense, one of the three
14-
[`MOI.OptimizationSense`](@ref) values must be used.
11+
## `sense`
12+
13+
The objective sense must be either be the literals `Min` or `Max`, or one of the
14+
three [`MOI.OptimizationSense`](@ref) enum values ([`MIN_SENSE`](@ref),
15+
[`MAX_SENSE`](@ref), or [`FEASIBILITY_SENSE`](@ref)).
16+
17+
In order to set the sense programmatically, that is, when `sense` is a Julia
18+
variable whose value is the sense, you must use a [`MOI.OptimizationSense`](@ref).
19+
20+
## FEASIBILITY_SENSE
21+
22+
[`FEASIBILITY_SENSE`](@ref) implies that there is no objective function.
23+
Therefore, you should not set `sense` to [`FEASIBILITY_SENSE`](@ref) with a
24+
non-zero `func`.
25+
26+
To reset the model to [`FEASIBILITY_SENSE`](@ref), do
27+
`@objective(model, FEASIBILITY_SENSE, 0)`, or use [`set_objective_sense`](@ref):
28+
`set_objective_sense(model, FEASIBILITY_SENSE)`.
1529
1630
## Example
1731
@@ -50,6 +64,29 @@ MIN_SENSE::OptimizationSense = 0
5064
julia> @objective(model, sense, x^2 - 2x + 1)
5165
x² - 2 x + 1
5266
```
67+
68+
Remove an objective function:
69+
```jldoctest
70+
julia> model = Model();
71+
72+
julia> @variable(model, x >= 0);
73+
74+
julia> @objective(model, Min, 2x + 1)
75+
2 x + 1
76+
77+
julia> print(model)
78+
Min 2 x + 1
79+
Subject to
80+
x ≥ 0
81+
82+
julia> @objective(model, FEASIBILITY_SENSE, 0)
83+
0
84+
85+
julia> print(model)
86+
Feasibility
87+
Subject to
88+
x ≥ 0
89+
```
5390
"""
5491
macro objective(input_args...)
5592
error_fn = Containers.build_error_fn(:objective, input_args, __source__)

src/objective.jl

+38-18
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,12 @@ end
200200
201201
Sets the objective sense of the model to the given sense.
202202
203-
See [`set_objective_function`](@ref) to set the objective function.
203+
See also: [`@objective`](@ref), [`set_objective_function`](@ref), [`set_objective`](@ref)
204204
205-
These are low-level functions; the recommended way to set the objective is with
206-
the [`@objective`](@ref) macro.
205+
## FEASIBILITY_SENSE
206+
207+
Setting the objective sense to [`FEASIBILITY_SENSE`](@ref) will remove any
208+
existing objective.
207209
208210
## Example
209211
@@ -220,7 +222,8 @@ MAX_SENSE::OptimizationSense = 1
220222
```
221223
"""
222224
function set_objective_sense(model::GenericModel, sense::MOI.OptimizationSense)
223-
return MOI.set(model, MOI.ObjectiveSense(), sense)
225+
MOI.set(model, MOI.ObjectiveSense(), sense)
226+
return
224227
end
225228

226229
"""
@@ -229,12 +232,9 @@ end
229232
set_objective_function(model::GenericModel, func::Real)
230233
set_objective_function(model::GenericModel, func::Vector{<:AbstractJuMPScalar})
231234
232-
Sets the objective function of the model to the given function.
233-
234-
See [`set_objective_sense`](@ref) to set the objective sense.
235+
Set the objective function of `model` to the given function `func`.
235236
236-
These are low-level functions; the recommended way to set the objective is with
237-
the [`@objective`](@ref) macro.
237+
See also: [`@objective`](@ref), [`set_objective_function`](@ref), [`set_objective`](@ref)
238238
239239
## Example
240240
@@ -277,14 +277,16 @@ end
277277

278278
function set_objective_function(model::GenericModel, func::AbstractJuMPScalar)
279279
check_belongs_to_model(func, model)
280-
return set_objective_function(model, moi_function(func))
280+
set_objective_function(model, moi_function(func))
281+
return
281282
end
282283

283284
function set_objective_function(model::GenericModel{T}, func::Real) where {T}
284-
return set_objective_function(
285+
set_objective_function(
285286
model,
286287
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm{T}[], convert(T, func)),
287288
)
289+
return
288290
end
289291

290292
function set_objective_function(
@@ -294,7 +296,8 @@ function set_objective_function(
294296
for f in func
295297
check_belongs_to_model(f, model)
296298
end
297-
return set_objective_function(model, moi_function(func))
299+
set_objective_function(model, moi_function(func))
300+
return
298301
end
299302

300303
function set_objective_function(model::AbstractModel, func)
@@ -306,24 +309,41 @@ end
306309
307310
The functional equivalent of the [`@objective`](@ref) macro.
308311
309-
Sets the objective sense and objective function simultaneously, and is
310-
equivalent to calling [`set_objective_sense`](@ref) and
311-
[`set_objective_function`](@ref) separately.
312+
This function sets the objective sense and objective function simultaneously,
313+
and it is equivalent to calling [`set_objective_sense`](@ref) followed by
314+
[`set_objective_function`](@ref).
315+
316+
This is a low-level function; the recommended way to set the objective function
317+
and sense is with the [`@objective`](@ref) macro.
318+
319+
## FEASIBILITY_SENSE
320+
321+
You should not set `sense` to [`FEASIBILITY_SENSE`](@ref) because
322+
[`FEASIBILITY_SENSE`](@ref) implies that there is no objective function.
323+
324+
Instead of `set_objective(model, FEASIBILITY_SENSE, f)`, do
325+
`set_objective_sense(model, FEASIBILITY_SENSE)`.
312326
313327
## Example
314328
315329
```jldoctest
316330
julia> model = Model();
317331
318-
julia> @variable(model, x)
319-
x
332+
julia> @variable(model, x);
320333
321334
julia> set_objective(model, MIN_SENSE, x)
335+
336+
julia> objective_sense(model)
337+
MIN_SENSE::OptimizationSense = 0
338+
339+
julia> objective_function(model)
340+
x
322341
```
323342
"""
324343
function set_objective(model::AbstractModel, sense::MOI.OptimizationSense, func)
325344
set_objective_sense(model, sense)
326-
return set_objective_function(model, func)
345+
set_objective_function(model, func)
346+
return
327347
end
328348

329349
"""

0 commit comments

Comments
 (0)