Exemplo n.º 1
0
        public override void Prepare(
            IList <IGenome> sampleGenomes,
            GenomeProductionSession thisSession,
            GenomeProductionSession totalSession,
            int totalNbToSelect)
        {
            base.Prepare(
                sampleGenomes,
                thisSession,
                totalSession,
                totalNbToSelect);

            var samplesCount = ComputeParticipantsCount(sampleGenomes.Count());
            var candidates   = sampleGenomes.Take(samplesCount).ToList();

            var minFitness = candidates.Min(x => x.Fitness);

            if (minFitness < 0)
            {
                minFitness *= -1;
            }
            else
            {
                minFitness = 0;
            }

            genomeAndFitn = candidates.ToDictionary(
                x => x,
                x => x.Fitness + minFitness + float.Epsilon);

            usedSetsOfGenomes = new List <IGenome[]>();
        }
Exemplo n.º 2
0
        public IList <IGenome> Produce(
            int totalRequired,
            IGenerationManager generationManager)
        {
            var totalSession = new GenomeProductionSession(totalRequired);

            foreach (var producer in Producers)
            {
                ExecuteProducer(generationManager, totalSession, producer);
            }

            while (totalSession.CurrentlyProduced.Count < totalRequired)
            {
                var currentCount = totalSession.CurrentlyProduced.Count;
                ExecuteProducer(generationManager, totalSession, Producers.Last());

                if (currentCount == totalSession.CurrentlyProduced.Count)
                {
                    throw new Exception("The last producer MUST yield at least " +
                                        "one genome");
                }
            }

            Trace.Assert(totalSession.CurrentlyProduced.Count == totalRequired);

            return(totalSession.CurrentlyProduced);
        }
Exemplo n.º 3
0
        protected override IList <IGenome> DoProduction(
            IList <IGenome> sampleGenomes,
            GenomeProductionSession thisSession,
            GenomeProductionSession totalSession)
        {
            var selections     = (int)Math.Ceiling((float)thisSession.requiredNb / Crossover.NbOfChildren);
            var totalNbToSelct = selections * Crossover.NbOfParents;

            Selection.Prepare(
                sampleGenomes,
                thisSession,
                totalSession,
                totalNbToSelect: totalNbToSelct);

            Crossover.Prepare(
                sampleGenomes,
                thisSession,
                totalSession);

            while (thisSession.CurrentlyProduced.Count < thisSession.requiredNb)
            {
                var parents  = Selection.Select(Crossover.NbOfParents).ToArray();
                var children = Crossover.Cross(parents);

                var usedChildren = thisSession.AddNewProducedGenomes(children);

                foreach (var child in usedChildren)
                {
                    this.MutationManager.ApplyMutations(child);
                }
            }

            return(thisSession.CurrentlyProduced);
        }
Exemplo n.º 4
0
        public override void Prepare(
            IList <IGenome> sampleGenomes,
            GenomeProductionSession thisSession,
            GenomeProductionSession totalSession,
            int totalNbToSelect)
        {
            base.Prepare(
                sampleGenomes,
                thisSession,
                totalSession,
                totalNbToSelect);

            var samplesCount = ComputeParticipantsCount(sampleGenomes.Count());
            var candidates   = sampleGenomes.Take(samplesCount).ToList();

            var minFitness = candidates.Min(x => x.Fitness);

            if (minFitness < 0)
            {
                minFitness *= -1;
            }
            else
            {
                minFitness = 0;
            }

            var genomAndFitn = candidates.ToDictionary(
                x => x,
                x => x.Fitness + minFitness + float.Epsilon);
            var fitnessSum = genomAndFitn.Values.Sum();

            allParents = new Queue <IGenome>(totalNbToSelect);
            for (int i = 0; i < totalNbToSelect; i++)
            {
                var     targetFitness = GARandomManager.NextFloat(0, fitnessSum);
                IGenome target        = null;

                foreach (var pair in genomAndFitn)
                {
                    if (targetFitness <= pair.Value)
                    {
                        target = pair.Key;
                        break;
                    }
                    else
                    {
                        targetFitness -= pair.Value;
                    }
                }

                Debug.Assert(target != null);

                allParents.Enqueue(target);
                fitnessSum -= genomAndFitn[target];
                genomAndFitn.Remove(target);
            }
        }
Exemplo n.º 5
0
 public virtual void Prepare(
     IEnumerable <IGenome> sampleGenomes,
     GenomeProductionSession thisSession,
     GenomeProductionSession totalSession)
 {
     this.sampleGenomes = sampleGenomes;
     this.thisSession   = thisSession;
     this.totalSession  = totalSession;
 }
Exemplo n.º 6
0
        public IList <IGenome> Produce(
            IList <IGenome> sampleGenomes,
            GenomeProductionSession thisSession,
            GenomeProductionSession totalSession)
        {
            var result = DoProduction(sampleGenomes, thisSession, totalSession);

            Trace.Assert(result.EveryoneIsUnique());
            return(result);
        }
Exemplo n.º 7
0
 public virtual void Prepare(
     IList <IGenome> sampleGenomes,
     GenomeProductionSession thisSession,
     GenomeProductionSession totalSession,
     int totalNbToSelect)
 {
     this.sampleGenomes   = sampleGenomes;
     this.thisSession     = thisSession;
     this.totalSession    = totalSession;
     this.totalNbToSelect = totalNbToSelect;
 }
Exemplo n.º 8
0
        protected void ExecuteProducer(
            IGenerationManager generationManager,
            GenomeProductionSession totalSession,
            IGenomeProducer producer)
        {
            var requiredNb = GetProducerNbOfGenomesToMake(
                totalSession.requiredNb,
                totalSession.requiredNb - totalSession.CurrentlyProduced.Count,
                producer);

            var thisSession = new GenomeProductionSession(requiredNb);

            producer.Produce(
                generationManager.GetGenomes().ToArray(),
                thisSession,
                totalSession
                );

            totalSession.Merge(thisSession);
        }
        public IList <IGenome> Produce(
            IList <IGenome> sampleGenomes,
            GenomeProductionSession thisSession,
            GenomeProductionSession totalSession)
        {
            selection.Prepare(
                sampleGenomes,
                thisSession,
                totalSession,
                thisSession.requiredNb);

            var participants = selection.Select(thisSession.requiredNb);
            var produced     = participants.Select(x => x.CreateNew(x.Genes))
                               .ToArray();

            Trace.Assert(produced.EveryoneIsUnique());

            thisSession.RegisterParticipants(participants);
            thisSession.AddNewProducedGenomes(produced);

            return(produced);
        }
Exemplo n.º 10
0
        public override void Prepare(
            IList <IGenome> sampleGenomes,
            GenomeProductionSession thisSession,
            GenomeProductionSession totalSession,
            int totalNbToSelect)
        {
            base.Prepare(
                sampleGenomes,
                thisSession,
                totalSession,
                totalNbToSelect);

            if (sampleGenomes.Count() < totalNbToSelect)
            {
                var msg = string.Format("Not enough samples: {0} available {1} are asked",
                                        sampleGenomes.Count(), totalNbToSelect);
                throw new Exception(msg);
            }

            var parents = sampleGenomes.OrderByDescending(g => g.Fitness)
                          .Take(totalNbToSelect);

            allParents = new Queue <IGenome>(parents);
        }
Exemplo n.º 11
0
 protected abstract IList <IGenome> DoProduction(
     IList <IGenome> sampleGenomes,
     GenomeProductionSession thisSession,
     GenomeProductionSession totalSession);