Example #1
0
        static void Main(string[] args)
        {
            NN nn = new NN();

            nn.Run();
            Console.ReadKey();
        }
Example #2
0
        private static void TrainAll()
        {
            //-------------------primas NN-------------------
            // Tikslas: nustatyti optimaliausia diena pamokai iterpti
            // Duomenys: visos savaites klases, kai langas = 0, uzimta pamoka = 1, tokia pati pamoka = 0.5
            // Rezultatai: diena nuo 1 iki 5 i kuria reikia ideti pamoka
            //
            List <NN> firstNets = new List <NN>();           // NN tinklu sarasas

            int[] firstLayers = new int[] { 40, 20, 10, 1 }; //sluoksniu sistema

            NN firstNet = new NN(firstLayers);

            TrainFirst(ref firstNets, firstLayers);


            //-------------------antras NN-------------------
            // Tikslas: nustatyti optimaliausia langa pamokai iterpti
            // Duomenys: vienos dienos pamokos, kai langas = 0, uzimta pamoka = 1, tokia pati pamoka = 0.5
            // Rezultatai: langas nuo 1 iki 8 i kuri reikia ideti pamoka
            //
            List <NN> secondNets = new List <NN>();          // NN tinklu sarasas

            int[] secondLayers = new int[] { 8, 10, 10, 1 }; //sluoksniu sistema

            NN secondNet = new NN(secondLayers);

            TrainSecond(ref secondNets, secondLayers);
        }
Example #3
0
        public int CompareTo(NN other)  //skirtas ivertinti ar NN yra geresnis uz kita
        {
            if (other == null)
            {
                return(1);
            }

            if (fitness > other.fitness)
            {
                return(1);
            }

            else if (fitness <= other.fitness)
            {
                return(-1);
            }

            else
            {
                return(0);
            }

            //reiksme 1: esamas NN yra geresnis arba kitas NN neaprasytas
            //reiksme 0: klaida
            //reiksme -1: esamas NN yra blogesnis uz kita NN
        } // CompareTo()
Example #4
0
        public static float LaunchFirst(NN net, int[] layers, float[] chosen)
        {
            StreamReader sk = new StreamReader(@"weightValues1.txt", System.Text.Encoding.GetEncoding(1257));

            net.ReadWeights(sk);
            sk.Close();

            float output = ((net.FeedForward(chosen)[0]) + 1f) * 2.5f; //gaunamas outputas is neural network

            Console.WriteLine("First AI result: {0}", output);

            List <float> possibilities = new List <float>();  //galimos vietos ikelti pamoka

            for (int i = 0; i < 5; i++)
            {
                if (chosen[i * 3 + 2] == 0f || chosen[i * 3] + chosen[i * 3 + 1] == 8f)
                {
                    possibilities.Add(i);
                }

                if (chosen[i * 3 + 2] == 0f && chosen[i * 3] + chosen[i * 3 + 1] == 8f)
                {
                    possibilities.Add(i);
                }
            }

            float nearest = ClosestTo(possibilities, output);

            return(nearest);
        }
Example #5
0
        public Form1()
        {            
            InitializeComponent();

            nn = new NN(2, 1, 1, 4, 1);
            pool = new GP(poolSize, nn.weightCount());
            
            for (byte b = 0; b < 4; b++)
            {
                bits[b, 0] = GetBit(b, 0) ? 1 : -1;
                bits[b, 1] = GetBit(b, 1) ? 1 : -1;
            }
        }
Example #6
0
        static void InitNN(ref List <NN> nets, ref int populationSize, int[] layers) //NN apsirasymas
        {
            if (populationSize % 2 != 0)                                             //jeigu nelyginis, paverciamas lyginiu
            {
                populationSize--;
            }

            for (int i = 0; i < populationSize; i++) //kiekvienam NN
            {
                NN net = new NN(layers);             //naujas net su layers
                net.Mutate();                        //mutuojamas
                nets.Add(net);                       //pridedamas prie bendro NN saraso
            }
        }
Example #7
0
        } // NN()

        public NN(NN copyNetwork)   // Constructor, perkopijuoja kita NN
        {
            this.layers = new int[copyNetwork.layers.Length];

            for (int i = 0; i < copyNetwork.layers.Length; i++)
            {
                this.layers[i] = copyNetwork.layers[i]; //kopijuojamas kiekvienas sluoksnis kaip naujas
            }

            rnd = new Random();               //random rodiklis

            InitNeurons();                    //aprasomi neuronai
            InitWeights();                    //aprasomi svoriai

            CopyWeights(copyNetwork.weights); //svoriu reiksmes perkopijuojamos is kito NN
        } // NN()
