{"record":{"id":"5af5a48dd15d8694","repo":"TheAlgorithms/Python","slug":"number-of-artificial-variables-must-be-a-natural","errorCode":null,"errorMessage":"number of (artificial) variables must be a natural number","messagePattern":"number of \\(artificial\\) variables must be a natural number","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"linear_programming/simplex.py","lineNumber":54,"sourceCode":"    ...\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\n        # Objectives for each stage\n        self.objectives = [\"max\"]\n","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/linear_programming/simplex.py#L36-L72","documentation":"Raised by the simplex solver when n_vars < 2 or n_artificial_vars < 0. The implementation requires at least two decision variables and a non-negative count of artificial variables (0 for standard-form problems, > 0 for problems needing phase 1).","triggerScenarios":"Constructing the solver with n_vars=1 (single-variable LP), passing n_vars=0, or passing a negative artificial-variable count such as -1.","commonSituations":"Trying to solve a trivial one-variable LP through this class, or computing n_artificial_vars by a buggy count (e.g. subtracting instead of adding) that can go negative.","solutions":["Pass n_vars >= 2; for one-variable problems solve directly or add the class's expected minimum by modeling an unused slack variable.","Compute the artificial-variable count as the number of '>='/'==' constraints (>= 0), never negative.","Log both values right before construction to catch off-by-one counting from the constraint matrix."],"exampleFix":"# before\nsolver = Simplex(tableau, n_vars=1, n_artificial_vars=-1)\n\n# after\nn_artificial = sum(1 for c in constraints if c.op in (\">=\", \"==\"))\nsolver = Simplex(tableau, n_vars=2, n_artificial_vars=n_artificial)","handlingStrategy":"validation","validationCode":"n_artificial = sum(1 for c in constraints if c.op in (\">=\", \"==\"))\nassert n_vars >= 2 and n_artificial >= 0\nsolver = Simplex(tableau, n_vars, n_artificial)","typeGuard":"def valid_var_counts(n_vars: int, n_artificial_vars: int) -> bool:\n    return isinstance(n_vars, int) and n_vars >= 2 and isinstance(n_artificial_vars, int) and n_artificial_vars >= 0","tryCatchPattern":"try:\n    Simplex(tableau, n_vars, n_artificial_vars)\nexcept ValueError as e:\n    if \"natural number\" in str(e):\n        raise ValueError(f\"bad counts: n_vars={n_vars}, n_artificial={n_artificial_vars}\") from e\n    raise","preventionTips":["Compute artificial counts from constraint operators, never by subtraction.","Reject one-variable LPs before reaching this class.","Log both counts at construction time in debug builds."],"tags":["linear-programming","simplex","argument-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}