private void PerformSwapps(Result result, Bag currentBag, double thresholdDrift) { var searchedItems = new List <Item>(); var accumulatedWeight = 0d; var accumulatedValue = 0d; var weightThreshold = (currentBag.RemainingCapacity + (result.LeftOverItems.OrderBy(i => i.Weight).FirstOrDefault().Weight) * thresholdDrift); foreach (Item item in result.LeftOverItems.OrderBy(i => i.Weight).ToList()) { if (accumulatedWeight < weightThreshold) { if (WeightToBeAdded(accumulatedWeight, item.Weight) <= weightThreshold) { accumulatedWeight += item.Weight; accumulatedValue += item.Value; searchedItems.Add(item); } } } var itemToSwapp = GetNeighboringItem(NeighborType.WeightNeighbor, currentBag, accumulatedWeight, accumulatedValue); if (itemToSwapp != null) { currentBag.RemoveItem(itemToSwapp); currentBag.AddItems(searchedItems.ToArray()); } }