Advertisement
ayurchyk1998

NN_lab1

Mar 25th, 2021
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.38 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Windows.Forms;
  4.  
  5. namespace NN_Lab1_SLP
  6. {
  7.     public partial class Form1 : Form
  8.     {
  9.         // variant 13
  10.         private double a = 4;
  11.         private double B = 2;
  12.         private double d = 0.5;
  13.         private static int p = 3;                   // number of input units
  14.         private static int m = 100;
  15.         private static int test_points = 20;
  16.         private double h = 0.1;                     // how much x increase
  17.         private double lr, deserror;                // the learning rate (alpha) and desired error
  18.         private int L = m - p;                      // number of training samples
  19.  
  20.         double[] x = new double[m + test_points];
  21.         double[] y = new double[m + test_points];   // desired points coordinates
  22.         double[] yf = new double[m + test_points];  //real
  23.         private double miny, maxy;
  24.         private double[] y_oblicz = new double[m + test_points];
  25.  
  26.         Random rand = new Random();
  27.         private double[] w = new double[p];
  28.         private double[] w_oblicz = new double[p];
  29.         private double t, t_oblicz;
  30.         private double[] sqerror = new double[10000];   // depends on slp how much time it will learn
  31.  
  32.         public Form1()
  33.         {
  34.             InitializeComponent();
  35.  
  36.             GiveGraphic();
  37.             for (int i = 0; i < w.Length; i++)
  38.             {
  39.                 w[i] = Getnum();
  40.             }
  41.             t = Getnum();
  42.  
  43.             tbrw1.Text = w[0].ToString();
  44.             tbrw2.Text = w[1].ToString();
  45.             tbrw3.Text = w[2].ToString();
  46.             tbrt.Text = t.ToString();
  47.  
  48.         }
  49.  
  50.         public double Getnum()
  51.         {
  52.             int a = rand.Next(0, 2);
  53.             if (a == 1)
  54.             {
  55.                 return Math.Round(rand.NextDouble(), 5);        // from 0 to 1
  56.             }
  57.             else
  58.             {
  59.                 return Math.Round((rand.NextDouble() * -1), 5); // from 0 to 1
  60.             }
  61.         }
  62.  
  63.         private void bLearn_Click(object sender, EventArgs e)
  64.         {
  65.             //learning
  66.             listBox2.Items.Clear();
  67.             if(tblearnrate.Text != null)
  68.             {
  69.                 lr = Convert.ToDouble(tblearnrate.Text);
  70.             }
  71.             if (tbdeserror.Text != null)
  72.             {
  73.                 deserror = Convert.ToDouble(tbdeserror.Text);
  74.             }
  75.  
  76.             this.chart1.Series[2].Points.Clear();
  77.             this.chart1.Series[3].Points.Clear();
  78.  
  79.             bool flag = false;
  80.             int qty = 0;
  81.  
  82.  
  83.             for (int i = p; (i < m) && (flag != true); i++)
  84.             {
  85.                 int k = 0;
  86.                 for (int j = (i - p); j < i; j++)
  87.                 {
  88.                     y_oblicz[i] = y_oblicz[i] * y[j] * w[k];
  89.                     k++;
  90.                 }
  91.                 y_oblicz[i] += -t;
  92.                 //y_oblicz[i] -= t;
  93.                 k = 0;
  94.                 for (int j = (i - p); j < i; j++)
  95.                 {
  96.                     w[k] = w[k] - lr * y[j] * (y_oblicz[i] - y[i]);
  97.                     w_oblicz[k] = w[k];
  98.                     k++;
  99.                 }
  100.  
  101.                 t = t + lr * (y_oblicz[i] - y[i]);
  102.                 t_oblicz = t;
  103.                 sqerror[i-p] = (Math.Pow(y_oblicz[i] - y[i], 2))/2;
  104.  
  105.                 y_oblicz[i] = Math.Round(y_oblicz[i], 3);
  106.                 t_oblicz = Math.Round(t_oblicz, 3);
  107.                 sqerror[i-p] = Math.Round(sqerror[i-p], 4);
  108.                 w[0] = Math.Round(w[0], 2);
  109.                 w[1] = Math.Round(w[1], 2);
  110.                 w[2] = Math.Round(w[2], 2);
  111.  
  112.                 string txt = "X[" + (i-p).ToString() + "] = " + x[i-p].ToString() + ", Y[" + (i-p).ToString() + "]= " + y_oblicz[i-p].ToString() + ", T = " +
  113.                     t_oblicz.ToString() + ", Es = " +sqerror[i-p].ToString() + ", W1 = " + w[0] +
  114.                     ", W2 = " + w[1] + ", W3 = " + w[2];
  115.                 listBox2.Items.Add(txt);
  116.  
  117.                 if (sqerror[i - p] <= deserror)
  118.                 {
  119.                     flag = true;
  120.                 }
  121.  
  122.                 qty++;
  123.             }
  124.             listBox2.Items.RemoveAt(listBox2.Items.Count - 1);
  125.  
  126.             for (int i = 0; i < p; i++)
  127.             {
  128.                 this.chart1.Series[2].Points.AddXY(x[i], y[i]);
  129.             }
  130.  
  131.             for (int i = p; i < (qty+1); i++)
  132.             {
  133.                 this.chart1.Series[2].Points.AddXY(x[i], y_oblicz[i]);
  134.             }
  135.  
  136.             //testing
  137.             for(int i =(qty-1); i <= (qty + test_points); i++)
  138.             {
  139.                 int k = 0;
  140.                 for (int j = (i-p); j < i; j++)
  141.                 {
  142.                     y_oblicz[i] += y[i] * w_oblicz[k];
  143.                     k++;
  144.                 }
  145.                 y_oblicz[i] += -t_oblicz;
  146.  
  147.                 string txttest = "X[" + i.ToString() + "] = " + x[i].ToString() + ", Y[" + i.ToString() + "] = " + y_oblicz[i].ToString();
  148.                 listBox3.Items.Add(txttest);
  149.             }
  150.  
  151.             for (int i = (qty-1); i <= (qty + test_points); i++)
  152.             {
  153.                 this.chart1.Series[3].Points.AddXY(x[i], y_oblicz[i]);
  154.             }
  155.  
  156.         }
  157.  
  158.         public void GiveGraphic()
  159.         {
  160.             this.chart1.Series[0].Points.Clear();
  161.             this.chart1.Series[1].Points.Clear();
  162.             for (int i = 0; i < (m + test_points); i++)
  163.             {
  164.                 if (i != 0)
  165.                 {
  166.                     x[i] = x[i - 1] + h;
  167.                 }
  168.                 else
  169.                 {
  170.                     x[i] = 0;
  171.                 }
  172.  
  173.                 y[i] = a * Math.Sin(B * x[i]) + d;
  174.                 this.chart1.Series[0].Points.AddXY(x[i], y[i]);
  175.                 x[i] = Math.Round(x[i], 3);
  176.                 y[i] = Math.Round(y[i], 3);
  177.                 string txt = "X[" + i.ToString() + "] = " + x[i].ToString() + ", Y[" + i.ToString() + "] = " + y[i].ToString();
  178.                 listBox1.Items.Add(txt);
  179.             }
  180.  
  181.             miny = y.Min();
  182.             maxy = y.Max();
  183.             for (int i = 0; i < (m + test_points); i++)
  184.             {
  185.                 yf[i] = ((y[i] - miny) * (1 - 0)) / (maxy - miny) + 0; // a = -0.6, b = 0.5
  186.                 this.chart1.Series[1].Points.AddXY(x[i], yf[i]);
  187.  
  188.                 if (i < p)
  189.                 {
  190.                     y_oblicz[i] = yf[i];
  191.                 }
  192.             }
  193.         }
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement