public int Compare(IIndividual x1, IIndividual x2) { var result = _fitnessFunction.GetValue(x1).CompareTo(_fitnessFunction.GetValue(x2)); if (OptimizationMode == EOptimizationMode.Maximize) { result *= -1; } return(result); }
public bool PointSetIsTrunk(PointSet pointSet) { if (pointSet == null) { return(false); } var bestPossibleParameters = FindBestParameters(pointSet); if (bestPossibleParameters is null) { return(false); } var fitness = _fitnessFunction.GetValue(bestPossibleParameters.Phenotype); var isTrunk = fitness <= GetTrunkThreshold(pointSet.Center.Z); if (!isTrunk) { return(false); } foreach (var t in pointSet) { t.Intensity = (float)fitness; } return(true); }
private string RetrieveBestIndividual(ISet <string> population, IFitnessFunction fitnessFn) { string bestIndividual = null; double bestSoFarFValue = double.NegativeInfinity; foreach (string individual in population) { var fitnessValue = fitnessFn.GetValue(individual); if (fitnessValue > bestSoFarFValue) { bestIndividual = individual; bestSoFarFValue = fitnessValue; } } return(bestIndividual); }
public bool Satisfied(IEvolutionaryPopulation population) { if (population == null) { throw new PopulationNotInitializedException(); } if (_fitnessFunction == null) { throw new FitnessFunctionNotInitializedException(); } var comparisonResult = _fitnessFunction.GetValue(population.Best).CompareTo(SufficientResult); comparisonResult *= population.CompareCriteria.OptimizationMode == EOptimizationMode.Minimize ? -1 : 1; return(comparisonResult >= 0); }
private string RandomSelection(ISet <string> population, IFitnessFunction fitnessFn) { string selected = null; // Determine all of the fitness values double[] fValues = new double[population.Count]; string[] popArray = population.ToArray(); for (int i = 0; i < popArray.Length; i++) { fValues[i] = fitnessFn.GetValue(popArray[i]); } // Normalize the fitness values fValues = Util.Util.Normalize(fValues); double prob = random.NextDouble(); double totalSoFar = 0.0; for (int i = 0; i < fValues.Length; i++) { // Are at last element so assign by default // in case there are rounding issues with the normalized values totalSoFar += fValues[i]; if (prob <= totalSoFar) { selected = popArray[i]; break; } } // selected may not have been assigned // if there was a rounding error in the // addition of the normalized values (i.e. did not total to 1.0) if (null == selected) { // Assign the last value selected = popArray[popArray.Length - 1]; } return(selected); }