Beispiel #1
0
        static Generation Train(IGenTrainer trainer, DataSet seed, Run run, int generationNum, Action <int, int, int> onGenerationProgress, MixtureInfo[] pop)
        {
            var gen = new Generation(run, generationNum);

            trainer.Train(seed, gen, pop.Select(mi => new MixtureInfo(new Mixture(gen, mi.Parents).Id, mi.Chromosomes)).ToArray(),
                          progress => onGenerationProgress.Fire(generationNum, progress.Completed, progress.Total));
            return(gen);
        }
Beispiel #2
0
        public static Generation MakeInitialGeneration(DataSet seed, Run run, IGenTrainer trainer)
        {
            var gen = new Generation(run, 0);

            Func <int, NetworkType, int, Chromosome[]> makeChromosomes = (n, type, orderOffset) =>
                                                                         Lists.Repeat(n, i => MakeRandomChromosome(type, run.ProtoRun.ProtoChromosome, orderOffset + i)).ToArray();

            Func <Chromosome[]> makeMixtureChromosomes = () =>
                                                         makeChromosomes(run.ProtoRun.RnnPerMixture, NetworkType.Rnn, 0).Concat(
                makeChromosomes(run.ProtoRun.RbfPerMixture, NetworkType.Rbf, run.ProtoRun.RnnPerMixture)).ToArray();

            Func <Mixture> makeMixture = () => new Mixture(gen, new Mixture[0]);

            Func <Mixture, MixtureInfo> makeMixtureInfo = m => new MixtureInfo(m.Id, makeMixtureChromosomes());

            var pop = Lists.Repeat(run.ProtoRun.MixturesPerGeneration, _ => makeMixture()).Select(makeMixtureInfo);

            trainer.Train(seed, gen, pop, progress => Console.WriteLine("Initialized {0} of {1}", progress.Completed, progress.Total));

            return(gen);
        }
Beispiel #3
0
        public static Run Evolve(ProtoRun protoRun, IGenTrainer trainer, DataSet trainingSet, DataSet validationSet,
                                 string symbol = null, DateTime startDate = default(DateTime), DateTime endDate = default(DateTime), double validationPct = 0,
                                 Action <int, int, int> onGenerationProgress = null,
                                 Action <Generation> onGenerationComplete    = null)
        {
            var run = new Run(protoRun, protoRun.ProtoChromosome, null, startDate, endDate, validationPct);
            var gen = Initialization.MakeInitialGeneration(trainingSet, run, trainer);

            while (true)
            {
                var evaluated = Evaluate(gen, validationSet);
                onGenerationComplete(gen);
                if (gen.Order == protoRun.NumGenerations - 1)
                {
                    return(run);
                }

                gen = Train(trainer, trainingSet, run, gen.Order + 1, onGenerationProgress,
                            Mutate(run,
                                   Combine(protoRun.MixturesPerGeneration,
                                           Select(protoRun.SelectionSize,
                                                  evaluated))));
            }
        }