[TSP problem] Solving TSP problem based on firefly algorithm [Matlab Issue 141] [Path Planning 24]

1. Background

Problem definition: Itinerant traveling salesman problem.
Given a set of n cities and the direct distance between them, find a closed journey so that each city passes through once and the total travel distance is the shortest.
The TSP problem, also known as the salesman burden problem, is an old problem. It can be traced back to the question of knight travel raised by Euler in 1759. In 1948, promoted by the American RAND Corporation, TSP became a typical problem in the field of modern portfolio optimization.
TSP is a combinatorial optimization problem with extensive application background and important theoretical value. In recent years, many more effective algorithms to solve this problem have been continuously introduced, such as the Hopfield neural network method, the simulated annealing method and the genetic algorithm method.
The TSP search space increases as the number of cities n increases, and the number of combinations of all journey routes is (n-1)!/2. To find the optimal solution in such a huge search space, there are many calculation difficulties for conventional methods and existing calculation tools. It is a natural idea to solve the TSP problem with the help of the search ability of genetic algorithm.

2. Overview

The Firefly Algorithm is a heuristic algorithm inspired by the blinking behavior of fireflies. The main purpose of the flash of fireflies is to act as a signal system to attract other fireflies.
The hypothesis is:
fireflies are gender-neutral, so that one firefly will attract all other fireflies; attraction is proportional to their brightness. For any two fireflies, the less bright fireflies are attracted and therefore move to the brighter one. One, however, the brightness decreases as its distance increases; if there is no firefly brighter than a given firefly, it will move randomly. The brightness should be related to the objective function. The Firefly algorithm is a heuristic optimization algorithm inspired by nature.
Insert picture description here
Insert picture description here

Three, source code

clear;
clc;
close all;
X=[16.47,96.10
    16.47,94.44
    20.09,92.54
    22.39,93.37
    25.23,97.24
    22.00,96.05
    20.47,97.02
    17.20,96.29
    16.30,97.38
    14.05,98.12
    16.53,97.38
    21.52,95.59
    19.41,97.13
    20.09,92.55];
R=11;
MAXGEN=200;
NIND=100;
D=Distanse(X);
N=size(D,1);
%%初始化种群
Chrom=InitPop(NIND,N);
%%在二维图上画出所有坐标点
figure
plot(X(:,1),X(:,2),'o');
%%画出随机解的路线图
DrawPath(Chrom(1,:),X);
pause(0.0001)
%%输出随机解的路线和总距离
disp('初始种群中的一个随机解:')
OutputPath(Chrom(1,:));
Rlength=PathLength(D,Chrom(1,:));
disp(['总距离:',num2str(Rlength)]);
disp('-------------------------------------------------------------------------------------------------')
%%优化
gen=0;
figure;
hold on;box on
xlim([0,MAXGEN])
title('优化过程')
xlabel('代数')
ylabel('最优值')
ObjV=PathLength(D,Chrom);                           %计算路线长度title('优化过程')xlable('代数')ylable('最优值')
preObjV=min(ObjV);
while gen<MAXGEN
    %%计算适应度
    ObjV=PathLength(D,Chrom);                        %计算路线长度
    %fprintf('%d    %1.10f\n',gen,min(ObjV))
    line([gen-1,gen],[preObjV,min(ObjV)]);pause(0.0001)
    preObjV=min(ObjV);
    FitnV=Fitness(ObjV);
    for i=1:NIND
        K=0;
        subject=zeros(NIND,N);
        for j=1:i-1
            dij=ristanse(Chrom(i,:),Chrom(j,:));
            if dij<=R
                K=K+1;
                subject(K,:)=Chrom(j,:);
            end
        end
        for j=i+1:NIND
            dij=ristanse(Chrom(i,:),Chrom(j,:));
             if dij<=R
                K=K+1;
                subject(K,:)=Chrom(j,:);
             end
        end
        if K==0
        subject1=zeros(1,N);
        else subject1=zeros(K,N);
        end
 
        end
        end
    end
     gen=gen+1;
end
%%画出最优解的路线图
ObjV=PathLength(D,Chrom);
[minObjV,minInd]=min(ObjV);
DrawPath(Chrom(minInd(1),:),X)
%%输出最优解的路线和总距离
disp('最优解')
p=OutputPath(Chrom(minInd(1),:));
disp(['总距离:',num2str(ObjV(minInd(1)))]);
disp('-------------------------------------------------------------------')

Four, operation effect

Insert picture description here
Note: Add QQ1564658423 for complete code or writing.
Past review>>>>>>
[Matlab 015] [Path planning 1] 3D UAV path planning based on particle swarm matlab source code
[Matlab 017] [Path planning 2] Using genetic algorithms to prepare the opening of multiple logistics centers -Style vehicle routing problem Matlab program
[Matlab 018] [Path planning 3] Robot grid path planning based on particle swarms
[Matlab 019] [Path planning 4] Ant colony algorithm to solve the shortest path matlab
[Matlab 020] [Path planning 5] Matlab immune algorithm for logistics center location problem
[Matlab 021] [Path planning 6] Three-dimensional path planning of drones based on artificial bee colony
[Matlab 027] [Path planning 7] Based on grid map-genetic algorithm Robot optimal path planning
[Matlab 034 period] [Path planning 8] Ant colony-based multi-UAV attack scheduling
based on grid map-genetic algorithm for robot optimal path planning [Matlab 022 period] [Path planning 9]
Multi-UAV cooperative target allocation modeling and genetic algorithm solution considering allocation order [Matlab 110] [Path planning 10]
Ant colony algorithm for multi-center vrp problem matlab [Matlab 111] [Path planning 11]
Based on ant colony algorithm Solving multi-center VRP problem with time windows matlab [Matlab 112] [Path planning 12]
Based on ant colony algorithm to solve multi-center VRP problems with time window matlab [Matlab 113] [Path planning 13]
Multi-center based on genetic algorithm VRP solution matlab【Matlab 114】【Path planning 14】
Simulated annealing to solve the VRP problem matlab [Matlab 115] [Path planning 15]
A star-based grid path planning [Matlab 116] [Path planning 16]
A bidirectional optimization particle swarm grid map path planning with cross factors [Matlab 117] [Path planning 17]
[TSP] Ant colony algorithm for solving TSP problems matlab with GUI [Matlab 118] [Path planning 18]
Based on ant colony algorithm grid map path planning matlab [Matlab 119] [Path planning 19]
Traveling salesman TSP problem based on genetic algorithm [Matlab 135] [Path planning 20]
Traveling salesman TSP problem based on simulated annealing algorithm [Matlab 136] [Path planning 21]
Smart car path planning based on ant colony algorithm [Matlab Issue 137] [Path Planning 22]
Huawei Cup: Optimal Use of UAVs in Rescue and Disaster Relief [Matlab Issue 138] [Path Planning 23]

Guess you like

Origin blog.csdn.net/TIQCmatlab/article/details/112799340