コード例 #1
0
        private void Timer_Tick(object sender, EventArgs e)
        {
            // This uses the eval's duration, and uses a neural net for the trial tick count.  This is to recreate what the training actually sees

            try
            {
                imageInput.Source = null;
                canvasMain.Children.Clear();

                DateTime now = DateTime.UtcNow;

                if (_harness == null || _evalArgs == null)
                {
                    _prevTick = now;
                    return;
                }

                // Reset
                if (_tickCounter >= _evalArgs.TotalNumberEvaluations)
                {
                    #region reset

                    if (_experiment != null)
                    {
                        NeatGenome winner = _ea.CurrentChampGenome;
                        _genomeViewer.RefreshView(winner);

                        if (winner == null)
                        {
                            _prevTick = now;
                            return;
                        }

                        bool showedHyper = false;
                        if (_experiment.IsHyperNEAT)
                        {
                            var decoder = _experiment.CreateGenomeDecoder(_hyperneatArgs);
                            if (decoder is HyperNeatDecoder hyperDecoder)
                            {
                                //NOTE: There is a flaw.  The INetworkDefinition that this method returns is acyclic, but the method that generates the IBlackBox is CreateSubstrateNetwork_FastCyclicNetwork.
                                //So that INetworkDefinition seems to be limited or incorrect
                                //
                                //Actually, that might not be a flaw.  The FastCyclic may be processing the CPPN which is cyclic

                                var finalPhenome = hyperDecoder.Decode2(winner);

                                _genomeViewer2.RefreshView(finalPhenome.Item2);

                                nnViewerHost2.Visibility = Visibility.Visible;
                                showedHyper = true;
                            }
                        }

                        if (!showedHyper)
                        {
                            nnViewerHost2.Visibility = Visibility.Collapsed;
                        }

                        if (_experiment.IsHyperNEAT)
                        {
                            _winningBrain = _experiment.GetBlackBox(winner, _hyperneatArgs);
                        }
                        else
                        {
                            _winningBrain = _experiment.GetBlackBox(winner);
                        }

                        _winningBrainTime = now;

                        _harness.ClearItem();

                        _harness.SetItem(AntPos_Evaluator.GetNewItem(_harness, _evalArgs));
                        _tickCounter = -1;
                    }

                    #endregion
                }

                // Tell the harness to go
                _harness.Tick(_evalArgs.ElapsedTime_Seconds);
                _tickCounter++;

                var prevPosition    = _harness.GetPreviousPosition(_harness.Time - _evalArgs.Delay_Seconds);
                var currentPosition = _harness.Item;

                #region draw input

                double[] inputArr = new double[_harness.InputSizeXY * _harness.InputSizeXY];

                AntPos_Evaluator.ClearArray(inputArr);

                if (prevPosition != null)
                {
                    double dotRadius = (_harness.VisionSize / _harness.InputSizeXY) * Math.Sqrt(2);
                    AntPos_Evaluator.ApplyPoint(inputArr, _harness.InputCellCenters, dotRadius, prevPosition.Item2, true);
                }

                imageInput.Source = UtilityWPF.GetBitmap(inputArr, _harness.InputSizeXY, _harness.InputSizeXY, invert: true);

                #endregion
                #region draw expected output

                double[] expectedOutput = null;

                if (currentPosition == null)
                {
                    expectedOutput = AntPos_Evaluator.GetExpectedOutput(null, _harness, _evalArgs);
                }
                else
                {
                    var currentPos = Tuple.Create(currentPosition, currentPosition.Position, currentPosition.Velocity);
                    expectedOutput = AntPos_Evaluator.GetExpectedOutput(currentPos, _harness, _evalArgs);
                }

                imageExpectedOutput.Source = UtilityWPF.GetBitmap(expectedOutput, _harness.OutputSizeXY, _harness.OutputSizeXY, invert: true);

                #endregion
                #region draw nn output

                double[] nnOutput = null;

                if (_winningBrain != null)
                {
                    nnOutput = new double[_harness.OutputSizeXY * _harness.OutputSizeXY];

                    // Brain.Tick
                    _winningBrain.InputSignalArray.CopyFrom(inputArr, 0);
                    _winningBrain.Activate();
                    _winningBrain.OutputSignalArray.CopyTo(nnOutput, 0);

                    imageNNOutput.Source = UtilityWPF.GetBitmap(nnOutput, _harness.OutputSizeXY, _harness.OutputSizeXY, invert: true);
                }
                else
                {
                    imageNNOutput.Source = null;
                }

                #endregion
                #region draw error (nn - expected)

                double[] error = null;

                if (nnOutput != null)
                {
                    error = Enumerable.Range(0, nnOutput.Length).
                            Select(o => Math.Abs(nnOutput[o] - expectedOutput[o])).
                            ToArray();

                    imageError.Source = UtilityWPF.GetBitmap(error, _harness.OutputSizeXY, _harness.OutputSizeXY, invert: true);
                }
                else
                {
                    imageError.Source = null;
                }

                #endregion
                #region draw actual

                // Vision Rectangle
                Rectangle visionRect = new Rectangle()
                {
                    Stroke          = Brushes.Silver,
                    StrokeThickness = .3,
                    Width           = _harness.VisionSize,
                    Height          = _harness.VisionSize,
                };

                Canvas.SetLeft(visionRect, _harness.VisionSize / -2);
                Canvas.SetTop(visionRect, _harness.VisionSize / -2);

                canvasMain.Children.Add(visionRect);

                // Dot Previous
                if (prevPosition != null)
                {
                    Ellipse dot = new Ellipse()
                    {
                        Fill            = new SolidColorBrush(prevPosition.Item1.Color),
                        Stroke          = Brushes.Black,
                        StrokeThickness = .3,
                        Width           = 2,
                        Height          = 2,
                    };

                    Canvas.SetLeft(dot, prevPosition.Item2.X - 1);
                    Canvas.SetTop(dot, prevPosition.Item2.Y - 1);

                    canvasMain.Children.Add(dot);
                }

                // Dot Current
                if (currentPosition != null)
                {
                    Ellipse dot = new Ellipse()
                    {
                        Fill            = new SolidColorBrush(currentPosition.Color),
                        Stroke          = Brushes.White,
                        StrokeThickness = .3,
                        Width           = 2,
                        Height          = 2,
                    };

                    Canvas.SetLeft(dot, currentPosition.Position.X - 1);
                    Canvas.SetTop(dot, currentPosition.Position.Y - 1);

                    canvasMain.Children.Add(dot);
                }

                // Transform
                TransformGroup transform = new TransformGroup();

                transform.Children.Add(new ScaleTransform(canvasMain.ActualWidth / _harness.MapSize, canvasMain.ActualHeight / _harness.MapSize));
                transform.Children.Add(new TranslateTransform(canvasMain.ActualWidth / 2, canvasMain.ActualHeight / 2));

                canvasMain.RenderTransform = transform;

                #endregion

                _prevTick = now;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
コード例 #2
0
        private void Timer_Tick_REAL(object sender, EventArgs e)
        {
            // This uses actual time elapsed, and runs continuously, grabbing a new neural net every 10 seconds

            try
            {
                imageInput.Source = null;
                canvasMain.Children.Clear();

                DateTime now = DateTime.UtcNow;

                if (_harness == null)
                {
                    _prevTick = now;
                    return;
                }

                // Tell the harness to go
                _harness.Tick((now - _prevTick).TotalSeconds);

                var prevPosition    = _harness.GetPreviousPosition(_harness.Time - _evalArgs.Delay_Seconds);
                var currentPosition = _harness.Item;

                if (_experiment != null && (_winningBrain == null || (now - _winningBrainTime).TotalSeconds > 10))
                {
                    NeatGenome winner = _ea.CurrentChampGenome;
                    _genomeViewer.RefreshView(winner);
                    _winningBrain     = _experiment.GetBlackBox(winner);
                    _winningBrainTime = now;
                }

                #region draw input

                double[] inputArr = new double[_harness.InputSizeXY * _harness.InputSizeXY];

                AntPos_Evaluator.ClearArray(inputArr);

                if (prevPosition != null)
                {
                    double dotRadius = (_harness.VisionSize / _harness.InputSizeXY) * Math.Sqrt(2);
                    AntPos_Evaluator.ApplyPoint(inputArr, _harness.InputCellCenters, dotRadius, prevPosition.Item2, true);
                }

                imageInput.Source = UtilityWPF.GetBitmap(inputArr, _harness.InputSizeXY, _harness.InputSizeXY, invert: true);

                #endregion
                #region draw expected output

                double[] expectedOutput = null;

                if (currentPosition == null)
                {
                    expectedOutput = AntPos_Evaluator.GetExpectedOutput(null, _harness, _evalArgs);
                }
                else
                {
                    var currentPos = Tuple.Create(currentPosition, currentPosition.Position, currentPosition.Velocity);
                    expectedOutput = AntPos_Evaluator.GetExpectedOutput(currentPos, _harness, _evalArgs);
                }

                imageExpectedOutput.Source = UtilityWPF.GetBitmap(expectedOutput, _harness.OutputSizeXY, _harness.OutputSizeXY, invert: true);

                #endregion
                #region draw nn output

                double[] nnOutput = null;

                if (_winningBrain != null)
                {
                    nnOutput = new double[_harness.OutputSizeXY * _harness.OutputSizeXY];

                    // Brain.Tick
                    _winningBrain.InputSignalArray.CopyFrom(inputArr, 0);
                    _winningBrain.Activate();
                    _winningBrain.OutputSignalArray.CopyTo(nnOutput, 0);

                    imageNNOutput.Source = UtilityWPF.GetBitmap(nnOutput, _harness.OutputSizeXY, _harness.OutputSizeXY, invert: true);
                }
                else
                {
                    imageNNOutput.Source = null;
                }

                #endregion
                #region draw error (nn - expected)

                double[] error = null;

                if (nnOutput != null)
                {
                    error = Enumerable.Range(0, nnOutput.Length).
                            Select(o => Math.Abs(nnOutput[o] - expectedOutput[o])).
                            ToArray();

                    imageError.Source = UtilityWPF.GetBitmap(error, _harness.OutputSizeXY, _harness.OutputSizeXY, invert: true);
                }
                else
                {
                    imageError.Source = null;
                }

                #endregion
                #region draw actual

                // Vision Rectangle
                Rectangle visionRect = new Rectangle()
                {
                    Stroke          = Brushes.Silver,
                    StrokeThickness = .3,
                    Width           = _harness.VisionSize,
                    Height          = _harness.VisionSize,
                };

                Canvas.SetLeft(visionRect, _harness.VisionSize / -2);
                Canvas.SetTop(visionRect, _harness.VisionSize / -2);

                canvasMain.Children.Add(visionRect);

                // Dot Previous
                if (prevPosition != null)
                {
                    Ellipse dot = new Ellipse()
                    {
                        Fill            = new SolidColorBrush(prevPosition.Item1.Color),
                        Stroke          = Brushes.Black,
                        StrokeThickness = .3,
                        Width           = 2,
                        Height          = 2,
                    };

                    Canvas.SetLeft(dot, prevPosition.Item2.X - 1);
                    Canvas.SetTop(dot, prevPosition.Item2.Y - 1);

                    canvasMain.Children.Add(dot);
                }

                // Dot Current
                if (currentPosition != null)
                {
                    Ellipse dot = new Ellipse()
                    {
                        Fill            = new SolidColorBrush(currentPosition.Color),
                        Stroke          = Brushes.White,
                        StrokeThickness = .3,
                        Width           = 2,
                        Height          = 2,
                    };

                    Canvas.SetLeft(dot, currentPosition.Position.X - 1);
                    Canvas.SetTop(dot, currentPosition.Position.Y - 1);

                    canvasMain.Children.Add(dot);
                }

                // Transform
                TransformGroup transform = new TransformGroup();

                transform.Children.Add(new ScaleTransform(canvasMain.ActualWidth / _harness.MapSize, canvasMain.ActualHeight / _harness.MapSize));
                transform.Children.Add(new TranslateTransform(canvasMain.ActualWidth / 2, canvasMain.ActualHeight / 2));

                canvasMain.RenderTransform = transform;

                #endregion

                _prevTick = now;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }