Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed May 28, 2024
1 parent 58a4b33 commit a6f3f37
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 25 deletions.
36 changes: 34 additions & 2 deletions src/aff_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,46 @@ Base.:(==)(x::GenericAffExpr, y::Number) = isempty(x.terms) && x.constant == y
coefficient(a::GenericAffExpr{C,V}, v::V) where {C,V}
Return the coefficient associated with variable `v` in the affine expression `a`.
## Example
```jldoctest
julia> model = Model();
julia> @variable(model, x);
julia> expr = 2.0 * x + 1.0;
julia> coefficient(expr, x)
2.0
```
"""
coefficient(a::GenericAffExpr{C,V}, v::V) where {C,V} = get(a.terms, v, zero(C))

coefficient(::GenericAffExpr{C,V}, ::V, ::V) where {C,V} = zero(C)

"""
drop_zeros!(expr::GenericAffExpr)
Remove terms in the affine expression with `0` coefficients.
## Example
```jldoctest
julia> model = Model();
julia> @variable(model, x[1:2]);
julia> expr = x[1] + x[2];
julia> add_to_expression!(expr, -1.0, x[1])
0 x[1] + x[2]
julia> drop_zeros!(expr)
julia> expr
x[2]
```
"""
function drop_zeros!(expr::GenericAffExpr)
_drop_zeros!(expr.terms)
Expand Down Expand Up @@ -380,7 +412,7 @@ function value(var_value::Function, ex::GenericAffExpr{T,V}) where {T,V}
end

"""
constant(aff::GenericAffExpr{C, V})::C
constant(aff::GenericAffExpr{C,V})::C
Return the constant of the affine expression.
Expand Down Expand Up @@ -410,7 +442,7 @@ struct LinearTermIterator{GAE<:GenericAffExpr}
end

