Пример #1
0
        private static void TestFromFile(string fileName, IBackpackAlgorithm algorithm)
        {
            using (StreamReader reader = new StreamReader(fileName))
            {
                var values = reader.ReadLine().Split(' ');

                int    countItems = int.Parse(values[0]);
                double maxMass    = int.Parse(values[1]);

                backpack = new Backpack(maxMass);

                var listOfMass = reader.ReadLine().Split(' ');
                var listOfCost = reader.ReadLine().Split(' ');
                for (int i = 0; i < countItems; i++)
                {
                    backpack.AddItem(Int32.Parse(listOfCost[i]), Int32.Parse(listOfMass[i]));
                }
            }

            using (StreamWriter writer = new StreamWriter("output.txt"))
            {
                var answer = algorithm.Run(backpack, ref sumCost);

                writer.WriteLine(sumCost);
                writer.WriteLine(string.Join(" ", answer));
                Console.WriteLine(sumCost);
                Console.WriteLine(string.Join(" ", answer));
            }
        }
Пример #2
0
        private static void AutoTest(IBackpackAlgorithm algorithm)
        {
            int maxCountItems = 25;

            using (StreamWriter writer = new StreamWriter($"{algorithm.ToString()}.txt"))
            {
                for (int countItems = 1; countItems <= maxCountItems; countItems++)
                {
                    Test.GenTest(countItems, ref backpack);

                    int countIter = 1;
                    if (countItems < 20)
                    {
                        countIter = 2;
                    }
                    if (countItems < 15)
                    {
                        countIter = 20;
                    }
                    if (countItems < 10)
                    {
                        countIter = 2000;
                    }

                    long ticks = 0;

                    for (int k = 0; k < countIter; k++)
                    {
                        stopWatch.Reset();
                        stopWatch.Start();
                        algorithm.Run(backpack, ref sumCost);
                        stopWatch.Stop();
                        ticks += stopWatch.Elapsed.Ticks;
                    }

                    double milliseconds = TimeSpan.FromTicks(ticks / countIter).TotalMilliseconds;

                    Console.WriteLine($"{algorithm} N = {countItems} time = {milliseconds} milliseconds");
                    writer.WriteLine($"{countItems}\t{milliseconds}");
                }
            }
        }