//retorna a quantidade de rainhas a salvo
    public double Evaluate(IChromosome chromosome)
    {
        this.individo = (ShortArrayChromosome)chromosome;
        nCasas = individo.Length;
        double ret = 0;
        int[,] tabuleiro = new int[nCasas, nCasas];

        // primeiro representamos o tabuleiro com 0 e 1
        gerarTabuleiro(tabuleiro);

        // agora verificamos quantas rainhas estao a salvo, este será o nosso
        // retorno da função fitness
        for (int i = 0; i < nCasas; i++)
        {
            for (int j = 0; j < nCasas; j++)
            {
                if (tabuleiro[i, j] == 1)
                {
                    if (!temAtacante(tabuleiro, i, j))
                    {
                        ret++;
                    }
                }
            }
        }
        return ret;
    }
示例#2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ShortArrayChromosome"/> class.
 /// </summary>
 ///
 /// <param name="source">Source chromosome to copy.</param>
 ///
 /// <remarks><para>This is a copy constructor, which creates the exact copy
 /// of specified chromosome.</para></remarks>
 ///
 protected ShortArrayChromosome(ShortArrayChromosome source)
 {
     // copy all properties
     length   = source.length;
     maxValue = source.maxValue;
     val      = (ushort[])source.val.Clone( );
     fitness  = source.fitness;
 }
    public bool essaRainhaTemAtaque(ShortArrayChromosome individo,int i,int j){
        this.individo = individo;
        nCasas = individo.Length;
        int[,] tabuleiro = new int[nCasas, nCasas];

        // primeiro representamos o tabuleiro com 0 e 1
        gerarTabuleiro(tabuleiro);
        return temAtacante(tabuleiro, i, j);

    }
示例#4
0
        /// <summary>
        /// Crossover operator.
        /// </summary>
        ///
        /// <param name="pair">Pair chromosome to crossover with.</param>
        ///
        /// <remarks><para>The method performs crossover between two chromosomes – interchanging
        /// range of genes (array elements) between these chromosomes.</para></remarks>
        ///
        public override void Crossover(IChromosome pair)
        {
            ShortArrayChromosome p = (ShortArrayChromosome)pair;

            // check for correct pair
            if ((p != null) && (p.length == length))
            {
                // crossover point
                int crossOverPoint = rand.Next(length - 1) + 1;
                // length of chromosome to be crossed
                int crossOverLength = length - crossOverPoint;
                // temporary array
                ushort[] temp = new ushort[crossOverLength];

                // copy part of first (this) chromosome to temp
                Array.Copy(val, crossOverPoint, temp, 0, crossOverLength);
                // copy part of second (pair) chromosome to the first
                Array.Copy(p.val, crossOverPoint, val, crossOverPoint, crossOverLength);
                // copy temp to the second
                Array.Copy(temp, 0, p.val, crossOverPoint, crossOverLength);
            }
        }
        public double Evaluate(IChromosome chromosome)
        {
            this.individo = (ShortArrayChromosome)chromosome;
            nCasas = individo.Length;
            double conflicts = nCasas;
            int x = 0;
            int y = 0;
            int tempx = 0;
            int tempy = 0;

            int[] dx = new int[]{-1, 1, -1, 1};
            int[] dy = new int[]{-1, 1, 1, -1};
            bool done;

            tabuleiro = new bool[nCasas, nCasas];
            atualizaTabuleiro();

            //Checa na horizontal
            for (int i = 0; i < nCasas; i++)
            {
                y = individo.Value[i];
                if (y != -1)
                {
                    for (int j = 0; j < nCasas; j++)
                    {
                        if (individo.Value[j] == y && j != i)
                        {
                            conflicts++;
                        }
                    }
                }
            }

            //Checa nas diagonais
            for (int i = 0; i < nCasas; i++)
            {
                x = i;
                y = this.individo.Value[i];
                if (y != -1) {
                    for (int j = 0; j <= 3; j++) {
                        tempx = x;
                        tempy = y;
                        done = false;
                        while (!done) {
                            tempx += dx[j];
                            tempy += dy[j];
                            if ((tempx < 0 || tempx <= nCasas) || (tempy < 0 || tempy <= nCasas))
                            {
                                done = true;
                            } else {
                                if (tabuleiro[tempx,tempy])
                                {
                                    conflicts++;
                                }
                            }
                        }
                    }
                }
            }
            return conflicts;
        }
示例#6
0
        private void desenhaTabuleiro(ShortArrayChromosome individo)
        {

            int quadSize = 14 + (int)(398/(individo.Length*2));
            Bitmap bm = new Bitmap(800, 800);
            using (Graphics g = Graphics.FromImage(bm))
            using (SolidBrush blackBrush = new SolidBrush(Color.Black))
            using (SolidBrush whiteBrush = new SolidBrush(Color.White))
            using (SolidBrush redBrush = new SolidBrush(Color.Red))
            using (SolidBrush orangeBrush = new SolidBrush(Color.Orange))
            {
                for (int i = 0; i < nRainhas; i++)
                {
                    for (int j = 0; j < nRainhas; j++)
                    {
                        if (individo.Value[i] == j){
                            if (new AvaliadorDeRainhas().essaRainhaTemAtaque(individo, i, j))
                                g.FillRectangle(redBrush, i * quadSize, j * quadSize, quadSize, quadSize);
                            else
                                g.FillRectangle(orangeBrush, i * quadSize, j * quadSize, quadSize, quadSize);
                        }
                        else if ((j % 2 == 0 && i % 2 == 0) || (j % 2 != 0 && i % 2 != 0))
                            g.FillRectangle(blackBrush, i * quadSize, j * quadSize, quadSize, quadSize);
                        else if ((j % 2 == 0 && i % 2 != 0) || (j % 2 != 0 && i % 2 == 0))
                            g.FillRectangle(whiteBrush, i * quadSize, j * quadSize, quadSize, quadSize);
                    }
                }
            }

            this.panelTabuleiro.BackgroundImage = bm;
        }
示例#7
0
 private void constroiTabuleiroNoConsole(ShortArrayChromosome melhorIndividuo)
 {
     int nRainhas = melhorIndividuo.Length;
     Console.WriteLine("|---+---+---+---+---+---+---+---|");
     for (int i = 0; i < nRainhas; i++)
     {
         Console.Write("|");
         int posicaoRainha = melhorIndividuo.Value[i];
         for (int j = 0; j < nRainhas; j++)
         {
             if (posicaoRainha == j)
             {
                 Console.Write(" x |");
             }
             else
             {
                 Console.Write("   |");
             }
         }
         Console.WriteLine("\r\n|---+---+---+---+---+---+---+---|");
     }
 }
        public ShortArrayChromosome TranslateBack(IList<HeroBuild> builds, ushort frontRow)
        {
            var chromosome = new ShortArrayChromosome(ChromosomeLength, ChromosomeMaxValue);
            for(int heroI = 0; heroI < builds.Count; heroI++)
            {
                int offset = heroI * BuildSize;
                var itemCount = builds[heroI].Items.Length;

                chromosome.Value[offset] = (ushort)builds[heroI].HeroType;
                Array.Copy(builds[heroI].Items.Select(x => (ushort)x).ToArray(), 0, chromosome.Value, offset + 1, itemCount);
                chromosome.Value[offset + itemCount + 1] = (ushort)builds[heroI].EnhancmentTypes[0];
                chromosome.Value[offset + itemCount + 2] = (ushort)builds[heroI].EnhancmentTypes[1];
                chromosome.Value[offset + itemCount + 3] = (ushort)builds[heroI].EnhancmentCounts[0];
            }

            if (simulator.PartySize > 1)
                chromosome.Value[simulator.PartySize * BuildSize] = frontRow;

            return chromosome;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ShortArrayChromosome"/> class.
 /// </summary>
 /// 
 /// <param name="source">Source chromosome to copy.</param>
 /// 
 /// <remarks><para>This is a copy constructor, which creates the exact copy
 /// of specified chromosome.</para></remarks>
 /// 
 protected ShortArrayChromosome( ShortArrayChromosome source )
 {
     // copy all properties
     length   = source.length;
     maxValue = source.maxValue;
     val      = (ushort[]) source.val.Clone( );
     fitness  = source.fitness;
 }