"""
linear_terms(aff::GenericAffExpr{C, V})
linear_terms(aff::GenericAffExpr{C,V})
Provides an iterator over coefficient-variable tuples `(a_i::C, x_i::V)` in the
linear part of the affine expression.
Expand Down
139 changes: 129 additions & 10 deletions src/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -724,17 +724,95 @@ moi_set(constraint::AbstractConstraint) = constraint.set
"""
constraint_object(con_ref::ConstraintRef)
Return the underlying constraint data for the constraint referenced by `ref`.
Return the underlying constraint data for the constraint referenced by `con_ref`.
## Example
A scalar constraint:
```jldoctest
julia> model = Model();
julia> @variable(model, x);
julia> @constraint(model, c, 2x <= 1)
c : 2 x ≤ 1
julia> object = constraint_object(c)
ScalarConstraint{AffExpr, MathOptInterface.LessThan{Float64}}(2 x, MathOptInterface.LessThan{Float64}(1.0))
julia> typeof(object)
ScalarConstraint{AffExpr, MathOptInterface.LessThan{Float64}}
julia> object.func
2 x
julia> object.set
MathOptInterface.LessThan{Float64}(1.0)
```
A vector constraint:
```jldoctest
julia> model = Model();
julia> @variable(model, x[1:3]);
julia> @constraint(model, c, x in SecondOrderCone())
c : [x[1], x[2], x[3]] ∈ MathOptInterface.SecondOrderCone(3)
julia> object = constraint_object(c)
VectorConstraint{VariableRef, MathOptInterface.SecondOrderCone, VectorShape}(VariableRef[x[1], x[2], x[3]], MathOptInterface.SecondOrderCone(3), VectorShape())
julia> typeof(object)
VectorConstraint{VariableRef, MathOptInterface.SecondOrderCone, VectorShape}
julia> object.func
3-element Vector{VariableRef}:
x[1]
x[2]
x[3]
julia> object.set
MathOptInterface.SecondOrderCone(3)
```
"""
function constraint_object end

"""
struct ScalarConstraint
The data for a scalar constraint. The `func` field contains a JuMP object
representing the function and the `set` field contains the MOI set.
The data for a scalar constraint.
See also the [documentation](@ref Constraints) on JuMP's representation of
constraints for more background.
## Fields
* `.func`: field contains a JuMP object representing the function
* `.set`: field contains the MOI set
## Example
A scalar constraint:
```jldoctest
julia> model = Model();
julia> @variable(model, x);
julia> @constraint(model, c, 2x <= 1)
c : 2 x ≤ 1
julia> object = constraint_object(c)
ScalarConstraint{AffExpr, MathOptInterface.LessThan{Float64}}(2 x, MathOptInterface.LessThan{Float64}(1.0))
julia> typeof(object)
ScalarConstraint{AffExpr, MathOptInterface.LessThan{Float64}}
julia> object.func
2 x
julia> object.set
MathOptInterface.LessThan{Float64}(1.0)
```
"""
struct ScalarConstraint{
F<:Union{Number,AbstractJuMPScalar},
Expand All @@ -745,6 +823,7 @@ struct ScalarConstraint{
end

reshape_set(set::MOI.AbstractScalarSet, ::ScalarShape) = set

shape(::ScalarConstraint) = ScalarShape()

function constraint_object(
Expand All @@ -765,12 +844,47 @@ end
"""
struct VectorConstraint
The data for a vector constraint. The `func` field contains a JuMP object
representing the function and the `set` field contains the MOI set. The
`shape` field contains an [`AbstractShape`](@ref) matching the form in which
the constraint was constructed (for example, by using matrices or flat vectors).
The data for a vector constraint.
See also the [documentation](@ref Constraints) on JuMP's representation of
constraints.
## Fields
* `func`: field contains a JuMP object representing the function
* `set`: field contains the MOI set.
* `shape`: field contains an [`AbstractShape`](@ref) matching the form in which
the constraint was constructed (for example, by using matrices or flat
vectors).
## Example
```jldoctest
julia> model = Model();
julia> @variable(model, x[1:3]);
julia> @constraint(model, c, x in SecondOrderCone())
c : [x[1], x[2], x[3]] ∈ MathOptInterface.SecondOrderCone(3)
julia> object = constraint_object(c)
VectorConstraint{VariableRef, MathOptInterface.SecondOrderCone, VectorShape}(VariableRef[x[1], x[2], x[3]], MathOptInterface.SecondOrderCone(3), VectorShape())
julia> typeof(object)
VectorConstraint{VariableRef, MathOptInterface.SecondOrderCone, VectorShape}
julia> object.func
3-element Vector{VariableRef}:
x[1]
x[2]
x[3]
julia> object.set
MathOptInterface.SecondOrderCone(3)
julia> object.shape
VectorShape()
```
"""
struct VectorConstraint{
F<:Union{Number,AbstractJuMPScalar},
Expand Down Expand Up @@ -806,7 +920,9 @@ function VectorConstraint(
end

reshape_set(set::MOI.AbstractVectorSet, ::VectorShape) = set

shape(con::VectorConstraint) = con.shape

function constraint_object(
con_ref::ConstraintRef{
<:AbstractModel,
Expand All @@ -818,6 +934,7 @@ function constraint_object(
s = MOI.get(model, MOI.ConstraintSet(), con_ref)::SetType
return VectorConstraint(jump_function(model, f), s, con_ref.shape)
end

function check_belongs_to_model(con::VectorConstraint, model)
for func in con.func
check_belongs_to_model(func, model)
Expand Down Expand Up @@ -1165,7 +1282,7 @@ end
Return the dual value of constraint `con_ref` associated with result index
`result` of the most-recent solution returned by the solver.
Use `has_dual` to check if a result exists before asking for values.
Use [`has_dual`](@ref) to check if a result exists before asking for values.
See also: [`result_count`](@ref), [`shadow_price`](@ref).
"""
Expand Down Expand Up @@ -1195,8 +1312,10 @@ constraint.
This value is computed from [`dual`](@ref) and can be queried only when
`has_duals` is `true` and the objective sense is `MIN_SENSE` or `MAX_SENSE`
(not `FEASIBILITY_SENSE`). For linear constraints, the shadow prices differ at
most in sign from the `dual` value depending on the objective sense.
(not `FEASIBILITY_SENSE`).
For linear constraints, the shadow prices differ at most in sign from the `dual`
value depending on the objective sense.
See also [`reduced_cost`](@ref JuMP.reduced_cost).
Expand Down
5 changes: 0 additions & 5 deletions src/nlp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -658,11 +658,6 @@ end
### Nonlinear dual solutions
###

