Advertisement
hishlishter

Untitled

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