From e58271cee83cb3ec970ab5eb51f409103460545a Mon Sep 17 00:00:00 2001 From: radhakrishnatg Date: Wed, 15 Jan 2025 11:21:45 -0500 Subject: [PATCH] Removed variable fixing methods --- primo/opt_model/efficiency_block.py | 53 ------------------- .../opt_model/tests/test_efficiency_block.py | 46 ---------------- 2 files changed, 99 deletions(-) diff --git a/primo/opt_model/efficiency_block.py b/primo/opt_model/efficiency_block.py index 160bb8f..5defab5 100644 --- a/primo/opt_model/efficiency_block.py +++ b/primo/opt_model/efficiency_block.py @@ -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 = {} diff --git a/primo/opt_model/tests/test_efficiency_block.py b/primo/opt_model/tests/test_efficiency_block.py index b955bb1..0ff670c 100644 --- a/primo/opt_model/tests/test_efficiency_block.py +++ b/primo/opt_model/tests/test_efficiency_block.py @@ -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"