Exemplo n.º 1
0
        public List <string[]> SelectNeuroNetDefinitions()
        {
            List <string[]> defs = new List <string[]>();

            connector.ConnectToDB();
            SQLiteCommand cmd = new SQLiteCommand(connector.connection);

            cmd.CommandText = "SELECT NeuroNet.Name, TopologyTypeName, TASK.NAME, NeuronCount, " +
                              "LayerCount, ActivateFunctionType " +
                              "FROM NeuroNet,TASK WHERE NeuroNet.TaskID = TASK.ID";
            try
            {
                SQLiteDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    string[] line = new string[6];
                    for (int i = 0; i < 6; i++)
                    {
                        line[i] = reader[i].ToString();
                    }
                    line[1] = LibraryOfTopologies.GetTopologyName(line[1]);
                    line[5] = LibraryOfActivateFunctions.GetActivateFunctionName(line[5]);
                    defs.Add(line);
                }
                reader.Close();
            }
            catch (SQLiteException ex)
            {
                MessageBox.Show(ex.Message);
            }
            connector.DisconnectFromDB();

            return(defs);
        }
Exemplo n.º 2
0
        public string[] SelectNeuroNetDefinitionByName(string name)
        {
            connector.ConnectToDB();
            SQLiteCommand cmd = new SQLiteCommand(connector.connection);

            cmd.CommandText = "SELECT NeuroNet.Name, TopologyTypeName, TASK.NAME, NeuronCount, " +
                              "LayerCount, NeuronsInLayer, ActivateFunctionType, AFParameters " +
                              "FROM NeuroNet,TASK WHERE NeuroNet.TaskID = TASK.ID AND NeuroNet.Name ='"
                              + name + "'";
            string[] line = new string[8];
            try
            {
                SQLiteDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    for (int i = 0; i < 8; i++)
                    {
                        line[i] = reader[i].ToString();
                    }
                }
                line[1] = LibraryOfTopologies.GetTopologyName(line[1]);
                line[6] = LibraryOfActivateFunctions.GetActivateFunctionName(line[6]);
                reader.Close();
            }
            catch (SQLiteException ex)
            {
                MessageBox.Show(ex.Message);
            }
            connector.DisconnectFromDB();

            return(line);
        }
Exemplo n.º 3
0
        public void InsertTopologyOfNeuroNet(string NameNN, string TopologyTypeName,
                                             int CountNeurons, int CountLayers, int[] NeuronsInLayers, LoadingWindow loadingWindow)
        {
            connector.ConnectToDB();
            SQLiteCommand cmd = new SQLiteCommand(connector.connection);

            cmd.CommandText = "SELECT ID FROM NeuroNet WHERE Name = '" + NameNN + "'";
            int NetID = Convert.ToInt32(cmd.ExecuteScalar());

            Topology topology = LibraryOfTopologies.GetTopology(TopologyTypeName,
                                                                LibraryOfTopologies.GetterParameter.TypeOfTopologyName);

            bool[,] connections = topology.CreateNet(CountNeurons, CountLayers, NeuronsInLayers);

            double progressValue = 0.0;

            for (int i = 0; i < CountNeurons; i++)
            {
                for (int j = 0; j < CountNeurons; j++)
                {
                    if (connections[i, j] == true)
                    {
                        cmd.CommandText = "INSERT INTO NetTopology (IndexInputNeuron, " +
                                          "IndexOutputNeuron, NeuroNetID) values('" + Convert.ToString(j) +
                                          "','" + Convert.ToString(i) + "','" + Convert.ToString(NetID) + "')";
                        cmd.ExecuteNonQuery();
                    }
                    progressValue += 100.0 / Convert.ToDouble(CountNeurons * CountNeurons);
                    Action f = new Action(() => loadingWindow.LoadingBar.Value = Convert.ToInt32(progressValue));
                    if (loadingWindow.LoadingBar.InvokeRequired)
                    {
                        loadingWindow.LoadingBar.Invoke(f);
                    }
                    else
                    {
                        f();
                    }
                }
            }
            connector.DisconnectFromDB();
            loadingWindow.Invoke(new Action(() => loadingWindow.Close()));
        }
