Skip to content

Commit

Permalink
WIP: add syntax for MOI.Parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Aug 7, 2023
1 parent a325eb6 commit 3ce84fc
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,30 @@ end
function moi_set(set::Semiinteger{T}) where {T}
return MOI.Semiinteger{T}(set.lower, set.upper)
end

"""
Parameter(value)
A short-cut for the [`MOI.Parameter`](@ref) set.
## Example
```jldoctest
julia> model = Model();
julia> @variable(model, x in Parameter(2.0))
x
julia> print(model)
Feasibility
Subject to
x ∈ MathOptInterface.Parameter{Float64}(2.0)
```
"""
struct Parameter{T} <: AbstractScalarSet
value::T
end

function moi_set(set::Parameter{T}) where {T}
return MOI.Parameter{T}(set.value)

Check warning on line 372 in src/sets.jl

View check run for this annotation

Codecov / codecov/patch

src/sets.jl#L371-L372

Added lines #L371 - L372 were not covered by tests
end
49 changes: 49 additions & 0 deletions src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,55 @@ function BinaryRef(v::GenericVariableRef)
return ConstraintRef(owner_model(v), _binary_index(v), ScalarShape())
end

"""
ParameterRef(x::GenericVariableRef)
Return a constraint reference to the constraint constraining `x` to be a
parameter.
Errors if one does not exist.
See also [`is_parameter`](@ref), [`set_parameter`](@ref).
"""
function ParameterRef(x::GenericVariableRef)
return ParameterRef(owner_model(x), _parameter_index(x), ScalarShape())

Check warning on line 1105 in src/variables.jl

View check run for this annotation

Codecov / codecov/patch

src/variables.jl#L1104-L1105

Added lines #L1104 - L1105 were not covered by tests
end

function _parameter_index(x::GenericVariableRef)
S = MOI.Parameter{value_type(typeof(x))}
return MOI.ConstraintIndex{MOI.VariableIndex,S}(index(x).value)

Check warning on line 1110 in src/variables.jl

View check run for this annotation

Codecov / codecov/patch

src/variables.jl#L1108-L1110

Added lines #L1108 - L1110 were not covered by tests
end

"""
is_parameter(x::GenericVariableRef)
Return `true` if `x` is constrained to be a parameter.
See also [`ParameterRef`](@ref), [`set_parameter`](@ref).
"""
function is_parameter(x::GenericVariableRef)
return _moi_is_parameter(backend(owner_model(x)), x)

Check warning on line 1121 in src/variables.jl

View check run for this annotation

Codecov / codecov/patch

src/variables.jl#L1120-L1121

Added lines #L1120 - L1121 were not covered by tests
end

function _moi_is_parameter(moi_backend, x::GenericVariableRef)
return MOI.is_valid(moi_backend, _parameter_index(x))

Check warning on line 1125 in src/variables.jl

View check run for this annotation

Codecov / codecov/patch

src/variables.jl#L1124-L1125

Added lines #L1124 - L1125 were not covered by tests
end

"""
set_parameter(x::GenericVariableRef, value)
Update the parameter constraint on the variable `x` to `value`.
See also [`ParameterRef`](@ref), [`is_parameter`](@ref).
"""
function set_parameter(x::GenericVariableRef, value)
model = owner_model(x)
model.is_model_dirty = true
set = MOI.Parameter(value)
MOI.set(backend(model), MOI.ConstraintSet(), _parameter_index(x), set)
return

Check warning on line 1140 in src/variables.jl

View check run for this annotation

Codecov / codecov/patch

src/variables.jl#L1135-L1140

Added lines #L1135 - L1140 were not covered by tests
end

"""
start_value(v::GenericVariableRef)
Expand Down

0 comments on commit 3ce84fc

Please sign in to comment.