Guest User

Ant runner v0.1

a guest
Jan 19th, 2012
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <algorithm>
  5. #include <memory.h>
  6. #include <cmath>
  7. #include <time.h>
  8. #include <climits>
  9. #include <boost/random.hpp>
  10.  
  11. #define PI 3.1415926535897932384626433832795
  12. #define sqr(x) ((x)*(x))
  13. #define OUT_RT cerr << (float(clock()) / CLOCKS_PER_SEC) << endl
  14. #define sqr(x) ((x)*(x))
  15.  
  16. typedef long long LL;
  17. typedef unsigned long long ULL;
  18.  
  19. using namespace std;
  20.  
  21. const int ant_count = 10;
  22. const int max_n = 105;
  23.  
  24. const long double minus_coef = 1e-7;
  25. const long double plus_coef = 5;
  26. const long double alpha = 2;
  27. const long double beta = 5;
  28. const long double up_edge = 1e100;
  29.  
  30. int n;
  31. long double W[max_n][max_n], D[max_n][max_n], T[max_n];
  32.  
  33. struct Tant {
  34.     int pos;
  35.     int mem[max_n], memn, p[max_n];
  36.     long double curlen;
  37. } ant[ant_count];
  38.  
  39. struct Tp {
  40.     long double x, y;
  41. } a[max_n];
  42.  
  43. int main(){
  44.     freopen("input.txt","r",stdin);
  45.  
  46.     boost::random::mt19937 gen(time(0));
  47.     srand((unsigned)time(NULL));
  48.  
  49.     cin >> n;
  50.     for(int i=0;i<n;i++) {
  51.         cin >> a[i].x >> a[i].y;
  52.         for(int j=0;j<i;j++) {
  53.             D[i][j] = D[j][i] = sqrt(sqr(a[i].x - a[j].x) + sqr(a[i].y - a[j].y));
  54.             W[i][j] = W[j][i] = 1;
  55.         }
  56.     }
  57.  
  58.     long double ans = 1e100;
  59.     int iter = 0;
  60.     while (true) {
  61.         iter++;
  62.  
  63.         for(int i=0; i<ant_count && i < iter; ++i) {
  64.             int x = ant[i].pos;
  65.  
  66.             if(ant[i].memn == n - 1) {
  67.                 long double l = ant[i].curlen + D[x][0];
  68.                 ant[i].p[0] = ant[i].pos;
  69.  
  70.                 if(l < ans){
  71.                     ans = l;
  72.  
  73.                     ofstream f("res.txt");
  74.                     f << ans << endl;
  75.                     int y = 0, len = 0;
  76.                     do {
  77.                         f << (y + 1) << " ";
  78.                         y = ant[i].p[y];
  79.                     } while(y != 0);
  80.                     f << endl;
  81.                     f.close();
  82.  
  83.                     cout << iter << " : " << ans << endl;
  84.                 }
  85.                
  86.                 int y = 0;
  87.                 do {
  88.                     W[ant[i].p[y]][y] = min(W[ant[i].p[y]][y] + plus_coef / l, up_edge);
  89.                     y = ant[i].p[y];
  90.                 } while(y != 0);
  91.  
  92.                 ant[i].pos = 0;
  93.                 ant[i].curlen = 0;
  94.                 ant[i].memn = 0;
  95.                 memset(ant[i].mem, 0, sizeof(ant[i].mem));
  96.  
  97.                 continue;
  98.             }
  99.  
  100.             double sum = 0;
  101.             for(int j = 1; j < n; ++j) if (!ant[i].mem[j]) {
  102.                 T[j] = pow(W[x][j], alpha) + pow(D[x][j], -beta);
  103.                 sum += T[j];
  104.             }
  105.             double r = boost::random::uniform_real_distribution<double>(0, sum)(gen);
  106.             for(int j = 1; j < n; ++j) if (!ant[i].mem[j]) {
  107.                 if (r < T[j]) {
  108.                     ant[i].pos = j;
  109.                     ant[i].mem[j] = true;
  110.                     ant[i].memn++;
  111.                     ant[i].curlen += D[x][j];
  112.                     ant[i].p[j] = x;
  113.                     break;
  114.                 }
  115.                 r -= T[j];
  116.             }
  117.         }
  118.  
  119.         long double max = -1e100, min = 1e100;
  120.         for(int i=0;i<n;i++)
  121.             for(int j=0;j<n;j++) if(i != j) {
  122.                 W[i][j] = (1 - minus_coef) * W[i][j];
  123. //              if(W[i][j] > max) max = W[i][j];
  124. //              if(W[j][i] < min) min = W[j][i];
  125.             }
  126. //      cerr << min << " " << max << endl;
  127.     }  
  128.  
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment