private static double[,] createConstraintMatrix(int numberOfVariables, LinearConstraint[] constraintArray, out double[] b, out int equalities) { // First of all, separate the equality constraints from the inequalities. constraintArray.StableSort((c1, c2) => c1.ShouldBe.CompareTo(c2.ShouldBe)); int numberOfConstraints = constraintArray.Length; double[,] A = new double[numberOfConstraints, numberOfVariables]; b = new double[numberOfConstraints]; equalities = 0; for (int i = 0; i < constraintArray.Length; i++) { LinearConstraint constraint = constraintArray[i]; if (constraint.NumberOfVariables > numberOfVariables) throw new ArgumentException("The number of variables in the constraint exceeds the number of variables for the problem."); for (int j = 0; j < constraint.VariablesAtIndices.Length; j++) { int k = constraint.VariablesAtIndices[j]; if (k >= numberOfVariables) throw new ArgumentException("The constraint refers to a variable index which is not present on the objective function."); if (constraint.ShouldBe == ConstraintType.GreaterThanOrEqualTo || constraint.ShouldBe == ConstraintType.EqualTo) { A[i, k] = constraint.CombinedAs[j]; b[i] = constraint.Value; } else if (constraint.ShouldBe == ConstraintType.LesserThanOrEqualTo) { A[i, k] = -constraint.CombinedAs[j]; b[i] = -constraint.Value; } else throw new ArgumentException("The provided constraint type is not supported."); } if (constraint.ShouldBe == ConstraintType.EqualTo) equalities++; } return A; }