Exemplo n.º 1
0
        private void start_button_Click(object sender, EventArgs e)
        {
            // Protection of the entered data
            if (liczba_neuronow_textBox.Text.Length == 0)
            {
                MessageBox.Show("Podaj liczbe neuronow!", "Blad", MessageBoxButtons.OK);
                return;
            }

            if (liczba_iteracji_textBox.Text.Length == 0)
            {
                MessageBox.Show("Podaj liczbe iteracji!", "Blad", MessageBoxButtons.OK);
                return;
            }

            // Preparation and launch of the algorithm
            int neuronCount = Convert.ToInt32(liczba_neuronow_textBox.Text);

            som = new SOM(neuronCount, shape, Int32.Parse(promien_sasiedztwa_textBox.Text));

            som.RandomNeurons();
            shape.Draw(som_pictureBox);

            List <Point> neurony = som.ReturnNeuronList();

            for (int i = 0; i < neurony.Count - 1; i++)
            {
                if (i == 0)
                {
                    DrawPoint(neurony[i].X, neurony[i].Y, Brushes.Black);
                }
                DrawEdge(neurony[i], neurony[i + 1]);
                DrawPoint(neurony[i + 1].X, neurony[i + 1].Y, Brushes.Black);
            }

            if (som == null)
            {
                MessageBox.Show("Som nie zostal prawidlowo zainicjowany", "Blad", MessageBoxButtons.OK);
                return;
            }
            else if (som.ReturnNeuronCount() == 0)
            {
                MessageBox.Show("Podaj poprawna liczbe neuronow", "Blad", MessageBoxButtons.OK);
                return;
            }
            else if (wspolczynnik_uczenia_textBox.Text.Length == 0)
            {
                MessageBox.Show("Podaj wspolczynnik uczenia", "Blad", MessageBoxButtons.OK);
                return;
            }

            som.IterationCount = Convert.ToInt32(liczba_iteracji_textBox.Text);
            som.LearningRate   = double.Parse(wspolczynnik_uczenia_textBox.Text, System.Globalization.CultureInfo.InvariantCulture);

            som_progressBar.Value = 0;
            som.StartSOM(DrawSOM, som_progressBar);
        }
Exemplo n.º 2
0
 private void button2_Click(object sender, EventArgs e)//Start SOM button
 {
     button3.Enabled = false;
     if (!int.TryParse(textBox1.Text, out SOM.maxIter))
     {
         SOM.maxIter = 100;
     }
     DataOperations.preProcess(SOM.fields, SOM.records);
     SOM.Start();
     button3.Enabled = true;
 }