Exemplo n.º 4
0
        public AddChangeNeuroNetDialog(DataBaseHandler _dbHandler, NeuroNetDefinition neuro_def = null)
        {
            InitializeComponent();

            if (neuro_def == null)
            {
                mode = form_destination.Creating;
            }
            else
            {
                mode = form_destination.Editing;
                definition_of_editing_net = neuro_def;
            }

            dbHandler = _dbHandler;

            List <string> ls = dbHandler.SelectTasks();

            foreach (string par in ls)
            {
                cbTask.Items.Add(par);
            }

            ls = LibraryOfTopologies.GetAllTopologyTypeNames();
            foreach (string item in ls)
            {
                cbTopology.Items.Add(item);
            }

            string[] namesAF = LibraryOfActivateFunctions.GetAllActivateFunctionNames();
            for (int i = 0; i < LibraryOfActivateFunctions.GetCountActivateFunctions(); i++)
            {
                cbActivateFunction.Items.Add(namesAF[i]);
            }

            ls = dbHandler.SelectCountParametersInTasks();
            countInputParamsInTask = new int[ls.Count];
            for (int i = 0; i < ls.Count; i++)
            {
                countInputParamsInTask[i] = Convert.ToInt32(ls[i]);
            }

            dgwEditNeuronsInLayers.ColumnHeadersDefaultCellStyle.Font = new Font("Book Antiqua", 9);
            dgwEditNeuronsInLayers.Columns.Add("Layer", "Слой");
            dgwEditNeuronsInLayers.Columns.Add("Neurons", "Число нейронов");
            RefreshDgwNeuronsInLayers();

            dgwEditParametersAF.ColumnHeadersDefaultCellStyle.Font = new Font("Book Antiqua", 9);
            dgwEditParametersAF.Columns.Add("Parameter", "Параметр");
            dgwEditParametersAF.Columns.Add("Value", "Значение");

            switch (mode)
            {
            case form_destination.Creating:
                Text = "Создание нейронной сети";
                break;

            case form_destination.Editing:
                Text = "Изменение Нейронной Сети";
                tbNameNeuroNet.Text = definition_of_editing_net.Name;
                for (int i = 0; i < cbTask.Items.Count; i++)
                {
                    if (String.Compare(cbTask.Items[i].ToString(), definition_of_editing_net.TaskName) == 0)
                    {
                        cbTask.SelectedIndex = i;
                        break;
                    }
                }
                for (int i = 0; i < cbTopology.Items.Count; i++)
                {
                    if (String.Compare(cbTopology.Items[i].ToString(), definition_of_editing_net.TopologyName) == 0)
                    {
                        cbTopology.SelectedIndex = i;
                        break;
                    }
                }
                if (cbTopology.SelectedIndex == -1)
                {
                    MessageBox.Show("Топология, примененная в данной нейронной сети, " +
                                    "не найдена. Она будет недоступна при изменении сети.");
                }

                numNeuronsNumber.Value = definition_of_editing_net.NeuronsCount;
                numLayersNumber.Value  = definition_of_editing_net.LayerCount;

                for (int i = 0; i < definition_of_editing_net.LayerCount; i++)
                {
                    dgwEditNeuronsInLayers.Rows[i].Cells[1].Value = definition_of_editing_net.NeuronsInLayer[i];
                }

                for (int i = 0; i < cbActivateFunction.Items.Count; i++)
                {
                    if (String.Compare(cbActivateFunction.Items[i].ToString(), definition_of_editing_net.ActivateFunction) == 0)
                    {
                        cbActivateFunction.SelectedIndex = i;
                        break;
                    }
                }

                for (int i = 0; i < definition_of_editing_net.AFParameters.Length; i++)
                {
                    dgwEditParametersAF.Rows[i].Cells[1].Value = definition_of_editing_net.AFParameters[i];
                }
                break;
            }
        }
