mamamaria

NN

Oct 2nd, 2021 (edited)
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 KB | None | 0 0
  1. // TravellingSalesman.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. #include <ctime>
  7. #include <random>
  8.  
  9. const int N = 10;
  10. const double A = 1;
  11. const double B = 1;
  12. const double C = 1;
  13. const double D = 0.1;
  14.  
  15. using namespace std;
  16. default_random_engine eng;
  17.  
  18. using Graph = vector<vector<double>>;
  19. using NN = vector<double>;//сеть с нейронами, где нейрон - это double
  20. using Weights = vector<vector<double>>;
  21. using Ways = vector<double>;
  22.  
  23. void GraphPrinter(Graph graph) {
  24.     for (int i = 0; i < graph.size(); i++) {
  25.         for (int j = 0; j < graph.size(); j++)
  26.             cout << graph[i][j] << " ";
  27.         cout << endl;
  28.     }
  29. }
  30. void WaysPrinter(Ways ways) {
  31.     for (int i = 0; i < ways.size(); i++)
  32.         cout << ways[i] << " ";
  33.     cout << endl;
  34. }
  35.  
  36. void NNPrinter(NN nn) {
  37.     for (int i = 0; i < N; i++) {
  38.         for (int j = 0; j < N; j++)
  39.             cout << nn[i*N+j] << " ";
  40.         cout << endl;
  41.     }
  42. }
  43. Graph WaysGenerator() {
  44.     Graph graph(N);
  45.     for (int i = 0; i < graph.size(); i++) {
  46.         graph[i].resize(N);
  47.     }
  48.     uniform_real_distribution<double> distr(1, 10);
  49.     for (int i = 0; i < graph.size(); i++) {
  50.         for (int j = 0; j < i; j++) {
  51.             double dist = distr(eng);
  52.             graph[i][j] = dist;
  53.             graph[j][i] = graph[i][j];
  54.         }
  55.     }
  56.     return graph;
  57. }
  58.  
  59. Weights WeightsCalculator(Graph graph) {
  60.     Weights weights(N * N);
  61.     for (int i = 0; i < weights.size(); i++) {
  62.         weights[i].resize(N * N);
  63.     }
  64.     for (int index = 0; index < N * N; index++) {
  65.         int x = index / N;
  66.         int i = index % N;
  67.         for (int endex = 0; endex < N * N; endex++) {
  68.             int y = endex / N;
  69.             int j = endex % N;
  70.             double w = 0;
  71.             if (x == y && i != j)
  72.                 w -= A;
  73.             if (x != y && i == j)
  74.                 w -= B;
  75.             w -= C;
  76.             if (j == i + 1 || j == i - 1)
  77.                 w -= (D * graph[x][y]);
  78.             if (index == endex) w = 0;
  79.             weights[index][endex] = w;
  80.         }
  81.     }
  82.     return weights;
  83. }
  84.  
  85. void NeuronUpdater(NN& nn, Weights weights) {
  86.     int j = rand() % (N * N);
  87.     double NET = 0;
  88.     for (int i = 0; i < nn.size(); i++)
  89.         NET += weights[i][j] * nn[i];
  90.     double w_0_j = C * (N - 0.5);
  91.     NET += w_0_j;
  92.  
  93.     if (NET > 0)
  94.         nn[j] = 1;
  95.     if (NET < 0)
  96.         nn[j] = 0;
  97.  
  98. }
  99.  
  100. double EnegryChecker(NN nn, Weights weights) {
  101.     double E = 0;
  102.     double w_0_j = C * (N - 0.5);
  103.     for (int i = 0; i < nn.size(); i++)
  104.         for (int j = 0; j < nn.size(); j++)
  105.             E += weights[i][j] * nn[i] * nn[j];
  106.     E *= -0.5;
  107.  
  108.     for (int j = 0; j < nn.size(); j++)
  109.         E -= (w_0_j * nn[j]);
  110.     return E;
  111. }
  112.  
  113. bool OkayNNChecker(NN nn, Ways &ways) {
  114.     ways.resize(N);
  115.     int counter1 = 0, counter2 = 0;
  116.     for (int i = 0; i < N; i++) {
  117.         int counter1 = 0;
  118.         for (int j = 0; j < N; j++) {
  119.             int index = i * N + j;
  120.             if (nn[index] == 1) {
  121.                 counter1++;
  122.                 ways[j]=i;
  123.             }
  124.         }
  125.         counter2 = 0;
  126.         for (int k = 0; k < N; k++) {
  127.             if (nn[i + k * N] == 1)
  128.                 counter2++;
  129.         }
  130.         if (counter1 != 1 || counter2 != 1) return false;
  131.     }
  132.     return true;
  133. }
  134. void FillingTheNN(NN &nn) {
  135.     for (int i = 0; i < N; i++)
  136.         nn[i] = rand() % 2;
  137. }
  138. //нужно сделать чтобы в каждом городе ровно 1 единица матрицы nn и в кадждый момент времени был только один город
  139. // найти длину пути
  140. int main()
  141. {
  142.     Graph graph = WaysGenerator();
  143.     GraphPrinter(graph);
  144.     cout << endl;
  145.     Weights weights = WeightsCalculator(graph);
  146.     NN nn(N * N);
  147.     FillingTheNN(nn);
  148.     Ways ways(N, 0);
  149.  
  150.     for (int i = 0; i < 1000; i++) {
  151.         NeuronUpdater(nn, weights);
  152.         double E = EnegryChecker(nn, weights);
  153.        
  154.         if (i % 100 == 0) {
  155.             cout << "Count 1 in NN: " << OkayNNChecker(nn, ways) << endl;
  156.             WaysPrinter(ways);
  157.             ways.clear();
  158.             cout << endl;
  159.             NNPrinter(nn);
  160.             cout << endl;
  161.             cout << "E: " << E << endl;
  162.         }
  163.        
  164.     }
  165. }
  166.  
  167.  
Add Comment
Please, Sign In to add comment