private Reduct ChooseNextNeighbor(IEnumerable <Reduct> neighborsList, out int?indexOfIndividualChange) { Reduct bestNeighbor = null; indexOfIndividualChange = null; foreach (var neighbor in neighborsList) { var index = GetIndexOfIndividualStringChange(neighbor); if (index == null) { continue; } // ReSharper disable once InvertIf if ((ShouldChangeBestSolution(neighbor) || _tabuList[(int)index] == 0)) // && //(neighbor.Approximation >= 0.9 * AllAttributesSolution.Approximation || neighbor.FitnessFunction <= BestSolution.FitnessFunction) { bestNeighbor = neighbor; indexOfIndividualChange = index; break; } } return(bestNeighbor); }
private void SetInitialSolution() { var individual = BinaryStringHelper.GenerateRandomIndividual(IndividualLength); ActualSolution = new Reduct(individual, ClusteredDataObjects); CheckedReducts.Add(ActualSolution); }
public override void Calculate() { CalculateApproximationForAllAttributes(); SetInitialSolution(); BestSolution = ActualSolution; while (++IterationWithoutImprovementCount != _inputValues.IterationWithoutImprovement) { ++IterationNumber; var neighborsList = GenerateSortedNeighborhoodForActualSolution(); int?indexOfIndividualChange; var bestNeighbor = ChooseNextNeighbor(neighborsList, out indexOfIndividualChange); if (indexOfIndividualChange == null) { continue; } ActualSolution = bestNeighbor; TabuListActualization((int)indexOfIndividualChange); TryToUpdateBestSolution(ActualSolution); AddToIterationResultsList(BestSolution.FitnessFunction, BestSolution.Approximation, BestSolution.Subset.Count); } }
private Reduct MutateIndividual(Reduct reduct) { var individual = reduct.Individual; var mutationIndex = RandomIntNumber(IndividualLength); var newGeneValue = individual[mutationIndex] == '1' ? '0' : '1'; var newIndividual = individual.Substring(0, mutationIndex) + newGeneValue + individual.Substring(mutationIndex + 1); return(new Reduct(newIndividual, ClusteredDataObjects)); }
protected void TryToUpdateBestSolution(Reduct reduct) { if (!ShouldChangeBestSolution(reduct)) { return; } BestSolution = reduct; IterationWithoutImprovementCount = 0; }
private int?GetIndexOfIndividualStringChange(Reduct bestNeighbor) { for (var i = 0; i < IndividualLength; i++) { if (ActualSolution == null || bestNeighbor == null || ActualSolution.Individual[i] == bestNeighbor.Individual[i]) { continue; } return(i); } return(null); }
public override void Calculate() { CalculateApproximationForAllAttributes(); for (var i = 1; i < _numberOfSolutions; i++) { var individual = BinaryStringHelper.ConvertIntToBinaryString(i, IndividualLength); var reduct = new Reduct(individual, ClusteredDataObjects); if (ShouldChangeBestSolution(reduct)) { BestSolution = reduct; } } }
private void GenerateNewSolutionFromNeighbors(Reduct eliteReduct, int neighborhoodSize, int neighborhoodSteps) { var neighborhood = new List <Reduct>(); for (var i = 0; i < neighborhoodSize; i++) { var neighborIndividual = string.Empty; for (var j = 0; j < neighborhoodSteps; j++) { neighborIndividual = BinaryStringHelper.GenerateNeighborSolution(eliteReduct.Individual); } TryAddReductToCheckedReductsList(neighborIndividual); neighborhood.Add(CheckedReducts.FirstOrDefault(r => r.Individual == neighborIndividual)); } ActualPopulation.Individuals.Add(neighborhood.OrderByDescending(r => r.Approximation).ThenBy(i => i.Subset.Count).FirstOrDefault()); }
protected bool ShouldChangeBestSolution(Reduct reduct) { bool shouldChangeBestSolution; if (reduct == null) { shouldChangeBestSolution = false; } else if (BestSolution == null) { shouldChangeBestSolution = true; } else { shouldChangeBestSolution = (reduct.FitnessFunction < BestSolution.FitnessFunction || (reduct.FitnessFunction == BestSolution.FitnessFunction && reduct.Subset.Count < BestSolution.Subset.Count)) && (reduct.Approximation > 0.9 * AllAttributesSolution.Approximation || reduct.Approximation >= BestSolution.Approximation); } return(shouldChangeBestSolution); }
private void CreateNewReduct(string individual) { var reduct = new Reduct(individual, ClusteredDataObjects); CheckedReducts.Add(reduct); }
protected void CalculateApproximationForAllAttributes() { var individual = BinaryStringHelper.GenerateIndividualWithAllAttributes(IndividualLength); AllAttributesSolution = new Reduct(individual, ClusteredDataObjects); }