예제 #1
0
        public static IList <ConstructiveResult> SolveWithPerformanceTest(IList <KnapsackInstance> instances, ConstructiveStrategy strategy, CommandLineOptions options)
        {
            PreparePerformanceTest(instances, strategy);

            var stopWatch = new Stopwatch();
            var results   = new List <ConstructiveResult>();

            foreach (var instance in instances)
            {
                Console.WriteLine($"Processing instance no. {instance.Id}");
                ConstructiveResult result = null;

                //The algorithm must run repeat at least the set amount of times;
                for (int i = 0; i < REPEAT_COUNT; i++)
                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();

                    //GC.TryStartNoGCRegion(200000000);
                    stopWatch.Start();
                    result = strategy.Solve(instance);
                    stopWatch.Stop();
                    strategy.FreeAll();
                    //GC.EndNoGCRegion();
                }
                var averageRuntime = stopWatch.Elapsed.TotalMilliseconds / REPEAT_COUNT;

                //Save only the last result
                if (result != null)
                {
                    result.DataSetName = options.DataSetName;
                    result.RunTimeMs   = averageRuntime;
                    if (result.Strategy == null)
                    {
                        result.Strategy = options.Strategy;
                    }
                    results.Add(result);
                }

                stopWatch.Reset();
            }

            return(results);
        }
예제 #2
0
        public override ConstructiveResult Solve(KnapsackInstance instance)
        {
            BestConfiguration = new KnapsackConfiguration {
                Price = int.MinValue, Weight = 0, ItemVector = CreateEmptySolution(instance.ItemCount)
            };

            FindBestConfiguration(0, new KnapsackConfiguration {
                Price = 0, Weight = 0, ItemVector = new List <bool>()
            }, instance.GetPriceOfAllItems(), instance);

            var result = new ConstructiveResult
            {
                KnapsackInstance = instance,
                NumberOfSteps    = numberOfSteps,
                Configuration    = BestConfiguration
            };

            return(result);
        }
예제 #3
0
        public override ConstructiveResult Solve(KnapsackInstance instance)
        {
            BestConfiguration = new KnapsackConfiguration {
                Price = int.MinValue, Weight = 0, ItemVector = new List <bool>()
            };
            numberOfSteps = 0;

            FindBestConfiguration(0, new KnapsackConfiguration {
                Price = 0, Weight = 0, ItemVector = new List <bool>()
            }, instance);

            var result = new ConstructiveResult
            {
                KnapsackInstance = instance,
                NumberOfSteps    = numberOfSteps,
                Configuration    = BestConfiguration
            };

            return(result);
        }
예제 #4
0
        public static bool ConstructiveComparator(ConstructiveResult result, KnapsackReferenceSolution referenceSolution)
        {
            //Calculate and save epsilon
            var optimalPrice = referenceSolution.Price;
            var actualPrice  = result.Configuration.Price;

            if (Math.Max(actualPrice, optimalPrice) == 0)
            {
                result.Epsilon = 0;
            }
            else
            {
                result.Epsilon = (float)Math.Abs(actualPrice - optimalPrice) / Math.Max(actualPrice, optimalPrice);
            }

            if (result.Configuration.Price != referenceSolution.Price || result.Configuration.Weight > result.KnapsackInstance.KnapsackSize)
            {
                Console.WriteLine($"Permutation instance solution (id {result.KnapsackInstance.Id}) incorrect," +
                                  $" result: {OutputWriter.ItemVectorToString(result.Configuration.ItemVector)} (price {result.Configuration.Price})" +
                                  $" expected result: {OutputWriter.ItemVectorToString(referenceSolution.ItemVector)} (price {referenceSolution.Price})");
                return(false);
            }
            return(true);
        }