From 882e1f95bf5da5aa954ad1ab357e6bba3688e4e0 Mon Sep 17 00:00:00 2001 From: joaquimg Date: Sat, 12 Apr 2025 00:56:21 -0700 Subject: [PATCH 1/2] Better names --- src/constrained_variables.jl | 4 +++ src/structures.jl | 60 ++++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/constrained_variables.jl b/src/constrained_variables.jl index 7b9dfdca..1f79c632 100644 --- a/src/constrained_variables.jl +++ b/src/constrained_variables.jl @@ -50,6 +50,8 @@ function _add_constrained_variables( for ci in cis f = MOI.get(primal_model, MOI.ConstraintFunction(), ci) if all( + # no element of the VectorOfVariables is a constrained variable + # and not a parameter vi -> !haskey(m.constrained_var_idx, vi) && !(vi in params), f.variables, ) @@ -76,6 +78,8 @@ function _add_constrained_variable( ) for ci in cis f = MOI.get(primal_model, MOI.ConstraintFunction(), ci) + # no element of the VectorOfVariables is a constrained variable + # and not a parameter if !haskey(m.constrained_var_idx, f) && !(f in params) set = MOI.get(primal_model, MOI.ConstraintSet(), ci) if !iszero(MOI.constant(set)) diff --git a/src/structures.jl b/src/structures.jl index 4f458f22..80697488 100644 --- a/src/structures.jl +++ b/src/structures.jl @@ -30,50 +30,76 @@ MOI.Utilities.@model( Maps information from all structures of the primal to the dual model. +Main user maps: + * `constrained_var_idx::Dict{MOI.VariableIndex,Tuple{MOI.ConstraintIndex,Int}}`: - maps original primal constrained variables to their primal original + maps primal constrained variables to their primal constraints (the special ones that makes them constrained variables) and - their internal index (if vector constraints, VectorOfVariables-in-Set), 1 - otherwise (VariableIndex-in-Set). + their internal index from 1 to dimension(set) (if vector constraints: + VectorOfVariables-in-Set), 1 otherwise (scalar: VariableIndex-in-Set). + # primal_convar_to_primal_convarcon_and_index * `constrained_var_dual::Dict{MOI.ConstraintIndex,MOI.ConstraintIndex}`: maps - the original primal constraint index of constrained variables to the dual - model's constraint index of the associated dual constraint. + the primal constraint index of constrained variables to the dual + model's constraint index of the associated dual constraint. This dual + constraint is a regular constraint (not a constrained variable constraint). + `VectorOfVariables`-in-`Zeros` and `VariableIndex`-in-`EqualTo(zero(T))` + are not in this map, as they are not dualized (See + # primal_convarcon_to_dual_function). + # primal_convarcon_to_dual_con - * `constrained_var_zero::Dict{MOI.ConstraintIndex,Unions{MOI.ScalarAffineFunction,MOI.VectorAffineFunction}}`: - caches scalar affine functions or vector affine functions associated with - constrained variables of type `VectorOfVariables`-in-`Zeros` or - `VariableIndex`-in-`EqualTo(zero(T))` as their duals would be `func`-in-`Reals`, - which are "irrelevant" to the model. This information is cached for - completeness of the `DualOptimizer` for `get`ting `ConstraintDuals`. + note: from the above two maps, we can get primal_convar_to_dual_con_and_index * `primal_var_dual_con::Dict{MOI.VariableIndex,MOI.ConstraintIndex}`: maps - "free" primal variables to their associated dual constraints. Free variables - as opposed to constrained variables. Note that Dualization will select - automatically which variables are free and which are constrained. + "free" primal variables to their associated dual (equality) constraints. + Free variables as opposed to constrained variables. Note that Dualization + will select automatically which variables are free and which are + constrained. + # primal_var_to_dual_con + + note: from the above three maps, we can get primal_var_to_dual_con_and_index * `primal_con_dual_var::Dict{MOI.ConstraintIndex,Vector{MOI.VariableIndex}}`: maps primal constraint indices to vectors of dual variable indices. For scalar constraints those vectors will be single element vectors. + Primal Constrained variables constraints (the main ones) are not in this + map. + # primal_con_to_dual_var_vec * `primal_con_dual_con::Dict{MOI.ConstraintIndex,MOI.ConstraintIndex}`: maps - primal constraints to dual variable constraints (if there is such constraint - the dual dual variable is said to be constrained). If the primal + primal constraints to dual constrained variable. If the primal constraint's set is EqualTo or Zeros, no constraint is added in the dual variable (the dual variable is said to be free). + The keys are similar to the (# primal_con_to_dual_var_vec) map, except + that `VariableIndex`-in-`EqualTo(zero(T))` and `VectorOfVariables`-in-`Zeros` + are not in this map. + # primal_con_to_dual_convarcon + + Additional helper maps: * `primal_con_constants::Dict{MOI.ConstraintIndex,Vector{T}}`: maps primal constraints to their respective constants, which might be inside the set. This map is used in `MOI.get(::DualOptimizer,::MOI.ConstraintPrimal,ci)` that requires extra information in the case that the scalar set constrains a constant (`EqualtTo`, `GreaterThan`, `LessThan`). + # primal_con_to_primal_constants_vec * `primal_parameter::Dict{MOI.VariableIndex,MOI.VariableIndex}`: maps parameters in the primal to parameters in the dual model. + # primal_parameter_to_dual_parameter + + * `constrained_var_zero::Dict{MOI.ConstraintIndex,Unions{MOI.ScalarAffineFunction,MOI.VectorAffineFunction}}`: + caches scalar affine functions or vector affine functions associated with + constrained variables of type `VectorOfVariables`-in-`Zeros` or + `VariableIndex`-in-`EqualTo(zero(T))` as their duals would be `func`-in-`Reals`, + which are "irrelevant" to the model. This information is cached for + completeness of the `DualOptimizer` for `get`ting `ConstraintDuals`. + # primal_convarcon_to_dual_function * `primal_var_dual_quad_slack::Dict{MOI.VariableIndex,MOI.VariableIndex}`: maps primal variables (that appear in quadratic objective terms) to dual - "slack" variables. + "slack" variables. These primal variables might appear in other maps. + # primal_var_in_quad_obj_to_dual_slack_var """ mutable struct PrimalDualMap{T} constrained_var_idx::Dict{MOI.VariableIndex,Tuple{MOI.ConstraintIndex,Int}} From 114fe2df2b14d7f8a4ae9e1cfd9d0ad8f7e5369e Mon Sep 17 00:00:00 2001 From: joaquimg Date: Sat, 12 Apr 2025 22:23:51 -0700 Subject: [PATCH 2/2] rm hashtags --- src/structures.jl | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/structures.jl b/src/structures.jl index 80697488..029198d3 100644 --- a/src/structures.jl +++ b/src/structures.jl @@ -37,7 +37,7 @@ Main user maps: constraints (the special ones that makes them constrained variables) and their internal index from 1 to dimension(set) (if vector constraints: VectorOfVariables-in-Set), 1 otherwise (scalar: VariableIndex-in-Set). - # primal_convar_to_primal_convarcon_and_index + Future name: primal_convar_to_primal_convarcon_and_index * `constrained_var_dual::Dict{MOI.ConstraintIndex,MOI.ConstraintIndex}`: maps the primal constraint index of constrained variables to the dual @@ -45,8 +45,8 @@ Main user maps: constraint is a regular constraint (not a constrained variable constraint). `VectorOfVariables`-in-`Zeros` and `VariableIndex`-in-`EqualTo(zero(T))` are not in this map, as they are not dualized (See - # primal_convarcon_to_dual_function). - # primal_convarcon_to_dual_con + primal_convarcon_to_dual_function). + Future name: primal_convarcon_to_dual_con note: from the above two maps, we can get primal_convar_to_dual_con_and_index @@ -55,7 +55,7 @@ Main user maps: Free variables as opposed to constrained variables. Note that Dualization will select automatically which variables are free and which are constrained. - # primal_var_to_dual_con + Future name: primal_var_to_dual_con note: from the above three maps, we can get primal_var_to_dual_con_and_index @@ -64,7 +64,7 @@ Main user maps: scalar constraints those vectors will be single element vectors. Primal Constrained variables constraints (the main ones) are not in this map. - # primal_con_to_dual_var_vec + Future name: primal_con_to_dual_var_vec * `primal_con_dual_con::Dict{MOI.ConstraintIndex,MOI.ConstraintIndex}`: maps primal constraints to dual constrained variable. If the primal @@ -73,7 +73,7 @@ Main user maps: The keys are similar to the (# primal_con_to_dual_var_vec) map, except that `VariableIndex`-in-`EqualTo(zero(T))` and `VectorOfVariables`-in-`Zeros` are not in this map. - # primal_con_to_dual_convarcon + Future name: primal_con_to_dual_convarcon Additional helper maps: @@ -82,11 +82,11 @@ Main user maps: This map is used in `MOI.get(::DualOptimizer,::MOI.ConstraintPrimal,ci)` that requires extra information in the case that the scalar set constrains a constant (`EqualtTo`, `GreaterThan`, `LessThan`). - # primal_con_to_primal_constants_vec + Future name: primal_con_to_primal_constants_vec * `primal_parameter::Dict{MOI.VariableIndex,MOI.VariableIndex}`: maps parameters in the primal to parameters in the dual model. - # primal_parameter_to_dual_parameter + Future name: primal_parameter_to_dual_parameter * `constrained_var_zero::Dict{MOI.ConstraintIndex,Unions{MOI.ScalarAffineFunction,MOI.VectorAffineFunction}}`: caches scalar affine functions or vector affine functions associated with @@ -94,12 +94,12 @@ Main user maps: `VariableIndex`-in-`EqualTo(zero(T))` as their duals would be `func`-in-`Reals`, which are "irrelevant" to the model. This information is cached for completeness of the `DualOptimizer` for `get`ting `ConstraintDuals`. - # primal_convarcon_to_dual_function + Future name: primal_convarcon_to_dual_function * `primal_var_dual_quad_slack::Dict{MOI.VariableIndex,MOI.VariableIndex}`: maps primal variables (that appear in quadratic objective terms) to dual "slack" variables. These primal variables might appear in other maps. - # primal_var_in_quad_obj_to_dual_slack_var + Future name: primal_var_in_quad_obj_to_dual_slack_var """ mutable struct PrimalDualMap{T} constrained_var_idx::Dict{MOI.VariableIndex,Tuple{MOI.ConstraintIndex,Int}}