public DSFilePlaybackEVR(Device _device) { m_Device = _device; m_SamplePool = new SamplePool(m_evStop); m_scheduler.SetCallback(this); }
private static void Selection() { // stats of each chromosome of the pool SortedSet <KeyValuePair <ushort, EvoStats> > evoStats = new SortedSet <KeyValuePair <ushort, EvoStats> >( SamplePool.Select((c, i) => new KeyValuePair <ushort, EvoStats>((ushort)i, c.Select())), new EvoStatsComparer() ); //SortedList<EvoStats, int> evoStats = new SortedList<EvoStats, int>(SAMPLE_POOL_SIZE, new EvoStatsComparer()); // inferior chromosomes aren't replaced so much as they are shifted down int currentPoolSize = POOL_SIZE; List <Chromosome> oldPool = new List <Chromosome>(Pool); int statsPointer = 0; int startingIndex = -1; int currentNumFit = PoolNumFit; var candidates = evoStats.Take(POOL_SIZE); var first = candidates.First(); IEnumerator <KeyValuePair <ushort, EvoStats> > candidatesEnumerator = null; void ReplaceFit() { candidatesEnumerator.MoveNext(); for (int i = startingIndex; i < currentNumFit; ++i) { var current = candidatesEnumerator.Current.Value; if (current.Fitness >= PoolLastStats[statsPointer]) { oldPool.Insert(i, SamplePool[candidatesEnumerator.Current.Key]); ++currentNumFit; ++currentPoolSize; candidatesEnumerator.MoveNext(); } else { ++statsPointer; if (statsPointer == PoolNumFit) { break; } } } } void ReplaceUnfit() { candidatesEnumerator.MoveNext(); for (int i = currentNumFit; i < currentPoolSize; ++i) { var current = candidatesEnumerator.Current.Value; if (current.Survives || current.Fertility >= PoolLastStats[statsPointer]) { oldPool.Insert(i, SamplePool[candidatesEnumerator.Current.Key]); ++currentPoolSize; candidatesEnumerator.MoveNext(); } else { // essentially searching for the next startingIndex for the remaining candidates ++statsPointer; if (statsPointer == POOL_SIZE) { break; } } } } if (PoolMinFitness != 0) { if (first.Value.Fitness >= PoolMinFitness) { // using greater-than-or-equals-to for diversity startingIndex = Array.FindIndex(PoolLastStats, s => s <= first.Value.Fitness); statsPointer = startingIndex; if (startingIndex >= PoolNumFit) { // if the only replaceable chromosomes are unfit ones candidates = candidates.TakeWhile( c => c.Value.Survives || c.Value.Fertility >= PoolMinFitness ); candidatesEnumerator = candidates.GetEnumerator(); ReplaceUnfit(); } else if (PoolNumFit != POOL_SIZE) { candidates = candidates.TakeWhile( c => c.Value.Survives || c.Value.Fertility >= PoolMinFitness ); candidatesEnumerator = candidates.GetEnumerator(); ReplaceFit(); ReplaceUnfit(); } else { candidates = candidates.TakeWhile(c => c.Value.Fitness >= PoolMinFitness); candidatesEnumerator = candidates.GetEnumerator(); ReplaceFit(); } } } candidatesEnumerator.Dispose(); Pool = oldPool.Take(16).ToArray(); PoolLastStats = Pool.Select(c => c.Stats.GetStat()).ToArray(); PoolMinFitness = PoolLastStats[POOL_SIZE - 1]; Array.Clear(SamplePool, 0, SAMPLE_POOL_SIZE); }