Ant Colony Algorithm for Finding the optimal path of a maze

Source: Internet
Author: User
Tags bitset map class stackpath

The basic idea of this program is to use the ant week model in the ant colony algorithm to update the information elements of the global camouflage map.

Select the next square for each ant. RcMax = 2000 rounds of simulation will be performed in total (the more simulation times theoretically, the more results

Will be closer to the actual value), and in each round, M = 10 ants will be excluded to explore the path. At the same time, we use

Is implemented by the stack data structure. When the stack is finally empty, it indicates no solution. But at the same time, one disadvantage of this program is:

The algorithm adopts the multi-thread mode for each round of probing ant, so the overall operation efficiency is not very high. If the reader has a good

Leave a message for your thoughts or suggestions.

# Include <iostream >#include <stack >#include <bitset> using namespace std; // coordinate class struct Point {int x; int y ;}; // Map class template <int A, int B> class Map {public: int (* p) [B]; // 1 indicates the obstacle square, 0 indicates that the square can pass bitset <4> (* around) [B]; // records the optional int row marked by four methods around each square; // number of rows int col; // Number of columns Map () {p = new int [A] [B]; around = new bitset <4> [A] [B];} Map (Map <, b> & B1) {p = new int [A] [B]; und = new bitset <4> [A] [B]; row = B1.row; col = B1.col; for (int I = 0; I <row; I ++) {for (int j = 0; j <col; j ++) {p [I] [j] = B1.p [I] [j]; around [I] [j] = B1.around [I] [j] ;}} Map <, b> & operator = (Map <A, B> & B1) {row = B1.row; col = B1.col; for (int I = 0; I <row; I ++) {for (int j = 0; j <col; j ++) {this-> p [I] [j] = B1.p [I] [j]; around [I] [j] = B1.around [I] [j] ;}} return * this ;}; // start point, end termination point template <int, int B> bool FindPath (Map <A, B> & map, Point & start, Point & end) {cons T int N1 = A; const int N2 = B; const int M = 10; // Number of ants in each round const int RcMax = 2000; // number of iterations const int IN = 1; // double add [N1] [N2]; // The double phe [N1] [N2]; // The double MAX = 0x7fffff; double alphe, betra, rout, Q; // The Influence Factor of alphe information, the influence factor of betra route distance, and the degree of tolerance of rout information. Q is used to calculate the incremental double bestSolution = MAX of the information elements left by each ant on its path; // The shortest distance stack <Point> Beststackpath; // Optimal Route // The initialization Variable Parameter and information array alphe = betra = 2; rout = 0.7; Q = 10; // Add the obstacle for (int I = 0; I <map. col; I ++) {map. p [0] [I] = map. p [map. row-1] [I] = 1 ;}for (int I = 0; I <map. row; I ++) {map. p [I] [0] = map. p [I] [map. col-1] = 1;} // indicates the access bits around each square in the initialization diagram. 0 indicates accessible. // initializes the information element array for (int I = 0; I <N1; I ++) {for (int j = 0; j <N2; j ++) {phe [I] [j] = IN; map. around [I] [j]. reset (); // set all four directions to optional} // The offset Array Used for direction selection to Point offset in clockwise direction [4]; offset [0]. x = 0; offset [0]. y = 1; // offset to the right [1]. x = 1; offset [1]. y = 0; // offset down [2]. x = 0; offset [2]. y =-1; // offset to the left [3]. x =-1; offset [3]. y = 0; // up // each round of M is only ant, And the stack is updated with global information elements after each round ends. <Point> stackpath [M]; // copy the obstacle Map <A, B> Ini_map [M]; // record the current position of each ant, Point Allposition [M]; int s = 0; while (s <RcMax) // a total of RcMax wheels {// first clear the route storage stack for each ant (int I = 0; I <M; I ++) {while (! Stackpath [I]. empty () {stackpath [I]. pop () ;}for (int I = 0; I <M; I ++) {Ini_map [I] = map; // initialize the start point as the obstacle point Ini_map [I]. p [start. x] [start. y] = 1; // start stack stackpath [I]. push (start); // initialize the current position of each ant, Allposition [I] = start;} // enable M for only ant loops (int j = 0; j <M; j ++) {while (Allposition [j]. x )! = (End. x) | (Allposition [j]. y )! = (End. y) {// select next double psum = 0; for (int op = 0; op <4; op ++) {// calculate the next possible coordinate int x = Allposition [j]. x + offset [op]. x; int y = Allposition [j]. y + offset [op]. y; if (Ini_map [j]. around [Allposition [j]. x] [Allposition [j]. y]) [op] = 0 & Ini_map [j]. p [x] [y]! = 1) {psum + = pow (phe [x] [y], alphe) * pow (10.0/stackpath [j]. size (), betra) ;}/// determine whether there is a choice // if the next point is found if (psum! = 0) {double drand = (double) (rand ()/(RAND_MAX + 1); double pro = 0; int re; int x, y; for (re = 0; re <4; re ++) {// calculate the next possible coordinate x = Allposition [j]. x + offset [re]. x; y = Allposition [j]. y + offset [re]. y; if (Ini_map [j]. around [Allposition [j]. x] [Allposition [j]. y]) [re] = 0 & Ini_map [j]. p [x] [y]! = 1) {pro + = (pow (phe [x] [y], alphe) * pow (10.0/stackpath [j]. size (), betra)/psum; if (pro >=drand) {break ;}}// inbound stack Allposition [j]. x = x; Allposition [j]. y = y; stackpath [j]. push (Allposition [j]); // sets the obstacle Ini_map [j]. p [Allposition [j]. x] [Allposition [j]. y] = 1;} else // failed to find the next point {// back to stack stackpath [j]. pop (); // remove the obstacle Ini_map [j]. p [Allposition [j]. x] [Allposition [j]. y] = 0; if (stackpath [j]. empty () {r Eturn false;} // sets the Allposition if (Allposition [j]. x = stackpath [j]. top (). x) {if (Allposition [j]. y-stackpath [j]. top (). y) = 1) // to the right {(Ini_map [j]. around [stackpath [j]. top (). x] [stackpath [j]. top (). y]) [0] = 1; // indicates that the direction has been accessed} if (Allposition [j]. y-stackpath [j]. top (). y) =-1) // to the left {(Ini_map [j]. around [stackpath [j]. top (). x] [stackpath [j]. top (). y]) [2] = 1; // mark the direction that has been accessed} if (Allposition [j]. y = stackpath [j]. Top (). y) {if (Allposition [j]. x-stackpath [j]. top (). x) = 1) // down {(Ini_map [j]. around [stackpath [j]. top (). x] [stackpath [j]. top (). y]) [1] = 1; // indicates that the direction has been accessed} if (Allposition [j]. x-stackpath [j]. top (). x) =-1) // up {(Ini_map [j]. around [stackpath [j]. top (). x] [stackpath [j]. top (). y]) [3] = 1; // mark the direction that has been accessed} Allposition [j]. x = stackpath [j]. top (). x; Allposition [j]. y = stackpath [j]. top (). y ;}}// Save the optimal double sol Ution = 0; for (int I = 0; I <M; I ++) {solution = 0; solution = stackpath [I]. size (); if (solution <bestSolution) {Beststackpath = stackpath [I]; bestSolution = solution ;}} // calculate the metadata increment left by each ant on each segment of its path // initialize the metadata increment array for (int I = 0; I <N1; I ++) {for (int j = 0; j <N2; j ++) {add [I] [j] = 0 ;}} for (int I = 0; I <M; I ++) {// calculate the total distance of each ant route from solu double solu = 0; solu = stackpath [I]. size (); double d = Q/solu; while (! Stackpath [I]. empty () {add [stackpath [I]. top (). x] [stackpath [I]. top (). y] + = d; stackpath [I]. pop () ;}// update the information element for (int I = 0; I <N1; I ++) {for (int j = 0; j <N2; j ++) {phe [I] [j] = phe [I] [j] * rout + add [I] [j]; // set an if (phe [I] [j] <0.0001) {phe [I] [j] = 0.0001 ;} if (phe [I] [j]> 20) {phe [I] [j] = 20 ;}}s ++ ;}// wheel // locate the path, output stackpathcout <"to find the optimal path! "<Endl; cout <" Shortest Route Length: total "<Beststackpath. size () <" square! "<Endl; while (! Beststackpath. empty () {cout <"<" <Beststackpath. top (). x <"," <Beststackpath. top (). y <">" <endl; Beststackpath. pop ();} return true;} int main () {// create a maze Map <10, 10> map; map. col = map. row = 10; int p [10] [10]; for (int I = 0; I <10; I ++) // initialize the maze {for (int j = 0; j <10; j ++) {p [I] [j] = 0 ;}}// set obstacle for the maze p [1] [3] = 1; p [1] [7] = 1; p [2] [3] = 1; p [2] [7] = 1; p [3] [5] = 1; p [3] [6] = 0; p [4] [2] = 1; p [4] [3] = 1; p [4] [4] = 1; p [5] [4] = 1; p [6] [2] = 1; p [6] [6] = 1; p [7] [2] = 1; p [7] [3] = 1; p [7] [4] = 1; p [7] [6] = 1; p [8] [1] = 1; map. p = p; Point start, end; start. x = start. y = 1; end. x = 8, end. y = 8; if (! FindPath <10, 10> (map, start, end) {cout <"this maze has no solution! "<Endl ;}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.