码迷,mamicode.com
首页 > 编程语言 > 详细

【TSP】基于matlab GUI改进的遗传算法求解旅行商问题【含Matlab源码 926期】

时间:2021-06-20 18:49:44      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:sha   计算模型   use   pop   pairs   elf   until   ica   oca   

一、简介

1 遗传算法概述
遗传算法(Genetic Algorithm,GA)是进化计算的一部分,是模拟达尔文的遗传选择和自然淘汰的生物进化过程的计算模型,是一种通过模拟自然进化过程搜索最优解的方法。该算法简单、通用,鲁棒性强,适于并行处理。

2 遗传算法的特点和应用
遗传算法是一类可用于复杂系统优化的具有鲁棒性的搜索算法,与传统的优化算法相比,具有以下特点:
(1)以决策变量的编码作为运算对象。传统的优化算法往往直接利用决策变量的实际值本身来进行优化计算,但遗传算法是使用决策变量的某种形式的编码作为运算对象。这种对决策变量的编码处理方式,使得我们在优化计算中可借鉴生物学中染色体和基因等概念,可以模仿自然界中生物的遗传和进化激励,也可以很方便地应用遗传操作算子。
(2)直接以适应度作为搜索信息。传统的优化算法不仅需要利用目标函数值,而且搜索过程往往受目标函数的连续性约束,有可能还需要满足“目标函数的导数必须存在”的要求以确定搜索方向。遗传算法仅使用由目标函数值变换来的适应度函数值就可确定进一步的搜索范围,无需目标函数的导数值等其他辅助信息。直接利用目标函数值或个体适应度值也可以将搜索范围集中到适应度较高部分的搜索空间中,从而提高搜索效率。
(3)使用多个点的搜索信息,具有隐含并行性。传统的优化算法往往是从解空间的一个初始点开始最优解的迭代搜索过程。单个点所提供的搜索信息不多,所以搜索效率不高,还有可能陷入局部最优解而停滞;遗传算法从由很多个体组成的初始种群开始最优解的搜索过程,而不是从单个个体开始搜索。对初始群体进行的、选择、交叉、变异等运算,产生出新一代群体,其中包括了许多群体信息。这些信息可以避免搜索一些不必要的点,从而避免陷入局部最优,逐步逼近全局最优解。
(4) 使用概率搜索而非确定性规则。传统的优化算法往往使用确定性的搜索方法,一个搜索点到另一个搜索点的转移有确定的转移方向和转移关系,这种确定性可能使得搜索达不到最优店,限制了算法的应用范围。遗传算法是一种自适应搜索技术,其选择、交叉、变异等运算都是以一种概率方式进行的,增加了搜索过程的灵活性,而且能以较大概率收敛于最优解,具有较好的全局优化求解能力。但,交叉概率、变异概率等参数也会影响算法的搜索结果和搜索效率,所以如何选择遗传算法的参数在其应用中是一个比较重要的问题。
综上,由于遗传算法的整体搜索策略和优化搜索方式在计算时不依赖于梯度信息或其他辅助知识,只需要求解影响搜索方向的目标函数和相应的适应度函数,所以遗传算法提供了一种求解复杂系统问题的通用框架。它不依赖于问题的具体领域,对问题的种类有很强的鲁棒性,所以广泛应用于各种领域,包括:函数优化、组合优化生产调度问题、自动控制
、机器人学、图像处理(图像恢复、图像边缘特征提取......)、人工生命、遗传编程、机器学习。

3 遗传算法的基本流程及实现技术
基本遗传算法(Simple Genetic Algorithms,SGA)只使用选择算子、交叉算子和变异算子这三种遗传算子,进化过程简单,是其他遗传算法的基础。

