コード例 #1
0
ファイル: BoxTrialCase.cs プロジェクト: legendvijay/aifh
 public IntPair InitTestCase(int largeBoxRelativePos)
 {
     IntPair[] loc = GenerateRandomTestCase(largeBoxRelativePos);
     smallBoxTopLeft = loc[0];
     largeBoxTopLeft = (IntPair)loc[1].Clone();
     largeBoxTopLeft.Add(-1);
     return loc[1];
 }
コード例 #2
0
        public double CalculateScore(IMLMethod phenotype)
        {
            BoxTrialCase    test = new BoxTrialCase(new Random());
            TrialEvaluation eval = new TrialEvaluation(phenotype, test);

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 25; j++)
                {
                    IntPair targetPos = eval.Test.InitTestCase(i);
                    IntPair actualPos = eval.Query(this.resolution);

                    eval.Accumulate(
                        CalcRealDistanceSquared(targetPos, actualPos),
                        Math.Max(0.0, eval.MaxActivation - eval.MinActivation));
                }
            }

            return(eval.CalculateFitness());
        }
コード例 #3
0
ファイル: BoxTrialCase.cs プロジェクト: legendvijay/aifh
        private IntPair[] GenerateRandomTestCase(int largeBoxRelativePos)
        {
            IntPair smallBoxPos = new IntPair(rnd.Next(BoxTrialCase.BASE_RESOLUTION),
                    rnd.Next(BoxTrialCase.BASE_RESOLUTION));

            IntPair largeBoxPos = (IntPair)smallBoxPos.Clone();
            switch (largeBoxRelativePos)
            {
                case 0:
                    largeBoxPos.AddX(5);
                    break;
                case 1:
                    largeBoxPos.AddY(5);
                    break;
                case 2:
                    if (rnd.NextDouble() > 0.5)
                    {
                        largeBoxPos.Add(3, 4);
                    }
                    else
                    {
                        largeBoxPos.Add(4, 3);
                    }
                    break;
            }

            if (largeBoxPos.X > BoxTrialCase.BOUNDS)
            {
                largeBoxPos.AddX(-BoxTrialCase.BASE_RESOLUTION);

                if (0 == largeBoxPos.X)
                {
                    largeBoxPos.Add(1);
                }
            }
            else if (BoxTrialCase.BOUNDS == largeBoxPos.X)
            {
                largeBoxPos.AddX(-1);
            }
            else if (largeBoxPos.X == 0)
            {
                largeBoxPos.AddX(1);
            }

            if (largeBoxPos.Y > BoxTrialCase.BOUNDS)
            {
                largeBoxPos.AddY(-BoxTrialCase.BASE_RESOLUTION);

                if (0 == largeBoxPos.Y)
                {
                    largeBoxPos.AddY(1);
                }
            }
            else if (BoxTrialCase.BOUNDS == largeBoxPos.Y)
            {
                largeBoxPos.AddY(-1);
            }
            else if (0 == largeBoxPos.Y)
            {
                largeBoxPos.AddY(1);
            }
            return new IntPair[] { smallBoxPos, largeBoxPos };
        }
コード例 #4
0
 private double CalcRealDistanceSquared(IntPair a, IntPair b)
 {
     double xdelta = (a.X - b.X) * pixelSize;
     double ydelta = (a.Y - b.Y) * pixelSize;
     return xdelta * xdelta + ydelta * ydelta;
 }
コード例 #5
0
        private IntPair[] GenerateRandomTestCase(int largeBoxRelativePos)
        {
            IntPair smallBoxPos = new IntPair(rnd.Next(BoxTrialCase.BASE_RESOLUTION),
                                              rnd.Next(BoxTrialCase.BASE_RESOLUTION));

            IntPair largeBoxPos = (IntPair)smallBoxPos.Clone();

            switch (largeBoxRelativePos)
            {
            case 0:
                largeBoxPos.AddX(5);
                break;

            case 1:
                largeBoxPos.AddY(5);
                break;

            case 2:
                if (rnd.NextDouble() > 0.5)
                {
                    largeBoxPos.Add(3, 4);
                }
                else
                {
                    largeBoxPos.Add(4, 3);
                }
                break;
            }

            if (largeBoxPos.X > BoxTrialCase.BOUNDS)
            {
                largeBoxPos.AddX(-BoxTrialCase.BASE_RESOLUTION);

                if (0 == largeBoxPos.X)
                {
                    largeBoxPos.Add(1);
                }
            }
            else if (BoxTrialCase.BOUNDS == largeBoxPos.X)
            {
                largeBoxPos.AddX(-1);
            }
            else if (largeBoxPos.X == 0)
            {
                largeBoxPos.AddX(1);
            }

            if (largeBoxPos.Y > BoxTrialCase.BOUNDS)
            {
                largeBoxPos.AddY(-BoxTrialCase.BASE_RESOLUTION);

                if (0 == largeBoxPos.Y)
                {
                    largeBoxPos.AddY(1);
                }
            }
            else if (BoxTrialCase.BOUNDS == largeBoxPos.Y)
            {
                largeBoxPos.AddY(-1);
            }
            else if (0 == largeBoxPos.Y)
            {
                largeBoxPos.AddY(1);
            }
            return(new IntPair[] { smallBoxPos, largeBoxPos });
        }
コード例 #6
0
        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);
        }