The code does not work. It is necessary to read the data from the file (a matrix of 256x256) and then apply the ant algorithm (after reading, you need to specify the base for the algorithm, and it breaks on it). If you read a 10x10 matrix, then everything is ok. The matrix looks like this (3x3): 1 5 3 1 8 4 9 8 2 Please tell me why it does not work ...

/* ΠœΡƒΡ€Π°Π²ΡŒΠΈΠ½Ρ‹ΠΉ Π°Π»Π³ΠΎΡ€ΠΈΡ‚ΠΌ для Ρ€Π΅ΡˆΠ΅Π½ΠΈΡ Π·Π°Π΄Π°Ρ‡ΠΈ коммивояТёра */ #include <fstream> #include <locale> #include <stdlib.h> #include <iostream> #include <malloc.h> #include <cmath> using namespace std; #define N_MIN 3 // минимальноС количСство Π²Π΅Ρ€ΡˆΠΈΠ½ #define N_MAX 30 // максимальноС количСство Π²Π΅Ρ€ΡˆΠΈΠ½ #define ALPHA 1 // вСс Ρ„Π΅Ρ€ΠΌΠ΅Π½Ρ‚Π° #define BETTA 3 // коэффициСнт эвристики #define T_MAX 100 // врСмя ΠΆΠΈΠ·Π½ΠΈ ΠΊΠΎΠ»ΠΎΠ½ΠΈΠΈ #define M 200 // количСство ΠΌΡƒΡ€Π°Π²ΡŒΠ΅Π² Π² ΠΊΠΎΠ»ΠΎΠ½ΠΈΠΈ #define Q 100 // количСство #define RHO 0.5 // коэффициСнт испарСния Ρ„Π΅Ρ€ΠΎΠΌΠΎΠ½Π° // структура ПУВЬ (Π΄Π»ΠΈΠ½Π½Π°, массив Π²Π΅Ρ€ΡˆΠΈΠ½, количСство Π²Π΅Ρ€ΡˆΠΈΠ½) struct WAY_TYPE { int itabu; int length; int *tabu; }; // Π²Π΅Ρ€ΠΎΡΡ‚Π½ΠΎΡΡ‚ΡŒ ΠΏΠ΅Ρ€Π΅Ρ…ΠΎΠ΄Π° ΠΌΡƒΡ€Π°Π²ΡŒΡ ant Π² Π²Π΅Ρ€ΡˆΠΈΠ½Ρƒ to double probability (int to, WAY_TYPE ant, double **pheromone, double **distance, int vertex) { // Ссли Π²Π΅Ρ€ΡˆΠΈΠ½Π° ΡƒΠΆΠ΅ посСщСна, Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ 0 for (int i=0; i<ant.itabu; ++i) if (to == ant.tabu[i]) return 0; double sum = 0.0; int from = ant.tabu[ant.itabu-1]; // считаСм сумму Π² Π·Π½Π°ΠΌΠΈΠ½Π°Ρ‚Π΅Π»Π΅ for (int j=0; j<vertex; ++j) { int flag = 1; // провСряСм, посСщал Π»ΠΈ ΠΌΡƒΡ€Π°Π²Π΅ΠΉ j Π²Π΅Ρ€ΡˆΠΈΠ½Ρƒ for (int i=0; i<ant.itabu; ++i) if (j == ant.tabu[i]) flag = 0; // Ссли Π½Π΅Ρ‚, Ρ‚ΠΎΠ³Π΄Π° прибавляСм ΠΊ ΠΎΠ±Ρ‰Π΅ΠΉ суммС if (flag) sum += pow (pheromone[from][j], ALPHA) * pow (distance[from][j], BETTA); } // Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ вСроятности return pow (pheromone[from][to], ALPHA) * pow (distance[from][to], BETTA) / sum; } // основная функция Π°Π»Π³ΠΎΡ€ΠΈΡ‚ΠΌΠ° поиска WAY_TYPE AntColonyOptimization (double **distance0, int vertex, int start) { // инициализация Π΄Π°Π½Π½Ρ‹Ρ… ΠΎ Π»ΡƒΡ‡ΡˆΠ΅ΠΌ ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚Π΅ WAY_TYPE way; way.itabu = 0; way.length = -1; way.tabu = (int *) malloc (sizeof (int) * vertex); // инициализация Π΄Π°Π½Π½Ρ‹Ρ… ΠΎ расстоянии ΠΈ количСствС Ρ„Π΅Ρ€ΠΎΠΌΠΎΠ½Π° double **distance = NULL, **pheromone = NULL; distance = (double **) malloc (sizeof (double *) * vertex); pheromone = (double **) malloc (sizeof (double *) * vertex); for (int i=0; i<vertex; ++i) { distance[i] = (double *) malloc (sizeof (double) * vertex); pheromone[i] = (double *) malloc (sizeof (double) * vertex); for (int j=0; j<vertex; ++j) { pheromone[i][j] = 1.0 / vertex; if (i != j) distance[i][j] = 1.0 / distance0[i][j]; } } // инициализация ΠΌΡƒΡ€Π°Π²ΡŒΠ΅Π² WAY_TYPE ants[M]; for (int k=0; k<M; ++k) { ants[k].itabu = 0; ants[k].length = 0.0; ants[k].tabu = (int *) malloc (sizeof (int) * vertex); ants[k].tabu[ants[k].itabu++] = start; } // основной Ρ†ΠΈΠΊΠ» for (int t=0; t<T_MAX; ++t) { // Ρ†ΠΈΠΊΠ» ΠΏΠΎ ΠΌΡƒΡ€Π°Π²ΡŒΡΠΌ for (int k=0; k<M; ++k) { // поиск ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚Π° для k-Π³ΠΎ ΠΌΡƒΡ€Π°Π²ΡŒΡ do { int j_max = -1; double p_max = 0.0; for (int j=0; j<vertex; ++j) { // ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° вСроятности ΠΏΠ΅Ρ€Π΅Ρ…ΠΎΠ΄Π° Π² Π²Π΅Ρ€ΡˆΠΈΠ½Ρƒ j if (ants[k].tabu[ants[k].itabu-1] != j) { double p = probability (j, ants[k], pheromone, distance, vertex); if (p && p >= p_max) { p_max = p; j_max = j; } } } ants[k].length += distance0[ants[k].tabu[ants[k].itabu-1]][j_max]; ants[k].tabu[ants[k].itabu++] = j_max; } while (ants[k].itabu < vertex ); ants[k].length += distance0[ants[k].tabu[ants[k].itabu-1]][start]; ants[k].tabu[ants[k].itabu++] = start; // оставляСм Ρ„Π΅Ρ€ΠΎΠΌΠΎΠ½ Π½Π° ΠΏΡƒΡ‚ΠΈ ΠΌΡƒΡ€Π°Π²ΡŒΡ for (int i=0; i<ants[k].itabu-1; ++i) { int from = ants[k].tabu[i % ants[k].itabu]; int to = ants[k].tabu[(i+1) % ants[k].itabu]; pheromone[from][to] += Q / ants[k].length; pheromone[to][from] = pheromone[from][to]; } // ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° Π½Π° Π»ΡƒΡ‡ΡˆΠ΅Π΅ Ρ€Π΅ΡˆΠ΅Π½ΠΈΠ΅ if (ants[k].length < way.length || way.length < 0) { way.itabu = ants[k].itabu; way.length = ants[k].length; for (int i=0; i<way.itabu; ++i) way.tabu[i] = ants[k].tabu[i]; } // ΠΎΠ±Π½ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΌΡƒΡ€Π°Π²ΡŒΠ΅Π² ants[k].itabu = 1; ants[k].length = 0.0; } // Ρ†ΠΈΠΊΠ» ΠΏΠΎ Ρ€Π΅Π±Ρ€Π°ΠΌ for (int i=0; i<vertex; ++i) for (int j=0; j<vertex; ++j) // ΠΎΠ±Π½ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ Ρ„Π΅Ρ€ΠΎΠΌΠΎΠ½Π° для Ρ€Π΅Π±Ρ€Π° (i, j) if (i != j) pheromone[i][j] *= (1 - RHO); } // Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ ΠΊΡ€Π°Ρ‚Ρ‡Π°ΠΉΡˆΠΈΠΉ ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚ return way; } // Ρ‚ΠΎΡ‡ΠΊΠ° Π²Ρ…ΠΎΠ΄Π° Π² ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡƒ int main (int argc, char *argv[]) { setlocale (LC_ALL, "Russian"); double **D = NULL, *V = NULL; int N = 0, A = 0; // инициализация ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Ρ‹ расстояний /*while (N < N_MIN || N > N_MAX) { cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ количСство Π²Π΅Ρ€ΡˆΠΈΠ½ [" << N_MIN << ", " << N_MAX << "]: "; cin >> N; }*/ ifstream in("vert1.txt"); in >> N; V = (double *) malloc (sizeof (double) * N); for (int i=0; i<N; ++i) in >> V[i]; for (int i = 0; i < N; i++){ cout << V[i] << "\t"; cout << "\n"; } system("pause"); ifstream in2("graph1.txt"); //cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Ρƒ расстояний" << endl; D = (double **) malloc (sizeof (double *) * N); for (int i=0; i<N; ++i) { D[i] = (double *) malloc (sizeof (double) * N); for (int j=0; j<N; ++j) in2 >> D[i][j]; } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) cout << D[i][j] << "\t"; cout << "\n"; } in.close(); in2.close(); // инициализация Π±Π°Π· Π°Π³Π΅Π½Ρ‚ΠΎΠ² while (A < 1 || A > N) { cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Π±Π°Π·Ρƒ для Π°Π³Π΅Π½Ρ‚Π°: "; cin >>A; } for (int i=0; i<N; i++){ if(V[i]==A){ A = i; break; } } // запускаСм Π°Π»Π³ΠΎΡ€ΠΈΡ‚ΠΌ WAY_TYPE way = AntColonyOptimization (D, N, A); // Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ ofstream out("Path.txt"); out << way.length <<' '<<endl; out << V[way.tabu[0]]; for (int i=1; i<way.itabu; ++i) out << " -> " << V[way.tabu[i]]; out.close(); //cout << "Π”Π»ΠΈΠ½Π° ΠΏΡƒΡ‚ΠΈ: " << way.length << endl; //cout << "ΠŸΡƒΡ‚ΡŒ: " << V[way.tabu[0]]; //for (int i=1; i<way.itabu; ++i) cout << " -> " << V[way.tabu[i]]; return 0; } 

