public KnapsackContent Solve()
        {
            KnapsackContent solution = new KnapsackContent();

            solution = FindBetterSubOptions(solution);

            return(solution);
        }
 public KnapsackContent(KnapsackContent currentKnapsackToCopyFrom = null)
 {
     if (currentKnapsackToCopyFrom != null)
     {
         ConsideredItems = new List <bool>(currentKnapsackToCopyFrom.ConsideredItems);
         TotalValue      = currentKnapsackToCopyFrom.TotalValue;
         TotalWeight     = currentKnapsackToCopyFrom.TotalWeight;
     }
     else
     {
         ConsideredItems = new List <bool>();
     }
 }
        private static void SolveKnapsack()
        {
            Console.WriteLine("Solving your Knapsack problem..");

            KnapsackSolver solver = new KnapsackSolver();
            Stopwatch      timer  = new Stopwatch();

            timer.Start();

            KnapsackContent optimalSolution = solver.Solve();

            timer.Stop();

            Console.WriteLine($"\nOptimal solution found for Knapsack:\n{optimalSolution.Description}");

            Console.WriteLine($"That took {timer.ElapsedMilliseconds:#,0}ms");
        }
        private KnapsackContent FindBetterSubOptions(KnapsackContent currentSack)
        {
            if (currentSack.ConsideredItems.Count >= _itemValues.Length)
            {
                return(currentSack);
            }

            KnapsackContent bestSolution = currentSack;

            foreach (bool objectWillBeTaken in new[] { true, false })   // We consider both the cases in which we take or we leave the object where it is
            {
                KnapsackContent extendedSack = new KnapsackContent(currentSack);
                extendedSack.ConsideredItems.Add(objectWillBeTaken);                                  // we store the choice of taking this object or not
                if (objectWillBeTaken)                                                                // if we take it..
                {
                    extendedSack.TotalWeight += _itemWeights[extendedSack.ConsideredItems.Count - 1]; // we add the weight:

                    if (!extendedSack.IsValid)                                                        // if this makes our knapsack explode, we'll ignore this combination
                    {
                        continue;
                    }

                    extendedSack.TotalValue += _itemValues[extendedSack.ConsideredItems.Count - 1];   // otherwise we also add its value
                }

                extendedSack = FindBetterSubOptions(extendedSack);                                         // we then check the best solution we can have among all the objects we still have to evaluate

                if ((bestSolution != currentSack) && (bestSolution.TotalValue >= extendedSack.TotalValue)) // If we already have a better solution for this initial combination
                {
                    continue;                                                                              // ...we discard it...
                }
                bestSolution = extendedSack;                                                               // ...otherwise, it will become our current best local solution.
            }

            Console.Write(".");

            return(bestSolution);
        }