public IEnumerable <Roster> Vary(Roster roster, IEnumerable <Player> players) { foreach (var player3 in roster) { foreach (var player in roster) { foreach (var player2 in roster) { if (player == player2 || player == player3 || player2 == player3) { continue; } var newRoster = roster.Clone(); newRoster.Remove(player); newRoster.Remove(player2); newRoster.Remove(player3); var skips = new List <Player>(); skips.Add(player); skips.Add(player2); skips.Add(player3); var irreplaceablePositions = new List <string>(); double goal = 50000; var newPlayer1 = players .Where(p => p.Position == player.Position) .Where(p => !newRoster.Contains(p)) .Except(skips) .OrderByDescending(p => p.Projection) .First(); newRoster.Add(newPlayer1); skips.Add(newPlayer1); var newPlayer2 = players .Where(p => p.Position == player2.Position) .Where(p => !newRoster.Contains(p)) .Except(skips) .OrderByDescending(p => p.Projection) .First(); newRoster.Add(newPlayer2); skips.Add(newPlayer2); var newPlayer3 = players .Where(p => p.Position == player3.Position) .Where(p => !newRoster.Contains(p)) .Except(skips) .OrderByDescending(p => p.Projection) .First(); newRoster.Add(newPlayer3); skips.Add(newPlayer3); while (newRoster.Salary > goal) { var leastEfficentOnRoster = newRoster .Where(p => !irreplaceablePositions.Contains(p.Position)) .OrderBy(p => p.PointPerCost) .First(); var newPlayer = players .Where(p => p.Position == leastEfficentOnRoster.Position) .Where(p => p.Salary < leastEfficentOnRoster.Salary) .Except(skips) .OrderByDescending(p => p.Projection) .FirstOrDefault(); if (newPlayer == null) { irreplaceablePositions.Add(leastEfficentOnRoster.Position); } else if (newRoster.Contains(newPlayer)) { skips.Add(newPlayer); } else { newRoster.Remove(leastEfficentOnRoster); newRoster.Add(newPlayer); } } yield return(newRoster); } } } }
public IEnumerable<Roster> Permutations(IEnumerable<Player> players) { var byPos = players .GroupBy(p => p.Position) .ToDictionary( g => g.Key, g => { if (new[] { "QB" }.Contains(g.Key)) { return g.ToArray(); } else if (g.Key == "DST") { return g.OrderByDescending(p => p.Projection).Take(3).ToArray(); } else { return g.Where(p => p.Projection > 10d).ToArray(); } }); var flexes = byPos["RB"].Concat(byPos["WR"]).ToArray(); var rosters = new ConcurrentDictionary<Roster, byte>(); int limit = 20; Roster lowest = new Roster(); double index = 0; double count = flexes.Length * byPos["QB"].Length * byPos["TE"].Length * byPos["DST"].Length * (byPos["RB"].Length - 1) * (byPos["RB"].Length - 1) * (byPos["WR"].Length - 2) * (byPos["WR"].Length - 2) * (byPos["WR"].Length - 2); Parallel.ForEach(flexes, new ParallelOptions { MaxDegreeOfParallelism = 7 }, flex => { foreach (var qb in byPos["QB"]) { foreach (var te in byPos["TE"]) { foreach (var dst in byPos["DST"]) { var rbs = byPos["RB"]; var wrs = byPos["WR"]; for (int iRb1 = 0; iRb1 < rbs.Length - 1; iRb1++) { for (int iRb2 = 1; iRb2 < rbs.Length; iRb2++) { for (int iWr1 = 0; iWr1 < wrs.Length - 2; iWr1++) { for (int iWr2 = 1; iWr2 < wrs.Length - 1; iWr2++) { for (int iWr3 = 2; iWr3 < wrs.Length; iWr3++) { ++index; if (Progress != null && index % 100000d == 0) { Progress(this, index / count); } var dupeChecker = new HashSet<Player>(); dupeChecker.Add(flex); if (!dupeChecker.Add(rbs[iRb1]) || !dupeChecker.Add(rbs[iRb2]) || !dupeChecker.Add(wrs[iWr1]) || !dupeChecker.Add(wrs[iWr2]) || !dupeChecker.Add(wrs[iWr3])) { continue; } var roster = new Roster(); roster.Add(qb); roster.Add(te); roster.Add(flex); roster.Add(dst); roster.Add(rbs[iRb1]); roster.Add(rbs[iRb2]); roster.Add(wrs[iWr1]); roster.Add(wrs[iWr2]); roster.Add(wrs[iWr3]); if (roster.Salary > 50000) continue; if (rosters.Count < limit) { if (rosters.TryAdd(roster, 0)) { lowest = rosters.Keys.OrderBy(r => r.Projection).First(); } } else if (roster.Projection > lowest.Projection) { rosters.TryAdd(roster, 0); if (rosters.Count > limit) { rosters.TryRemove(lowest, out byte z); } lowest = rosters.Keys.OrderBy(r => r.Projection).First(); } } } } } } } } } }); return rosters.Keys; }