JavaScript implementation of ACS ant colony Algorithm for solving symmetric tsp traveling salesman problem

Source: Internet
Author: User

It was not difficult to realize this strange algorithm on the basis of understanding ant colony algorithm, the result is actually very different. It took nearly three days to change to the shape of the shot that could barely be taken now. Since the formulas are all pictures, let's replace that part of the content, mark.

JavaScript implementation of solving tsp problem by ant colony algorithm directory (again a magical manual directory.) )

1 Ant colony algorithm

(1) Introduction to Ant colony as algorithm

(2) Ant colony as algorithm process

(3) Application of ant colony as algorithm tsp problem

(4) Ant colony algorithm applied to TSP step

2 Ant colony Systems (Ant Colony System, ACS)

(1) Ant colony System

(2) ACS solves TSP problem

A. Initializing global parameters

B. Outer circulation

C. Inner Loop

3 Concrete Implementations

(1) Functions.js

(2) Antsystem.js

(3) Action.js

(4) Jquery-1.11.0.js

4 Algorithm Testing

5 Analysis and summary

6 References

1 Ant colony Algorithm (1) Introduction to Ant colony as algorithm

The 1990s Italian scholar M. Dorigo,v. Maniezzo,a. Colorni is inspired by the mechanism of biological evolution, and by simulating the behavior of ant search Path in nature, a new simulated evolutionary algorithm-ant colony algorithm is proposed, which is a main algorithm in the field of swarm intelligence theory research. This method is used to solve TSP problem, distribution problem and job-shop scheduling problem, and good experimental results are obtained. Although the study time is not long, but now the research shows that the ant colony algorithm in solving complex optimization problems (especially discrete optimization problems) has some advantages, indicating that it is a promising algorithm.

This approach can be used to solve most optimization problems or to transform them into optimal solutions. Now its application field has been extended to multi-objective optimization, data classification, data clustering, pattern recognition, Telecom QoS Management, biological system modeling, process planning, signal processing, robot control, decision support, simulation and system identification, and so on, the Swarm intelligence theory and method provides a new way to solve such application problems.

(2) Ant colony as algorithm process

Ant colony Algorithm (ACO) is a bionic algorithm which can be used to find the path of ants in nature. In the course of the movement, the ant can leave a substance called exogenous hormone (pheromone) on the path it passes through, and the ant can perceive the substance in the course of its movement and guide its direction of movement. Therefore, the collective behavior of ant colony composed by a large number of ants shows a positive feedback phenomenon: the more ants walk on a certain path, the greater the probability that the latter will choose the path.

In order to illustrate the principle of ant colony algorithm, we first briefly introduce the specific process of ant searching for food. When ants search for food, they can always find an optimal path from food to nests. This is because ants release a special pheromone on the path as they look for a path. When they meet a junction that has not yet passed. Randomly pick a path to go ahead. At the same time, the pheromone associated with the length of the path is released. The longer the path, the lower the concentration of the emitted cables. When the ant came across the intersection again. The probability of choosing a higher path of hormone concentration would be relatively large. This forms a positive feedback. The concentration of the excitation cable on the optimal path is getting larger. The hormone concentrations in other pathways are reduced over time. Eventually the entire ant colony will find the optimal path.

Figure 1 Ant single Walk

Ants start from point A, the same speed, food at point D, may randomly choose the route abd or ACD. Suppose an ant is initially assigned to each route, one step at a time unit, this figure is the case after 9 units of time: The ants that walk abd reach the finish line, and the ant on the ACD just goes to point C, half way.

Figure 2 ants walking back and forth

Figure 2 is the case from the beginning, after 18 time units: After the ant arrives at the end of the Abd to get the food and return to the beginning a, and the ant to go to the ACD just go to point D.

Assuming that each of the ants left behind by the pheromone for a unit, then after 36 time units, all the ants started together through different routes from D point to obtain food, when Abd's route to and fro 2 times, each of the pheromone is 4 units, and the ACD route to and from the trip, The pheromone in each place is 2 units with a ratio of 2:1.

The process of finding food continued, and the ant group sent an ant (total of 2) on the ABD route, as instructed by pheromone, while the ACD route remained an ant. After a further 36 time units, the pheromone units on both lines accumulate at 12 and 4, and the ratio is 3:1.

If the above rules continue, the ant colony will send an additional ant (total of 3) to the ABD route, while the ACD route is still an ant. After a further 36 time units, the pheromone units on both lines accumulate at 24 and 6, and the ratio is 4:1.

If you continue, you will be guided by the pheromone, and eventually all the ants would abandon the ACD route and choose the Abd route. This is the positive feedback effect mentioned earlier.

