public void CalculateGoals(ObjectiveType objective, params ConstraintType[] constraints) { var simplex = new SimplexSolver(); var goalEarned = GoalEarned; var objCoefficients = new Dictionary <object, double>(); var consCoefficients = new Dictionary <object, double>(); foreach (var section in sections) { var staticEarned = section.Assignments.Where(a => !a.GoalSelected).Sum(a => a.Earned); var totalWorth = section.Assignments.Sum(a => a.Worth); goalEarned -= section.Weight * 100 * (staticEarned / totalWorth); foreach (var assignment in section.Assignments) { if (assignment.GoalSelected) { var objCoefficient = objective == ObjectiveType.Equal ? 1 : section.Weight; objCoefficients.Add(assignment.Id, objCoefficient); var consCoefficient = section.Weight * 100 / totalWorth; consCoefficients.Add(assignment.Id, consCoefficient); var maxEarnedCoeff = new Dictionary <object, double>(); maxEarnedCoeff.Add(assignment.Id, 1); simplex.AddConstraint(maxEarnedCoeff, Relationship.LessThanOrEqual, assignment.GoalEarned); } } } simplex.SetObjective(Optimization.Min, objCoefficients); simplex.AddConstraint(consCoefficients, Relationship.GreaterThanOrEqual, goalEarned); simplex.Solve(out IDictionary <object, double> solution); // 100, 100, 36.923 }
public void Test1() { var s = new SimplexSolver(); var coeff = new Dictionary<object, double> { { 1, .35 }, { 2, .65 }, { 3, .65 } }; s.SetObjective(Optimization.Min, coeff); coeff = new Dictionary<object, double> { { 1, .116667 }, { 2, .216667 }, { 3, .216667 } }; s.AddConstraint(coeff, Relationship.GreaterThanOrEqual, 43.5); coeff = new Dictionary<object, double> { { 1, 1 } }; s.AddConstraint(coeff, Relationship.LessThanOrEqual, 95); coeff = new Dictionary<object, double> { { 2, 1 } }; s.AddConstraint(coeff, Relationship.LessThanOrEqual, 95); coeff = new Dictionary<object, double> { { 3, 1 } }; s.AddConstraint(coeff, Relationship.LessThanOrEqual, 90); coeff = new Dictionary<object, double> { { 2, 1 }, { 3, -1 } }; s.AddConstraint(coeff, Relationship.Equal, 0); s.Solve(out IDictionary<object, double> solution); }