Example #1
0
        void Main()
        {
            try
            {
                panel1.Enabled = false;
                panel2.Enabled = false;
                panel3.Enabled = false;
                panel4.Enabled = false;
                LoadData();
                File.Delete("1.txt");
                File.Delete("2.txt");
                File.Delete("3.txt");
                string dirname = configuration.Filename + '_';
                for (int i = 0; i < configuration.Size.Length; i++)
                {
                    dirname += configuration.Size[i].ToString() + ';';
                }
                dirname = dirname.TrimEnd(';');
                Directory.CreateDirectory(dirname);
                progressBar1.Maximum = configuration.Webs;
                for (int z = 0; z < configuration.Webs; z++)
                {
                    progressBar1.Value = z;
                    int n = 0;
                    n += configuration.BackEnabled ? 1 : 0;
                    n += configuration.GenEnabled ? 1 : 0;
                    n += configuration.SwarmEnabled ? 1 : 0;
                    int k = 0;
                    progressBar2.Maximum = n * configuration.Count;
                    progressBar2.Value   = 0;
                    this.Update();
                    if (configuration.BackEnabled)
                    {
                        FileStream    stream = new FileStream(dirname + '\\' + res_back + '_' + configuration.BackInfluence.ToString() + extension, FileMode.Create);
                        StreamWriter  writer = new StreamWriter(stream);
                        List <String> tmp    = new List <string>();
                        if (File.Exists("1.txt"))
                        {
                            FileStream   fileStream = new FileStream("1.txt", FileMode.Open);
                            StreamReader reader     = new StreamReader(fileStream);
                            while (!reader.EndOfStream)
                            {
                                tmp.Add(reader.ReadLine());
                            }
                            fileStream.Close();
                        }
                        else
                        {
                            tmp.AddRange(new String[configuration.Count]);
                        }
                        Web       web       = new Web(configuration.Size, configuration.BackInfluence);
                        Stopwatch stopwatch = new Stopwatch();
                        for (int i = 0; i < configuration.Count; i++)
                        {
                            progressBar2.Value = i;
                            this.Update();
                            stopwatch.Restart();
                            web.Teach(_input1, _output1);
                            stopwatch.Stop();
                            tmp[i] += (web.GetMistake(_input2, _output2) / (_output2.GetLength(0) * _output2.GetLength(1) - 1)).ToString() + "\t" + stopwatch.ElapsedMilliseconds.ToString() + "\t\t";
                        }
                        for (int i = 0; i < configuration.Count; i++)
                        {
                            writer.WriteLine(tmp[i]);
                        }
                        writer.Flush();
                        writer.Close();
                        stream.Close();
                        File.Delete("1.txt");
                        File.Move(dirname + '\\' + res_back + '_' + configuration.BackInfluence.ToString() + extension, "1.txt");
                        k++;
                        FileStream   Stream       = new FileStream(dirname + '\\' + "example" + '_' + res_back + '_' + configuration.BackInfluence.ToString() + extension, FileMode.Create);
                        StreamWriter streamWriter = new StreamWriter(Stream);
                        for (int i = 0; i < _output2.GetLength(0); i++)
                        {
                            double[] otmp = new double[_output2.GetLength(1)];
                            double[] itmp = new double[_input2.GetLength(1)];
                            for (int j = 0; j < _output2.GetLength(1); j++)
                            {
                                streamWriter.Write(_output2[i, j].ToString() + '\t');
                            }
                            for (int j = 0; j < _input2.GetLength(1); j++)
                            {
                                itmp[j] = _input2[i, j];
                            }
                            web.Input = itmp;
                            otmp      = web.Output;
                            streamWriter.Write("|\t");
                            for (int j = 0; j < otmp.GetLength(0); j++)
                            {
                                streamWriter.Write(otmp[j].ToString() + '\t');
                            }
                            streamWriter.WriteLine();
                        }
                        streamWriter.Flush();
                        streamWriter.Close();
                        Stream.Close();
                    }
                    if (configuration.GenEnabled)
                    {
                        FileStream stream = new FileStream(dirname + '\\' + res_gen
                                                           + '_' + configuration.GenPoolSize.ToString() + '_' + configuration.GenMutate
                                                           + '_' + configuration.GenMutateTime.ToString() + '_' + configuration.GenChange.ToString()
                                                           + '_' + configuration.GenElite.ToString() + '_' + configuration.GenBirth.ToString()
                                                           + '_' + configuration.GenM.ToString() + extension, FileMode.Create);
                        StreamWriter  writer = new StreamWriter(stream);
                        List <String> tmp    = new List <string>();
                        if (File.Exists("2.txt"))
                        {
                            FileStream   fileStream = new FileStream("2.txt", FileMode.Open);
                            StreamReader reader     = new StreamReader(fileStream);
                            while (!reader.EndOfStream)
                            {
                                tmp.Add(reader.ReadLine());
                            }
                            fileStream.Close();
                        }
                        else
                        {
                            tmp.AddRange(new String[configuration.Count]);
                        }
                        Genetic.size = configuration.Size;
                        Genetic genetic = new Genetic(configuration.GenPoolSize);
                        genetic.Mutatetime = configuration.GenMutateTime;
                        genetic.MutateRate = configuration.GenMutate;
                        genetic.Change     = configuration.GenChange;
                        genetic.Elite      = configuration.GenElite;
                        genetic.BirthRate  = configuration.GenBirth;
                        genetic.M          = configuration.GenM;
                        Stopwatch stopwatch = new Stopwatch();
                        int       l         = 0;
                        progressBar2.Value = k * configuration.Count;
                        for (int i = 0; i < configuration.Count; i++)
                        {
                            progressBar2.Value = k * configuration.Count + i;
                            this.Update();
                            stopwatch.Restart();
                            genetic.Generate(_input1, _output1);
                            stopwatch.Stop();
                            tmp[i] += (genetic.GetMistake(_input2, _output2) / (_output2.GetLength(0) * _output2.GetLength(1) - 1)).ToString() + "\t" + stopwatch.ElapsedMilliseconds.ToString() + "\t\t";
                        }
                        for (int i = 0; i < configuration.Count; i++)
                        {
                            writer.WriteLine(tmp[i]);
                        }
                        writer.Flush();
                        writer.Close();
                        stream.Close();
                        File.Delete("2.txt");
                        File.Move(dirname + '\\' + res_gen
                                  + '_' + configuration.GenPoolSize.ToString() + '_' + configuration.GenMutate
                                  + '_' + configuration.GenMutateTime.ToString() + '_' + configuration.GenChange.ToString()
                                  + '_' + configuration.GenElite.ToString() + '_' + configuration.GenBirth.ToString()
                                  + '_' + configuration.GenM.ToString() + extension, "2.txt");
                        k++;
                        FileStream Stream = new FileStream(dirname + '\\' + "example" + '_' + res_gen
                                                           + '_' + configuration.GenPoolSize.ToString() + '_' + configuration.GenMutate
                                                           + '_' + configuration.GenMutateTime.ToString() + '_' + configuration.GenChange.ToString()
                                                           + '_' + configuration.GenElite.ToString() + '_' + configuration.GenBirth.ToString()
                                                           + '_' + configuration.GenM.ToString() + extension, FileMode.Create);
                        StreamWriter streamWriter = new StreamWriter(Stream);
                        for (int i = 0; i < _output2.GetLength(0); i++)
                        {
                            double[] otmp = new double[_output2.GetLength(1)];
                            double[] itmp = new double[_input2.GetLength(1)];
                            for (int j = 0; j < _output2.GetLength(1); j++)
                            {
                                streamWriter.Write(_output2[i, j].ToString() + '\t');
                            }
                            for (int j = 0; j < _input2.GetLength(1); j++)
                            {
                                itmp[j] = _input2[i, j];
                            }
                            Web web = genetic.GetWeb();
                            web.Input = itmp;
                            otmp      = web.Output;
                            streamWriter.Write("|\t");
                            for (int j = 0; j < otmp.GetLength(0); j++)
                            {
                                streamWriter.Write(otmp[j].ToString() + '\t');
                            }
                            streamWriter.WriteLine();
                        }
                        streamWriter.Flush();
                        streamWriter.Close();
                        Stream.Close();
                    }
                    if (configuration.SwarmEnabled)
                    {
                        FileStream    stream = new FileStream(dirname + '\\' + res_swarm + '_' + configuration.SwarmPoolSize.ToString() + '_' + configuration.SwarmA1.ToString() + '_' + configuration.SwarmA2.ToString() + '_' + configuration.SwarmXLimit.ToString() + '_' + configuration.SwarmVLimit.ToString() + extension, FileMode.Create);
                        StreamWriter  writer = new StreamWriter(stream);
                        List <String> tmp    = new List <string>();
                        if (File.Exists("3.txt"))
                        {
                            FileStream   fileStream = new FileStream("3.txt", FileMode.Open);
                            StreamReader reader     = new StreamReader(fileStream);
                            while (!reader.EndOfStream)
                            {
                                tmp.Add(reader.ReadLine());
                            }
                            fileStream.Close();
                        }
                        else
                        {
                            tmp.AddRange(new String[configuration.Count]);
                        }
                        Swarm swarm = new Swarm(configuration.SwarmPoolSize, configuration.Size, configuration.SwarmXLimit, configuration.SwarmVLimit, configuration.SwarmA1, configuration.SwarmA2);
                        swarm.UpdateCoords(_input1, _output1);
                        Stopwatch stopwatch = new Stopwatch();
                        for (int i = 0; i < configuration.Count; i++)
                        {
                            progressBar2.Value = k * configuration.Count + i;
                            this.Update();
                            stopwatch.Restart();
                            swarm.Move(_input1, _output1);
                            stopwatch.Stop();
                            tmp[i] += (swarm.GetMistake(_input2, _output2) / (_output2.GetLength(0) * _output2.GetLength(1) - 1)).ToString() + "\t" + stopwatch.ElapsedMilliseconds.ToString() + "\t\t";
                        }
                        for (int i = 0; i < configuration.Count; i++)
                        {
                            writer.WriteLine(tmp[i]);
                        }
                        writer.Flush();
                        writer.Close();
                        stream.Close();
                        File.Delete("3.txt");
                        File.Move(dirname + '\\' + res_swarm + '_' + configuration.SwarmPoolSize.ToString() + '_' + configuration.SwarmA1.ToString()
                                  + '_' + configuration.SwarmA2.ToString() + '_' + configuration.SwarmXLimit.ToString() + '_' + configuration.SwarmVLimit.ToString() + extension, "3.txt");
                        FileStream Stream = new FileStream(dirname + '\\' + "example" + '_' + res_swarm + '_' + configuration.SwarmPoolSize.ToString() + '_' + configuration.SwarmA1.ToString()
                                                           + '_' + configuration.SwarmA2.ToString() + '_' + configuration.SwarmXLimit.ToString() + '_' + configuration.SwarmVLimit.ToString() + extension, FileMode.Create);
                        StreamWriter streamWriter = new StreamWriter(Stream);
                        for (int i = 0; i < _output2.GetLength(0); i++)
                        {
                            double[] otmp = new double[_output2.GetLength(1)];
                            double[] itmp = new double[_input2.GetLength(1)];
                            for (int j = 0; j < _output2.GetLength(1); j++)
                            {
                                streamWriter.Write(_output2[i, j].ToString() + '\t');
                            }
                            for (int j = 0; j < _input2.GetLength(1); j++)
                            {
                                itmp[j] = _input2[i, j];
                            }
                            Web web = swarm.GetWeb();
                            web.Input = itmp;
                            otmp      = web.Output;
                            streamWriter.Write("|\t");
                            for (int j = 0; j < otmp.GetLength(0); j++)
                            {
                                streamWriter.Write(otmp[j].ToString() + '\t');
                            }
                            streamWriter.WriteLine();
                        }
                        streamWriter.Flush();
                        streamWriter.Close();
                        Stream.Close();
                    }
                }
                if (File.Exists("1.txt"))
                {
                    File.Move("1.txt", dirname + '\\' + res_back + '_' + configuration.BackInfluence.ToString() + extension);
                }
                if (File.Exists("2.txt"))
                {
                    File.Move("2.txt", dirname + '\\' + res_gen
                              + '_' + configuration.GenPoolSize.ToString() + '_' + configuration.GenMutate
                              + '_' + configuration.GenMutateTime.ToString() + '_' + configuration.GenChange.ToString()
                              + '_' + configuration.GenElite.ToString() + '_' + configuration.GenBirth.ToString()
                              + '_' + configuration.GenM.ToString() + extension);
                }
                if (File.Exists("3.txt"))
                {
                    File.Move("3.txt", dirname + '\\' + res_swarm + '_' + configuration.SwarmPoolSize.ToString() + '_' + configuration.SwarmA1.ToString() + '_' + configuration.SwarmA2.ToString() + '_' + configuration.SwarmXLimit.ToString() + '_' + configuration.SwarmVLimit.ToString() + extension);
                }
                MessageBox.Show("Обучение завершено!");
            }

            /*catch (Exception e)
             * {
             *  MessageBox.Show(e.Message);
             * }*/
            finally
            {
                panel1.Enabled = true;
                panel2.Enabled = true;
                panel3.Enabled = true;
                panel4.Enabled = true;
            }
        }