示例#1
0
        static LibraryResult ComputeLibraryPotential(Library library)
        {
            // var watch = System.Diagnostics.Stopwatch.StartNew();
            LibraryResult result = new LibraryResult()
            {
                Library = library
            };

            int productiveDays = _daysRemaining - library.SignupDuration;

            if (productiveDays <= 0)
            {
                result.ScoreYield = 0;

                return(result);
            }
            ;

            int[] notScannedBookIds = library.BookIds.Except(_alreadyScanned).ToArray();
            int   maxBookYield      = Math.Min((productiveDays * library.ShippableBookCount), notScannedBookIds.Length);

            IEnumerable <KeyValuePair <int, int> > chosenBooksWithScores = notScannedBookIds
                                                                           // .AsParallel()
                                                                           .Select(i => new KeyValuePair <int, int>(i, _bookScores[i]))
                                                                           .OrderByDescending(pair => pair.Value)
                                                                           // .AsSequential()
                                                                           .Take(maxBookYield);

            // IEnumerable<KeyValuePair<int, int>> chosenBooksWithScores = _bookScores
            //     .Where(bookScore => notScannedBookIds.Contains(bookScore.Key))
            //     .OrderByDescending(pair => pair.Value)
            //     .Take(maxBookYield);

            result.ScoreYield     = chosenBooksWithScores.Sum(pair => pair.Value);
            result.BookIdSequence = chosenBooksWithScores.Select(pair => pair.Key);


            // watch.Stop();
            // var elapsedMs = watch.ElapsedMilliseconds;
            // DateTime stop = DateTime.Now;

            // TimeSpan timeSpan = stop - start;
            // Console.WriteLine("Duration for ComputeLibraryPotential: " + new TimeSpan(watch.ElapsedTicks));
            // Console.WriteLine("Computed library " + library.Id + ". Score: " + result.ScoreYield);
            return(result);
        }
示例#2
0
        private static void DutzuGreedy(string inputFileName)
        {
            Console.WriteLine("Processing file: " + inputFileName);
            InputData inputData = ReadInputData(inputFileName);

            Console.WriteLine("Finished reading input data. Received " + inputData.Libraries.Count + " libraries.");

            _daysRemaining = inputData.DayCount;
            _bookScores    = inputData.BookScores;

            int currentScore = 0;
            List <LibraryResult> chosenLibraryResults = new List <LibraryResult>();

            LibraryResult x = GetNextBestLibrary(inputData.Libraries);

            while (true)
            {
                LibraryResult nextBestLibrary = GetNextBestLibrary(inputData.Libraries);
                if (nextBestLibrary == null)
                {
                    break;
                }
                if (nextBestLibrary.ScoreYield == 0)
                {
                    break;
                }

                chosenLibraryResults.Add(nextBestLibrary);
                inputData.Libraries.Remove(nextBestLibrary.Library);

                _alreadyScanned.AddRange(nextBestLibrary.BookIdSequence);
                currentScore   += nextBestLibrary.ScoreYield;
                _daysRemaining -= nextBestLibrary.Library.SignupDuration;

                // Console.WriteLine("Next best library: " + nextBestLibrary.Library.Id + " - score: " +
                // nextBestLibrary.ScoreYield + ". Remaining days: " + _daysRemaining);
            }

            Console.WriteLine("Total Score: " + currentScore);

            WriteOutput("out_" + Path.GetFileName(inputFileName), chosenLibraryResults);
        }
示例#3
0
        static LibraryResult GetNextBestLibrary(IEnumerable <Library> availableLibraries)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();

            LibraryResult mostProfitableLibrary = availableLibraries.AsParallel()
                                                  .Select(ComputeLibraryPotential)
                                                  .OrderByDescending(libraryResult => libraryResult?.ScoreYield)
                                                  .FirstOrDefault();

            // ParallelLoopResult parallelLoopResult = Parallel.ForEach(availableLibraries, library => ComputeLibraryPotential(library));
            // availableLibraries.AsParallel().Select(ComputeLibraryPotential)
            //     .OrderByDescending(libraryResult => libraryResult.ScoreYield)
            //     .ToArray();

            watch.Stop();
            // var elapsedMs = watch.ElapsedMilliseconds;
            // Console.WriteLine("Duration for GetNextBestLibrary: " + elapsedMs);
            Console.WriteLine("Duration for GetNextBestLibrary: " + new TimeSpan(watch.ElapsedTicks) + " id:" + mostProfitableLibrary.Library.Id + " - score - " + mostProfitableLibrary.ScoreYield);
            return(mostProfitableLibrary);
        }