Example #8
0
        private void Form1_Load(object sender, EventArgs e)
        {
            label2.Visible = false;
            network        = new NN();
            Bitmap bmp = new Bitmap(168, 168);

            pictureBox1.Image = bmp;
            ifsPixelst        = new FileStream(testpixelFile, FileMode.Open);
            ifsLabelst        = new FileStream(testlabelFile, FileMode.Open);
            brImagest         = new BinaryReader(ifsPixelst);
            brLabelst         = new BinaryReader(ifsLabelst);
            for (int i = 0; i < 4; i++)
            {
                brImagest.ReadInt32();
            }
            brLabelst.ReadInt32();
            brLabelst.ReadInt32();
        }
Example #9
0
        static void Update(ref int generation, ref List <NN> nets, ref int populationSize, int[] layers)
        {
            SortNet(ref nets, populationSize);                    //surusiuoja didejancia tvarka

            for (int i = 0; i < populationSize / 2; i++)          //puse zemiausia arba auksciausi NN
            {
                nets[i] = new NN(nets[i + (populationSize / 2)]); //sukuria kopija tokia paties bet analogiskai geresnio NN
                nets[i].Mutate();

                nets[i + (populationSize / 2)] = new NN(nets[i + (populationSize / 2)]);    //geriausi NN is naujo perdaromi identiskais
            }

            for (int i = 0; i < populationSize; i++) //kiekvienas NN
            {
                nets[i].SetFitness(0f);              //nustato pagrinda kokybes lygiui
            }

            generation++; //generacija padideja
        }
Example #10
0
        public static int LaunchSecond(NN net, int[] layers, ref string[] chosen, string reference)
        {
            StreamReader sk = new StreamReader(@"weightValues2.txt", System.Text.Encoding.GetEncoding(1257));

            net.ReadWeights(sk);
            sk.Close();

            const int dataCount = 8;                    //kiek pradiniu duomenu

            float[] primalData = new float[dataCount];  //naujas duomenu masyvas NN'ui

            for (int i = 0; i < dataCount; i++)
            {
                primalData[i] = 1f;
            }

            primalData = TurnSecond(chosen, reference);                  //jau suzymetos klases ir langai paverciami i duomenis NN'ui

            float output = ((net.FeedForward(primalData)[0]) + 1f) * 4f; //gaunamas outputas is neural network

            Console.WriteLine("Second AI result: {0}", output);

            List <float> possibilities = new List <float>();      //galimos vietos ikelti pamoka

            for (int j = 0; j < dataCount; j++)
            {
                if (chosen[j] == "-")
                {
                    possibilities.Add(j);     //surenkamos visos tuscios vietos
                }
            }

            float nearest = ClosestTo(possibilities, output);   //surandama artimiausia tuscia vieta pamokai

            return((int)nearest);
        }
Example #11
0
 private void buttonReset_Click(object sender, EventArgs e)
 {
     nn = new NN(2, 1, 1, 4, 1);
     pool = new GP(poolSize, nn.weightCount());
 }
