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; MaximumNumberOfFitnessFunctionEvaluations = MyParameters.MaximumNumberOfFitnessFunctionEvaluations; var myParameters = (FSPParameters)MyParameters; CalculateRankingPhrasePosition(); SortLengths(); CapturePoints = new List <FSPSolution>(); var pi = new List <FSPSolution>(); // Local memory of the best solution found in each neighborhood of the capture point var lemt = new List <TabuMemory>(); Best = new FSPSolution(this); // Population initialization, fitness is calculated for each agent and local search is applied for (var i = 0; i < myParameters.N; i++) { var newCapturePoint = new FSPSolution(this); newCapturePoint.RandomInitialization(); CapturePoints.Add(newCapturePoint); pi.Add(new FSPSolution(newCapturePoint)); lemt.Add(new TabuMemory(myParameters.Tenure)); lemt[lemt.Count - 1].Include(newCapturePoint.SelectedPhrases); if (CurrentFFEs > MaximumNumberOfFitnessFunctionEvaluations) // MaxFFEs exceeded? { break; } } CapturePoints.Sort((x, y) => - 1 * x.Fitness.CompareTo(y.Fitness)); pi.Sort((x, y) => - 1 * x.Fitness.CompareTo(y.Fitness)); Best = new FSPSolution(CapturePoints[0]); while (CurrentFFEs < MaximumNumberOfFitnessFunctionEvaluations) { for (var i = 0; i < myParameters.N; i++) { // Each fisherman at his capture throws the net L times for (var auxL = 0; auxL < myParameters.L; auxL++) { // Throw the network, update PI and Best if it is the case var mejorsolucionLocal = CapturePoints[i].GenerateNeighbor(lemt[i]); lemt[i].Include(mejorsolucionLocal.SelectedPhrases); if (mejorsolucionLocal.Fitness > pi[i].Fitness) { pi[i] = new FSPSolution(mejorsolucionLocal); } if (CurrentFFEs >= MaximumNumberOfFitnessFunctionEvaluations) { break; } } if (pi[i].Fitness > CapturePoints[i].Fitness) { CapturePoints[i] = new FSPSolution(pi[i]); } if (CapturePoints[i].Fitness > Best.Fitness) { Best = new FSPSolution(CapturePoints[i]); } if (CurrentFFEs >= MaximumNumberOfFitnessFunctionEvaluations) { break; } } CapturePoints.Sort((x, y) => - 1 * x.Fitness.CompareTo(y.Fitness)); pi.Sort((x, y) => - 1 * x.Fitness.CompareTo(y.Fitness)); if (CurrentFFEs >= MaximumNumberOfFitnessFunctionEvaluations) { break; } } var mostRepeated = SelectToCompleteSummary(new List <BaseSolution>(CapturePoints), Best); var phrasesList = SelectPhrasesFromFinalSummary(Best.SelectedPhrases, mostRepeated); return(phrasesList); }