Add Extension Friendly Variable Creation Methods#123
Merged
pulsipher merged 10 commits intoinfiniteopt:masterfrom Sep 22, 2025
Merged
Add Extension Friendly Variable Creation Methods#123pulsipher merged 10 commits intoinfiniteopt:masterfrom
pulsipher merged 10 commits intoinfiniteopt:masterfrom
Conversation
Collaborator
|
This is a good start, I would suggest the following: # Stores all the info that could be used to create a JuMP variable
mutable struct VariableProperties{L, U, F, S, SET, T}
info::JuMP.VariableInfo{L, U, F, S}
name::String
set::SET
variable_type::T # this is critical for extensions
end
# Default JuMP constructor (extensions will need to define this)
function VariableProperties(vref::JuMP.GenericVariableRef{T}) where T
info = JuMP.VariableInfo(
JuMP.has_lower_bound(vref),
JuMP.has_lower_bound(vref) ? JuMP.lower_bound(vref) : zero(T),
JuMP.has_upper_bound(vref),
JuMP.has_upper_bound(vref) ? JuMP.upper_bound(vref) : zero(T),
JuMP.is_fixed(vref),
JuMP.is_fixed(vref) ? JuMP.fix_value(vref) : zero(T),
!isnothing(JuMP.start_value(vref)),
JuMP.start_value(vref),
JuMP.is_binary(vref),
JuMP.is_integer(vref)
)
name = JuMP.name(vref)
set = JuMP.is_variable_in_set(vref) ? JuMP.moi_set(JuMP.constraint_object(JuMP.VariableInSetRef(vref))) : nothing
return VariableProperties(info, name, set, nothing)
end
# Function for converting VariableProperties into variables (can be extended, but set up to avoid needing to do so)
function create_variable(model::JuMP.AbstractModel, props::VariableProperties)
var = _make_variable_object(props)
if !isnothing(props.set)
var = JuMP.build_variable(error, var, props.set)
end
return JuMP.add_variable(model, var, props.name)
end
function _make_variable_object(props::VariableProperties{L, U, F, S, SET, Nothing}) where {L, U, F, S, SET}
return JuMP.build_variable(error, props.info)
end
function _make_variable_object(props::VariableProperties)
return JuMP.build_variable(error, props.info, props.variable_type)
endThen we can use this to define useful functions for the reformulations, like copy: function variable_copy(model::JuMP.AbstractModel, vref::JuMP.AbstractVariableRef)
props = VariableProperties(vref)
return create_variable(model, props)
end Such that we can do something like: using JuMP
m = Model()
@variable(m, 0 <= x <= 4, Int)
@variable(m, p in Parameter(42))
x_copy = copy_variable(m, x)
m2 =Model()
p_copy = copy_variable(m2, p) |
Contributor
Author
|
Change has been made to use JuMP.VariableInfo to store the data. For example in my tests: function test_make_variable_object()
model = Model()
@variable(model, x, lower_bound = 1.0, upper_bound = 5.0)
props = DP.VariableProperties(x)
# props.info.upper_bound = 10.0 <= can not set because Variable.Info is immutable
modified_info = JuMP.VariableInfo(
props.info.has_lb,
props.info.lower_bound,
props.info.has_ub,
10.0, # Modified upper bound
props.info.has_fix,
props.info.fixed_value,
props.info.has_start,
props.info.start,
props.info.binary,
props.info.integer
)
# Create new VariableProperties with modified info
modified_props = DP.VariableProperties(modified_info, props.name, props.set, props.variable_type)
var_obj = DP._make_variable_object(modified_props)
@test var_obj.info.upper_bound == 10.0
end |
pulsipher
requested changes
Sep 22, 2025
Collaborator
pulsipher
left a comment
There was a problem hiding this comment.
This is coming along. This PR should also update the existing transformation methods (e.g., Hull) to use these methods as appropriate.
pulsipher
requested changes
Sep 22, 2025
…w) tests, added variable docstrings, updated hull to include zero(T)
Contributor
Author
Please give the workflows another try 🤞 |
pulsipher
approved these changes
Sep 22, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For the purposes of copying variables from one model to another, while also having the flexibility to modify properties before copy with the VariableProperties. Simple example below.