Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }