Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TravellingSalesman.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
- //
- #include <iostream>
- #include <vector>
- #include <ctime>
- #include <random>
- const int N = 10;
- const double A = 1;
- const double B = 1;
- const double C = 1;
- const double D = 0.1;
- using namespace std;
- default_random_engine eng;
- using Graph = vector<vector<double>>;
- using NN = vector<double>;//сеть с нейронами, где нейрон - это double
- using Weights = vector<vector<double>>;
- using Ways = vector<double>;
- void GraphPrinter(Graph graph) {
- for (int i = 0; i < graph.size(); i++) {
- for (int j = 0; j < graph.size(); j++)
- cout << graph[i][j] << " ";
- cout << endl;
- }
- }
- void WaysPrinter(Ways ways) {
- for (int i = 0; i < ways.size(); i++)
- cout << ways[i] << " ";
- cout << endl;
- }
- void NNPrinter(NN nn) {
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < N; j++)
- cout << nn[i*N+j] << " ";
- cout << endl;
- }
- }
- Graph WaysGenerator() {
- Graph graph(N);
- for (int i = 0; i < graph.size(); i++) {
- graph[i].resize(N);
- }
- uniform_real_distribution<double> distr(1, 10);
- for (int i = 0; i < graph.size(); i++) {
- for (int j = 0; j < i; j++) {
- double dist = distr(eng);
- graph[i][j] = dist;
- graph[j][i] = graph[i][j];
- }
- }
- return graph;
- }
- Weights WeightsCalculator(Graph graph) {
- Weights weights(N * N);
- for (int i = 0; i < weights.size(); i++) {
- weights[i].resize(N * N);
- }
- for (int index = 0; index < N * N; index++) {
- int x = index / N;
- int i = index % N;
- for (int endex = 0; endex < N * N; endex++) {
- int y = endex / N;
- int j = endex % N;
- double w = 0;
- if (x == y && i != j)
- w -= A;
- if (x != y && i == j)
- w -= B;
- w -= C;
- if (j == i + 1 || j == i - 1)
- w -= (D * graph[x][y]);
- if (index == endex) w = 0;
- weights[index][endex] = w;
- }
- }
- return weights;
- }
- void NeuronUpdater(NN& nn, Weights weights) {
- int j = rand() % (N * N);
- double NET = 0;
- for (int i = 0; i < nn.size(); i++)
- NET += weights[i][j] * nn[i];
- double w_0_j = C * (N - 0.5);
- NET += w_0_j;
- if (NET > 0)
- nn[j] = 1;
- if (NET < 0)
- nn[j] = 0;
- }
- double EnegryChecker(NN nn, Weights weights) {
- double E = 0;
- double w_0_j = C * (N - 0.5);
- for (int i = 0; i < nn.size(); i++)
- for (int j = 0; j < nn.size(); j++)
- E += weights[i][j] * nn[i] * nn[j];
- E *= -0.5;
- for (int j = 0; j < nn.size(); j++)
- E -= (w_0_j * nn[j]);
- return E;
- }
- bool OkayNNChecker(NN nn, Ways &ways) {
- ways.resize(N);
- int counter1 = 0, counter2 = 0;
- for (int i = 0; i < N; i++) {
- int counter1 = 0;
- for (int j = 0; j < N; j++) {
- int index = i * N + j;
- if (nn[index] == 1) {
- counter1++;
- ways[j]=i;
- }
- }
- counter2 = 0;
- for (int k = 0; k < N; k++) {
- if (nn[i + k * N] == 1)
- counter2++;
- }
- if (counter1 != 1 || counter2 != 1) return false;
- }
- return true;
- }
- void FillingTheNN(NN &nn) {
- for (int i = 0; i < N; i++)
- nn[i] = rand() % 2;
- }
- //нужно сделать чтобы в каждом городе ровно 1 единица матрицы nn и в кадждый момент времени был только один город
- // найти длину пути
- int main()
- {
- Graph graph = WaysGenerator();
- GraphPrinter(graph);
- cout << endl;
- Weights weights = WeightsCalculator(graph);
- NN nn(N * N);
- FillingTheNN(nn);
- Ways ways(N, 0);
- for (int i = 0; i < 1000; i++) {
- NeuronUpdater(nn, weights);
- double E = EnegryChecker(nn, weights);
- if (i % 100 == 0) {
- cout << "Count 1 in NN: " << OkayNNChecker(nn, ways) << endl;
- WaysPrinter(ways);
- ways.clear();
- cout << endl;
- NNPrinter(nn);
- cout << endl;
- cout << "E: " << E << endl;
- }
- }
- }
Add Comment
Please, Sign In to add comment