public void Initialize_PopulationSizeZero_EmptyPopulation()
        {
            var init = new FromTimeSeriesRandomInitializer(null);

            var result = init.Initialize <IChromosome <decimal> >(0, 0, 0);

            Assert.AreEqual(result.Count, 0);
        }
        public void Initialize_PopulationSize3_PopulationWithSizeThree()
        {
            var trainigSet = new double[3][];

            trainigSet[0] = new double[] { 0.1, 0.2, 0.3 };
            trainigSet[1] = new double[] { 1.1, 1.2, 1.3 };
            trainigSet[2] = new double[] { 2.1, 2.2, 2.3 };

            var init = new FromTimeSeriesRandomInitializer(trainigSet);

            var result = init.Initialize <decimal>(3, 1, 0.1m);

            Assert.AreEqual(result.Count, 3);
        }
Beispiel #3
0
        public IPredictionResult Predict <TDistribution>(IHiddenMarkovModel <TDistribution> model, IPredictionRequest request) where TDistribution : IDistribution
        {
            var selectionMethod    = new TournamentSelection(TournamentSize);
            var crossoverAlgorithm = new Crossover(CrossoverProbability);
            var mutationAlgorithm  = new Mutator(MutationProbability);
            var evaluator          = new HmmEvaluator <TDistribution>(model, new ForwardBackward(true));

            var parameters = new GeneticSolverParameters
            {
                CrossOverProbability = CrossoverProbability,
                MutationProbability  = MutationProbability,
                NumberOfGenerations  = NumberOfGenerations,
                PopulationSize       = request.NumberOfDays * 10,
                TournamentSize       = TournamentSize
            };
            var predictions = new PredictionResult {
                Predicted = new double[request.NumberOfDays][]
            };

            var solver = new GeneticSolver(parameters, mutationAlgorithm, crossoverAlgorithm, evaluator, selectionMethod);
            var populationInitializer = new FromTimeSeriesRandomInitializer(request.TrainingSet);
            var population            = populationInitializer.Initialize <decimal>(parameters.PopulationSize, request.NumberOfDays, MutationProbability);

            for (int i = 0; i < population.Count; i++)
            {
                var chromosome = population[i];
                population[i].FintnessValue = evaluator.Evaluate(chromosome);
            }

            var result = solver.Solve(population);
            // Get best fitted chromosome
            var maximumFitness = result[0].FintnessValue;
            var solution       = result[0];

            foreach (var chromosome in result)
            {
                if (maximumFitness <= chromosome.FintnessValue)
                {
                    solution       = chromosome;
                    maximumFitness = chromosome.FintnessValue;
                }
            }
            // Convert it to array
            for (int i = 0; i < solution.Representation.Length; i++)
            {
                predictions.Predicted[i] = Array.ConvertAll(solution.Representation[i].Representation, x => (double)Convert.ChangeType(x, typeof(double)));
            }

            return(predictions);
        }
Beispiel #4
0
        public void Solve_InitialPopulationFromTimeSeriesAndTrainedHmmEvaluator_SolvedPopulation()
        {
            var util   = new TestDataUtils();
            var series = util.GetSvcData(util.GOOGFilePath, new DateTime(2010, 12, 18), new DateTime(2011, 12, 18));
            var test   = util.GetSvcData(util.GOOGFilePath, new DateTime(2011, 12, 18), new DateTime(2012, 01, 18));
            var model  = (HiddenMarkovModelMixtureDistribution)HiddenMarkovModelFactory.GetModel(new ModelCreationParameters <Mixture <IMultivariateDistribution> >()
            {
                NumberOfComponents = NumberOfComponents, NumberOfStates = NumberOfStates
            });

            model.Normalized = true;
            model.Train(series, NumberOfIterations, LikelihoodTolerance);

            var selectionMethod    = new TournamentSelection(TournamentSize);
            var crossoverAlgorithm = new Crossover(CrossoverProbability);
            var mutationAlgorithm  = new Mutator(MutationProbability);

            var evaluator = new HmmEvaluator <Mixture <IMultivariateDistribution> >(model, new ForwardBackward(true));

            var parameters = new GeneticSolverParameters
            {
                CrossOverProbability = CrossoverProbability,
                MutationProbability  = MutationProbability,
                NumberOfGenerations  = 10,
                PopulationSize       = PopulationSize,
                TournamentSize       = TournamentSize
            };
            var populationInitializer = new FromTimeSeriesRandomInitializer(series);
            var population            = populationInitializer.Initialize <decimal>(parameters.PopulationSize, test.Length, MutationProbability);

            for (int i = 0; i < population.Count; i++)
            {
                var chromosome = population[i];
                population[i].FintnessValue = evaluator.Evaluate(chromosome);
            }
            var solver = new GeneticSolver(parameters, mutationAlgorithm, crossoverAlgorithm, evaluator, selectionMethod);

            var result = solver.Solve(population);

            Assert.AreEqual(result.Count, parameters.PopulationSize);
        }