# Application of ant algorithm (01 knapsack, function extremum, TSP)

The author is a freshman. This algorithm is written by referring to Literature and other people's blogs and their own understanding. If there is any mistake, please correct it in time. I use Matlab for this article, and I will give the python version later. In the future, there will be other optimization algorithms, artificial intelligence algorithms, machine learning, deep learning and so on.

This is where I am Detailed explanation of station b

#### catalog:

## 1. Principle

Before understanding the ant algorithm, the first thing is to understand the background of the algorithm. In nature, ants can always find the shortest path from ant hole to food, which is the result of people's observation. This is why, because there is a matter called pheromones. In the process of movement, ants can sense the existence and concentration of the substance, and release the substance at the same time.

### Principle introduction

In the initial stage, the pheromone concentration in the environment is 0, at this time, ants will randomly choose the path to the food. As shown in the figure below:

Then the ants, according to the pheromones on the previous path, choose which path they will take. Focus! Pheromone is a volatile substance over time. Assuming that each ant leaves the same pheromone in unit time, the shorter the path, the more pheromone remains. The more likely the ants are to choose this path. There are more and more ants and pheromones on this road. Therefore, positive feedback is formed. Finally, the optimal path is obtained.

### Parameters and formulas of basic ant algorithm

Take TSP as an example.

First, initialization parameters:

m: The number of ants is about 1.5 times that of cities. If the number of ants is too large, the pheromone concentration on each path tends to be average, and the positive feedback effect is weakened, resulting in the slow convergence rate; if it is too small, it may cause the pheromone concentration of some paths that have never been searched to be reduced to 0, resulting in premature convergence, and the overall optimality of the solution to be reduced

alpha: pheromone factor, which reflects the relative importance of information accumulated in the process of ant movement in guiding ant colony search. The value range is usually between [1,4]. If the value of pheromone factor is set too large, it is easy to weaken the randomness of searching; if the value is too small, it is easy to fall into the local optimum too early. Why is that? Take a look at the following Equation 1 , the larger the alpha is, the larger the proportion of pheromones is, that is, the greater the importance of pheromones is, and the ants are more inclined to follow the shortest path they have gone before. If the path is as shown in the following figure:

Then, it will fall into the local optimal solution. On the contrary, if the alpha value is smaller, the important new pheromone will be smaller, and the result will tend to the local optimal solution.

belta: heuristic function factor, which reflects the relative importance of heuristic information in guiding ant colony search. The value range is between [3,5]. If the value is set too large, although the convergence speed is accelerated, it is easy to fall into local optimum; if the value is too small, the ant colony is easy to fall into pure random search, and it is difficult to find the optimal solution. Again, refer to the following Equation 1 . The larger the belta is, the more important the heuristic function is. When ants are at a local point, the probability of choosing the local optimal solution is greater. But the local optimal does not represent the global optimal solution, so it is easy to fall into the local optimal solution.

rho: pheromone volatilization factor, which reflects the disappearance level of pheromone, on the contrary, it reflects the maintenance level of pheromone, and the value range is usually between [0.2, 0.5]. When the value is too large, it is easy to affect the randomness and global optimality; otherwise, the convergence rate is reduced

Q: Pheromone constant, which represents the total amount of pheromones released by an ant when it traverses all cities once. The larger the convergence rate is, the faster the convergence rate is, but it is easy to fall into the local optimum; otherwise, it will affect the convergence rate

n: Number of cities

d: Distance between city i and city j

tau: the pheromone concentration between city i and city j at time t

Probability of ant k transfer from city i to city j at pc: t time

Heu_F: Heuristic function, which represents the expected degree of ant transfer from city i to city j, here we take the reciprocal of distance. (well understood)

allow: the collection of cities to be visited by ant k. there is an element in the initial time, that is, excluding other cities other than the city where the ant started. As time goes on, there are fewer and fewer cities until it is empty, indicating that all cities have been traversed

: indicates the contribution of the k-th ant to the total increase of pheromone concentration between city i and city j when all ants traverse all cities

: indicates the cumulative increase of pheromone concentration between city i and city j when all ants traverse all cities

: indicates the total distance that ant k experiences after traversing all cities

Then three important formulas are given

Formula 1:

