private void Run1D() { var index = 0; var end = BarList.Count; // While Boards and Parts avaible. while (index < end && PartsBar.Count > 0) { // Continue (next Bar) if no Parts for this Bar avaible. if (!PartAvaible(BarList[index].Thickness, true, BarList[index].Width, BarList[index].Height)) { index++; continue; } Bars.Add(new Board { Width = BarList[index].Width, Height = BarList[index].Height, Price = BarList[index].Price, Price_string = BarList[index].Price_string, Amount = BarList[index].Amount, Unlimited = BarList[index].Unlimited, Bar = BarList[index].Bar, Thickness = BarList[index].Thickness }); // Contains indexes of used parts while recursion. List <int> deleted = new List <int> (); // Actual Length and Thickness (Bar). Length = BarList[index].Width; Thickness = BarList[index].Thickness; BestLength = 0; var length = 0; Stage = 0; // Run recursion. Recursion1D(deleted, length); BestBar.Sort(); BestBar.Reverse(); // Add used parts to Bar and delete from List. foreach (int i in BestBar) { Bars[Bars.Count - 1].Parts.Add(new Part(PartsBar[i])); PartsBar.RemoveAt(i); } BestBar.Clear(); // Delete Bar from List. if (!BarList[index].Unlimited) { index++; } } }
private bool Recursion1D(List <int> deleted, int length) { Stage++; if (Stage > Options.MaxStage) { return(true); } List <Part> Tested = new List <Part> { }; var i = -1; foreach (Part part in PartsBar) { i++; if (IsTested(part, Tested)) { continue; } if ((part.Thickness == 0 || part.Thickness == Thickness) && !deleted.Contains(PartsBar.IndexOf(part))) { Tested.Add(part); var lengthOld = length; length = (length == 0) ? length + part.Width : length + part.Width + SpaceBar; if (length <= Length) { deleted.Add(i); if (Recursion1D(deleted, length)) { return(true); } deleted.RemoveAt(deleted.Count - 1); } length = lengthOld; } } if (length > BestLength) { BestLength = length; BestBar = new List <int> (deleted); // Stop Recursion if perfect solution found. if (BestLength == Length) { return(true); } } return(false); }