private static void Main(string[] args) { CutOrder cutOrder = CutListCalculator.FindOptimal(); const string outFile = "out.csv"; File.WriteAllText(outFile, cutOrder.ToString()); }
public static CutOrder FindOptimal() { var calculator = new CutListCalculator(); var dimensionOptimals = new Dictionary <string, CutOrder>(); var dimensionWasteLengths = new Dictionary <string, decimal>(); calculator.CutOrderFound += (sender, e) => { string dimension = e.CutOrder.First().Key.Dimension; lock (dimensionOptimals) { lock (dimensionWasteLengths) { CutOrder optimalCutOrder; if (!dimensionOptimals.TryGetValue(dimension, out optimalCutOrder)) { dimensionOptimals.Add(dimension, e.CutOrder); return; } decimal optimalWasteLength; if (!dimensionWasteLengths.TryGetValue(dimension, out optimalWasteLength)) { optimalWasteLength = optimalCutOrder.ComputeWaste(); dimensionWasteLengths.Add(dimension, optimalWasteLength); } // Compute the waste length fo the current cut order decimal wasteLength = e.CutOrder.ComputeWaste(); // If the waste length of the current is better than the best, then use current as the best going forward if (wasteLength < optimalWasteLength) { dimensionWasteLengths[dimension] = wasteLength; dimensionOptimals[dimension] = e.CutOrder; } } } }; calculator.FindAll(); var optimal = new CutOrder(); foreach (string dimension in dimensionOptimals.Keys) { foreach (Board board in dimensionOptimals[dimension].Keys) { optimal.Add(board, dimensionOptimals[dimension][board]); } } return(optimal); }