public void CorrectGrayAlgorithm_WhenSequenceLengthIsFour() { var grayCode = GrayAlgorithm.GetGrayCode(4).ToArray(); foreach (var code in grayCode) for (var j = 0; j < code.Length; ++j) Assert.AreEqual(SecondTestData[j], grayCode[j]); }
public static void Solve(List <Subject> subjects, int maxWeight) { var weight = 0; var bestCost = 0; var bestWeight = 0; var previousBits = BinaryConverter.Expand(BinaryConverter.Convert(0), subjects.Count); var bestMask = previousBits; foreach (var currentBits in GrayAlgorithm.GetGrayCode(subjects.Count).Skip(1)) { var(index, isOne) = GetIndexOfDifference(previousBits, currentBits); previousBits = currentBits; if (isOne) { weight += subjects[index].Weight; } else { weight -= subjects[index].Weight; } if (weight > maxWeight) { continue; } var cost = subjects.Where((t, i) => currentBits[i] == 1).Sum(x => x.Cost); if (cost <= bestCost) { continue; } bestCost = cost; bestMask = currentBits; bestWeight = weight; } Console.WriteLine("Max weight: " + bestWeight); Console.WriteLine("Max cost: " + bestCost); Console.Write("Using weights: "); for (var i = 0; i < subjects.Count; i++) { if (bestMask[i] == 1) { Console.Write(subjects[i].Weight + " "); } } Console.WriteLine(); }