Skip to content

Commit bf46749

Browse files
committed
Begin documentation of problem modificatino
1 parent 8097841 commit bf46749

File tree

1 file changed

+113
-4
lines changed

1 file changed

+113
-4
lines changed

docs/src/apimanual.md

Lines changed: 113 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,123 @@ primal_variable_result = MOI.get(optimizer, MOI.VariablePrimal(), x)
412412
@show primal_variable_result
413413
```
414414

415+
## Problem Modification
416+
417+
In addition to adding and deleting constraints and variables, MathOptInterface
418+
supports modifying, in-place, coefficients in the constraints and the objective
419+
function of a model. In the following, we detail the various ways this can be
420+
achieved. Readers should note that some solvers will not support problem
421+
modification.
422+
423+
### Modify the set of a constraint
424+
425+
In this section we discuss how, given a constraint of type `F`-in-`S` (see
426+
[Constraints by function-set pairs](@ref) above for an explanation), we can
427+
modify parameters (but not the type) of the set `S`. For example, given the
428+
variable bound ``x \\le 1``:
429+
```julia
430+
c = addconstraint(m, SingleVariable(x), LessThan(1.0))
431+
```
432+
we can modify the set so that the bound now ``x \\le 2`` as follows:
433+
```julia
434+
set!(m, ConstraintSet(), c, LessThan(2.0))
435+
```
436+
where `m` is our [`ModelLike`](@ref) model. However, the following will fail as
437+
the new set (`GreaterThan`) is of a different type to the original set
438+
(`LessThan`):
439+
```julia
440+
set!(m, ConstraintSet(), c, GreaterThan(2.0)) # errors
441+
```
442+
If our constraint is an affine inequality, then this corresponds to modifying
443+
the right hand-side of a constraint in linear programming.
444+
445+
### Modify the function of a constraint
446+
447+
... still to do ...
448+
449+
### Change the constant term in a function
450+
451+
Another modification that MathOptInterface supports is the ability to modify the
452+
constant term within a function. This includes the objective function, as well
453+
as the function in a function-pair constraint.
454+
455+
```julia
456+
modify!(m::ModelLike, ref, change::AbstractFunctionModification)
457+
```
458+
`ref` is either the constraint index of the constraint we wish to modify or an
459+
instance of the [`ObjectiveFunction`](@ref) attribute. There are two types of
460+
[`AbstractFunctionModification`](@ref) that we can use to modify constant terms:
461+
[`ScalarConstantChange`](@ref) if the function is scalar, and
462+
[`VectorConstantChange`](@ref) if the function is a vector function.
463+
464+
For example, consider a problem `m` with the objective ``\\max 1.0x + 0.0``:
465+
```julia
466+
set!(m,
467+
ObjectiveFunction{ScalarAffineFunction{Float64}}(),
468+
ScalarAffineFunction([ScalarAffineTerm(1.0, x)], 0.0)
469+
)
470+
```
471+
We can modify the constant term in the objective function as follows:
472+
```julia
473+
modify!(m,
474+
ObjectiveFunction{ScalarAffineFunction{Float64}}(),
475+
ScalarConstantChange(1.0)
476+
)
477+
```
478+
The objective function will now be ``\\max 1.0x + 1.0``.
479+
480+
As another example, consider a model with the following
481+
`VectorAffineFunction`-in-`Nonpositives` constraint:
482+
```julia
483+
c = addconstraint!(m,
484+
VectorAffineFunction([
485+
VectorAffineTerm(1, ScalarAffineTerm(1.0, x)),
486+
VectorAffineTerm(1, ScalarAffineTerm(2.0, y))
487+
],
488+
[0.0, 0.0]
489+
),
490+
Nonpositives(2)
491+
)
492+
```
493+
We can modify the constant vector in the `VectorAffineFunction` from `[0.0, 0.0]`
494+
to `[1.0, 2.0]` as follows:
495+
```julia
496+
modify!(m, c, VectorConstantChange([1.0, 2.0])
497+
)
498+
```
499+
The constraints are now ``1.0x + 1.0 \\le 0.0`` and ``2.0y + 2.0 \\le 0.0``.
500+
501+
### Change affine coefficients in a function
502+
503+
In addition to modifying the constant terms in a function, we can also modify
504+
the affine variable coefficients using [`ScalarCoefficientChange`](@ref). For
505+
example, give the constraint ``1.0x <= 1.0``:
506+
```julia
507+
c = addconstraint!(m,
508+
ScalarAffineFunction([ScalarAffineTerm(1.0, x)], 0.0),
509+
LessThan(1.0)
510+
)
511+
```
512+
we can modify the coefficient of the `x` variable so that the constraint becomes
513+
``2.0x <= 1.0`` as follows:
514+
```julia
515+
modify!(m, c, ScalarCoefficientChange(x, 2.0))
516+
```
517+
518+
`ScalarCoefficientChange` can also be used to modify the objective function by
519+
passing an instance of [`ObjectiveFunction`](@ref) instead of the constraint
520+
index `c` as we saw above.
521+
522+
### MultirowChange
523+
524+
Column generation
525+
526+
... still to do ...
527+
415528
## Advanced
416529

417530
### Duals
418531

419-
420532
Conic duality is the starting point for MOI's duality conventions. When all functions are affine (or coordinate projections), and all constraint sets are closed convex cones, the model may be called a conic optimization problem.
421533
For conic-form minimization problems, the primal is:
422534

@@ -529,9 +641,6 @@ If the set ``C_i`` of the section [Duals](@ref) is one of these three cones,
529641
then the rows of the matrix ``A_i`` corresponding to off-diagonal entries are twice the value of the `coefficients` field in the `VectorAffineFunction` for the corresponding rows.
530642
See [`PositiveSemidefiniteConeTriangle`](@ref MathOptInterface.PositiveSemidefiniteConeTriangle) for details.
531643

532-
### Modifying a model
533-
534-
[Explain `modify!`.]
535644

536645
### Constraint bridges
537646

0 commit comments

Comments
 (0)