(3) Application of ant colony as algorithm tsp problem

Based on the problem of optimal path selection for the above ant colony, artificial ant colony can be constructed to solve the optimization problem, such as TSP problem.

The working units with simple functions are considered as ants in the artificial ant colony. The similarity of the two is that it is the preferred way to choose the concentration of pheromone. The shorter path has a high pheromone concentration, so it can eventually be selected by all ants, which is the ultimate optimization result. The difference between the two is that the artificial ant colony has a certain memory ability, can remember the nodes that have been visited. At the same time, when the artificial ant colony chooses the next path, it is consciously searching for the shortest path, rather than being blind, according to certain algorithm rules. For example, in the TSP problem, you can know the distance from the current city to the next destination in advance.

The TSP problem is represented as an g= graph of N cities (n,a),

In the artificial ant colony algorithm for TSP, it is assumed that the M ants move between the neighboring nodes of the graph, thus the solution of the problem can be obtained by cooperating and asynchronously. The one-step transfer probability for each ant is determined by the two types of parameters on each edge of the diagram:

1. Pheromone values are also known as pheromone traces.

2. Visibility, which is a priori value.

There are 2 ways to update pheromone, one is volatilization, that is, all the information on the path is reduced to a certain rate, simulating the natural ant colony of pheromone with the process of volatile, and the second is to enhance the value of "good" (with ants walk) side of the increase pheromone.

The movement of ants to the next target is realized by a stochastic principle, that is, using the information stored in the current node, the probability of the next node can be calculated, and the probability is realized by the one-step movement, which is more and more close to the optimal solution.

When the ant is looking for a solution, or finds one, it evaluates the degree of optimization of the solution or part of the solution, and stores the evaluation information in the associated pheromone.

(4) Ant colony algorithm applied to TSP step

2 Ant colony Systems (Ant Colony System, ACS) (1) Ant colony System

In order to further overcome the problems exposed in as, an ant colony system (ant Colony Systems, ACS) is proposed. The proposed system is based on the ANT-Q algorithm. Ant-q combines ant algorithm with an enhanced learning algorithm q-learning organically. There are three main differences between ACS and as: First, ACS adopts a more aggressive behavior selection rule, and secondly, it only enhances the pheromone on the path that belongs to the global optimal solution. Among them, 0<ρ<1 is the pheromone volatilization parameter, which is the global optimal path length from the start of pathfinding to the present.

Thirdly, the negative feedback mechanism is introduced, and whenever an ant is moved from one node to another, the pheromone on the path is partially eliminated according to the following formula, thus realizing a local adjustment of pheromone to reduce the probability that the selected path will be selected again.

(2) ACS solves TSP problem

In this article, using ACS to solve the symmetric TSP problem, the specific steps are as follows:

A. Initializing global parameters

N: Number of cities N,

M: Ants only number M (the value between n^0.5 ~ N/2 with random numbers),

P: Pheromone evaporation coefficient p=0.7,

Nc,nc_max: The number of initial outer cycles nc=1, the maximum number of cycles nc_max=60,

D: Calculates the distance matrix D between 22 cities using coordinates,

T: Pheromone concentration matrix T between the initial city routes (initial default equals, both 1/(n (n-1)), i.e., and 1),

E: The urban route of each ant as it progresses. E (Each round is the t at that time, assuming that each round of ants affects each other very small, negligible, leaving the pheromone mainly to the back of the ant function),

Last,lastroute: The last and optimal process Lastroute for storing the total length of the current optimal route.

B. Outer circulation

The loop variable is the number of wheel NC. Each outer loop initializes the current wheel count of the current ant I's walking path. After the end of each inner loop, calculate the total walking length of each ant in the current NC Sall, record The walking process of each ant and get the shortest total length of walk and Route Lastroute, then increase the pheromone of the best route according to Equation 1, and the other route pheromone, Again into the next round.

C. Inner Loop

For each ant I, record their need to visit the city list route.

The default is from City 0 (which is all the same) and randomly selects the next city according to the proportion of other urban pheromone. The concrete way to find out the next city to the current city distance than the sum of the total distance, and then accumulate one by one, so to obtain a proportional range [0 ~ p1, p1 ~ p1 + p2, p1 + P2 ~ p1 + p2 + p3 ... 1]. The Math.random function takes the random value between 0~1 to determine which interval, then the next step to the corresponding city. Then the route in the corresponding city 0, and the selected city and the distance between the original city 0, the time to recalculate the scale interval will not affect, when the sum of all the values in the route is 0, except that the initial city all visited, and finally visited the initial city 0, an ant to complete the mission.