Example #12
0
        //Sekanti dalis skirta ikelti i button_press ar kazka tokio,
        //cia todel kad negaliu tame darbe dabar keisti nieko, tai
        //rasau cia

        public static void GenerateAllTimetables(TTdata[] fullList, bool[][,] preferedTimes)
        {
            //pakeiciami duomenys i klases, reikalingas irasyti i
            //lentele, t.y. klase arba kelios klases vienam mokytojui
            //per viena pamoka. Po to pamokos surasomos mokytojams

            const char symbol = '-';

            List <string[, ]> allTimetables = new List <string[, ]>(); //pagrindine lentele
            List <string>     classList     = new List <string>();     //ka irasyti vienam mokytojui
            List <string>     usedTeachers  = new List <string>();     //jau surasyti mokytojai
            List <string>     classes       = new List <string>();     //visos esamos klases
            List <string>     teacherList   = new List <string>();     //visi mokytojai, gali ir kartotis

            for (int i = 0; i < fullList.Length; i++)
            {
                teacherList.Add(fullList[i].GetTeacher());

                //mokytoju sarasas (mokytojai kartojasi!)
                //to reikia kad skirtingus dalykus vienam mokytojui rodytu skirtingose eilutese tvarkarastyje
                //norint pakeisti, ideti if(teacherList.Contains(fullList[i].GetTeacher())) funkcija.
            }

            int[] firstLayers  = new int[] { 40, 20, 10, 1 };
            int[] secondLayers = new int[] { 8, 10, 10, 1 };

            NN pickDay    = new NN(firstLayers);
            NN pickLesson = new NN(secondLayers);

            for (int i = 0; i < fullList.Length; i++)   //sarasas visu klasiu, be pasikartojimu
            {
                if (!classes.Contains(fullList[i].GetTeacher()))
                {
                    classes.Add(fullList[i].GetClass());
                }
            }

            for (int i = 0; i < classes.Capacity; i++)  //visa tvarkarasti nustato i '-' simboli
            {
                for (int j = 0; j < teacherList.Capacity; j++)
                {
                    for (int k = 0; k < 40; k++)
                    {
                        allTimetables[i][j, k] = symbol.ToString();
                    }
                }
            }

            string[][,] complete = allTimetables.ToArray();     //sukuriamas specifinis trimatis masyvas
            int[] timetableFitness = new int[classes.Capacity]; //sukuriamas masyvas stebeti tvarkarasciu kokybei

            for (int u = 0; u < classes.Capacity; u++)
            {
                for (int i = 0; i < teacherList.Capacity; i++)
                {
                    if (!usedTeachers.Contains(fullList[i].GetTeacher()))
                    {
                        classList = ExpandClass(fullList, fullList[i].GetTeacher(), preferedTimes[i]).ToList(); //sukuriamas sarasas mokytojo pamokoms

                        usedTeachers.Add(fullList[i].GetTeacher());                                             //kad to paties mokytojo neidetu i kelias klases

                        for (int j = 0; j < classList.Capacity; j++)
                        {
                            BeginFeeding(ref complete[u], i, classList[j], pickDay, pickLesson, firstLayers, secondLayers); //ikeliaos pamokos mokytojams po viena
                        }
                    }
                }

                timetableFitness[u] = GenerateClassTimetable(fullList, classes[u], complete[u], teacherList);   //sudarytas vienas tvarkarastis
            }

            SortTimetables(ref complete, timetableFitness); //surusiuojami tvarkarasciai pagal ju kokybe, geriausi pirmieji

            //Prideti isvedima i dataGridView
            //Prideti esamo lango uzdaryma
            //Prideti naujo lango (perziurejimo lenteles) atidaryma
        }
Example #13
0
        public static void BeginFeeding(ref string[,] timetableMatrica, int teacherIndex, string toInput, /**/ NN firstNet, NN secondNet, int[] firstLayers, int[] secondLayers)
        {
            //init

            //assumes timetableMatrica is not jagged array
            string[] byName = new string[timetableMatrica.GetLength(0)];
            float[]  input;

            string[] dayClasses = new string[8];

            //set up data for feedforward
            for (int i = 0; i < timetableMatrica.Length; i++)
            {
                for (int j = 0; j < timetableMatrica.GetLength(i); j++)
                {
                    if (timetableMatrica[i, j] == toInput)
                    {
                        byName[i] = toInput;
                    }
                    else if (timetableMatrica[i, j] != " " && timetableMatrica[i, j] != "-" && timetableMatrica[i, j] != "")
                    {
                        byName[i] = timetableMatrica[i, j];
                    }
                    else
                    {
                        byName[i] = "-";
                    }
                }
            }

            input = TurnFirst(byName, toInput);
            float day = LaunchFirst(firstNet, firstLayers, input);

            for (int i = 0; i < 8; i++)
            {
                dayClasses[i] = byName[5 * (int)day + i];
            }

            int lesson = LaunchSecond(secondNet, secondLayers, ref dayClasses, toInput);

            timetableMatrica[teacherIndex, (int)day * 5 + lesson] = toInput;
        }