3.1 遗传算法的基本流程
通过随机方式产生若干由确定长度(长度与待求解问题的精度有关)编码的初始群体;
通过适应度函数对每个个体进行评价,选择适应度值高的个体参与遗传操作,适应度低的个体被淘汰;
经遗传操作(复制、交叉、变异)的个体集合形成新一代种群,直到满足停止准则(进化代数GEN>=?);
将后代中变现最好的个体作为遗传算法的执行结果。
技术图片
其中,GEN是当前代数;M是种群规模,i代表种群数量。

3.2 遗传算法的实现技术
基本遗传算法(SGA)由编码、适应度函数、遗传算子(选择、交叉、变异)及运行参数组成。
3.2.1 编码
(1)二进制编码
二进制编码的字符串长度与问题所求解的精度有关。需要保证所求解空间内的每一个个体都可以被编码。
优点:编、解码操作简单,遗传、交叉便于实现
缺点:长度大
(2)其他编码方法
格雷码、浮点数编码、符号编码、多参数编码等
3.2.2 适应度函数
适应度函数要有效反映每一个染色体与问题的最优解染色体之间的差距。
3.2.3选择算子
技术图片
3.2.4 交叉算子
交叉运算是指对两个相互配对的染色体按某种方式相互交换其部分基因,从而形成两个新的个体;交叉运算是遗传算法区别于其他进化算法的重要特征,是产生新个体的主要方法。在交叉之前需要将群体中的个体进行配对,一般采取随机配对原则。
常用的交叉方式:
单点交叉
双点交叉(多点交叉,交叉点数越多,个体的结构被破坏的可能性越大,一般不采用多点交叉的方式)
均匀交叉
算术交叉
3.2.5 变异算子
遗传算法中的变异运算是指将个体染色体编码串中的某些基因座上的基因值用该基因座的其他等位基因来替换,从而形成一个新的个体。

就遗传算法运算过程中产生新个体的能力方面来说,交叉运算是产生新个体的主要方法,它决定了遗传算法的全局搜索能力;而变异运算只是产生新个体的辅助方法,但也是必不可少的一个运算步骤,它决定了遗传算法的局部搜索能力。交叉算子与变异算子的共同配合完成了其对搜索空间的全局搜索和局部搜索,从而使遗传算法能以良好的搜索性能完成最优化问题的寻优过程。

3.2.6 运行参数
技术图片
4 遗传算法的基本原理
4.1 模式定理
技术图片
4.2 积木块假设
具有低阶、定义长度短,且适应度值高于群体平均适应度值的模式称为基因块或积木块。
积木块假设:个体的基因块通过选择、交叉、变异等遗传算子的作用,能够相互拼接在一起,形成适应度更高的个体编码串。
积木块假设说明了用遗传算法求解各类问题的基本思想,即通过积木块直接相互拼接在一起能够产生更好的解。

二、源代码

