Skip to content

Commit

Permalink
Removed variable fixing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
radhakrishnatg committed Jan 15, 2025
1 parent b0a8106 commit e58271c
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 99 deletions.
53 changes: 0 additions & 53 deletions primo/opt_model/efficiency_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,59 +103,6 @@ def calculate_cluster_efficiency(blk):
for eff_metric_blk in blk.component_data_objects(Block)
)

def append_sub_problem_cuts(self):
"""
Fixes the first-stage/complicating variable values.
This method is needed for Benders Decomposition.
"""
cm = self.parent_block() # ClusterBlock Model
if not self._has_fixing_var_constraints:
self._has_fixing_var_constraints = True

# Constructing dummy constraints for now. Will be updated later
@self.Constraint(cm.set_wells)
def fixing_select_well_var(_, w):
return cm.select_well[w] == w

@self.Constraint(cm.num_wells_var.index_set())
def fixing_num_wells_var(_, w):
return cm.num_wells_var[w] == w

@self.Constraint()
def fixing_select_cluster_var(_):
return cm.select_cluster == 0

# Activate the constraint if it has been deactivated before
self.fixing_select_well_var.activate()
self.fixing_num_wells_var.activate()
self.fixing_select_cluster_var.activate()

# Update the constraint based on the current value of the
# first-stage/complicating variables
for w in cm.set_wells:
self.fixing_select_well_var[w].set_value(
cm.select_well[w] == round(cm.select_well[w].value)
)

for w in cm.num_wells_var:
self.fixing_num_wells_var[w].set_value(
cm.num_wells_var[w] == round(cm.num_wells_var[w].value)
)

self.fixing_select_cluster_var.set_value(
cm.select_cluster == round(cm.select_cluster.value)
)

def remove_sub_problem_cuts(self):
"""
Deactivates constraints that fix first-stage/complicating variable
values, if they exist. This method is needed for Benders Decomposition.
"""
if self._has_fixing_var_constraints:
self.fixing_select_well_var.deactivate()
self.fixing_num_wells_var.deactivate()
self.fixing_select_cluster_var.deactivate()

def get_efficiency_scores(self):
"""Returns a dictionary containing efficiency scores"""
scores = {}
Expand Down
46 changes: 0 additions & 46 deletions primo/opt_model/tests/test_efficiency_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,49 +103,3 @@ def test_eff_block_with_input_var(get_dummy_model):

eff_blk.v2.value = 10
assert eff_blk.get_efficiency_scores() == {"v1": None, "v2": 10}


def test_eff_block_fixing_var_methods(get_dummy_model):
"""
Tests the append_sub_problem_cuts and the
remove_sub_problem_cuts methods of the EfficiencyBlock class
"""
m = get_dummy_model
eff_blk = m.efficiency_model
eff_blk.append_cluster_eff_vars()

# Fixing variable constraints must not be added by default
# pylint: disable = protected-access
assert not eff_blk._has_fixing_var_constraints
assert not hasattr(eff_blk, "fixing_select_well_var")
assert not hasattr(eff_blk, "fixing_select_cluster_var")
assert not hasattr(eff_blk, "fixing_num_wells_var")

m.select_well.fix(1)
m.select_cluster.fix(1)
m.num_wells_var.fix(1)

# Append the sub-problem cuts
eff_blk.append_sub_problem_cuts()
assert eff_blk._has_fixing_var_constraints
assert str(eff_blk.fixing_select_well_var[1].expr) == "select_well[1] == 1"
assert str(eff_blk.fixing_select_well_var[5].expr) == "select_well[5] == 1"
assert str(eff_blk.fixing_num_wells_var[1].expr) == "num_wells_var[1] == 1"
assert str(eff_blk.fixing_num_wells_var[5].expr) == "num_wells_var[5] == 1"
assert str(eff_blk.fixing_select_cluster_var.expr) == "select_cluster == 1"

# Test remove_sub_problem_cuts
eff_blk.remove_sub_problem_cuts()
assert not eff_blk.fixing_select_well_var.active
assert not eff_blk.fixing_num_wells_var.active
assert not eff_blk.fixing_select_cluster_var.active

# Test activation again
m.select_well.fix(0)
eff_blk.append_sub_problem_cuts()
assert eff_blk.fixing_select_well_var.active
assert eff_blk.fixing_num_wells_var.active
assert eff_blk.fixing_select_cluster_var.active

# Check if the constraint has been updated
assert str(eff_blk.fixing_select_well_var[1].expr) == "select_well[1] == 0"

0 comments on commit e58271c

Please sign in to comment.