Closed due to the fact that it was off topic by aleksandr barakin , user194374, D-side , zRrr , AseN Jun 17 '16 at 23:09 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • β€œQuestions asking for help with debugging (β€œ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - D-side, zRrr
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Although your example compiles (this is an achievement), but it is difficult to reproduce - at least there are no graph1.txt and vert1.txt files, and it is not clear what data to enter. - KoVadim
  • Links to files. yadi.sk/i/Ac0fB4Q0sSoLN yadi.sk/i/eepkWrp4sSoLr At the input, the number of vertices and the name of the vertices (in files), manually enter the base. At the exit the minimum route and its length. This is the traveling salesman problem solved with the help of ant algorithms. - Igor
  • you have in line 39 ( if (flag) sum += pow (pheromone[from][j], ALPHA) * pow (distance[from][j], BETTA); ) from is -1. Therefore, falls. - KoVadim
  • Where is the information about debug, on which line falls? The answer to this question in this form will be useless for any other readers, so -1. \ - Arkady
  • Why don't you use a trivial debugger? - VladD

2 answers 2

(One and possible reasons) Where you are going // ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° вСроятности ΠΏΠ΅Ρ€Π΅Ρ…ΠΎΠ΄Π° Π² Π²Π΅Ρ€ΡˆΠΈΠ½Ρƒ j , the variable j_max first set to -1, then, if the probability is not satisfied, remains at -1, and then used as an array index in ants[k].length += distance0[ants[k].tabu[ants[k].itabu - 1]][j_max]; .

  ... int j_max = -1; double p_max = 0.0; for (int j=0; j<vertex; ++j) { // ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° вСроятности ΠΏΠ΅Ρ€Π΅Ρ…ΠΎΠ΄Π° Π² Π²Π΅Ρ€ΡˆΠΈΠ½Ρƒ j if (ants[k].tabu[ants[k].itabu-1] != j) { double p = probability (j, ants[k], pheromone, distance, vertex); if (p && p >= p_max) { p_max = p; j_max = j; } } } ants[k].length += distance0[ants[k].tabu[ants[k].itabu-1]][j_max]; ... 
  • one
    It's cool that you noticed this, but answering this useless for anyone except the questioner, the question is, you contribute to the cluttering of the resource with questions and answers that do not bring any benefit to the users of the resource, but only complicate the search for a solution to the error. - Arkady
  • @Arkady, you are right of course. Probably should mark the issue to delete. - Vladimir Gamalyan

The problem was in the line: ... if (p && p> = p_max) ... changed it to if (p> = p_max) and everything became normal

  • considering that p is double, very interesting. - KoVadim