Exemplo n.º 5
0
        private void LoadNeuroNetIntoDB()
        {
            if (mode == form_destination.Creating)
            {
                try
                {
                    string NameNN = tbNameNeuroNet.Text;
                    if (NameNN == "")
                    {
                        throw new Exception("Имя НС не может быть пустым");
                    }

                    if (dbHandler.IsNeuroNetExist(NameNN))
                    {
                        throw new Exception("Такое имя НС уже используется");
                    }

                    if (cbTopology.SelectedItem == null)
                    {
                        throw new Exception("Необходимо выбрать топологию");
                    }
                    string TopologyTypeName = LibraryOfTopologies.GetTopologyTypeName(cbTopology.SelectedItem.ToString());

                    if (cbTask.SelectedItem == null)
                    {
                        throw new Exception("Необходимо выбрать задачу");
                    }
                    string TaskName = cbTask.SelectedItem.ToString();

                    int    CountNeurons = Convert.ToInt32(numNeuronsNumber.Value);
                    int    CountLayers  = Convert.ToInt32(numLayersNumber.Value);
                    string typeAF       = LibraryOfActivateFunctions.GetActivateFunctionTypeName(cbActivateFunction.SelectedItem.ToString());
                    if (cbActivateFunction.SelectedIndex < 0)
                    {
                        throw new Exception("Необходимо выбрать активационную функцию");
                    }

                    string NeuronsInLayers = "";
                    int[]  NeuInL          = new int[CountLayers];
                    string ParametersAF    = "";

                    int sumNeurons = 0;
                    for (int i = 0; i < CountLayers; i++)
                    {
                        int neurs = Convert.ToInt32(dgwEditNeuronsInLayers.Rows[i].Cells[1].Value);
                        if (neurs <= 0)
                        {
                            throw new Exception("В каждом слое должен быть по крайней мере 1 нейрон");
                        }
                        sumNeurons += neurs;

                        NeuInL[i] = neurs;

                        NeuronsInLayers += Convert.ToString(dgwEditNeuronsInLayers.Rows[i].Cells[1].Value);
                        if (i != CountLayers - 1)
                        {
                            NeuronsInLayers += " ";
                        }
                    }
                    if (sumNeurons != CountNeurons)
                    {
                        throw new Exception("Сумма нейронов по слоям должна равняться общему числу нейронов");
                    }

                    for (int i = 0; i < LibraryOfActivateFunctions.GetCountParametersOfAF(typeAF,
                                                                                          LibraryOfActivateFunctions.GetterParameter.TypeOfActivateFunctionName); i++)
                    {
                        double par = Convert.ToDouble(dgwEditParametersAF.Rows[i].Cells[1].Value);
                        ParametersAF += Convert.ToString(par);
                        if (i != LibraryOfActivateFunctions.GetCountParametersOfAF(typeAF,
                                                                                   LibraryOfActivateFunctions.GetterParameter.TypeOfActivateFunctionName) - 1)
                        {
                            ParametersAF += " ";
                        }
                    }

                    dbHandler.InsertNeuroNet(NameNN, TopologyTypeName, TaskName, CountNeurons, CountLayers, NeuronsInLayers, typeAF, ParametersAF);

                    LoadingWindow loadingWindow = new LoadingWindow();
                    loadingWindow.MakeLoading(
                        () => dbHandler.InsertTopologyOfNeuroNet(NameNN, TopologyTypeName, CountNeurons, CountLayers, NeuInL, loadingWindow),
                        "Запись нейронной сети в БД");
                    Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else if (mode == form_destination.Editing)
            {
                try
                {
                    string NameNN = tbNameNeuroNet.Text;
                    if (NameNN == "")
                    {
                        throw new Exception("Имя НС не может быть пустым");
                    }

                    if (cbTopology.SelectedItem == null)
                    {
                        throw new Exception("Необходимо выбрать топологию");
                    }
                    string TopologyTypeName = LibraryOfTopologies.GetTopologyTypeName(cbTopology.SelectedItem.ToString());

                    string TaskName = cbTask.SelectedItem.ToString();

                    int    CountNeurons = Convert.ToInt32(numNeuronsNumber.Value);
                    int    CountLayers  = Convert.ToInt32(numLayersNumber.Value);
                    string typeAF       = LibraryOfActivateFunctions.GetActivateFunctionTypeName(cbActivateFunction.SelectedItem.ToString());

                    string   NeuronsInLayers = "";
                    int[]    NeuInL          = new int[CountLayers];
                    double[] ParAF           = new double[LibraryOfActivateFunctions.GetCountParametersOfAF(typeAF,
                                                                                                            LibraryOfActivateFunctions.GetterParameter.TypeOfActivateFunctionName)];
                    string ParametersAF = "";

                    int sumNeurons = 0;
                    for (int i = 0; i < CountLayers; i++)
                    {
                        int neurs = Convert.ToInt32(dgwEditNeuronsInLayers.Rows[i].Cells[1].Value);
                        if (neurs <= 0)
                        {
                            throw new Exception("В каждом слое должен быть по крайней мере 1 нейрон");
                        }
                        sumNeurons += neurs;

                        NeuInL[i] = neurs;

                        NeuronsInLayers += Convert.ToString(dgwEditNeuronsInLayers.Rows[i].Cells[1].Value);
                        if (i != CountLayers - 1)
                        {
                            NeuronsInLayers += " ";
                        }
                    }
                    if (sumNeurons != CountNeurons)
                    {
                        throw new Exception("Сумма нейронов по слоям должна равняться общему числу нейронов");
                    }

                    for (int i = 0; i < LibraryOfActivateFunctions.GetCountParametersOfAF(typeAF,
                                                                                          LibraryOfActivateFunctions.GetterParameter.TypeOfActivateFunctionName); i++)
                    {
                        ParAF[i]      = Convert.ToDouble(dgwEditParametersAF.Rows[i].Cells[1].Value);
                        ParametersAF += Convert.ToString(dgwEditParametersAF.Rows[i].Cells[1].Value);
                        if (i != LibraryOfActivateFunctions.GetCountParametersOfAF(typeAF,
                                                                                   LibraryOfActivateFunctions.GetterParameter.TypeOfActivateFunctionName) - 1)
                        {
                            ParametersAF += " ";
                        }
                    }

                    bool isContining           = true;
                    bool isNeuronsInLayersSame = false;
                    bool isParametersAFSame    = false;
                    bool isNeedToDelete        = false;

                    if (CountLayers == definition_of_editing_net.LayerCount)
                    {
                        int k = 0;
                        for (int i = 0; i < CountLayers; i++)
                        {
                            if (NeuInL[i] == definition_of_editing_net.NeuronsInLayer[i])
                            {
                                k++;
                            }
                        }
                        if (k == CountLayers)
                        {
                            isNeuronsInLayersSame = true;
                        }
                    }
                    if (String.Compare(cbActivateFunction.SelectedItem.ToString(), definition_of_editing_net.ActivateFunction) == 0)
                    {
                        int k = 0;
                        for (int i = 0; i < definition_of_editing_net.AFParameters.Length; i++)
                        {
                            if (ParAF[i] == definition_of_editing_net.AFParameters[i])
                            {
                                k++;
                            }
                        }
                        if (k == definition_of_editing_net.AFParameters.Length)
                        {
                            isParametersAFSame = true;
                        }
                    }
                    if (String.Compare(TaskName, definition_of_editing_net.TaskName) != 0 ||
                        String.Compare(TopologyTypeName, definition_of_editing_net.TopologyName) != 0 ||
                        CountNeurons != definition_of_editing_net.NeuronsCount ||
                        isNeuronsInLayersSame == false || isParametersAFSame == false)
                    {
                        DialogResult res = MessageBox.Show("Данные изменения влекут удаление обученных весов сети. " +
                                                           "Нейронную сеть необходимо будет обучать заново.", "Предупреждение потери данных", MessageBoxButtons.OKCancel);

                        isNeedToDelete = true;
                        if (res == DialogResult.Cancel)
                        {
                            isContining = false;
                        }
                    }

                    if (isContining == true)
                    {
                        if (isNeedToDelete == true)
                        {
                            LoadingWindow loadingWindow = new LoadingWindow();

                            if (isNeuronsInLayersSame && (String.Compare(cbTopology.SelectedItem.ToString(), definition_of_editing_net.TopologyName) == 0))
                            {
                                loadingWindow.MakeLoading(
                                    () => dbHandler.DeleteWeightsMatrix(definition_of_editing_net.Name, loadingWindow),
                                    "Удаление устаревших весовых коэффициентов");
                            }
                            else
                            {
                                loadingWindow.MakeLoading(
                                    () => dbHandler.DeleteTopologyAndWeightsMatrix(definition_of_editing_net.Name, loadingWindow),
                                    "Удаление старой топологии нейронной сети");
                                loadingWindow.MakeLoading(
                                    () => dbHandler.InsertTopologyOfNeuroNet(definition_of_editing_net.Name, TopologyTypeName, CountNeurons, CountLayers, NeuInL, loadingWindow),
                                    "Запись измененной нейронной сети в БД");
                            }
                        }

                        dbHandler.UpdateNeuroNet(NameNN, definition_of_editing_net.Name, TopologyTypeName, TaskName, CountNeurons, CountLayers,
                                                 NeuronsInLayers, typeAF, ParametersAF);
                        Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }