Exemplo n.º 1
0
        public IEnumerable <InputDataEntity> GetEntities(string filePath)
        {
            var reader = new StreamReader(File.OpenRead(filePath));

            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                if (line == null)
                {
                    continue;
                }
                var rowValues = line.Split(',');
                var entity    = new InputDataEntity
                {
                    Date   = DateTime.Parse(rowValues[0]),
                    Open   = float.Parse(rowValues[1].Replace('.', ',')),
                    High   = float.Parse(rowValues[2].Replace('.', ',')),
                    Low    = float.Parse(rowValues[3].Replace('.', ',')),
                    Close  = float.Parse(rowValues[4].Replace('.', ',')),
                    Volume = int.Parse(rowValues[5])
                };

                yield return(entity);
            }
        }
 public MainPage()
 {
     InitializeComponent();
     InputDataEntity          = new InputDataEntity();
     functionOptimizerService = new FunctionOptimizerService();
     selectionMethodMapper    = new SelectionMethodMapper();
     crossMethodMapper        = new CrossMethodMapper();
     mutationMethodMapper     = new MutationMethodMapper();
 }
        private ISelection GetSelectionMethod(SelectionMethod selectionMethod, InputDataEntity inputDataEntity)
        {
            switch (selectionMethod)
            {
            case (SelectionMethod.Best):
                return(new BestSelection(inputDataEntity.BestSelectionPercentage, inputDataEntity.Maximize));

            case (SelectionMethod.Roulette):
                return(new RouletteSelection(inputDataEntity.BestSelectionPercentage, inputDataEntity.Maximize));

            case (SelectionMethod.Tournament):
                return(new TournamentSelection(inputDataEntity.TournamentAmount, inputDataEntity.Maximize));

            default:
                throw new ArgumentException("Selection method was not foud.");
            }
        }
        protected override void Arrange()
        {
            base.Arrange();
            var reader = MockRepository.GenerateStub <IFileReader>();

            expectedEntity = new InputDataEntity
            {
                Date   = DateTime.Now,
                Open   = 1.1f,
                High   = 2.2f,
                Low    = 3.3f,
                Close  = 4.4f,
                Volume = 1000
            };
            reader.Stub(m => m.GetEntities(Arg <string> .Is.Anything)).Return(new List <InputDataEntity> {
                expectedEntity
            });
            this.fileProcessor = new FileProcessor(reader, "DummyFileName");
        }
        public OptimizationResult Optimize(InputDataEntity inputDataEntity)
        {
            InputDataEntity          = inputDataEntity;
            NumberOfBitsInChromosome = PopulationService.CalculateNumberOfBitsInChromosome(inputDataEntity.DataRange, inputDataEntity.Accuracy);
            BinaryChromosomes        = PopulationService.GeneratePopulation(inputDataEntity.PopulationAmount, NumberOfBitsInChromosome);
            // Method selection
            ISelection selectionMethod                = GetSelectionMethod(InputDataEntity.SelectionMethod, inputDataEntity);
            ICross     crossMethod                    = GetCrossMethod(InputDataEntity.CrossMethod);
            IMutation  mutationMethod                 = GetMutationMethod(InputDataEntity.MutationMethod);
            var        bestOfEachEpoch                = new List <FunctionResult>();
            List <BinaryChromosome> tmpBest           = new List <BinaryChromosome>();
            List <double>           means             = new List <double>();
            List <double>           standardDeviation = new List <double>();
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            for (int i = 0; i < InputDataEntity.EpochsAmount; i++)
            {
                List <BinaryChromosome> newPopulation = selectionMethod.ApplySelection(BinaryChromosomes, inputDataEntity.DataRange, NumberOfBitsInChromosome);
                if (i != 0)
                {
                    double newValue = GetFunctionResultForChromosomes(newPopulation[0], newPopulation[1], inputDataEntity.DataRange).FunctionValue;
                    double oldValue = GetFunctionResultForChromosomes(tmpBest[0], tmpBest[1], inputDataEntity.DataRange).FunctionValue;
                    if ((inputDataEntity.Maximize == false && newValue > oldValue) || (inputDataEntity.Maximize == true && newValue < oldValue))
                    {
                        newPopulation.InsertRange(0, tmpBest);
                    }
                    else
                    {
                        newPopulation.InsertRange(newPopulation.Count - 1, tmpBest);
                    }
                }
                bestOfEachEpoch.Add(GetFunctionResultForChromosomes(newPopulation[0], newPopulation[1], inputDataEntity.DataRange));
                tmpBest.Clear();
                for (int j = 0; j < inputDataEntity.EliteStrategyAmount * 2; j++)
                {
                    tmpBest.Add(newPopulation[j]);
                }
                means.Add(CalculateMean(newPopulation, inputDataEntity.DataRange));
                standardDeviation.Add(CalculateDeviation(newPopulation, inputDataEntity.DataRange, means[i]));
                List <BinaryChromosome> crossedPopulation        = crossMethod.Cross(newPopulation, InputDataEntity.CrossProbability);
                List <BinaryChromosome> mutatedPopulation        = mutationMethod.Mutate(crossedPopulation, InputDataEntity.MutationProbability);
                List <BinaryChromosome> populationAfterInversion = Inversion.PerformInversion(mutatedPopulation, InputDataEntity.InversionProbability);
                BinaryChromosomes = populationAfterInversion;
                if (BinaryChromosomes.Count <= 2)
                {
                    break;
                }
            }

            var    bestIndividuals = selectionMethod.ApplySelection(BinaryChromosomes, inputDataEntity.DataRange, NumberOfBitsInChromosome).Take(2).ToArray();
            double x1       = BinaryUtils.BinaryToDecimalRepresentation(bestIndividuals[0].BinaryRepresentation, inputDataEntity.DataRange, NumberOfBitsInChromosome);
            double x2       = BinaryUtils.BinaryToDecimalRepresentation(bestIndividuals[1].BinaryRepresentation, inputDataEntity.DataRange, NumberOfBitsInChromosome);
            double extremum = Function.Compute(x1, x2);

            stopwatch.Stop();
            return(new OptimizationResult
            {
                ExtremeValue = extremum,
                X1 = x1,
                X2 = x2,
                BestFromPreviousEpochs = bestOfEachEpoch,
                MeanFromPreviousEpochs = means,
                StandardDeviation = standardDeviation,
                TimeElapsed = stopwatch.ElapsedMilliseconds
            });
        }