diff --git a/src/topupopt/problems/esipp/problem.py b/src/topupopt/problems/esipp/problem.py index ce148d327e2d2a21fdd28340d1e02fd6d6d1fd01..2770f9c2a39c480895955e9955115cecdca903a1 100644 --- a/src/topupopt/problems/esipp/problem.py +++ b/src/topupopt/problems/esipp/problem.py @@ -480,6 +480,7 @@ class InfrastructurePlanningProblem(EnergySystem): use_sos1: bool = False, use_interface: bool = False, use_nnr_variables_if_possible: bool = False, + sos1_weight_method: int = SOS1_ARC_WEIGHTS_NONE ) -> int: """ Create a group of arcs whose invesment is to be decided collectively. @@ -500,14 +501,14 @@ class InfrastructurePlanningProblem(EnergySystem): Returns the key that identifies the arc group, an integer. """ - + # make sure there is at least one arc if len(gllj_tuples) < 2: raise ValueError( "At least two arcs need to be identified to create a group." ) - + for arc_number, gllj in enumerate(gllj_tuples): # does the network exist? @@ -565,10 +566,10 @@ class InfrastructurePlanningProblem(EnergySystem): # groups using interfaces self.groups_int[new_t] = use_interface - + # TODO: allow users to set the weights # groups using sos1 for arc selection - - self.groups_arc_sos1[new_t] = use_sos1 + if use_sos1: + self.groups_arc_sos1[new_t] = [i for i in range(number_options)] # groups using nnr for arc selection @@ -2455,7 +2456,7 @@ class InfrastructurePlanningProblem(EnergySystem): # set of arc groups relying on SOS1 - set_T_sos1 = tuple(t for t in set_T if self.groups_arc_sos1[t]) + set_T_sos1 = tuple(t for t in set_T if t in self.groups_arc_sos1) # set of arg groups relying on non-negative real variables @@ -3196,7 +3197,7 @@ class InfrastructurePlanningProblem(EnergySystem): } param_arc_inv_sos1_weights_th = { - (t, h): h + (t, h): self.groups_arc_sos1[t][h] # (t, h): mean( # (self.use_sos1_arc_inv[(g,u,v,j)][h] # if self.use_sos1_arc_inv[(g,u,v,j)] is not None else h) diff --git a/tests/test_esipp.py b/tests/test_esipp.py index ba37d21328739a353c66a1a2ca07a39b95e5c4dd..d134a51c71f78474ad0dcb1ce02aa37f775fbf06 100644 --- a/tests/test_esipp.py +++ b/tests/test_esipp.py @@ -192,13 +192,11 @@ def build_solve_ipp( if type(arc_groups_dict) != type(None): for key in arc_groups_dict: - ipp.create_arc_group(arc_groups_dict[key]) - - # set up the use of sos for arc selection in groups - - if use_sos_arc_groups: - for group_key, group in ipp.arc_groups.items(): - ipp.groups_arc_sos1[group_key] = group + ipp.create_arc_group( + arc_groups_dict[key], + use_sos1=use_sos_arc_groups, + sos1_weight_method=arc_sos_weight_key + ) # ********************************************************************* diff --git a/tests/test_esipp_problem.py b/tests/test_esipp_problem.py index 1ace5bf66abb460d210df97101b4ac08ba0cb515..a1072d36aba25e99bf9a862065ed77a30ef57fae 100644 --- a/tests/test_esipp_problem.py +++ b/tests/test_esipp_problem.py @@ -2209,13 +2209,7 @@ class TestESIPPProblem: # ************************************************************************* # ************************************************************************* - # TODO: test arc groups with sos - @pytest.mark.parametrize( - "solver, use_sos_arc_groups, arc_sos_weight_key", - [('scip', True, InfrastructurePlanningProblem.SOS1_ARC_WEIGHTS_CAP), - ('scip', False, None)] - ) - def test_arc_groups_individual_ref(self, solver, use_sos_arc_groups, arc_sos_weight_key): + def test_arc_groups_individual_ref(self): # time frame q = 0 @@ -2387,10 +2381,10 @@ class TestESIPPProblem: solver_options["absolute_mip_gap"] = 1e-4 ipp = build_solve_ipp( - solver=solver, + solver='glpk', solver_options=solver_options, use_sos_arcs=False, - use_sos_arc_groups=use_sos_arc_groups, + use_sos_arc_groups=False, arc_sos_weight_key=None, arc_use_real_variables_if_possible=False, use_sos_sense=False, @@ -2411,10 +2405,7 @@ class TestESIPPProblem: # ********************************************************************* # validation - if use_sos_arc_groups: - assert len(ipp.instance.constr_arc_group_sos1) != 0 - else: - assert len(ipp.instance.constr_arc_group_sos1) == 0 + assert len(ipp.instance.constr_arc_group_sos1) == 0 # overview (imports_qpk, @@ -2521,7 +2512,13 @@ class TestESIPPProblem: # ************************************************************************* # ************************************************************************* - def test_arc_groups_individual_undirected(self): + # TODO: test arc groups with sos + @pytest.mark.parametrize( + "solver, use_sos_arc_groups, arc_sos_weight_key", + [('scip', True, InfrastructurePlanningProblem.SOS1_ARC_WEIGHTS_CAP), + ('scip', False, None)] + ) + def test_arc_groups_individual_undirected(self, solver, use_sos_arc_groups, arc_sos_weight_key): # time frame q = 0 @@ -2680,10 +2677,11 @@ class TestESIPPProblem: ] = False ipp = build_solve_ipp( - solver='scip', + solver=solver, solver_options=solver_options, use_sos_arcs=False, - arc_sos_weight_key=None, + use_sos_arc_groups=use_sos_arc_groups, + arc_sos_weight_key=arc_sos_weight_key, arc_use_real_variables_if_possible=False, use_sos_sense=False, sense_sos_weight_key=None, @@ -2700,6 +2698,11 @@ class TestESIPPProblem: arc_groups_dict=arc_groups_dict ) + if use_sos_arc_groups: + assert len(ipp.instance.constr_arc_group_sos1) != 0 + else: + assert len(ipp.instance.constr_arc_group_sos1) == 0 + # overview (imports_qpk, exports_qpk,