Example #14
0
        static void Main(string[] args)
        {
            //Random r = new Random();
            //while (true)
            //{
            //    Console.WriteLine(r.NextDouble());
            //}


            Console.Title = "";
            NN ntw = new NN(
                layers_count: 1,
                neurons_count: 3,
                inputs_count: 2,
                learning_rate: .1);

            double[] vals = new double[4] {
                0, 0, 0, 0
            };
            double[] oldvals = new double[4];// = vals;

            vals[0] = ntw.GetResult(new double[] { 0, 0 });
            Console.WriteLine(vals[0]);
            vals[1] = ntw.GetResult(new double[] { 0, 1 });
            Console.WriteLine(vals[1]);
            vals[2] = ntw.GetResult(new double[] { 1, 0 });
            Console.WriteLine(vals[2]);
            vals[3] = ntw.GetResult(new double[] { 1, 1 });
            Console.WriteLine(vals[3]);

            oldvals[0] = vals[0];
            oldvals[1] = vals[1];
            oldvals[2] = vals[2];
            oldvals[3] = vals[3];

            //https://ai.stackexchange.com/questions/6167/what-is-the-best-xor-neural-network-configuration-out-there-in-terms-of-low-erro
            //ntw.Network[1][1].Weights = new double[] { 10.0676, -6.6619, -6.3597 };
            //ntw.Network[1][2].Weights = new double[] { 2.8261, -5.9874, -9.9025 };
            //ntw.Network[2][0].Weights = new double[] { -4.6458, 9.461, -9.9307 };

            for (int i = 0; i < 10000000; i++)
            {
                ntw.Teach(new double[] { 0, 1 }, 1);
                ntw.Teach(new double[] { 1, 0 }, 1);
                ntw.Teach(new double[] { 0, 0 }, 0);
                ntw.Teach(new double[] { 1, 1 }, 0);



                vals[0] = ntw.GetResult(new double[] { 0, 0 });
                vals[1] = ntw.GetResult(new double[] { 0, 1 });
                vals[2] = ntw.GetResult(new double[] { 1, 0 });
                vals[3] = ntw.GetResult(new double[] { 1, 1 });



                string title = "";
                //title += (vals[0] == oldvals[0] ? "E" : vals[0] < oldvals[0] ? "D" : "U") + "  ";
                //title += (vals[1] == oldvals[1] ? "E" : vals[1] < oldvals[1] ? "D" : "U") + "  ";
                //title += (vals[2] == oldvals[2] ? "E" : vals[2] < oldvals[2] ? "D" : "U") + "  ";
                //title += (vals[3] == oldvals[3] ? "E" : vals[3] < oldvals[3] ? "D" : "U") + "  ";
                title        += (vals[0] == oldvals[0] ? "-" : vals[0] < oldvals[0] ? "↓" : "↑") + "  ";
                title        += (vals[1] == oldvals[1] ? "-" : vals[1] < oldvals[1] ? "↓" : "↑") + "  ";
                title        += (vals[2] == oldvals[2] ? "-" : vals[2] < oldvals[2] ? "↓" : "↑") + "  ";
                title        += (vals[3] == oldvals[3] ? "-" : vals[3] < oldvals[3] ? "↓" : "↑") + "  ";
                title        += "          ";
                title        += Math.Round(vals[0], 3) + "  ";
                title        += Math.Round(vals[1], 3) + "  ";
                title        += Math.Round(vals[2], 3) + "  ";
                title        += Math.Round(vals[3], 3) + "  ";
                Console.Title = title;

                oldvals[0] = vals[0];
                oldvals[1] = vals[1];
                oldvals[2] = vals[2];
                oldvals[3] = vals[3];


                Console.Write(i);
                Console.Write("    ");
                Console.Write(vals[0]);
                Console.Write("    ");
                Console.Write(vals[1]);
                Console.Write("    ");
                Console.Write(vals[2]);
                Console.Write("    ");
                Console.Write(vals[3]);
                Console.WriteLine("");


                //Console.Write("    ");
                //Console.Write(ntw.GetResult(new double[] { 0, 0 }));
                //Console.Write("    ");
                //Console.Write(ntw.GetResult(new double[] { 0, 1 }));
                //Console.Write("    ");
                //Console.Write(ntw.GetResult(new double[] { 1, 0 }));
                //Console.Write("    ");
                //Console.Write(ntw.GetResult(new double[] { 1, 1 }));
                //Console.WriteLine("");
            }



            //Console.WriteLine(ntw.GetResult(new double[] { 0.54, 0.2 }));
            //Console.WriteLine(ntw.GetResult(new double[] { 0, 1 }));
            //Console.WriteLine(ntw.GetResult(new double[] { 1, 0 }));
            //Console.WriteLine(ntw.GetResult(new double[] { 1, 1 }));

            //for (int i = 0; i < 10000; i++)
            //{
            //    ntw.Teach(new double[] { 0, 0 }, 0);
            //    ntw.Teach(new double[] { 0, 1 }, 1);
            //    ntw.Teach(new double[] { 1, 0 }, 1);
            //    ntw.Teach(new double[] { 1, 1 }, 0);
            //}

            //Console.WriteLine("--");

            //Console.WriteLine(ntw.GetResult(new double[] { 0, 0 }));
            //Console.WriteLine(ntw.GetResult(new double[] { 0, 1 }));
            //Console.WriteLine(ntw.GetResult(new double[] { 1, 0 }));
            //Console.WriteLine(ntw.GetResult(new double[] { 1, 1 }));

            Console.ReadLine();
        }