コード例 #1
0
        /// <summary>
        /// Looks for optimal solution of a function
        /// </summary>
        /// <returns></returns>
        /// <inheritdoc/>
        public override Harmony <T> SearchForHarmony()
        {
            InitializeHarmonyMemory();

            ImprovisationCount = 0;
            ImprovisationCountWithTheSameBestValue = 0;

            while (SearchingShouldContinue())
            {
                var worstHarmony = HarmonyMemory.WorstHarmony;

                // Get parameters
                var harmonyMemoryConsiderationRatio = ParameterProvider.HarmonyMemoryConsiderationRatio;
                var pitchAdjustmentRatio            = ParameterProvider.PitchAdjustmentRatio;

                // Improvise harmony with the new parameters
                var improvisedHarmony = HarmonyGenerator.ImproviseHarmony(harmonyMemoryConsiderationRatio, pitchAdjustmentRatio);

                if (improvisedHarmony.IsBetterThan(worstHarmony) && !HarmonyMemory.Contains(improvisedHarmony))
                {
                    HarmonyMemory.SwapWithWorstHarmony(improvisedHarmony);
                }

                // Update global pheromone using the best harmony
                _antColonyOptimizer.UpdateGlobalPheromone(HarmonyMemory.BestHarmony);

                // Get ant solutions
                var antSolutions = _antColonyOptimizer.GetAntColonySolutions(HarmonyMemory.MaxCapacity);

                // Merge ant solutions with improvised ones
                MergeHarmonyMemoryWithAntSolutions(antSolutions);

                // Save best solution
                SaveBestHarmony(HarmonyMemory.BestHarmony);

                ImprovisationCount++;
            }

            return(HarmonyMemory.BestHarmony);
        }