It can be seen from the formula that the pheromone factor is the index of pheromone concentration, and the heuristic function factor is the index of heuristic function, so it is easy to understand the role of these two parameters, which determine the contribution of pheromone concentration and transfer expectation to the possibility of ant k transferring from city i to City j.

Formula 2:

This formula reflects the iterative updating law of pheromone concentration. It can be seen that after all ants traverse all cities once, the current pheromone concentration consists of two parts. The first part is the residual pheromone on the path after all ants traverse all cities last time. The second part is the new increment of pheromone on each path after all ants traverse all cities this time.

Formula 3:

Formula 3 reflects the contribution of each ant to the pheromone concentration on the path between the cities it passes. It can be seen that the longer the distance it experiences, the lower the contribution to the pheromone concentration along the path. If the distance it tries to make is shorter, the higher the contribution to the pheromone concentration along the path. Combining with formula 2, it can be seen that the cumulative contribution of pheromone concentration The path with large contribution has a high probability of being selected, which is why the shortest path can be selected. There are some other models about the calculation of TSP, which will not be introduced in detail here. What we give here is ant cycle system model, which is also a commonly used model in TSP.

### The flow of basic ant algorithm

## 2. Application

### TSP

`clear; close all; clc; n = 31; x = rand(1,n)*100; % y = rand(1,n)*100; % D = zeros(n,n); for i = 1:n for j = 1:n if i~=j D(i,j) = sqrt(power(x(i)-x(j),2)+power(y(i)-y(j),2)); else D(i,j) = 1e-3; end end end m = 75; Alpha = 1; Beta = 5; Rho = 0.2; Q = 10; Heu_F = 1./D; Tau = ones(n,n); Table = zeros(m,n); iter = 1; iter_max = 500; % Route_best = zeros(iter_max,n); Length_best = zeros(iter_max,1); while iter <= iter_max start = zeros(m,1); for i = 1:m temp = randperm(n); start(i) = temp(1); end Table(:,1) = start; city_index = 1:n; for i = 1:m for j = 2:n has_visited = Table(i,1:(j-1)); % allow_index = ~ismember(city_index,has_visited); allow = city_index(allow_index); P = allow; for k = 1:length(allow) P(k) = Tau(has_visited(end),allow(k))^Alpha * Heu_F(has_visited(end),allow(k))^Beta; end P = P / sum(P); Pc = cumsum(P); target_index = find(Pc>=rand); target = allow(target_index(1)); Table(i,j) = target; end end Length = zeros(m,1); for i = 1:m Route = Table(i,:); for j = 1:(n-1) Length(i) = Length(i) + D(Route(j),Route(j+1)); end Length(i) = Length(i) + D(Route(n),Route(1)); end [min_Length,min_index] = min(Length); if iter > 1 if min_Length > Length_best(iter - 1) Length_best(iter) = Length_best(iter-1); Route_best(iter,:) = Route_best(iter-1,:); else Length_best(iter) = min_Length; Route_best(iter,:) = Table(min_index,:); end else Length_best(iter) = min_Length; Route_best(iter,:) = Table(min_index,:); end iter iter = iter + 1; Delta_Tau = zeros(n,n); for i = 1:m for j = 1:(n-1) Delta_Tau(Table(i,j),Table(i,j+1)) = Delta_Tau(Table(i,j),Table(i,j+1)) + Q/Length(i); end end Tau = (1-Rho)*Tau + Delta_Tau; Table = zeros(m,n); end plot(Length_best);`

### Function extremum

