{"record":{"id":"e68cf2cdf70654a9","repo":"TheAlgorithms/Python","slug":"rhs-must-be-0","errorCode":null,"errorMessage":"RHS must be > 0","messagePattern":"RHS must be > 0","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"linear_programming/simplex.py","lineNumber":51,"sourceCode":"\n    >>> Tableau(np.array([[-1,-1,0,0,1],[1,3,1,0,4],[3,1,0,1,4.]]), -2, 2)\n    Traceback (most recent call last):\n    ...\n    ValueError: number of (artificial) variables must be a natural number\n    \"\"\"\n\n    # Max iteration number to prevent cycling\n    maxiter = 100\n\n    def __init__(\n        self, tableau: np.ndarray, n_vars: int, n_artificial_vars: int\n    ) -> None:\n        if tableau.dtype != \"float64\":\n            raise TypeError(\"Tableau must have type float64\")\n\n        # Check if RHS is negative\n        if not (tableau[:, -1] >= 0).all():\n            raise ValueError(\"RHS must be > 0\")\n\n        if n_vars < 2 or n_artificial_vars < 0:\n            raise ValueError(\n                \"number of (artificial) variables must be a natural number\"\n            )\n\n        self.tableau = tableau\n        self.n_rows, n_cols = tableau.shape\n\n        # Number of decision variables x1, x2, x3...\n        self.n_vars, self.n_artificial_vars = n_vars, n_artificial_vars\n\n        # 2 if there are >= or == constraints (nonstandard), 1 otherwise (std)\n        self.n_stages = (self.n_artificial_vars > 0) + 1\n\n        # Number of slack variables added to make inequalities into equalities\n        self.n_slack = n_cols - self.n_vars - self.n_artificial_vars - 1\n","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/linear_programming/simplex.py#L33-L69","documentation":"Raised by the simplex solver when the RHS column (last column of the tableau) contains any negative value. Phase-II simplex pivoting requires a feasible starting basis (non-negative RHS); negative right-hand sides mean the origin is infeasible and a two-phase/big-M preprocessing step is needed.","triggerScenarios":"Passing a tableau whose last column has a negative entry, typically from a constraint like 2x + y <= -10, or from multiplying a '>=' constraint row by -1 without then adding artificial variables and going through phase 1.","commonSituations":"Converting '>=' constraints to '<=' by negating the row, modeling negative demand/requirement values, or hand-building the tableau without running the standard two-phase preprocessing.","solutions":["Rewrite '>=' constraints as '<=' by negating both sides so the RHS is non-negative, then add surplus/artificial variables as required.","If the negative RHS is a modeling error, fix the constraint data.","Verify feasibility before constructing: assert (tableau[:, -1] >= 0).all()."],"exampleFix":"# before: row for -2x - y >= -10 kept with RHS -10 after negation mismatch\ntableau = np.array([[-2.0, -1.0, 10.0]])  # last col negative elsewhere\n\n# after: ensure last column non-negative\nrow = np.array([[-2.0, -1.0, 10.0]])\ntableau = np.vstack([tableau, row]) if (tableau[:, -1] >= 0).all() else fix_constraints(tableau)","handlingStrategy":"validation","validationCode":"if not (tableau[:, -1] >= 0).all():\n    raise ValueError(\"RHS has negative entries; convert '>=' rows and add artificials\")\nsolver = Simplex(tableau, n_vars, n_artificial_vars)","typeGuard":"def has_nonneg_rhs(t: np.ndarray) -> bool:\n    return bool((t[:, -1] >= 0).all())","tryCatchPattern":"try:\n    Simplex(tableau, n_vars, n_artificial_vars)\nexcept ValueError as e:\n    if \"RHS\" in str(e):\n        raise ValueError(\"LP is not in canonical form; negate '>=' rows first\") from e\n    raise","preventionTips":["Normalize '>=' constraints to '<=' by negating rows during model building.","Never hand-negate a row without updating its slack/surplus variables.","Assert non-negative RHS as part of your tableau-building helper."],"tags":["linear-programming","simplex","infeasible-start","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}