"""
dual(c::NonlinearConstraintRef)
Return the dual of the nonlinear constraint `c`.
"""
function dual(c::NonlinearConstraintRef)
_init_NLP(c.model)
evaluator =
Expand Down
2 changes: 1 addition & 1 deletion src/optimizer_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ function _try_get_solver_name(model_like)
end

"""
solver_name(model::GenericModel) --> String
solver_name(model::GenericModel)
If available, returns the [`MOI.SolverName`](@ref) property of the underlying
optimizer.
Expand Down
3 changes: 2 additions & 1 deletion src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,8 @@ end
constraint_string(
mode::MIME,
ref::ConstraintRef;
in_math_mode::Bool = false)
in_math_mode::Bool = false,
)
Return a string representation of the constraint `ref`, given the `mode`.
"""
Expand Down
62 changes: 57 additions & 5 deletions src/quad_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,31 @@ end
Base.:(==)(x::GenericQuadExpr, y::Number) = isempty(x.terms) && x.aff == y

"""
coefficient(a::GenericAffExpr{C,V}, v1::V, v2::V) where {C,V}
coefficient(a::GenericQuadExpr{C,V}, v1::V, v2::V) where {C,V}
Return the coefficient associated with the term `v1 * v2` in the quadratic expression `a`.
Return the coefficient associated with the term `v1 * v2` in the quadratic
expression `a`.
Note that `coefficient(a, v1, v2)` is the same as `coefficient(a, v2, v1)`.
## Example
```jldoctest
julia> model = Model();
julia> @variable(model, x[1:2]);
julia> expr = 2.0 * x[1] * x[2];
julia> coefficient(expr, x[1], x[2])
2.0
julia> coefficient(expr, x[2], x[1])
2.0
julia> coefficient(expr, x[1], x[1])
0.0
```
"""
function coefficient(q::GenericQuadExpr{C,V}, v1::V, v2::V) where {C,V}
return get(q.terms, UnorderedPair(v1, v2), zero(C))
Expand All @@ -204,14 +224,46 @@ end
"""
coefficient(a::GenericQuadExpr{C,V}, v::V) where {C,V}
Return the coefficient associated with variable `v` in the affine component of `a`.
Return the coefficient associated with variable `v` in the affine component of
`a`.
## Example
```jldoctest
julia> model = Model();
julia> @variable(model, x);
julia> expr = 2.0 * x^2 + 3.0 * x;
julia> coefficient(expr, x)
3.0
```
"""
coefficient(q::GenericQuadExpr{C,V}, v::V) where {C,V} = coefficient(q.aff, v)

"""
drop_zeros!(expr::GenericQuadExpr)
Remove terms in the quadratic expression with `0` coefficients.
## Example
```jldoctest
julia> model = Model();
julia> @variable(model, x[1:2]);
julia> expr = x[1]^2 + x[2]^2;
julia> add_to_expression!(expr, -1.0, x[1], x[1])
0 x[1]² + x[2]²
julia> drop_zeros!(expr)
julia> expr
x[2]²
```
"""
function drop_zeros!(expr::GenericQuadExpr)
drop_zeros!(expr.aff)
Expand Down Expand Up @@ -318,7 +370,7 @@ julia> constant(quad)
constant(quad::GenericQuadExpr) = constant(quad.aff)

"""
linear_terms(quad::GenericQuadExpr{C, V})
linear_terms(quad::GenericQuadExpr{C,V})
Provides an iterator over tuples `(coefficient::C, variable::V)` in the
linear part of the quadratic expression.
Expand All @@ -336,7 +388,7 @@ struct QuadTermIterator{GQE<:GenericQuadExpr}
end

"""
quad_terms(quad::GenericQuadExpr{C, V})
quad_terms(quad::GenericQuadExpr{C,V})
Provides an iterator over tuples `(coefficient::C, var_1::V, var_2::V)` in the
quadratic part of the quadratic expression.
Expand Down
Loading

0 comments on commit a6f3f37

Please sign in to comment.