Skip to content

Commit

Permalink
Reverting the generalization of the dual-to-primal mapping API
Browse files Browse the repository at this point in the history
  • Loading branch information
emma58 committed Nov 10, 2024
1 parent 9e07057 commit 1c48982
Showing 1 changed file with 28 additions and 53 deletions.
81 changes: 28 additions & 53 deletions pyomo/core/plugins/transform/lp_dual.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,47 +164,6 @@ def _take_dual(self, model, std_form):

return dual

def _get_corresponding_component(
self, model, model_type, component, component_type, mapping
):
"""Return the corresponding component based on the provided mapping.
Parameters
----------
model: ConcreteModel
The model containing the mappings.
component: Var or Constraint
The primal/dual component for which we want to find the corresponding
dual/primal component.
component_type: str
A string indicating whether the component is 'dual' or 'primal'.
mapping: dict
The mapping to look up the corresponding component.
Returns
-------
Var or Constraint
The corresponding component.
Raises
------
ValueError
If the component is not found in the mapping.
"""
if component in mapping:
return mapping[component]
else:
raise ValueError(
"It does not appear that %s '%s' is a %s %s on model '%s'"
% (
component_type,
component.name,
model_type,
'variable' if component_type == 'Var' else 'constraint',
model.name,
)
)

def get_primal_constraint(self, model, dual_var):
"""Return the primal constraint corresponding to 'dual_var'
Expand All @@ -221,9 +180,13 @@ def get_primal_constraint(self, model, dual_var):
"""
primal_constraint = model.private_data().primal_constraint
return self._get_corresponding_component(
model, 'dual', dual_var, 'Var', primal_constraint
)
if dual_var in primal_constraint:
return primal_constraint[dual_var]
else:
raise ValueError(
"It does not appear that Var '%s' is a dual variable on model '%s'"
% (dual_var.name, model.name)
)

def get_dual_constraint(self, model, primal_var):
"""Return the dual constraint corresponding to 'primal_var'
Expand All @@ -241,9 +204,13 @@ def get_dual_constraint(self, model, primal_var):
"""
dual_constraint = model.private_data().dual_constraint
return self._get_corresponding_component(
model, 'primal', primal_var, 'Var', dual_constraint
)
if primal_var in dual_constraint:
return dual_constraint[primal_var]
else:
raise ValueError(
"It does not appear that Var '%s' is a primal variable on model '%s'"
% (primal_var.name, model.name)
)

def get_primal_var(self, model, dual_constraint):
"""Return the primal variable corresponding to 'dual_constraint'
Expand All @@ -261,9 +228,13 @@ def get_primal_var(self, model, dual_constraint):
"""
primal_var = model.private_data().primal_var
return self._get_corresponding_component(
model, 'dual', dual_constraint, 'Constraint', primal_var
)
if dual_constraint in primal_var:
return primal_var[dual_constraint]
else:
raise ValueError(
"It does not appear that Constraint '%s' is a dual constraint on "
"model '%s'" % (dual_constraint.name, model.name)
)

def get_dual_var(self, model, primal_constraint):
"""Return the dual variable corresponding to 'primal_constraint'
Expand All @@ -281,6 +252,10 @@ def get_dual_var(self, model, primal_constraint):
"""
dual_var = model.private_data().dual_var
return self._get_corresponding_component(
model, 'primal', primal_constraint, 'Constraint', dual_var
)
if primal_constraint in dual_var:
return dual_var[primal_constraint]
else:
raise ValueError(
"It does not appear that Constraint '%s' is a primal constraint on "
"model '%s'" % (primal_constraint.name, model.name)
)

0 comments on commit 1c48982

Please sign in to comment.