function varargout = tsp_ga_gui(varargin)
% TSP_GA_GUI MATLAB code for tsp_ga_gui.fig
%      TSP_GA_GUI, by itself, creates a new TSP_GA_GUI or raises the existing
%      singleton*.
%
%      H = TSP_GA_GUI returns the handle to a new TSP_GA_GUI or the handle to
%      the existing singleton*.
%
%      TSP_GA_GUI(‘CALLBACK‘,hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TSP_GA_GUI.M with the given input arguments.
%
%      TSP_GA_GUI(‘Property‘,‘Value‘,...) creates a new TSP_GA_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before tsp_ga_gui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to tsp_ga_gui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE‘s Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help tsp_ga_gui

% Last Modified by GUIDE v2.5 25-Feb-2021 15:15:58

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct(‘gui_Name‘,       mfilename, ...
                   ‘gui_Singleton‘,  gui_Singleton, ...
                   ‘gui_OpeningFcn‘, @tsp_ga_gui_OpeningFcn, ...
                   ‘gui_OutputFcn‘,  @tsp_ga_gui_OutputFcn, ...
                   ‘gui_LayoutFcn‘,  [] , ...
                   ‘gui_Callback‘,   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before tsp_ga_gui is made visible.
function tsp_ga_gui_OpeningFcn(hObject, eventdata, handles, varargin)
global hnds
global r nn dsm asz G
global startf



% Choose default command line output for tsp_ga_gui
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes tsp_ga_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);

hnds=handles;

startf=false; % start flag

asz=10; % area size   asx x asz
nn=str2num(get(handles.nn,‘string‘)); % number of cities
ps=str2num(get(handles.ps,‘string‘)); % population size

r=asz*rand(2,nn); % randomly distribute cities
% r(1,:) -x coordinaties of cities
% r(2,:) -y coordinaties of cities

dsm=zeros(nn,nn); % matrix of distancies
for n1=1:nn-1
    r1=r(:,n1);
    for n2=n1+1:nn
        r2=r(:,n2);
        dr=r1-r2;
        dr2=dr‘*dr;
        drl=sqrt(dr2);
        dsm(n1,n2)=drl;
        dsm(n2,n1)=drl;
    end
end

% start from random closed pathes:
G=zeros(ps,nn); % genes, G(i,:) - gene of i-path, G(i,:) is row-vector with cities number in the path
for psc=1:ps
    G(psc,:)=randperm(nn);
end

update_plots;

% --- Outputs from this function are returned to the command line.
function varargout = tsp_ga_gui_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in randomize.
function randomize_Callback(hObject, eventdata, handles)
global r nn dsm asz

nn=str2num(get(handles.nn,‘string‘)); % number of cities

r=asz*rand(2,nn); % randomly distribute cities
% r(1,:) -x coordinaties of cities
% r(2,:) -y coordinaties of cities

dsm=zeros(nn,nn); % matrix of distancies
for n1=1:nn-1
    r1=r(:,n1);
    for n2=n1+1:nn
        r2=r(:,n2);
        dr=r1-r2;
        dr2=dr‘*dr;
        drl=sqrt(dr2);
        dsm(n1,n2)=drl;
        dsm(n2,n1)=drl;
    end
end


update_plots;


% --- Executes on button press in circle.
function circle_Callback(hObject, eventdata, handles)
global r nn dsm asz

nn=str2num(get(handles.nn,‘string‘)); % number of cities

r=zeros(2,nn);

% circle
al1=linspace(0,2*pi,nn+1);
al=al1(1:end-1);
r(1,:)=0.5*asz+0.45*asz*cos(al);
r(2,:)=0.5*asz+0.45*asz*sin(al);

% r(1,:) -x coordinaties of cities
% r(2,:) -y coordinaties of cities

dsm=zeros(nn,nn); % matrix of distancies
for n1=1:nn-1
    r1=r(:,n1);
    for n2=n1+1:nn
        r2=r(:,n2);
        dr=r1-r2;
        dr2=dr‘*dr;
        drl=sqrt(dr2);
        dsm(n1,n2)=drl;
        dsm(n2,n1)=drl;
    end
end


update_plots;



function nn_Callback(hObject, eventdata, handles)
update_plots_nn_ps;


% --- Executes during object creation, after setting all properties.
function nn_CreateFcn(hObject, eventdata, handles)
% hObject    handle to nn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,‘BackgroundColor‘), get(0,‘defaultUicontrolBackgroundColor‘))
    set(hObject,‘BackgroundColor‘,‘white‘);
end



function ps_Callback(hObject, eventdata, handles)
update_plots_nn_ps;

% --- Executes during object creation, after setting all properties.
function ps_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ps (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,‘BackgroundColor‘), get(0,‘defaultUicontrolBackgroundColor‘))
    set(hObject,‘BackgroundColor‘,‘white‘);
end

三、运行结果

技术图片
技术图片

四、备注

版本:2014a
完整代码或代写加1564658423

【TSP】基于matlab GUI改进的遗传算法求解旅行商问题【含Matlab源码 926期】

标签:sha   计算模型   use   pop   pairs   elf   until   ica   oca   

原文地址:https://www.cnblogs.com/homeofmatlab/p/14906978.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!