Advertisement
hishlishter

Untitled

Mar 6th, 2025
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.53 KB | Source Code | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4.  
  5. namespace NeuralNetworkPrediction
  6. {
  7.     public class MainPanel : Panel
  8.     {
  9.         // Константы
  10.         private const int N = 300;
  11.         private const double TAU = 0.01;
  12.         private const double TAU_STEP = 0.01;
  13.         private const double D_H = 0.05;
  14.         private const double ALPHA = 0.01;
  15.         private const int R = 10;
  16.         private const double START_X = -10.0;
  17.         private const double END_X = 5.0;
  18.  
  19.         private const double A0 = 4.5;
  20.         private const double A1 = 0.6;
  21.         private const double A2 = -23.2;
  22.         private const double A3 = 108.6;
  23.         private const double A4 = -147.7;
  24.         private const double A5 = 38;
  25.  
  26.         // Переменные нейросети
  27.         private double c_x;
  28.         private double[] u_t;
  29.         private double[] u;
  30.         private double[] u_n;
  31.         private double[] g;
  32.         private int n_t;
  33.         private int step;
  34.         private int err;
  35.         private double learningPercentage;
  36.  
  37.         // Графические параметры
  38.         private double graphW, graphH, graphDx, graphBx, graphRx;
  39.  
  40.         private Timer timer;
  41.  
  42.         public MainPanel()
  43.         {
  44.             // Инициализация переменных
  45.             c_x = START_X;
  46.             u_t = new double[N];
  47.             u = new double[N];
  48.             u_n = new double[N];
  49.             g = new double[R];
  50.  
  51.             for (int i = 0; i < N; i++)
  52.             {
  53.                 u[i] = 0.0;
  54.                 u_t[i] = 0.0;
  55.                 u_n[i] = 0.0;
  56.             }
  57.             for (int i = 0; i < R; i++)
  58.             {
  59.                 g[i] = 0.0;
  60.             }
  61.             n_t = N / 2 + 1;
  62.             step = 1;
  63.             err = 0;
  64.             learningPercentage = 0.0;
  65.  
  66.             // Настройка панели
  67.             DoubleBuffered = true;
  68.  
  69.             // Запуск таймера
  70.             timer = new Timer();
  71.             timer.Interval = 1;
  72.             timer.Tick += Timer_Tick;
  73.             timer.Start();
  74.         }
  75.  
  76.         private double Sign(double x)
  77.         {
  78.             if (x > 0) return 1.0;
  79.             else if (x < 0) return -1.0;
  80.             return 0.0;
  81.         }
  82.  
  83.         private double F(double x)
  84.         {
  85.             return A0 * Math.Pow(x, 5) + A1 * Math.Pow(x, 4) + A2 * Math.Pow(x, 3)
  86.                 + A3 * Math.Pow(x, 2) + A4 * x + A5;
  87.         }
  88.  
  89.         private void Teach(double[] u, double[] g, int n_t, double delta)
  90.         {
  91.             double[] delta_g = new double[R];
  92.             for (int r = 0; r < R; r++)
  93.             {
  94.                 double x_r = u[n_t - 1 - r];
  95.                 delta_g[r] = ALPHA * Sign(x_r) * Sign(delta);
  96.             }
  97.             for (int r = 0; r < R; r++)
  98.             {
  99.                 g[r] += delta_g[r];
  100.             }
  101.             err++;
  102.         }
  103.  
  104.         private void CalculateLearningPercentage()
  105.         {
  106.             double totalError = 0.0;
  107.             double minVal = u[0], maxVal = u[0];
  108.             for (int i = 0; i < N; i++)
  109.             {
  110.                 totalError += Math.Abs(u_n[i] - u_t[i]);
  111.                 if (u[i] < minVal) minVal = u[i];
  112.                 if (u[i] > maxVal) maxVal = u[i];
  113.             }
  114.             double avgError = totalError / N;
  115.             double range = maxVal - minVal;
  116.             if (range < 1e-9) range = 1.0;
  117.             learningPercentage = 100 * (1 - avgError / range);
  118.             if (learningPercentage < 0) learningPercentage = 0;
  119.             if (learningPercentage > 100) learningPercentage = 100;
  120.         }
  121.  
  122.         private void NewStep()
  123.         {
  124.             for (int i = 0; i < N; i++)
  125.             {
  126.                 u_n[i] = F(c_x + i * TAU);
  127.             }
  128.             c_x += TAU_STEP;
  129.  
  130.             if (c_x > END_X)
  131.             {
  132.                 c_x = START_X;
  133.             }
  134.  
  135.             for (int i = 0; i < N; i++)
  136.             {
  137.                 u_t[i] = 0.0;
  138.             }
  139.             for (int i = R; i < N; i++)
  140.             {
  141.                 for (int r = 0; r < R; r++)
  142.                 {
  143.                     u_t[i] += g[r] * u[i - 1 - r];
  144.                 }
  145.             }
  146.             for (int i = 0; i < R; i++)
  147.             {
  148.                 u_t[i] = u[i];
  149.             }
  150.  
  151.             double d_u = u_n[n_t] - u_t[n_t];
  152.             if (d_u < -D_H || d_u > D_H)
  153.             {
  154.                 Teach(u, g, n_t, d_u);
  155.             }
  156.             step++;
  157.         }
  158.  
  159.         private void UpdateVars()
  160.         {
  161.             NewStep();
  162.             for (int i = 0; i < N; i++)
  163.             {
  164.                 u[i] = u_n[i];
  165.             }
  166.             CalculateLearningPercentage();
  167.         }
  168.  
  169.         protected override void OnPaint(PaintEventArgs e)
  170.         {
  171.             base.OnPaint(e);
  172.             Graphics g2 = e.Graphics;
  173.  
  174.             // Инициализация графических параметров
  175.             graphW = Width;
  176.             graphH = Height;
  177.             graphDx = graphW / N;
  178.             graphBx = (graphW - graphDx * (N - 1)) / 2.0;
  179.             graphRx = graphBx + (N - 1) * graphDx;
  180.  
  181.             // Масштабирование
  182.             double minVal = u[0], maxVal = u[0];
  183.             for (int i = 1; i < N; i++)
  184.             {
  185.                 if (u[i] < minVal) minVal = u[i];
  186.                 if (u[i] > maxVal) maxVal = u[i];
  187.             }
  188.             double mid = (maxVal + minVal) / 2.0;
  189.             double diff = maxVal - minVal;
  190.             minVal = mid - diff * 1.5;
  191.             maxVal = mid + diff * 1.5;
  192.             if (Math.Abs(maxVal - minVal) < 1e-9)
  193.             {
  194.                 minVal -= 1.0;
  195.                 maxVal += 1.0;
  196.             }
  197.             double localMtt = graphH / (maxVal - minVal);
  198.             double y_c = localMtt * maxVal;
  199.  
  200.             // Рисуем оси
  201.             using (Pen pen = new Pen(Color.Black))
  202.             {
  203.                 g2.DrawLine(pen, (float)graphBx, 0, (float)graphBx, (float)graphH);
  204.                 g2.DrawLine(pen, (float)graphRx, 0, (float)graphRx, (float)graphH);
  205.                 g2.DrawLine(pen, (float)graphBx, 0, (float)graphRx, 0);
  206.                 g2.DrawLine(pen, (float)graphBx, (float)graphH, (float)graphRx, (float)graphH);
  207.                 g2.DrawLine(pen, (float)graphBx, (float)y_c, (float)graphRx, (float)y_c);
  208.  
  209.                 for (int i = 1; i < N - 1; i++)
  210.                 {
  211.                     float x = (float)(graphBx + i * graphDx);
  212.                     g2.DrawLine(pen, x, (float)(y_c - 3), x, (float)(y_c + 3));
  213.                 }
  214.                 float x_nt = (float)(graphBx + (n_t - 1) * graphDx);
  215.                 g2.DrawLine(pen, x_nt, 0, x_nt, (float)graphH);
  216.             }
  217.  
  218.             // Рисуем кривую u
  219.             using (Pen greenPen = new Pen(Color.Green))
  220.             {
  221.                 float oldX = (float)graphBx;
  222.                 float oldY = (float)(y_c - localMtt * u[0]);
  223.                 for (int i = 1; i < N; i++)
  224.                 {
  225.                     float x = (float)(graphBx + i * graphDx);
  226.                     float y = (float)(y_c - localMtt * u[i]);
  227.                     g2.DrawLine(greenPen, oldX, oldY, x, y);
  228.                     oldX = x;
  229.                     oldY = y;
  230.                 }
  231.             }
  232.  
  233.             // Рисуем прогноз u_t
  234.             using (Pen redPen = new Pen(Color.Red))
  235.             {
  236.                 float oldX = (float)graphBx;
  237.                 float oldY = (float)(y_c - localMtt * u_t[0]);
  238.                 for (int i = 1; i < N; i++)
  239.                 {
  240.                     float x = (float)(graphBx + i * graphDx);
  241.                     float y = (float)(y_c - localMtt * u_t[i]);
  242.                     g2.DrawLine(redPen, oldX, oldY, x, y);
  243.                     oldX = x;
  244.                     oldY = y;
  245.                 }
  246.             }
  247.  
  248.             // Рисуем текст
  249.             using (Font font = new Font("Arial", 10))
  250.             using (Brush brush = new SolidBrush(Color.Black))
  251.             {
  252.                 float textX = 20;
  253.                 float textY = 20;
  254.                 g2.DrawString($"Step = {step}", font, brush, textX, textY); textY += 16;
  255.                 g2.DrawString($"dH = {D_H:F2}", font, brush, textX, textY); textY += 16;
  256.                 g2.DrawString($"dG = {ALPHA:F3}", font, brush, textX, textY); textY += 16;
  257.                 for (int i = 0; i < R; i++)
  258.                 {
  259.                     g2.DrawString($"G[{i}] = {g[i]:F3}", font, brush, textX, textY);
  260.                     textY += 16;
  261.                 }
  262.                 g2.DrawString($"Learning: {learningPercentage:F2}%", font, brush, textX, textY);
  263.                 textY += 16;
  264.                 g2.DrawString($"c_x = {c_x:F3}", font, brush, textX, textY);
  265.             }
  266.         }
  267.  
  268.         protected override Size DefaultSize => new Size(800, 600);
  269.  
  270.         private void Timer_Tick(object sender, EventArgs e)
  271.         {
  272.             UpdateVars();
  273.             Invalidate();
  274.         }
  275.  
  276.         [STAThread]
  277.         static void Main()
  278.         {
  279.             Application.EnableVisualStyles();
  280.             Application.SetCompatibleTextRenderingDefault(false);
  281.             Form form = new Form
  282.             {
  283.                 Text = "Прогнозирование функции (нейросеть)",
  284.                 ClientSize = new Size(800, 600)
  285.             };
  286.             MainPanel panel = new MainPanel();
  287.             form.Controls.Add(panel);
  288.             panel.Dock = DockStyle.Fill;
  289.             form.ShowDialog();
  290.         }
  291.     }
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement