Пример #1
0
 public nodo(int dimsTablero, string nombreNodo)
 {
     this.tablero    = new char[dimsTablero, dimsTablero];
     this.nodoPadre  = new nodo();
     this.nodosHijos = new List <nodo>();
     this.nombreNodo = nombreNodo;
 }
Пример #2
0
        public void insertarNodo(nodo padre, nodo vectorHijos)
        {
            // conectar al padre con sus hijos
            padre.nodosHijos.Add(vectorHijos);

            // aumentar el numero de nodos en el arbol
            this.numeroNodos += 1;
        }
Пример #3
0
        public void insertarNodoEn(nodo nuevoNodo, nodo padre)
        {
            // la insercion se realiza por defecto en la raiz

            nuevoNodo.nodoPadre = padre;

            // padre con hijos
            padre.nodosHijos.Add(nuevoNodo);
        }
Пример #4
0
        public void insertarNodo(nodo nuevoNodo)
        {
            // la insercion se realiza por defecto en la raiz

            nuevoNodo.nodoPadre = this.raiz;

            // padre con hijos
            raiz.nodosHijos.Add(nuevoNodo);

            /// aumentar las dimenciones del arbol
            this.numeroNodos += 1;
        }
Пример #5
0
        // realiza un movimiento a partir de un estado actual
        public nodo  generarMovimientos(nodo nodoActual, char turno)
        {
            Console.WriteLine("Antes del movimiento ");
            imprimirTablero(nodoActual.tablero);
            // recorrer el tablero en busca de una pieza para el turno indicado
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    nodo nuevo = new nodo();
                    nuevo.nodoPadre = nodoActual;

                    char[,] TableroACtual = copiarTablero(nodoActual.tablero);
                    char[,] NuevoTAblero  = null;

                    char c = TableroACtual[j, i];
                    nuevo.nombreNodo = c.ToString();
                    nuevo.valor      = this.calcularHeuristicas(c);

                    if (turno == arbol.ColorBlanco)
                    {
                        if (this.PiesasBlanco.Contains(c))
                        {
                            NuevoTAblero = MovimientoValido(TableroACtual, turno, new Point(j, i), c);
                        }
                    }
                    else
                    {
                        if (this.PiesasNegro.Contains(c))
                        {
                            NuevoTAblero = MovimientoValido(TableroACtual, turno, new Point(j, i), c);
                        }
                    }

                    if (NuevoTAblero != null)
                    {
                        nuevo.tablero = NuevoTAblero;
                        this.insertarNodoEn(nuevo, nodoActual);
                    }
                }
            }

            // buscar al mejor movimiento del arbol
            nodo m = this.MejorEstado(nodoActual);

            Console.WriteLine("Despues del movimiento ");
            imprimirTablero(m.tablero);
            return(m);
        }
Пример #6
0
        private void imprimirTablero(nodo nodoEntrada)
        {
            double dy = Math.Round((double)chesagrp.Height / 10);
            double dx = Math.Round((double)chesagrp.Width / 10);

            chesagrp.Controls.Clear();
            chesagrp.Refresh();

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    Label l = new Label();
                    l.Size     = new Size((int)dx, (int)dy);
                    l.Location = new Point((int)((j + 1) * dx), (int)((i + 1) * dy));
                    if (nodoEntrada.tablero[j, i] != arbol.CasillaVacia)
                    {
                        l.Text = nodoEntrada.tablero[j, i].ToString();
                    }
                    l.ForeColor = Color.White;
                    if (i % 2 == 0)
                    {
                        if (j % 2 == 0)
                        {
                            l.BackColor = Color.Black;
                        }
                        else
                        {
                            l.BackColor = Color.Red;
                        }
                    }

                    else
                    if (j % 2 != 0)
                    {
                        l.BackColor = Color.Black;
                    }
                    else
                    {
                        l.BackColor = Color.Red;
                    }

                    chesagrp.Controls.Add(l);
                    l.Refresh();
                }
            }
            System.Threading.Thread.Sleep(600);
        }
Пример #7
0
        // buscar al nodo con la mejor euristica
        public nodo MejorEstado(nodo padre)
        {
            nodo n = new nodo();

            n.valor = 0;

            // iterar para nodo hijo de la raiz
            for (int i = 0; i < padre.nodosHijos.Count; i++)
            {
                nodo ns = padre.nodosHijos[i];
                if (ns.valor > n.valor)
                {
                    n = ns;
                }
            }
            return(n);
        }
