ACO Ant Colony Algorithm Global Path Planning

Author: Chihu
Link: https://www.zhihu.com/question/20207322/answer/27387066
Source: Zhihu
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, for non-commercial reprint, please indicate the source.

Ant colony algorithm is an approximation algorithm. It is not used to solve the problem of existing accurate and effective algorithm, but to solve the problem that no accurate and effective algorithm has been found so far, such as the traveling salesman problem (TSP).

The traveling salesman problem can also be said to seek the "shortest path", but it is to seek the minimum Hamiltonian cycle of a complete graph. This problem has not found a polynomial time algorithm so far, and it belongs to the NPC problem. That is to say, when the problem scale is slightly larger, The amount of computation of the existing exact algorithm will increase dramatically
 

What are the advantages of ant colony algorithm?

To understand this problem, it is necessary to figure out what the algorithm is used for?

Algorithms are used to solve various problems, such as the shortest path problem in the main problem.

In the field of mathematics, there are classifications according to the complexity of the problem itself.

In simple terms, problems are divided into P problems, NP problems, NPC problems, and NP-hard problems.

P is the initial letter of the word polynomial, that is, Polynomial, [here refers to polynomial time]

The P problem means that the corresponding classical algorithm can always be found to obtain the optimal solution in polynomial time.

The NP problem means that it is uncertain whether the corresponding algorithm can be found to obtain the optimal solution in polynomial time.

The NPC problem can be understood as the most difficult (highest complexity) problem in the NP problem, that is to say, it is difficult to find a corresponding algorithm that can obtain the optimal solution in polynomial time.

The NP-hard problem can be simply understood as that no corresponding algorithm can find the optimal solution in polynomial time.

All studies on NP problems focus on one question, that is, is there P=NP? The so-called "NP problem" is actually just one sentence: prove or overthrow P=NP. The current progress is not ideal, so I won't say much. The relationship between the four, if you are interested in looking at the picture, if you are not interested in skipping, it will not affect.

It is simple to figure out the classification of problems that affect the research direction of computer algorithms .

To put it simply, traditional classical algorithms can solve P-type problems, but NP problems, especially NPC problems, are helpless, so scientists began to find a way. Since an optimal solution cannot be obtained in polynomial time, then we Let’s get a better solution, use prior experience knowledge as the heuristic operator, so the heuristic algorithm was born, but the heuristic algorithm can only target specific problems (for example, the prime algorithm and the kruskal algorithm are both classic heuristic algorithms, in The effect of the minimum spanning tree field is better), the heuristic algorithm is not suitable for cross-field, but there are so many NP problems, what to do, so various bionics theories began to be introduced into the field of computer algorithms, and the so-called ant colony, particle Group, genetic, and neural network algorithms are collectively referred to as intelligent optimization algorithms ( meta-heuristic algorithms ) to find better solutions.

Algorithm introduction:

 Ant colony algorithm (Ant Colony Algorithm, ACA) was first proposed in 1992, 'this algorithm simulates the foraging behavior of ants in nature.

When an ant is looking for a food source, it will release a pheromone on its path and can sense the pheromone released by other ants. The size of the pheromone concentration represents the distance of the path, and the higher the pheromone concentration, the shorter the corresponding path distance.

Usually, ants will preferentially choose the path with higher pheromone concentration with a greater probability, and release a certain amount of pheromone to enhance the pheromone concentration on this path, thus forming a positive feedback. Eventually, the ants can find an i-best path from the nest to the food source, that is, the shortest distance.

Algorithm idea:
use the walking path of ants to represent the feasible solution of the problem to be optimized, and all the paths of the entire ant colony constitute the solution space of the problem to be optimized.
Ants with shorter paths released more pheromones. As time went on, the concentration of pheromones accumulated on shorter paths gradually increased, and the number of ants who chose this path also increased.
In the end, the entire ants will concentrate on the best path under the action of positive feedback, which corresponds to the optimal solution of the problem to be optimized.
But it doesn't mean that if the concentration of pheromone is high, you must take that route. There is also a chance to take a route with low information concentration.

Specific algorithm: 

 

Parameter role:

Number of ants: The larger the population, the more accurate the optimal solution will be, but there will be a large number of ants passing through the same path repeatedly, and the running time will increase.
Alpha pheromone importance factor: If the alpha value is too large, the possibility of the ants choosing the path they have traveled before is greater, and the randomness of the ant colony's search path is weakened. If the alpha value is too small, the search range of the ant colony will be reduced, and it is easy to fall into a local optimal solution.
beta heuristic function factor: when the beta value increases, the ant colony is more likely to choose a local shorter path, which can speed up the running speed of the algorithm, but may fall into a local optimal solution.
ruo pheromone volatilization factor: when ruo is small, there is a lot of residual information in each path, and the search will be repeated, and the calculation time will increase; when ruo is large, many effective paths will be abandoned, and the optimal value may be lost .
Q information strength

The key code is as follows:

#生成蚁群
def get_ants(self, num_city):
    for i in range(self.m):
        start = np.random.randint(num_city - 1)
        self.Table[i][0] = start
        unvisit = list([x for x in range(num_city) if x != start])
        current = start
        j = 1
        while len(unvisit) != 0:
            P = []
            # 通过信息素计算城市之间的转移概率
            for v in unvisit:
                P.append(self.Tau[current][v] ** self.alpha * self.Eta[current][v] ** self.beta)
            P_sum = sum(P)
            P = [x / P_sum for x in P]
            # 轮盘赌选择一个一个城市
            index = self.rand_choose(P)
            current = unvisit[index]
            self.Table[i][j] = current
            unvisit.remove(current)
            j += 1


# 更新信息素
def update_Tau(self):
    delta_tau = np.zeros([self.num_city, self.num_city])
    paths = self.compute_paths(self.Table)
    for i in range(self.m):
        for j in range(self.num_city - 1):
            a = self.Table[i][j]
            b = self.Table[i][j + 1]
            delta_tau[a][b] = delta_tau[a][b] + self.Q / paths[i]
        a = self.Table[i][0]
        b = self.Table[i][-1]
        delta_tau[a][b] = delta_tau[a][b] + self.Q / paths[i]
    self.Tau = (1 - self.rho) * self.Tau + delta_tau


def aco(self):
        best_lenth = math.inf  # math.inf返回浮点正无穷大
        best_path = None
        for cnt in range(self.iter_max):
            # 生成新的蚁群
            self.get_ants(self.num_city)  # out>>self.Table
            self.paths = self.compute_paths(self.Table)
            # 取该蚁群的最优解
            tmp_lenth = min(self.paths)
            tmp_path = self.Table[self.paths.index(tmp_lenth)]
            # 可视化初始的路径
            if cnt == 0:
                init_show = self.location[tmp_path]
                init_show = np.vstack([init_show, init_show[0]])
            # 更新最优解
            if tmp_lenth < best_lenth:
                best_lenth = tmp_lenth
                best_path = tmp_path
            # 更新信息素
            self.update_Tau()

            # 保存结果
            self.iter_x.append(cnt)
            self.iter_y.append(best_lenth)
            print(cnt, best_lenth)
        return best_lenth, best_path

Code explanation:

The main operation of the code is to generate an ant colony, let each ant find a path, calculate the probability P and record the path Table of each ant in a roulette manner. Calculate the path of each ant and take the shortest path. Then update the pheromone. The main operation here is to calculate ΔTau, and the selection of ΔTau is only for the path, so it must be calculated for each ant that passes by, and finally the new Tau is calculated according to the formula of the volatilization principle.

Guess you like

Origin blog.csdn.net/weixin_62705892/article/details/128165589