Exemplo n.º 1
0
        /// <inheritdoc/>
        /// <summary>
        /// Looks for optimal solution of a function
        /// </summary>
        public virtual Harmony <T> SearchForHarmony()
        {
            InitializeHarmonyMemory();

            ImprovisationCountWithTheSameBestValue = 0;
            ImprovisationCount = 0;

            while (SearchingShouldContinue())
            {
                var harmonyMemoryConsiderationRatio = ParameterProvider.HarmonyMemoryConsiderationRatio;
                var pitchAdjustmentRatio            = ParameterProvider.PitchAdjustmentRatio;

                var improvisedHarmony = HarmonyGenerator.ImproviseHarmony(harmonyMemoryConsiderationRatio, pitchAdjustmentRatio);

                var worstHarmony = HarmonyMemory.WorstHarmony;

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

                SaveBestHarmony(HarmonyMemory.BestHarmony);

                ImprovisationCount++;
            }

            return(HarmonyMemory.BestHarmony);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Resets this instance.
        /// </summary>
        public virtual void Reset()
        {
            ImprovisationCount = 0;
            ImprovisationCountWithTheSameBestValue = 0;
            BestHarmonyObjectiveValue = double.PositiveInfinity;

            HarmonyMemory.Clear();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes harmony memory with random solutions
        /// </summary>
        public virtual void InitializeHarmonyMemory()
        {
            for (var i = 0; i < HarmonyMemory.MaxCapacity; i++)
            {
                var generateRandomHarmony = HarmonyGenerator.GenerateRandomHarmony();

                HarmonyMemory.Add(generateRandomHarmony);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Merges solutions from Harmony Memory and generated by Ant Colony optimization
        /// </summary>
        /// <param name="antColonySolutions">Ant colony solutions</param>
        private void MergeHarmonyMemoryWithAntSolutions(IEnumerable <Harmony <T> > antColonySolutions)
        {
            var allHarmonies = HarmonyMemory.GetAll()
                               .Concat(antColonySolutions)
                               .OrderBy(h => h.ObjectiveValue)
                               .ToList();

            HarmonyMemory.Clear();

            HarmonyMemory.AddRange(allHarmonies);
        }
Exemplo n.º 5
0
        /// <summary>
        /// The constructor
        /// </summary>
        /// <param name="harmonyGenerator">Harmony generator</param>
        /// <param name="parameterProvider"></param>
        /// <param name="harmonyMemorySize">Harmony memory size</param>
        /// <param name="maxImprovisationCount">Maximal improvisation count</param>
        public HarmonySearcher(IHarmonyGenerator <T> harmonyGenerator, IParameterProvider parameterProvider, int harmonyMemorySize = DefaultHarmonyMemorySize, long maxImprovisationCount = DefaultMaxImprovisationCount)
        {
            MaxImprovisationCount = maxImprovisationCount;

            HarmonyGenerator  = harmonyGenerator ?? throw new ArgumentNullException(nameof(harmonyGenerator));
            ParameterProvider = parameterProvider ?? throw new ArgumentNullException(nameof(parameterProvider));

            HarmonyMemory = new HarmonyMemory <T>(harmonyMemorySize);
            HarmonyGenerator.HarmonyMemory = HarmonyMemory;

            BestHarmonyObjectiveValue = double.PositiveInfinity;
            MaxImprovisationCountWithTheSameBestValue = (int)(maxImprovisationCount / 10);
        }
Exemplo n.º 6
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);
        }