Пример #8
0
 public arbol(nodo raiz)
 {
     this.raiz        = raiz;
     this.numeroNodos = 1;
 }
Пример #9
0
 public arbol()
 {
     this.numeroNodos = 0;
     this.raiz        = new nodo(8, "raiz estado inicial");
 }
Пример #10
0
        private void button26_Click(object sender, EventArgs e)
        {
            string[] nombres = { "Vacio", "Torres", "Caballos", "Alfiles", "Reinas", "Reyes", "Peones" };

            //ActivationNetwork rna = EntrenarRNA();

            // rna.Save("save1.rna");
            // para cada imagen en la direccion de entrenamiento
            DirectoryInfo sc = new DirectoryInfo(rutaEjemplos);

            FileInfo[] s = sc.GetFiles();
            Bitmap[,] Matzs = null;
            double[][] Carz = new double[8 * 8][];
            double[]   Obj  =
            {
                1, 2, 3, 4, 5, 3, 2, 1,
                6, 6, 6, 6, 6, 6, 6, 6,
                0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0,
                6, 6, 6, 6, 6, 6, 6, 6,
                1, 2, 3, 4, 5, 3, 2, 1,
            };

            for (int k = 0; k < 64; k++)
            {
                Carz[k] = new double[6];
            }

            int b = 0;

            foreach (FileInfo imgEjemp in s)
            {
                ImagenEntrada.Image           = PictureAnalizer.ImagenColor2Gray((Bitmap)Image.FromFile(imgEjemp.FullName));
                PictureAnalizer.ImagenEntrada = (Bitmap)ImagenEntrada.Image;

                Mascara mX = new Mascara(Mascara.PRDF_Opdr_LPcX, new Size(3, 3), new Point(1, 1));
                Mascara mY = new Mascara(Mascara.PRDF_Opdr_LpcY, new Size(3, 3), new Point(1, 1));

                ImagenEntrada.Image = PictureAnalizer.DetectarBordes(mX, mY, 0, (Bitmap)ImagenEntrada.Image);
                ImagenEntrada.Image.Save(@"C:\Users\frodo\Desktop\ejemplos - copia\" + "imgBorde" + b.ToString() + ".jpeg");
                b++;


                Matzs = PictureAnalizer.DividirImagen((Bitmap)ImagenEntrada.Image, 8);
                ImagenEntrada.Refresh();
                System.Threading.Thread.Sleep(1000);


                int n = 0;
                for (int i = 0; i < 8; i++)
                {
                    for (int j = 0; j < 8; j++)
                    {
                        Bitmap im = Matzs[j, i];
                        ImagenEntrada.Image = im;
                        int           p      = (int)Obj[n];
                        string        dir    = "Piesas/" + nombres[p] + "/";
                        DirectoryInfo dest   = new DirectoryInfo(dir);
                        FileInfo[]    infDir = dest.GetFiles();
                        switch (p)
                        {
                        case 0: im.Save(dir + nombres[p] + infDir.Length + ".jpeg"); break;

                        case 1: im.Save(dir + nombres[p] + infDir.Length + ".jpeg"); break;

                        case 2: im.Save(dir + nombres[p] + infDir.Length + ".jpeg"); break;

                        case 3: im.Save(dir + nombres[p] + infDir.Length + ".jpeg"); break;

                        case 4: im.Save(dir + nombres[p] + infDir.Length + ".jpeg"); break;

                        case 5: im.Save(dir + nombres[p] + infDir.Length + ".jpeg"); break;

                        case 6: im.Save(dir + nombres[p] + infDir.Length + ".jpeg"); break;
                        }

                        Carz[n] = PictureAnalizer.CalcularCaracteristicasTextura(im);


                        for (int v = 0; v < Carz[n].Length; v++)
                        {
                            Console.WriteLine(Carz[n][v]);
                        }



                        ImagenEntrada.Refresh();
                        System.Threading.Thread.Sleep(100);
                        n++;
                    }
                }
            }



            List <Bitmap[]>   ListaEntradas = new List <Bitmap[]>();
            List <double[][]> ListaCarz     = new List <double[][]>();
            double            carTota       = 0;

            for (int k = 0; k < 7; k++)
            {
                ListaEntradas.Add(new Bitmap[1]);
                ListaCarz.Add(new double[6][]);
            }


            string[] rutasInternas = Directory.GetDirectories("Piesas/");
            for (int i = 0; i < rutasInternas.Length; i++)
            {
                string        ruta      = rutasInternas[i];
                string[]      rutaSgmnt = ruta.Split('/');
                int           indx      = Array.IndexOf(nombres.ToArray(), rutaSgmnt[1]);
                DirectoryInfo infDir    = new DirectoryInfo(ruta);
                FileInfo[]    infFile   = infDir.GetFiles("*.jpeg", SearchOption.AllDirectories);
                Bitmap[]      imgArr    = new Bitmap[infFile.Length];
                double[][]    carArr    = new double[infFile.Length][];
                carTota += infFile.Length;

                for (int j = 0; j < imgArr.Length; j++)
                {
                    imgArr[j] = (Bitmap)Image.FromFile(infFile[j].FullName);
                    carArr[j] = PictureAnalizer.CalcularCaracteristicasTextura(imgArr[j]);
                }

                ListaEntradas[indx] = imgArr;
                ListaCarz[indx]     = carArr;
            }

            double[][] lSalidas = new double[(int)carTota][];

            // { "Vacio", "Torres", "Caballos","Alfiles", "Reinas", "Reyes", "Peones" };


            int c = 2;

            double[][] MatEntr = new double[(int)carTota][];
            int        t       = 0;
            int        pi      = 0;

            foreach (double[][] Mat in ListaCarz)
            {
                foreach (double[] vector in Mat)
                {
                    MatEntr[t] = vector;

                    switch (pi)
                    {
                    case 0: lSalidas[t] = new double[] { 0, 0, 0, 0 }; break;

                    case 1: lSalidas[t] = new double[] { 0, 0, 0, 1 }; break;

                    case 2: lSalidas[t] = new double[] { 0, 0, 1, 0 }; break;

                    case 3: lSalidas[t] = new double[] { 0, 0, 1, 1 }; break;

                    case 4: lSalidas[t] = new double[] { 0, 1, 0, 0 }; break;

                    case 5: lSalidas[t] = new double[] { 0, 1, 0, 1 }; break;

                    case 6: lSalidas[t] = new double[] { 0, 1, 1, 0 }; break;
                    }

                    t++;
                }
                pi++;
            }

            ActivationNetwork rna = null;

            if (!Directory.Exists("rnsa.rna"))
            {
                rna = CrearRNA(MatEntr, lSalidas);
                rna.Save("rnsa.rna");
            }
            else
            {
                rna = (ActivationNetwork)Network.Load("rnsa.rna");
            }

            Bitmap [] ns = ListaEntradas[0];
            foreach (Bitmap x in ns)
            {
                double [] text = rna.Compute(PictureAnalizer.CalcularCaracteristicasTextura(x));



                int l = 0;
                for (int f = 0; f < text.Length; f++)
                {
                    if (text[f] == 1)
                    {
                        l += (int)Math.Pow(2, f);
                    }
                }
                Console.Write(l + " ");
            }



            // generar arbol
            nodo r = new nodo(8, "s");

            arbol a1 = new arbol();

            r.tablero = a1.TabEjemplo;
            a1.raiz   = r;

            arbol a2 = new arbol();

            r.tablero = a2.TabEjemplo;
            a2.raiz   = r;
            imprimirTablero(r);

            nodo n1 = a1.generarMovimientos(a1.raiz, arbol.ColorBlanco);

            imprimirTablero(n1);


            for (int i = 0; i < 10; i++)
            {
                n1 = a1.generarMovimientos(n1, arbol.ColorBlanco);
                imprimirTablero(n1);

                nodo n2 = a2.generarMovimientos(n1, arbol.ColorNegroo);
                imprimirTablero(n2);

                n1 = n2;
            }
        }
Пример #11
0
 public void conectarPadre(nodo nodoPadre)
 {
     this.nodoPadre = nodoPadre;
 }
Пример #12
0
 public void conectarNodosHijos(nodo nodosHijos)
 {
     this.nodosHijos.Add(nodosHijos);
 }