|
32 | 32 | replace_by_map, |
33 | 33 | to_path, |
34 | 34 | ) |
35 | | -from linopy.constants import HELPER_DIMS, TERM_DIM, ModelStatus, TerminationCondition |
| 35 | +from linopy.constants import ( |
| 36 | + GREATER_EQUAL, |
| 37 | + HELPER_DIMS, |
| 38 | + LESS_EQUAL, |
| 39 | + TERM_DIM, |
| 40 | + ModelStatus, |
| 41 | + TerminationCondition, |
| 42 | +) |
36 | 43 | from linopy.constraints import AnonymousScalarConstraint, Constraint, Constraints |
37 | 44 | from linopy.expressions import ( |
38 | 45 | LinearExpression, |
@@ -583,6 +590,12 @@ def add_constraints( |
583 | 590 | f"Invalid type of `lhs` ({type(lhs)}) or invalid combination of `lhs`, `sign` and `rhs`." |
584 | 591 | ) |
585 | 592 |
|
| 593 | + invalid_infinity_values = ( |
| 594 | + (data.sign == LESS_EQUAL) & (data.rhs == -np.inf) |
| 595 | + ) | ((data.sign == GREATER_EQUAL) & (data.rhs == np.inf)) # noqa: F821 |
| 596 | + if invalid_infinity_values.any(): |
| 597 | + raise ValueError(f"Constraint {name} contains incorrect infinite values.") |
| 598 | + |
586 | 599 | # ensure helper dimensions are not set as coordinates |
587 | 600 | if drop_dims := set(HELPER_DIMS).intersection(data.coords): |
588 | 601 | # TODO: add a warning here, routines should be safe against this |
@@ -953,6 +966,7 @@ def solve( |
953 | 966 | keep_files: bool = False, |
954 | 967 | env: None = None, |
955 | 968 | sanitize_zeros: bool = True, |
| 969 | + sanitize_infinities: bool = True, |
956 | 970 | slice_size: int = 2_000_000, |
957 | 971 | remote: None = None, |
958 | 972 | **solver_options, |
@@ -1003,6 +1017,8 @@ def solve( |
1003 | 1017 | Whether to set terms with zero coefficient as missing. |
1004 | 1018 | This will remove unneeded overhead in the lp file writing. |
1005 | 1019 | The default is True. |
| 1020 | + sanitize_infinities : bool, optional |
| 1021 | + Whether to filter out constraints that are subject to `<= inf` or `>= -inf`. |
1006 | 1022 | slice_size : int, optional |
1007 | 1023 | Size of the slice to use for writing the lp file. The slice size |
1008 | 1024 | is used to split large variables and constraints into smaller |
@@ -1083,6 +1099,9 @@ def solve( |
1083 | 1099 | if sanitize_zeros: |
1084 | 1100 | self.constraints.sanitize_zeros() |
1085 | 1101 |
|
| 1102 | + if sanitize_infinities: |
| 1103 | + self.constraints.sanitize_infinities() |
| 1104 | + |
1086 | 1105 | if self.is_quadratic and solver_name not in quadratic_solvers: |
1087 | 1106 | raise ValueError( |
1088 | 1107 | f"Solver {solver_name} does not support quadratic problems." |
|
0 commit comments