Exemplo n.º 3
0
        public void printU_Matrix()
        {
            Graphics graphic = picGrid.CreateGraphics();
            int      i = 0, j = 0, mapI;
            double   min = double.MaxValue, max = double.MinValue;
            bool     flag = false;
            int      temp;

            PointF[] temp2;
            //create map for output node index -> hexagon index
            for (i = 0; i < SOM.outputR; i++)
            {
                temp = i * 2;
                mapI = temp;
                for (j = 0; j < SOM.outputC; j++)
                {
                    nodeMap[new Point(i, j)] = new Point(mapI, j * 2);
                    if (!flag)
                    {
                        mapI = temp + 1;
                        flag = true;
                    }
                    else
                    {
                        mapI = temp;
                        flag = false;
                    }
                }
            }
            Point p1Val, p2Val, p1Key, p2Key;
            Dictionary <Point, double> distMap = new Dictionary <Point, double>();
            int hexI, hexJ;

            //find all hexagons between output nodes(hexagons) and calculate distances
            foreach (KeyValuePair <Point, Point> node1 in nodeMap)
            {
                foreach (KeyValuePair <Point, Point> node2 in nodeMap)
                {
                    p1Val = node1.Value;
                    p2Val = node2.Value;
                    temp  = Math.Abs(p1Val.X - p2Val.X) + Math.Abs(p1Val.Y - p2Val.Y);
                    if (temp != 3 && temp != 2)
                    {
                        continue;
                    }
                    p1Key = node1.Key;
                    p2Key = node2.Key;
                    //find hexagon between p1Val-p2Val
                    if (p1Val.X < p2Val.X)
                    {
                        if (p1Val.Y < p2Val.Y)
                        {
                            hexI = p1Val.X;
                            hexJ = p1Val.Y + 1;
                        }
                        else if (p1Val.Y > p2Val.Y)
                        {
                            hexI = p1Val.X;
                            hexJ = p1Val.Y - 1;
                        }
                        else
                        {
                            hexI = p1Val.X + 1;
                            hexJ = p1Val.Y;
                        }
                        temp2 = HexToPoints(HexHeight, hexI, hexJ);
                        distMap[new Point(hexI, hexJ)] = SOM.EuclideanDist(SOM.outputs[p1Key.X][p1Key.Y].weights, SOM.outputs[p2Key.X][p2Key.Y].weights);
                        if (distMap[new Point(hexI, hexJ)] > max)
                        {
                            max = distMap[new Point(hexI, hexJ)];
                        }
                        if (distMap[new Point(hexI, hexJ)] < min)
                        {
                            min = distMap[new Point(hexI, hexJ)];
                        }
                    }
                }
            }
            int val;
            int rgbMax = 240, rgbMin = 15;
            Dictionary <Point, int> distColorMap = new Dictionary <Point, int>();

            //color all hexagons between outputs nodes(hexagons)
            foreach (KeyValuePair <Point, double> hex in distMap)
            {
                val = (int)Math.Round((hex.Value - min) / (max - min) * (rgbMax - rgbMin) + rgbMin);
                distColorMap[hex.Key] = val;
                temp2 = HexToPoints(HexHeight, hex.Key.X, hex.Key.Y);
                //val = rgbMax - val+rgbMin;
                using (SolidBrush brush = new SolidBrush(Color.FromArgb(val, val, val)))
                    graphic.FillPolygon(brush, temp2);
            }
            double temp3;
            int    count;

            //color all hexagons that are output nodes by calculating mean of adjacent ones
            foreach (KeyValuePair <Point, Point> node in nodeMap)
            {
                hexI  = node.Value.X;
                hexJ  = node.Value.Y;
                temp3 = 0;
                count = 0;
                if (distColorMap.ContainsKey(new Point(hexI - 1, hexJ)))
                {
                    temp3 += distColorMap[new Point(hexI - 1, hexJ)];
                    count++;
                }
                if (distColorMap.ContainsKey(new Point(hexI + 1, hexJ)))
                {
                    temp3 += distColorMap[new Point(hexI + 1, hexJ)];
                    count++;
                }
                if (distColorMap.ContainsKey(new Point(hexI - 1, hexJ - 1)))
                {
                    temp3 += distColorMap[new Point(hexI - 1, hexJ - 1)];
                    count++;
                }
                if (distColorMap.ContainsKey(new Point(hexI - 1, hexJ + 1)))
                {
                    temp3 += distColorMap[new Point(hexI - 1, hexJ + 1)];
                    count++;
                }
                if (distColorMap.ContainsKey(new Point(hexI, hexJ - 1)))
                {
                    temp3 += distColorMap[new Point(hexI, hexJ - 1)];
                    count++;
                }
                if (distColorMap.ContainsKey(new Point(hexI, hexJ + 1)))
                {
                    temp3 += distColorMap[new Point(hexI, hexJ + 1)];
                    count++;
                }
                val   = (int)Math.Round(temp3 / count);
                temp2 = HexToPoints(HexHeight, hexI, hexJ);
                //val = rgbMax - val+rgbMin;
                using (SolidBrush brush = new SolidBrush(Color.FromArgb(val, val, val)))
                    graphic.FillPolygon(brush, temp2);
            }
            //write record counts to output nodes(hexagons)
            foreach (KeyValuePair <Point, Point> node in nodeMap)
            {
                using (Font myFont = new Font("Arial", 8, FontStyle.Bold))
                {
                    graphic.DrawString(SOM.outputs[node.Key.X][node.Key.Y].records.Count.ToString(), myFont, Brushes.Blue, HexToPoints(HexHeight, node.Value.X, node.Value.Y)[1]);
                }
            }
        }