Exemplo n.º 1
0
        public async Task <HeatmapStepCache> CreateCache(DateTime startDate, DateTime endDate, HyperMetadataSet metadataSet, TimeSpan stepLength)
        {
            _percentageCompleted  = 0;
            GenerationWasCanceled = false;
            GenerationProgress?.Invoke(0);

            var newCache = new HeatmapStepCache()
            {
                MetadataSetId = metadataSet.Id
            };

            _stepToTasksMappings = new Dictionary <HeatmapCacheStep, MasksHeatmapRenderer>();
            var tasks          = new List <Task>();
            var tasksCompleted = 0;

            while (startDate < endDate)
            {
                var step = new HeatmapCacheStep
                {
                    From = startDate,
                    To   = startDate + stepLength > endDate ? endDate : startDate + stepLength
                };

                var renderer = new MasksHeatmapRenderer(_store, metadataSet, _heatmapSettings);
                renderer.PertcantageProcessedUpdated += (percentageLambda) =>
                {
                    _percentageCompleted += percentageLambda / tasks.Count;
                    this.GenerationProgress?.Invoke(_percentageCompleted);
                };
                var task = renderer.RunGenerationAsync(step.From, step.To)
                           .ContinueWith(t =>
                {
                    tasksCompleted++;
                });
                _stepToTasksMappings[step] = renderer;
                tasks.Add(task);

                startDate = startDate + stepLength;

                System.Diagnostics.Debug.WriteLine("Cache heatmap task added");
            }

            await Task.WhenAll(tasks);

            newCache.Steps.AddRange(_stepToTasksMappings.Keys);
            foreach (var kv in _stepToTasksMappings)
            {
                var step = kv.Key;
                step.ImageData = kv.Value.ImageProp.Value;
            }

            return(newCache);
        }
Exemplo n.º 2
0
        private void TrainButton_Click(object sender, EventArgs e)
        {
            int    generationSize, proceedToNext, generations, maxScore, gamesToPlay;
            double mutationProbability, mutationRate;

            if (!int.TryParse(GenerationSize.Text, out generationSize) || generationSize < 1)
            {
                MessageBox.Show("Generation size must be positive integer.");
                return;
            }
            if (!int.TryParse(ProceedToNext.Text, out proceedToNext) || proceedToNext < 1)
            {
                MessageBox.Show("Proceed to next size must be positive integer.");
                return;
            }
            if (!int.TryParse(GenerationsCount.Text, out generations) || generations < 1)
            {
                MessageBox.Show("Maximum count of generations must be positive integer.");
                return;
            }
            if (!int.TryParse(MaxScore.Text, out maxScore) || maxScore < 1)
            {
                MessageBox.Show("Maximum score must be positive integer.");
                return;
            }
            if (!int.TryParse(GamesToPlay.Text, out gamesToPlay) || gamesToPlay < 1)
            {
                MessageBox.Show("Games to play must be positive integer.");
                return;
            }
            if (!double.TryParse(MutationProbability.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out mutationProbability) || mutationProbability <= 0 || mutationProbability > 1)
            {
                MessageBox.Show("Mutation probability must be value between 0 and 1.");
                return;
            }
            if (!double.TryParse(MutationRate.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out mutationRate) || mutationRate <= 0)
            {
                MessageBox.Show("Mutation rate must be positive value");
                return;
            }
            string[]   splittedNeurons = HiddenNeurons.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            List <int> hiddenNeurons;

            try
            {
                hiddenNeurons = splittedNeurons.Select(x => int.Parse(x)).ToList();
                hiddenNeurons.ForEach(x =>
                {
                    if (x < 1)
                    {
                        MessageBox.Show("Neurons count in hidden layers must be positive integer");
                        return;
                    }
                });
            }
            catch
            {
                MessageBox.Show("Neurons count in hidden layers must be positive integer");
                return;
            }
            scoreHistory = new List <double>();
            GenerationProgress.Maximum = generationSize;
            Random r = new Random();
            Model  defaultModel;
            Game   etalonGame;

            if (GamesComboBox.SelectedItem == GamesComboBox.Items[0])
            {
                defaultModel = new Model(new NeuralNetwork(4, hiddenNeurons, 1, r), Model.Games.FlappyBird);
                etalonGame   = new FlappyBird(1, r, false);
            }
            else
            {
                MessageBox.Show("Dino is not supported yet");
                return;
                ////defaultModel = new Model(new NeuralNetwork(4, hiddenNeurons, 1, r), Model.Games.FlappyBird);
                //etalonGame = new Dino(1, r, false);
            }
            etalonGame.ControlSize = GameForm.ControlSize;
            etalonGame.SetDefaultOptions();
            Trainer trainer = new Trainer(defaultModel, etalonGame, generationSize,
                                          proceedToNext, generations, maxScore,
                                          gamesToPlay, mutationProbability, mutationRate, r);

            trainer.ModelTested += (obj, value) =>
            {
                GenerationProgress.Invoke((MethodInvoker)(() =>
                {
                    GenerationProgress.Value = value;
                }));
            };
            trainer.GenerationPassed  += GenerationPassed;
            TrainButton.Enabled        = false;
            TrainingParameters.Enabled = false;
            thread = new Thread(() => Train(trainer));
            thread.Start();
        }