HyperNEATBoxes.TrialEvaluation.Query C# (CSharp) Method

Query() public method

public Query ( int resolution ) : IntPair
resolution int
return IntPair
        public IntPair Query(int resolution)
        {
            // first, create the input data
            int index = 0;
            BasicMLData inputData = new BasicMLData(resolution * resolution);
            double pixelSize = 2.0 / resolution;
            double orig = -1.0 + (pixelSize / 2.0);

            double yReal = orig;
            for (int y = 0; y < resolution; y++, yReal += pixelSize)
            {
                double xReal = orig;
                for (int x = 0; x < resolution; x++, xReal += pixelSize)
                {
                    inputData.Data[index] = this.test.GetPixel(xReal, yReal);
                    index++;
                }
            }

            // second, query the network
            output = ((NEATNetwork)this.phenotype).Compute(inputData);

            // finally, process the output
            minActivation = Double.PositiveInfinity;
            maxActivation = Double.NegativeInfinity;
            int maxIndex = 0;

            for (int i = 0; i < output.Count; i++)
            {
                double d = output[i];

                if (d > maxActivation)
                {
                    maxActivation = d;
                    maxIndex = i;
                }
                else if (d < minActivation)
                {
                    minActivation = d;
                }
            }

            int yy = maxIndex / resolution;
            int xx = maxIndex - (yy * resolution);
            return new IntPair(xx, yy);
        }

Usage Example

        public void Render()
        {
            NEATGenome genome = (NEATGenome)this.pop.BestGenome;
            Substrate substrate = SubstrateFactory.factorSandwichSubstrate(resolution, resolution);
            HyperNEATCODEC codec = new HyperNEATCODEC();
            NEATNetwork phenotype = (NEATNetwork)codec.Decode(this.pop, substrate, genome);

            TrialEvaluation trial = new TrialEvaluation(phenotype, this.testCase);
            IntPair actualPos = trial.Query(resolution);

            // clear what was there before
            GridCanvas.Children.Clear();

            //
            double boxWidth = GridCanvas.ActualWidth / resolution;
            double boxHeight = GridCanvas.ActualHeight / resolution;
            double delta = 2.0 / resolution;
            int index = 0;

            for (int row = 0; row < resolution; row++)
            {
                double y = -1 + (row * delta);
                double boxY = row * boxHeight;
                for (int col = 0; col < resolution; col++)
                {
                    double x = -1 + (col * delta);
                    double boxX = col * boxWidth;

                    Rectangle r = new Rectangle();
                    r.SetValue(Canvas.LeftProperty, boxX);
                    r.SetValue(Canvas.TopProperty, boxY);
                    r.Width = boxWidth;
                    r.Height = boxHeight;

                    if (this.testCase.GetPixel(x, y) > 0)
                    {
                        r.Fill = Brushes.Blue;
                    }
                    else
                    {
                        double d = trial.Output[index];
                        int c = trial.Normalize(d, 255);
                        SolidColorBrush b = new SolidColorBrush(Color.FromRgb(255, (byte)c, 255));
                        r.Fill = b;
                        r.Stroke = Brushes.Black;
                    }

                    GridCanvas.Children.Add(r);
                    index++;
                }
            }

            Rectangle target = new Rectangle();
            target.SetValue(Canvas.LeftProperty, actualPos.X * boxWidth);
            target.SetValue(Canvas.TopProperty, actualPos.Y * boxHeight);
            target.Width = boxWidth;
            target.Height = boxHeight;
            target.Fill = Brushes.Red;
            GridCanvas.Children.Add(target);
        }
All Usage Examples Of HyperNEATBoxes.TrialEvaluation::Query