`clear; close all; clc; m = 20; Dimension = 2; iter_max = 500; Rho = 0.9; Q = 10; Alpha = 1; Belta = 5; Upper = [5;5]; Lower = [-5;-5]; X = zeros(m,Dimension); for i = 1:m X(i,:) = rand(2,1).*(Upper-Lower)+Lower; Tau(i) = func(X(i,:)); if Tau(i) == 0 Tau(i) = 1e-3; end end Heu_F = 1./Tau; step = 0.1; iter = 1; while iter <= iter_max P = zeros(m,1); for i = 1:m P(i) = P(i) + Tau(i)^Alpha * Heu_F(i)^Belta; end P = P / sum(P); Pc = cumsum(P); t = rand; jubu_index = find(Pc >= t); quanju_index = find(Pc < t); for i = 1:length(jubu_index) t = jubu_index(i); temp1 = X(i,1) + (2*rand-1)*step; temp2 = X(i,2) + (2*rand-1)*step; if temp1 > Upper(1) temp1 = Upper(1); end if temp1 < Lower(1) temp1 = Lower(1); end if temp2 > Upper(2) temp2 = Upper(2); end if temp2 < Lower(2) temp2 = Lower(2); end if func([temp1,temp2]) < func(X(t,:)) X(t,:) = [temp1,temp2] end end for i = 1:length(quanju_index) t = quanju_index(i); temp1 = rand*(Upper(1)-Lower(1))+Lower(1); temp2 = rand*(Upper(2)-Lower(2))+Lower(2); if temp1 > Upper(1) temp1 = Upper(1); end if temp1 < Lower(1) temp1 = Lower(1); end if temp2 > Upper(2) temp2 = Upper(2); end if temp2 < Lower(2) temp2 = Lower(2); end if func([temp1,temp2]) < func(X(t,:)) X(t,:) = [temp1,temp2] end end iter iter = iter + 1; for i = 1:m Tau(i) = (1-Rho)*Tau(i) + func(X(i,:)); end [Value,index] = min(Tau); trace(iter) = func(X(index,:)); end plot(trace) function results = func(x) results = 20*(x(1)^2-x(2)^2)^2 - (1-x(2))^2 -3*(1+x(2))^2 + 0.3; end`

### 01 Backpack

`clear; close all; clc; n = 50; V = 1000; u = [80 82 85 70 72 70 66 50 55 25 50 55 40 48 50 32 22 60 30 32 40 38 35 32 25 28 30 22 25 30 45 30 60 50 20 65 20 25 30 10 20 25 15 10 10 10 4 4 2 1]; p = [220 208 198 192 180 180 165 162 160 15 8 155 130 125 122 120 118 115 110 105 101 100 100 98 9 6 95 90 88 82 80 77 75 73 72 70 69 66 65 63 60 58 56 5 0 30 20 15 10 8 5 3 1 ]; Alpha = 0.7; Beta = 0.3; iter_max = 200; Q = 1; m = 10; Rho = 0.1; Tau = ones(1,n); Table = zeros(m,n); Route_best = zeros(iter_max,n); Value_best = zeros(iter_max,1); Heu_F = zeros(1,n); for i = 1:n Heu_F(i) = p(i) / u(i); end iter =1; while iter <= iter_max start = zeros(m,1); for i = 1:m temp = randperm(n); start(i) = temp(1); end Table(:,1) = start; city_index = 1:n; for i = 1:m for j = 2:n if Table(i,j-1) == 0 continue; end has_visited = Table(i,1:(j-1)); temp = 0; allow_index = ~ismember(city_index,has_visited); allow = city_index(allow_index); temp = 0; for l = 1:length(has_visited) temp = temp + u(has_visited(l)); end for l = 1:length(allow) if temp + u(allow(l)) > V allow(l) = 0; end end allow(allow == 0) = []; if isempty(allow) continue; end P = allow; for k = 1:length(allow) P(k) = (Tau(allow(k))^Alpha)*(Heu_F(allow(k))^Beta); end if P == 0 P = 0; else P = P / sum(P); Pc = cumsum(P); target_index = find(Pc >= rand); target = allow(target_index(1)); Table(i,j) = target; end end end Value = zeros(m,1); for i = 1:m Route = Table(i,:); for j = 1:n if Route(j) > 0 Value(i) = Value(i) + p(Route(j)); end end end [max_Value,max_index] = max(Value); if iter == 1 Value_best(iter) = max_Value; Route_best(iter,:) = Table(max_index,:); else if max_Value < Value_best(iter-1) Value_best(iter) = Value_best(iter-1); Route_best(iter,:) = Route_best(iter-1,:); else Value_best(iter) = max_Value; Route_best(iter,:) = Table(max_index,:); end end iter = iter + 1; Delta_Tau = zeros(n,n); for i = 1:m for j = 1:(n-1) if Table(i,j) > 0 && Table(i,j+1)>0 Delta_Tau(Table(i,j),Table(i,j+1)) = Delta_Tau(Table(i,j),Table(i,j+1)) + Q/Value(i); end end end Tau = (1-Rho)*Tau + Delta_Tau; Table = zeros(m,n); end plot(Value_best)`