Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. // Ant Colony optimization method
  2. void AntCo(string start, string end)
  3. {
  4. //heuristic and structure
  5. double alpha = 2; //pheremone constant
  6. float beta = 1; //heuristic constant
  7. float rho = .8; //pheremone persistence constant
  8. double MAX_PHEREMONE = 100; //max Pheremone
  9. bool solved = false;
  10.  
  11. float evaporation = 1 - rho; //evaporation rate
  12. int MAX_CYCLES = 5; // max cycles to run
  13. int MAX_ANTS = 10; // ant population of 10
  14.  
  15.  
  16. //create ants
  17. vector<vector<edge*>*>colony; //vector of edge vectors to act as colony
  18. for (int i = 0; i < MAX_ANTS; i++)
  19. {
  20. //add ant to vector
  21. colony.push_back(new vector<edge*>);
  22. cout <<i<< " ant in colony "<< endl;
  23. }
  24.  
  25.  
  26. //while (not terminated)
  27. int currentCycle = 0;
  28. while (currentCycle < MAX_CYCLES)
  29. {
  30. cout << "begin while loop" << endl;
  31. for (int i = 0; i < colony.size(); i++) // iterate through colony size to assign ants
  32. {
  33. vector<edge*> *ant = colony[i]; //create ant pointer
  34. vertex * currentNode = findVertex(start); //keep track of ant location
  35.  
  36. while (currentNode != findVertex(end)) //loop each ant and begin movement calculations
  37. {
  38. //transition rule for movement
  39. float probNumer = 0;
  40. float probDenum = 0;
  41. float mover = 0.0;
  42. for (int j = 0; j< currentNode->edgeList.size(); j++) // access the size of each ants adjacentcy list
  43. {
  44. cout << "edge list size for current node " << currentNode->edgeList.size() << endl;
  45. cout << "current Node start " << currentNode->edgeList[j]->start->city << endl;
  46. //add the total edges and pheremone
  47.  
  48. edge *e = currentNode->edgeList[j];
  49. if (e->end->visited ==false)
  50. {
  51. probDenum += powf(e->pheremone, alpha) * powf(1.0 / e->miles, beta);
  52. cout << "DeNumer = " << probDenum << endl;
  53. system("PAUSE");
  54. }
  55. for (int k = 0; k < currentNode->edgeList.size(); k++)
  56. {
  57. probNumer = pow(currentNode->edgeList[k]->pheremone, alpha) * pow((1 / currentNode->edgeList[k]->miles), beta); //take probability for current node
  58.  
  59. }
  60.  
  61. cout << "Numerator " << probNumer << " Denominator: " << probDenum <<endl;
  62.  
  63. mover = probNumer / probDenum;
  64.  
  65. cout << " probNumer / probDenum = " << mover << endl;
  66. system("PAUSE");
  67. currentCycle++;
  68. }
  69. }
  70. }
  71.  
  72. //generate solutions()
  73.  
  74. //damonActions()
  75.  
  76. //update pheremone()
  77.  
  78. //end while
  79. currentCycle++;
  80.  
  81. }
  82. //end funcion
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement