|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Test infeasibility detection for different solvers. |
| 4 | +""" |
| 5 | + |
| 6 | +import pandas as pd |
| 7 | +import pytest |
| 8 | + |
| 9 | +from linopy import Model, available_solvers |
| 10 | + |
| 11 | + |
| 12 | +class TestInfeasibility: |
| 13 | + """Test class for infeasibility detection functionality.""" |
| 14 | + |
| 15 | + @pytest.fixture |
| 16 | + def simple_infeasible_model(self): |
| 17 | + """Create a simple infeasible model.""" |
| 18 | + m = Model() |
| 19 | + |
| 20 | + time = pd.RangeIndex(10, name="time") |
| 21 | + x = m.add_variables(lower=0, coords=[time], name="x") |
| 22 | + y = m.add_variables(lower=0, coords=[time], name="y") |
| 23 | + |
| 24 | + # Create infeasible constraints |
| 25 | + m.add_constraints(x <= 5, name="con_x_upper") |
| 26 | + m.add_constraints(y <= 5, name="con_y_upper") |
| 27 | + m.add_constraints(x + y >= 12, name="con_sum_lower") |
| 28 | + |
| 29 | + # Add objective to avoid multi-objective issue with xpress |
| 30 | + m.add_objective(x.sum() + y.sum()) |
| 31 | + |
| 32 | + return m |
| 33 | + |
| 34 | + @pytest.fixture |
| 35 | + def complex_infeasible_model(self): |
| 36 | + """Create a more complex infeasible model.""" |
| 37 | + m = Model() |
| 38 | + |
| 39 | + # Create variables |
| 40 | + x = m.add_variables(lower=0, upper=10, name="x") |
| 41 | + y = m.add_variables(lower=0, upper=10, name="y") |
| 42 | + z = m.add_variables(lower=0, upper=10, name="z") |
| 43 | + |
| 44 | + # Add conflicting constraints |
| 45 | + m.add_constraints(x + y >= 15, name="con1") |
| 46 | + m.add_constraints(x <= 5, name="con2") |
| 47 | + m.add_constraints(y <= 5, name="con3") |
| 48 | + m.add_constraints(z >= x + y, name="con4") |
| 49 | + m.add_constraints(z <= 8, name="con5") |
| 50 | + |
| 51 | + # Add objective |
| 52 | + m.add_objective(x + y + z) |
| 53 | + |
| 54 | + return m |
| 55 | + |
| 56 | + @pytest.fixture |
| 57 | + def multi_dimensional_infeasible_model(self): |
| 58 | + """Create a multi-dimensional infeasible model.""" |
| 59 | + m = Model() |
| 60 | + |
| 61 | + # Create multi-dimensional variables |
| 62 | + i = pd.RangeIndex(5, name="i") |
| 63 | + j = pd.RangeIndex(3, name="j") |
| 64 | + |
| 65 | + x = m.add_variables(lower=0, upper=1, coords=[i, j], name="x") |
| 66 | + |
| 67 | + # Add constraints that make it infeasible |
| 68 | + m.add_constraints(x.sum("j") >= 2.5, name="row_sum") # Each row sum >= 2.5 |
| 69 | + m.add_constraints(x.sum("i") <= 1, name="col_sum") # Each column sum <= 1 |
| 70 | + |
| 71 | + # Add objective |
| 72 | + m.add_objective(x.sum()) |
| 73 | + |
| 74 | + return m |
| 75 | + |
| 76 | + @pytest.mark.parametrize("solver", ["gurobi", "xpress"]) |
| 77 | + def test_simple_infeasibility_detection(self, simple_infeasible_model, solver): |
| 78 | + """Test basic infeasibility detection.""" |
| 79 | + if solver not in available_solvers: |
| 80 | + pytest.skip(f"{solver} not available") |
| 81 | + |
| 82 | + m = simple_infeasible_model |
| 83 | + status, condition = m.solve(solver_name=solver) |
| 84 | + |
| 85 | + assert status == "warning" |
| 86 | + assert "infeasible" in condition |
| 87 | + |
| 88 | + # Test compute_infeasibilities |
| 89 | + labels = m.compute_infeasibilities() |
| 90 | + assert isinstance(labels, list) |
| 91 | + assert len(labels) > 0 # Should find at least one infeasible constraint |
| 92 | + |
| 93 | + # Test print_infeasibilities (just check it doesn't raise an error) |
| 94 | + m.print_infeasibilities() |
| 95 | + |
| 96 | + @pytest.mark.parametrize("solver", ["gurobi", "xpress"]) |
| 97 | + def test_complex_infeasibility_detection(self, complex_infeasible_model, solver): |
| 98 | + """Test infeasibility detection on more complex model.""" |
| 99 | + if solver not in available_solvers: |
| 100 | + pytest.skip(f"{solver} not available") |
| 101 | + |
| 102 | + m = complex_infeasible_model |
| 103 | + status, condition = m.solve(solver_name=solver) |
| 104 | + |
| 105 | + assert status == "warning" |
| 106 | + assert "infeasible" in condition |
| 107 | + |
| 108 | + labels = m.compute_infeasibilities() |
| 109 | + assert isinstance(labels, list) |
| 110 | + assert len(labels) > 0 |
| 111 | + |
| 112 | + # The infeasible set should include constraints that conflict |
| 113 | + # Different solvers might find different minimal IIS |
| 114 | + # We expect at least 2 constraints to be involved |
| 115 | + assert len(labels) >= 2 |
| 116 | + |
| 117 | + @pytest.mark.parametrize("solver", ["gurobi", "xpress"]) |
| 118 | + def test_multi_dimensional_infeasibility( |
| 119 | + self, multi_dimensional_infeasible_model, solver |
| 120 | + ): |
| 121 | + """Test infeasibility detection on multi-dimensional model.""" |
| 122 | + if solver not in available_solvers: |
| 123 | + pytest.skip(f"{solver} not available") |
| 124 | + |
| 125 | + m = multi_dimensional_infeasible_model |
| 126 | + status, condition = m.solve(solver_name=solver) |
| 127 | + |
| 128 | + assert status == "warning" |
| 129 | + assert "infeasible" in condition |
| 130 | + |
| 131 | + labels = m.compute_infeasibilities() |
| 132 | + assert isinstance(labels, list) |
| 133 | + assert len(labels) > 0 |
| 134 | + |
| 135 | + @pytest.mark.parametrize("solver", ["gurobi", "xpress"]) |
| 136 | + def test_no_solver_model_error(self, solver): |
| 137 | + """Test error when no solver model is available.""" |
| 138 | + if solver not in available_solvers: |
| 139 | + pytest.skip(f"{solver} not available") |
| 140 | + |
| 141 | + m = Model() |
| 142 | + x = m.add_variables(name="x") |
| 143 | + m.add_constraints(x >= 0) |
| 144 | + m.add_objective(1 * x) # Convert to LinearExpression |
| 145 | + |
| 146 | + # Don't solve the model - should raise error |
| 147 | + with pytest.raises(ValueError, match="No solver model available"): |
| 148 | + m.compute_infeasibilities() |
| 149 | + |
| 150 | + @pytest.mark.parametrize("solver", ["gurobi", "xpress"]) |
| 151 | + def test_feasible_model_iis(self, solver): |
| 152 | + """Test IIS computation on a feasible model.""" |
| 153 | + if solver not in available_solvers: |
| 154 | + pytest.skip(f"{solver} not available") |
| 155 | + |
| 156 | + m = Model() |
| 157 | + x = m.add_variables(lower=0, name="x") |
| 158 | + y = m.add_variables(lower=0, name="y") |
| 159 | + |
| 160 | + m.add_constraints(x + y >= 1) |
| 161 | + m.add_constraints(x <= 10) |
| 162 | + m.add_constraints(y <= 10) |
| 163 | + |
| 164 | + m.add_objective(x + y) |
| 165 | + |
| 166 | + status, condition = m.solve(solver_name=solver) |
| 167 | + assert status == "ok" |
| 168 | + assert condition == "optimal" |
| 169 | + |
| 170 | + # Calling compute_infeasibilities on a feasible model |
| 171 | + # Different solvers might handle this differently |
| 172 | + # Gurobi might raise an error, Xpress might return empty list |
| 173 | + try: |
| 174 | + labels = m.compute_infeasibilities() |
| 175 | + # If it doesn't raise an error, it should return empty list |
| 176 | + assert labels == [] |
| 177 | + except Exception: |
| 178 | + # Some solvers might raise an error when computing IIS on feasible model |
| 179 | + pass |
| 180 | + |
| 181 | + def test_unsupported_solver_error(self): |
| 182 | + """Test error for unsupported solvers.""" |
| 183 | + m = Model() |
| 184 | + x = m.add_variables(name="x") |
| 185 | + m.add_constraints(x >= 0) |
| 186 | + m.add_constraints(x <= -1) # Make it infeasible |
| 187 | + |
| 188 | + # Use a solver that doesn't support IIS |
| 189 | + if "cbc" in available_solvers: |
| 190 | + status, condition = m.solve(solver_name="cbc") |
| 191 | + assert "infeasible" in condition |
| 192 | + |
| 193 | + with pytest.raises(NotImplementedError): |
| 194 | + m.compute_infeasibilities() |
| 195 | + |
| 196 | + @pytest.mark.parametrize("solver", ["gurobi", "xpress"]) |
| 197 | + def test_deprecated_method(self, simple_infeasible_model, solver): |
| 198 | + """Test that deprecated method still works.""" |
| 199 | + if solver not in available_solvers: |
| 200 | + pytest.skip(f"{solver} not available") |
| 201 | + |
| 202 | + m = simple_infeasible_model |
| 203 | + status, condition = m.solve(solver_name=solver) |
| 204 | + |
| 205 | + assert status == "warning" |
| 206 | + assert "infeasible" in condition |
| 207 | + |
| 208 | + # Test deprecated method |
| 209 | + with pytest.warns(DeprecationWarning): |
| 210 | + subset = m.compute_set_of_infeasible_constraints() |
| 211 | + |
| 212 | + # Check that it returns a Dataset |
| 213 | + from xarray import Dataset |
| 214 | + |
| 215 | + assert isinstance(subset, Dataset) |
| 216 | + |
| 217 | + # Check that it contains constraint labels |
| 218 | + assert len(subset) > 0 |
0 commit comments