public FSPSolution GenerateNeighbor(TabuMemory explicitLocalTabuMemory) { var copyCurrentSolution = new FSPSolution(this); var selectedPhrases = copyCurrentSolution.ObtainSelectedPhrasesSortedByCoverageCosine(); // Remove the last sentence (phrases with low coverage) ... C = 1 copyCurrentSolution.InActivate(selectedPhrases[selectedPhrases.Count - 1].Position); var excludePhrases = new List <int> { selectedPhrases[selectedPhrases.Count - 1].Position }; var tries = 0; const int maxTries = 5; FSPSolution newCandidateSolution = null; do { newCandidateSolution = new FSPSolution(copyCurrentSolution); // Try to add one or more randomly selected phrases to complete the solution newCandidateSolution.AddValidPhrases(excludePhrases); if (tries++ < maxTries) { break; // avoid long time in the loop } } while (explicitLocalTabuMemory.IsTabu(newCandidateSolution.SelectedPhrases)); newCandidateSolution.CalculateFitness(); return(newCandidateSolution); }
public List <PositionValue> Execute() { CurrentFFEs = 0; var myParameters = (SFLAParameters)MyParameters; // Setup of Memeplexes Memeplex = new List <Frog> [myParameters.NumberOfMemeplexes]; for (var i = 0; i < myParameters.NumberOfMemeplexes; i++) { Memeplex[i] = new List <Frog>(); } FrogsByMemeplex = myParameters.PondSize / myParameters.NumberOfMemeplexes; // Calculate the ranking of the position of the phrases CalculateRankingPhrasePosition(); // Tabu memory of the specified type is created with the desired amount MyTabuMemory = new TabuMemory(myParameters.Tenure); // Initialize the Pond with frogs Pond = new List <Frog>(); while (Pond.Count < myParameters.PondSize) { var frog = new Frog(this); frog.RandomInitialization(); if (MyTabuMemory.IsTabu(frog.SelectedPhrases)) { continue; } Pond.Add(frog); MyTabuMemory.Include(frog.SelectedPhrases); } Pond.Sort((x, y) => - 1 * x.Fitness.CompareTo(y.Fitness)); GBest = new Frog(Pond[0]); while (CurrentFFEs < MaximumNumberOfFitnessFunctionEvaluations) { ShuffletheFrogs(); LocalSearch(); RegroupTheFrogs(); MutateTheFrogs(); Pond.Sort((x, y) => - 1 * x.Fitness.CompareTo(y.Fitness)); GBest = new Frog(Pond[0]); } var mostRepeated = SelectToCompleteSummary( new List <BaseSolution>(Pond), GBest); var listaFrases = SelectPhrasesFromFinalSummary( GBest.SelectedPhrases, mostRepeated); return(listaFrases); }
private void LocalSearch() { var myParameters = (SFLAParameters)MyParameters; for (var mplex = 0; mplex < myParameters.NumberOfMemeplexes; mplex++) { if (CurrentFFEs > MaximumNumberOfFitnessFunctionEvaluations) { break; } for (var iteration = 0; iteration < myParameters.MaxLocalIterations; iteration++) { if (CurrentFFEs > MaximumNumberOfFitnessFunctionEvaluations) { break; } var bestFrog = new Frog((Memeplex[mplex])[0]); var worstFrog = new Frog((Memeplex[mplex])[FrogsByMemeplex - 1]); var auxiliarFrog = new Frog(worstFrog); auxiliarFrog.Jump(bestFrog); if (MyTabuMemory.IsTabu(auxiliarFrog.SelectedPhrases)) { auxiliarFrog.Fitness = 0; } if (auxiliarFrog.Fitness > worstFrog.Fitness) { (Memeplex[mplex])[FrogsByMemeplex - 1] = auxiliarFrog; } else { auxiliarFrog = new Frog(worstFrog); auxiliarFrog.Jump(GBest); if (MyTabuMemory.IsTabu(auxiliarFrog.SelectedPhrases)) { auxiliarFrog.Fitness = 0; } if (auxiliarFrog.Fitness > worstFrog.Fitness) { (Memeplex[mplex])[FrogsByMemeplex - 1] = auxiliarFrog; } else { auxiliarFrog = new Frog(this); auxiliarFrog.RandomInitialization(); if (!MyTabuMemory.IsTabu(auxiliarFrog.SelectedPhrases)) { (Memeplex[mplex])[FrogsByMemeplex - 1] = auxiliarFrog; } } } if (auxiliarFrog.Fitness > GBest.Fitness) { GBest = new Frog(auxiliarFrog); } } Memeplex[mplex].Sort((x, y) => - 1 * x.Fitness.CompareTo(y.Fitness)); } }