Exemplo n.º 1
0
        private void SolveMultiple100()
        {
            KnapsackInput input;

            for (int i = 0; i < 100; i++)
            {
                input = new KnapsackInput();
                input.GenerateRandomItems();
                var stopwatch     = new Stopwatch();
                var solverBaB     = new BranchAndBoundSolver(input.Items, input.Capacity);
                var solverRand    = new RandomSolver(input.Items, input.Capacity);
                var solverGreedy  = new GreedySolver(input.Items, input.Capacity);
                var solverDynamic = new DynamicProgrammingSolver(input.Items, input.Capacity);
                stopwatch.Start();
                solverBaB.Solve();
                stopwatch.Stop();
                timeResultsBaB.Add(new DataPoint(i, stopwatch.Elapsed.TotalMilliseconds));
                stopwatch.Restart();
                solverRand.Solve();
                stopwatch.Stop();
                timeResultsRand.Add(new DataPoint(i, stopwatch.Elapsed.TotalMilliseconds));
                stopwatch.Restart();
                solverGreedy.Solve();
                stopwatch.Stop();
                timeResultsGreedy.Add(new DataPoint(i, stopwatch.Elapsed.TotalMilliseconds));
                stopwatch.Restart();
                solverDynamic.Solve();
                stopwatch.Stop();
                timeResultsDynamic.Add(new DataPoint(i, stopwatch.Elapsed.TotalMilliseconds));
            }
            timeAverageGreedy  = Math.Round((timeResultsGreedy.Sum(x => x.Y) / 100), 4);
            timeAverageRand    = Math.Round((timeResultsRand.Sum(x => x.Y) / 100), 4);
            timeAverageBaB     = Math.Round((timeResultsBaB.Sum(x => x.Y) / 100), 4);
            timeAverageDynamic = Math.Round((timeResultsDynamic.Sum(x => x.Y) / 100), 4);
        }
Exemplo n.º 2
0
 private void DP_Click(object sender, RoutedEventArgs e)
 {
     if (input == null)
     {
         MessageBox.Show("You should choose data first", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
     }
     else
     {
         var stopwatch = new Stopwatch();
         var solver    = new DynamicProgrammingSolver(input.Items, input.Capacity);
         stopwatch.Start();
         var solution = solver.Solve();
         stopwatch.Stop();
         itemsGrid.DataContext    = solution.Items;
         allItemsGrid.DataContext = input.Items;
         resultTextBlock.Text     = "Capacity: " + solution.Capacity + ". Total value = " + solution.Value + ". Total weight = "
                                    + solution.TotalWeight + "." + " Time: " + stopwatch.Elapsed;
     }
 }
Exemplo n.º 3
0
        public void InputWithItemsWithWeightsBiggerThanCapacityShouldReturn0AsValue()
        {
            int capacity       = 3;
            int expectedResult = 0;
            var items          = new List <Item>()
            {
                new Item()
                {
                    Name = "fourth", Weight = 4, Value = 12
                },
                new Item()
                {
                    Name = "third", Weight = 6, Value = 10
                }
            };

            var actualResult = new DynamicProgrammingSolver(items, capacity).Solve().Value;

            Assert.AreEqual(expectedResult, actualResult);
        }
Exemplo n.º 4
0
        public void StandartInputShouldReturnExpectedResult()
        {
            int capacity       = 18;
            int expectedResult = 44;
            var items          = new List <Item>()
            {
                new Item()
                {
                    Name = "fourth", Weight = 4, Value = 12
                },
                new Item()
                {
                    Name = "third", Weight = 6, Value = 10
                },
                new Item()
                {
                    Name = "second", Weight = 5, Value = 8
                },
                new Item()
                {
                    Name = "cheese", Weight = 7, Value = 11
                },
                new Item()
                {
                    Name = "first", Weight = 3, Value = 14
                },
                new Item()
                {
                    Name = "potatos", Weight = 1, Value = 7
                },
                new Item()
                {
                    Name = "bear", Weight = 6, Value = 9
                }
            };

            var actualResult = new DynamicProgrammingSolver(items, capacity).Solve().Value;

            Assert.AreEqual(expectedResult, actualResult);
        }
Exemplo n.º 5
0
        public void StandertInputShouldReturnResultWithLessThanOrEqualCapacity()
        {
            int capacity = 18;
            var items    = new List <Item>()
            {
                new Item()
                {
                    Name = "fourth", Weight = 4, Value = 12
                },
                new Item()
                {
                    Name = "third", Weight = 6, Value = 10
                },
                new Item()
                {
                    Name = "second", Weight = 5, Value = 8
                },
                new Item()
                {
                    Name = "cheese", Weight = 7, Value = 11
                },
                new Item()
                {
                    Name = "first", Weight = 3, Value = 14
                },
                new Item()
                {
                    Name = "potatos", Weight = 1, Value = 7
                },
                new Item()
                {
                    Name = "bear", Weight = 6, Value = 9
                }
            };

            var actualResult = new DynamicProgrammingSolver(items, capacity).Solve().TotalWeight;

            Assert.IsTrue(actualResult <= capacity);
        }