public void AddCreature(RatedCreature <Creature> newCreature) { if (IsBetterThanBest(newCreature)) { HandleNewBest(newCreature); } if (heap.IsFull) { if (newCreature.CompareTo(heap.PeekMin()) > 0) //if newCreature is better than the worst of creatures in heap { Creature theWorst = heap.ExtractMin().TheCreature; TrySaveDisposedCreature(theWorst); heap.Insert(newCreature); } else { TrySaveDisposedCreature(newCreature.TheCreature); } } else { heap.Insert(newCreature); } }
private void ComputeOneGenaration() { IEnumerable <Creature> newGeneration = mutationManager.GetChildren(); foreach (var child in newGeneration) { RatedCreature <Creature> evaluatedChild = EvaluateCreature(child); AddCreatureToSelector(evaluatedChild); } }
public DefaultSelector( NewBestCretureFoundEventDelegate <Creature> newBestFound, RatedCreature <Creature> foreFather, int NumberOfSurvivals, DisposedCreaturesStore <Creature> disposedCreatures) { NewBestCretureFound = newBestFound; DisposedCreatures = disposedCreatures; heap = new HeapOfMaximalSize <RatedCreature <Creature> >(NumberOfSurvivals); bestCreature = foreFather; AddCreature(foreFather); }
/// <summary> /// Return next creature and throws exception if set is empty /// </summary> public RatedCreature <Creature> GetNextCreature() { if (IsEmpty) { throw new PoolIsEmptyException(); } RatedCreature <Creature> toReturn = enumeratorOfCreatures.Current; IsEmpty = !enumeratorOfCreatures.MoveNext(); return(toReturn); }
private void HandleNewBest(RatedCreature <Creature> newBestCreature) { bestCreature = newBestCreature; NewBestCretureFound?.Invoke(newBestCreature.TheCreature, newBestCreature.FitnessValue); }
private bool IsBetterThanBest(RatedCreature <Creature> creature) => bestCreature.CompareTo(creature) < 0;
private void AddCreatureToSelector(RatedCreature <Creature> creature) { lock (myEnvironment.Selector) myEnvironment.Selector.AddCreature(creature); }
public int CompareTo(RatedCreature <Creature> other) => FitnessValue.CompareTo(other.FitnessValue);