/// <summary> /// Regroups sub Harmony Memories /// </summary> private void RegroupHarmonyMemories() { var allSolutions = _subHarmonyMemories.SelectMany(h => h.GetAll()).ToList(); var solutionIds = Enumerable.Range(0, allSolutions.Count).ToList(); foreach (var subHarmonyMemory in _subHarmonyMemories) { subHarmonyMemory.Clear(); for (var index = 0; index < subHarmonyMemory.MaxCapacity; index++) { var randomIndex = solutionIds.GetRandomElement(); var randomSolution = allSolutions.ElementAt(randomIndex); if (subHarmonyMemory.Add(new Harmony <T>(randomSolution))) { solutionIds.Remove(randomIndex); } } } foreach (var subHarmonyMemory in _subHarmonyMemories.Where(hm => hm.Count < hm.MaxCapacity)) { while (subHarmonyMemory.Count < subHarmonyMemory.MaxCapacity) { var randomHarmony = HarmonyGenerator.GenerateRandomHarmony(); subHarmonyMemory.Add(randomHarmony); } } }
void Awake() { rb2d = GetComponent <Rigidbody2D>(); movementVector.Normalize(); scoreTracker = GameObject.Find("ScoreTracker").GetComponent <ScoreTracker>(); audio = GetComponent <AudioSource>(); harmonyGenerator = new HarmonyGenerator(); trail = GetComponent <TrailRenderer>(); virtualCameraNoise = virtualCamera.GetCinemachineComponent <CinemachineBasicMultiChannelPerlin>(); SetUpColours(); }
/// <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); }
/// <summary> /// Looks for optimal solution of a function /// </summary> /// <returns></returns> /// <inheritdoc/> public override Harmony <T> SearchForHarmony() { InitializeHarmonyMemory(); ImprovisationCount = 0; ImprovisationCountWithTheSameBestValue = 0; while (SearchingShouldContinue()) { if (ImprovisationCount > 0 && ImprovisationCount % RegroupRate == 0) { RegroupHarmonyMemories(); } foreach (var subHarmonyMemory in _subHarmonyMemories) { // Set the right harmony memory HarmonyGenerator.HarmonyMemory = subHarmonyMemory; var harmonyMemoryConsiderationRatio = ParameterProvider.HarmonyMemoryConsiderationRatio; var pitchAdjustmentRatio = ParameterProvider.PitchAdjustmentRatio; var improvisedHarmony = HarmonyGenerator.ImproviseHarmony(harmonyMemoryConsiderationRatio, pitchAdjustmentRatio); var worstHarmony = subHarmonyMemory.WorstHarmony; if (improvisedHarmony.IsBetterThan(worstHarmony) && !subHarmonyMemory.Contains(improvisedHarmony)) { subHarmonyMemory.SwapWithWorstHarmony(improvisedHarmony); } SaveBestHarmony(GetBestHarmony()); ImprovisationCount++; } } return(GetBestHarmony()); }
/// <inheritdoc/> /// <summary> /// Initializes a collection of sub-harmony memories with random solutions /// </summary> public override void InitializeHarmonyMemory() { _subHarmonyMemories = new List <HarmonyMemory <T> >(4); var subHarmonyMemoriesCount = (int)Math.Ceiling((decimal)HarmonyMemory.MaxCapacity / 4); for (var i = 0; i < _subHarmonyMemories.Capacity; i++) { _subHarmonyMemories.Add(new HarmonyMemory <T>(subHarmonyMemoriesCount)); } foreach (var subHarmonyMemory in _subHarmonyMemories) { var triesCount = 0; while (subHarmonyMemory.Count < subHarmonyMemory.MaxCapacity && triesCount < subHarmonyMemory.MaxCapacity * 3) { var randomHarmony = HarmonyGenerator.GenerateRandomHarmony(); subHarmonyMemory.Add(randomHarmony); triesCount++; } } }