Evolution/Evolution/Core/MultithreadedCachedExecutor.cs
/// <summary>
/// Calculates the fitness of given individuals
/// </summary>
/// <param name="original">The original.</param>
/// <returns></returns>
Calculates the fitness of given individuals
Calculates the fitness of given individuals
public List<Individual<G, F>> UpdateFitness(List<Individual<G, F>> original)
        {
            Dictionary<G, int> waitUntilCalculation = new Dictionary<G, int>();
            List<Individual<G, F>> result = new List<Individual<G, F>>();
            foreach (Individual<G, F> individual in original)
            {
                F cachedFitness;
                if (fitnessCache.TryGet(individual.Genotype, out cachedFitness))
                {
                    result.Add(new Individual<G, F>(individual.Genotype, cachedFitness));
                }
                else
                {
                    int numberToCreate;
                    if (waitUntilCalculation.TryGetValue(individual.Genotype, out numberToCreate))
                        waitUntilCalculation.Remove(individual.Genotype);
                    numberToCreate++;
                    waitUntilCalculation.Add(individual.Genotype, numberToCreate);
                }
            }
            List<Individual<G, F>> newCalculatedFitneses =
                AddToQueueAndWait(i => new Individual<G, F>(i, FitnessFunction(i)),
                    waitUntilCalculation.Keys);
            Debug.Assert(newCalculatedFitneses.Count == waitUntilCalculation.Count);
            foreach (Individual<G, F> calculatedIndividual in newCalculatedFitneses)
            {
                fitnessCache.Add(calculatedIndividual.Genotype, calculatedIndividual.Fitness);
                int numberToCreate = waitUntilCalculation[calculatedIndividual.Genotype];
                result.AddRange(Enumerable.Repeat(calculatedIndividual, numberToCreate));
            }
            return result;
        }
public List < Individual < G , F > > UpdateFitness ( List < Individual < G , F > > original ) { Dictionary < G , int > waitUntilCalculation = new Dictionary < G , int > ( ) ; List < Individual < G , F > > result = new List < Individual < G , F > > ( ) ; foreach ( Individual < G , F > individual in original ) { F cachedFitness ; if ( fitnessCache . TryGet ( individual . Genotype , out cachedFitness ) ) { result . Add ( new Individual < G , F > ( individual . Genotype , cachedFitness ) ) ; } else { int numberToCreate ; if ( waitUntilCalculation . TryGetValue ( individual . Genotype , out numberToCreate ) ) waitUntilCalculation . Remove ( individual . Genotype ) ; numberToCreate ++ ; waitUntilCalculation . Add ( individual . Genotype , numberToCreate ) ; } } List < Individual < G , F > > newCalculatedFitneses = AddToQueueAndWait ( i => new Individual < G , F > ( i , FitnessFunction ( i ) ) , waitUntilCalculation . Keys ) ; Debug . Assert ( newCalculatedFitneses . Count == waitUntilCalculation . Count ) ; foreach ( Individual < G , F > calculatedIndividual in newCalculatedFitneses ) { fitnessCache . Add ( calculatedIndividual . Genotype , calculatedIndividual . Fitness ) ; int numberToCreate = waitUntilCalculation [ calculatedIndividual . Genotype ] ; result . AddRange ( Enumerable . Repeat ( calculatedIndividual , numberToCreate ) ) ; } return result ; }
Calculates the fitness of given individuals
Calculates the fitness of given individuals
None
original
List<Individual<G, F>>
None