/// <summary> /// Variant of 4th step of normalizing(should be used if constraint system has more than(>=) and equal(=) constraints, /// except less than constraints, which were changed on previous step) /// </summary> /// <param name="problem">Initial problem</param> /// <returns>Changed problem(new object)</returns> public LppForSimplexMethod ChangeBothTypesOfConstraints(LppForSimplexMethod problem) { var problemCopy = new LppForSimplexMethod(problem); //1. var maxIndex = EqualConstraintWithBiggestFreeCoefficient(problemCopy); //2. for (var i = 0; i < problemCopy.ConstraintCount; i++) { var currConstraint = problemCopy.GetConstraint(i); if (problemCopy.LessThanConstraintsIndexes.Contains(i) || currConstraint.Sign != ">=") continue; var constraintWithMaxCoef = problemCopy.GetConstraint(maxIndex); while (currConstraint.RightSide >= constraintWithMaxCoef.RightSide) currConstraint.Divide(2); currConstraint.Multiply(-1); problemCopy.AddAdditionalVariable(i); currConstraint.Add(constraintWithMaxCoef); } //3. for (var i = 0; i < problemCopy.ConstraintCount; i++) if (problemCopy.GetBasisVariableLabel(i) == null) problemCopy.AddArtificialVariable(i); return problemCopy; }
/// <summary> /// Returns true if all constraints in constraint system have basis variable /// </summary> public bool DoAllConstraintsHaveBasisVariable(LppForSimplexMethod problem) { for (var i = 0; i < problem.ConstraintCount; i++) if (problem.GetBasisVariableLabel(i) == null) return false; return true; }
/// <summary> /// Variant of 4th step of normalizing(should be used if constraint system has only equal constraints, /// except less than constraints, which were changed on previous step) /// </summary> /// <param name="problem">Initial problem</param> /// <returns>Changed problem(new object)</returns> public LppForSimplexMethod ChangeEqualConstraints(LppForSimplexMethod problem) { var problemCopy = new LppForSimplexMethod(problem); for (var i = 0; i < problemCopy.ConstraintCount; i++) if (problemCopy.GetConstraint(i).Sign == "=" && problemCopy.GetBasisVariableLabel(i) == null) problemCopy.AddArtificialVariable(i); return problemCopy; }
/// <summary> /// Variant of 4th step of normalizing(should be used if constraint system has only more than constraints, /// except less than constraints, which were changed on previous step) /// </summary> /// <param name="problem">Initial problem</param> /// <returns>Changed problem(new object)</returns> public LppForSimplexMethod ChangeMoreThanConstraints(LppForSimplexMethod problem) { var problemCopy = new LppForSimplexMethod(problem); //1. for (var i = 0; i < problemCopy.ConstraintCount; i++) if (!problemCopy.LessThanConstraintsIndexes.Contains(i)) problemCopy.AddAdditionalVariable(i); //2. var maxIndex = EqualConstraintWithBiggestFreeCoefficient(problemCopy); //3. for (var i = 0; i < problemCopy.ConstraintCount; i++) { if (problemCopy.LessThanConstraintsIndexes.Contains(i) || i == maxIndex) continue; var first = problemCopy.GetConstraint(maxIndex); var second = problemCopy.GetConstraint(i); second.Multiply(-1); second.Add(first); } //4. if (problemCopy.GetBasisVariableLabel(maxIndex) == null) problemCopy.AddArtificialVariable(maxIndex); return problemCopy; }
private void SetBasis(LppForSimplexMethod problem) { _basisVariables = new Dictionary<int, int>(); for (var i = 0; i < problem.ConstraintCount; i++) { var str = problem.GetBasisVariableLabel(i); var index = IndexOf(str); _basisVariables.Add(i, index); } }