3 Concrete Implementations

According to the above algorithm procedure, the program can be written as follows:

(1) Functions.js

Custom tool functions, reducing global objects and reusing code as much as possible.

which

Minall (ARR): Returns the smallest value in the one-dimensional array arr

All (arr): one-dimensional array arr summation

NextStep (x,e): According to the current city X and all route pheromone concentration matrix E randomly determine the next city, similar to the turntable method. The detailed algorithm is shown above 2. (2). C.

SUM (x,e): Achieve turntable effect with NeXTSTEP

New2arr (arr,x): Converts a new arr of type undefined to a two-dimensional array, which is typically used to initialize

Getantnum (x): Get the best number of ants by city number

GetType (o), Extend (Destination,source): Implements deep copy of the object, especially for multidimensional arrays (in general, one-dimensional slice or concat on the line), see reference [6].

(2) Antsystem.js

The implementation of ACS Ant colony algorithm is encapsulated in Antsystem (C) for easy invocation. Enter the city coordinate matrix C, enter the total length of the optimal path last and the optimal path Lastroute.

(3) Action.js

Use the Jquery.js tool library for custom data entry and final result presentation. Some data types are judged. (based on the guidelines of the algorithm itself, only the number of ants in the input n have certain judgments and hints)

(4) Jquery-1.11.0.js

A popular JS library for JS Fast-compatible development

4 Algorithm Testing

Click Default.html Follow the prompts to enter the data step-by-step submission. Compatible with browsers such as Ie8.0+,firefox,chrome.

Run the process:

    1. Enter the appropriate number and click confirm Popup coordinate matrix input window

2. Input coordinate matrix Click Confirm to get the final result

Inputs are (0,0), (0,1), (0,2), (0,3), (0,4), (1,0), (+), (up), (1,3), (2,2), (2,3), (2,4), (3,3), (3,4).

3. Set breakpoints in the appropriate place in the console script of FF you can step through the specific running process (because the specific display is too much space, the full output page display is too trivial, here is a picture) (you can take each round of important data to see the specific changes as needed)

5 Analysis and summary

This time ACS solves the symmetric TSP problem with JavaScript implementations, relative to the classic ACS, with some modifications:

1. The outer loop end condition is only the number of circular wheels nc<nc_max. does not consider whether it has been convergent.

2. The recommended volatility coefficient p-value on reference [4] is not confirmed by multiple data measurements.

JS, as a language of weak variables, has some implicit default rules:

1. Using the + symbol may change the data type of the variables on both sides to string by default. See references [7].

2. When declaring a variable x inside a function, assigning a value directly to the variable x instead of using the var statement causes the variable x to become a global variable, polluting the global space.

3. The parameters of the function exist as local variables inside the function, and end when the function ends (unless the closure is used), which does not affect the variables outside the same name.

4. Using the = Sign between object variables is a reference pass of an object, that is, two objects point to the same area in memory, and when one variable changes the properties of an object, the corresponding property of the other variable also changes. Therefore, you need to use the deep copy of the object. For a one-dimensional array, use the slice and concat functions for a multidimensional array reference reference [6].

6 References

[1] Peng Dai. Theory and application of swarm intelligence. Electronics newspaper, 2003 S1 period.

[2] Li Shiyong. Ant colony algorithm and its application. Harbin Press.

[3] Wu enlightened. Intelligent ant Colony Algorithm and its application. Shanghai Science and Technology Press.

[4] Janschang, Xu Xian, Oh Joon. Optimal selection of algorithm parameters in Ant colony Algorithm [J]. Bulletin of Science and Technology, 2003,05:381-386.

[5] Song Zhifei. Research on TSP problem based on ant colony algorithm [D]. Jiangxi University of Technology, 2013.

[6] JS deep copy http://www.cnblogs.com/Loofah/archive/2012/03/23/2413665.html

[7] JS type conversion http://weibo.com/p/230418c3aa9eb60102vh10#_loginLayer_1436233053863

The specific source code can download Https://github.com/codetker/AntSystem on my github.

As a result of the recent internship, we have to finish the work at hand (a sneak is really busy,,,). So follow the idea: in different browsers of the algorithm to perform the average speed test, as well as the C and MATLAB for the same code of the mean execution time comparison, there is a linear neural network implementation of the spammer Judgment system JavaScript implementation, it is estimated that a period of time to be baked.

Given my familiarity with many JavaScript built-in functions, there must be a lot of detours in the code, please point out, thank you.

ACS ant colony Algorithm for solving the problem of symmetric tsp traveling salesman JavaScript implementation

Related Article

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.