遗传算法求多旅行商问题

**

遗传算法求解多旅行商问题

**

由单个起点回到该起点

距离矩阵.txt
0 0.253811887383951 0.242535043507850 0.512925698596292 0.414928487147296 0.396590526618182 0.878630770364692 0.652554912507814 0.626783228597083 0.535474336304776 0.536909612456887 0.952569801696550 0.758701362514837 0.780215349463893 1.24901423356044 1.03930111103319 0.953702663110730 0.409252062102601 0.861125692919666 0.803865530824578 0.515668655909866 0.671187770843502 0.793510628384868 1.00282353086678 1.22243047384930 1.35789479912949 1.48471872590703 1.47892426664312 1.05199317954197 1.11135862809190
0.253811887383951 0 0.177186610855972 0.745795918739373 0.659929736280264 0.517356010343794 0.645619791860937 0.398800498608444 0.480994966311193 0.305688599497018 0.543465884492549 0.760622578479291 0.666273933677185 0.811643916197344 1.05169694033049 0.910368042013103 0.925093175172293 0.432428732106602 0.669273022552701 0.732010739838164 0.384202357133464 0.852937827383459 1.02841587149239 1.20500009080318 1.45806165184153 1.20030864257733 1.42162152785310 1.42902104432511 1.30508692057101 1.15169262617798
0.242535043507850 0.177186610855972 0 0.636986348990561 0.645644234279461 0.352340395992675 0.659892347057167 0.495009660145279 0.384792678085549 0.326854372241941 0.369691976350368 0.711917568686030 0.532654761305047 0.636746539676047 1.00847124475999 0.803069214446578 0.765106587544341 0.568648062568089 0.839468970704627 0.902807121156356 0.560133489386443 0.911202676349650 1.02859747834386 1.24535450113960 1.45522265009438 1.37538157072187 1.59357469804042 1.27981874169819 1.25431970895813 1.29292888386720
0.512925698596292 0.745795918739373 0.636986348990561 0 0.386074405729105 0.386604553817163 1.27818847546264 1.12761084145325 0.916813215994415 0.961124708267124 0.629081634174042 1.26362250730732 0.930945350548504 0.717651685120957 1.54617041266408 1.22291952434512 0.963780269149043 0.840384025718279 1.37201297537444 1.25528670346692 1.01706460269490 0.770348169548293 0.644166648494074 0.957010592292766 0.996902253625886 1.85099509914277 1.89603241463489 1.45802369465411 0.684395940309548 1.41241171180371
0.414928487147296 0.659929736280264 0.645644234279461 0.386074405729105 0 0.603189465101538 1.29354221060054 1.05466240527238 1.01697065615116 0.950387813507750 0.831958987391791 1.35570296826868 1.10848061474488 1.00788173702373 1.65189155154449 1.40249865378073 1.23144954213924 0.547713341844000 1.15399367854321 0.943424654679306 0.787056875755586 0.389035180614302 0.383814541877341 0.638169572760469 0.809647026715821 1.57026397701526 1.54163065698288 1.75223167462468 0.658208831549288 1.03100798715949
0.396590526618182 0.517356010343794 0.352340395992675 0.386604553817163 0.603189465101538 0 0.921326524593819 0.832383279628023 0.538421373926706 0.635944472597480 0.246294707626871 0.881425060716203 0.546464679889532 0.411139631669009 1.16054271052221 0.840916172968845 0.628306722710920 0.805827160641161 1.18660740303932 1.19447638186151 0.879498576456392 0.967986903222918 0.959741489542351 1.24043256028508 1.35361066296092 1.71418396311366 1.87959342544112 1.15001511137726 1.06873500573507 1.49669207024472
0.878630770364692 0.645619791860937 0.659892347057167 1.27818847546264 1.29354221060054 0.921326524593819 0 0.335762090187427 0.407573616259723 0.343497781964380 0.748838838180556 0.261512962879987 0.565523504689343 0.932335337349007 0.463995988440015 0.572364200075994 0.863474859754117 1.01553674922038 0.801214922797599 1.13218626287940 0.814171137474314 1.49243322428276 1.67009303475586 1.84839494945239 2.09974639865356 1.29913545384161 1.73346996217164 1.19606641190698 1.91419842358840 1.66014133311071
0.652554912507814 0.398800498608444 0.495009660145279 1.12761084145325 1.05466240527238 0.832383279628023 0.335762090187427 0 0.477849075869788 0.215373250850705 0.745194385874832 0.556994425339938 0.695060502394917 0.990278043255984 0.798530711325024 0.822252823607518 1.00574691851699 0.697516799493143 0.507649439465737 0.799055089299018 0.479619972440451 1.19758037821407 1.41332588203584 1.55811854584556 1.84091598329789 1.04375483592875 1.42254320058368 1.42953826416381 1.70363902947221 1.32441575457509
0.626783228597083 0.480994966311193 0.384792678085549 0.916813215994415 1.01697065615116 0.538421373926706 0.407573616259723 0.477849075869788 0 0.278187943848748 0.342454125455535 0.348460960302249 0.219603953779853 0.536780156584609 0.639630030059290 0.429402481193841 0.528054205249667 0.913392400545545 0.970911931102324 1.17207323705722 0.815043780663036 1.29579875482518 1.40056220768470 1.62893691269791 1.82193624462406 1.51577414619392 1.84401693056883 0.979343236233052 1.58442298186216 1.62939630884483
0.535474336304776 0.305688599497018 0.326854372241941 0.961124708267124 0.950387813507750 0.635944472597480 0.343497781964380 0.215373250850705 0.278187943848748 0 0.530611708524951 0.467701124006709 0.497675660676607 0.775327099258353 0.751635803735627 0.672065300799685 0.803071912685886 0.707440972701354 0.693106943914202 0.911446153156064 0.559395776818548 1.15805212121776 1.32662766641792 1.51068030836783 1.75625850088501 1.23903910318722 1.57258512345310 1.25345748941885 1.57727930230008 1.39773088969667
0.536909612456887 0.543465884492549 0.369691976350368 0.629081634174042 0.831958987391791 0.246294707626871 0.748838838180556 0.745194385874832 0.342454125455535 0.530611708524951 0 0.662412746663747 0.301916357686213 0.268484418810363 0.928797747209684 0.594885097137343 0.417528899181838 0.924791515269031 1.17860745385307 1.27249792784532 0.927632820323918 1.17643145606007 1.19932203633939 1.46964804082456 1.59861950435994 1.72380448415337 1.96325437805932 0.944130373966284 1.31310251980213 1.64338378397910
0.952569801696550 0.760622578479291 0.711917568686030 1.26362250730732 1.35570296826868 0.881425060716203 0.261512962879987 0.556994425339938 0.348460960302249 0.467701124006709 0.662412746663747 0 0.400694287801145 0.779667761748195 0.296556446355143 0.317375711608068 0.656516703495341 1.17504285775933 1.05059980134542 1.35104860267543 1.01237188121672 1.60878828051747 1.73938610148668 1.95288967711441 2.16394330095167 1.55889916080941 1.97730991656455 0.938487578935088 1.93286767645972 1.85877887515436
0.758701362514837 0.666273933677185 0.532654761305047 0.930945350548504 1.10848061474488 0.546464679889532 0.565523504689343 0.695060502394917 0.219603953779853 0.497675660676607 0.301916357686213 0.400694287801145 0 0.379787574835485 0.638287718037815 0.296356953937987 0.312269899891039 1.09387106793838 1.19051582132443 1.37999138331647 1.02254510357287 1.42642568296535 1.48487874732097 1.74024802096226 1.89233474932970 1.73514890916545 2.05764786136445 0.767650662799213 1.61474029237183 1.81695332916564
0.780215349463893 0.811643916197344 0.636746539676047 0.717651685120957 1.00788173702373 0.411139631669009 0.932335337349007 0.990278043255984 0.536780156584609 0.775327099258353 0.268484418810363 0.779667761748195 0.379787574835485 0 0.994706459681419 0.596106848390079 0.250734128193871 1.18080561759649 1.44246038577804 1.53888092333947 1.19584594045860 1.37872637622921 1.34601478610949 1.64038520289317 1.71455297354233 1.98904071343676 2.22979736745377 0.746970151266463 1.38282596277622 1.89154479517240
1.24901423356044 1.05169694033049 1.00847124475999 1.54617041266408 1.65189155154449 1.16054271052221 0.463995988440015 0.798530711325024 0.639630030059290 0.751635803735627 0.928797747209684 0.296556446355143 0.638287718037815 0.994706459681419 0 0.414899258669753 0.814811238699875 1.45654281239925 1.25363814377507 1.59617386122303 1.27351870610235 1.90270046879930 2.03564520299005 2.24855098981110 2.45960507841371 1.71981031311017 2.18726290115220 0.928225532280122 2.22136747463655 2.12128580951920
1.03930111103319 0.910368042013103 0.803069214446578 1.22291952434512 1.40249865378073 0.840916172968845 0.572364200075994 0.822252823607518 0.429402481193841 0.672065300799685 0.594885097137343 0.317375711608068 0.296356953937987 0.596106848390079 0.414899258669753 0 0.400109402007058 1.34271113655140 1.32919028501419 1.58349541224948 1.23005082362811 1.71027922857669 1.78038582441697 2.03122151616660 2.18869146000535 1.85454829913983 2.23872593945019 0.623781663539243 1.90730276221323 2.05657547206924
0.953702663110730 0.925093175172293 0.765106587544341 0.963780269149043 1.23144954213924 0.628306722710920 0.863474859754117 1.00574691851699 0.528054205249667 0.803071912685886 0.417528899181838 0.656516703495341 0.312269899891039 0.250734128193871 0.814811238699875 0.400109402007058 0 1.33226754608361 1.49550492784515 1.65501067032889 1.30033568947974 1.58909029972355 1.58259173402459 1.86863531095303 1.95993121864551 2.04209047233874 2.34099702579344 0.526985596729155 1.63316544734187 2.05491098650794
0.409252062102601 0.432428732106602 0.568648062568089 0.840384025718279 0.547713341844000 0.805827160641161 1.01553674922038 0.697516799493143 0.913392400545545 0.707440972701354 0.924791515269031 1.17504285775933 1.09387106793838 1.18080561759649 1.45654281239925 1.34271113655140 1.33226754608361 0 0.624023740784863 0.415046096843557 0.273169193386185 0.522428136393275 0.806759025952159 0.881186233416665 1.20965424711041 1.02519700875516 1.07932962003922 1.84839697234199 1.17868246708681 0.724455321723777
0.861125692919666 0.669273022552701 0.839468970704627 1.37201297537444 1.15399367854321 1.18660740303932 0.801214922797599 0.507649439465737 0.970911931102324 0.693106943914202 1.17860745385307 1.05059980134542 1.19051582132443 1.44246038577804 1.25363814377507 1.32919028501419 1.49550492784515 0.624023740784863 0 0.429310610550151 0.367206471320024 1.12451504389132 1.42965422194989 1.46748954359623 1.82264750091251 0.547974538384343 0.933732074470715 1.93641633368025 1.79998642520639 0.976944113450559
0.803865530824578 0.732010739838164 0.902807121156356 1.25528670346692 0.943424654679306 1.19447638186151 1.13218626287940 0.799055089299018 1.17207323705722 0.911446153156064 1.27249792784532 1.35104860267543 1.37999138331647 1.53888092333947 1.59617386122303 1.58349541224948 1.65501067032889 0.415046096843557 0.429310610550151 0 0.357488991322594 0.789359424277861 1.12814819292407 1.09857142361296 1.47971496114572 0.637405971349118 0.690918513838625 2.14763821184370 1.53193220639582 0.552278649397433
0.515668655909866 0.384202357133464 0.560133489386443 1.01706460269490 0.787056875755586 0.879498576456392 0.814171137474314 0.479619972440451 0.815043780663036 0.559395776818548 0.927632820323918 1.01237188121672 1.02254510357287 1.19584594045860 1.27351870610235 1.23005082362811 1.30033568947974 0.273169193386185 0.367206471320024 0.357488991322594 0 0.794076832077647 1.07651106558538 1.15050562755660 1.48271256209378 0.842533632780032 1.04084982764590 1.79017957459317 1.43605274105727 0.848125660176736
0.671187770843502 0.852937827383459 0.911202676349650 0.770348169548293 0.389035180614302 0.967986903222918 1.49243322428276 1.19758037821407 1.29579875482518 1.15805212121776 1.17643145606007 1.60878828051747 1.42642568296535 1.37872637622921 1.90270046879930 1.71027922857669 1.58909029972355 0.522428136393275 1.12451504389132 0.789359424277861 0.794076832077647 0 0.349126897212138 0.361219754043311 0.699130652791164 1.42098555083130 1.26208883304475 2.11499417349911 0.765994934756544 0.690501374189846
0.793510628384868 1.02841587149239 1.02859747834386 0.644166648494074 0.383814541877341 0.959741489542351 1.67009303475586 1.41332588203584 1.40056220768470 1.32662766641792 1.19932203633939 1.73938610148668 1.48487874732097 1.34601478610949 2.03564520299005 1.78038582441697 1.58259173402459 0.806759025952159 1.42965422194989 1.12814819292407 1.07651106558538 0.349126897212138 0 0.323350312865288 0.429768299866242 1.76384131469586 1.60474720178243 2.09248686244487 0.417834433217053 1.01512713772324
1.00282353086678 1.20500009080318 1.24535450113960 0.957010592292766 0.638169572760469 1.24043256028508 1.84839494945239 1.55811854584556 1.62893691269791 1.51068030836783 1.46964804082456 1.95288967711441 1.74024802096226 1.64038520289317 2.24855098981110 2.03122151616660 1.86863531095303 0.881186233416665 1.46748954359623 1.09857142361296 1.15050562755660 0.361219754043311 0.323350312865288 0 0.401341655863480 1.70665931093987 1.44412704325761 2.38684642201828 0.646549431316815 0.830219329009727
1.22243047384930 1.45806165184153 1.45522265009438 0.996902253625886 0.809647026715821 1.35361066296092 2.09974639865356 1.84091598329789 1.82193624462406 1.75625850088501 1.59861950435994 2.16394330095167 1.89233474932970 1.71455297354233 2.45960507841371 2.18869146000535 1.95993121864551 1.20965424711041 1.82264750091251 1.47971496114572 1.48271256209378 0.699130652791164 0.429768299866242 0.401341655863480 0 2.09928339399997 1.84363805500027 2.45202317685808 0.413602501826177 1.22893428769451
1.35789479912949 1.20030864257733 1.37538157072187 1.85099509914277 1.57026397701526 1.71418396311366 1.29913545384161 1.04375483592875 1.51577414619392 1.23903910318722 1.72380448415337 1.55889916080941 1.73514890916545 1.98904071343676 1.71981031311017 1.85454829913983 2.04209047233874 1.02519700875516 0.547974538384343 0.637405971349118 0.842533632780032 1.42098555083130 1.76384131469586 1.70665931093987 2.09928339399997 0 0.592426401396805 2.47075085374891 2.16931580951361 0.980438325532062
1.48471872590703 1.42162152785310 1.59357469804042 1.89603241463489 1.54163065698288 1.87959342544112 1.73346996217164 1.42254320058368 1.84401693056883 1.57258512345310 1.96325437805932 1.97730991656455 2.05764786136445 2.22979736745377 2.18726290115220 2.23872593945019 2.34099702579344 1.07932962003922 0.933732074470715 0.690918513838625 1.04084982764590 1.26208883304475 1.60474720178243 1.44412704325761 1.84363805500027 0.592426401396805 0 2.82333076536943 2.02177227636457 0.614868849111616
1.47892426664312 1.42902104432511 1.27981874169819 1.45802369465411 1.75223167462468 1.15001511137726 1.19606641190698 1.42953826416381 0.979343236233052 1.25345748941885 0.944130373966284 0.938487578935088 0.767650662799213 0.746970151266463 0.928225532280122 0.623781663539243 0.526985596729155 1.84839697234199 1.93641633368025 2.14763821184370 1.79017957459317 2.11499417349911 2.09248686244487 2.38684642201828 2.45202317685808 2.47075085374891 2.82333076536943 0 2.09988112978841 2.57274759379890
1.05199317954197 1.30508692057101 1.25431970895813 0.684395940309548 0.658208831549288 1.06873500573507 1.91419842358840 1.70363902947221 1.58442298186216 1.57727930230008 1.31310251980213 1.93286767645972 1.61474029237183 1.38282596277622 2.22136747463655 1.90730276221323 1.63316544734187 1.17868246708681 1.79998642520639 1.53193220639582 1.43605274105727 0.765994934756544 0.417834433217053 0.646549431316815 0.413602501826177 2.16931580951361 2.02177227636457 2.09988112978841 0 1.42665146708738
1.11135862809190 1.15169262617798 1.29292888386720 1.41241171180371 1.03100798715949 1.49669207024472 1.66014133311071 1.32441575457509 1.62939630884483 1.39773088969667 1.64338378397910 1.85877887515436 1.81695332916564 1.89154479517240 2.12128580951920 2.05657547206924 2.05491098650794 0.724455321723777 0.976944113450559 0.552278649397433 0.848125660176736 0.690501374189846 1.01512713772324 0.830219329009727 1.22893428769451 0.980438325532062 0.614868849111616 2.57274759379890 1.42665146708738 0

经纬度矩阵.txt

120.7015202 36.37423
120.6987175 36.37457569
120.6997954 36.37591239
120.70691 36.37579616
120.7056165 36.37248342
120.7031731 36.37753964
120.6928965 36.37800457
120.6943337 36.37521499
120.6973521 36.37876006
120.6962022 36.37643544
120.7011609 36.37905063
120.6939026 36.38021291
120.6983582 36.38056159
120.7025263 36.38120084
120.6914592 36.38201441
120.6960585 36.38247931
120.7005141 36.38276987
120.6998673 36.37079794
120.6928965 36.37079794
120.6964897 36.36824059
120.6969209 36.37143727
120.7052571 36.36899618
120.7088504 36.37021674
120.7087066 36.36731063
120.7130185 36.36829872
120.6896626 36.36661314
120.6937588 36.36242812
120.6993643 36.38741865
120.7129466 36.37201847
120.7002266 36.36428816

链接: link.

遗传算法matlab程序

% Author: Joseph Kirk
% Email: jdkirk630@gmail.com
% Release: 2.0
% Release Date: 05/01/2014
function varargout = mtspf_ga(varargin)
    % Initialize default configuration
    defaultConfig.xy          =load('经纬度.txt');
    defaultConfig.dmat        = load('距离矩阵.txt');  % N*N距离矩阵
    defaultConfig.nSalesmen   = 4;
    defaultConfig.minTour     = 4;
    defaultConfig.popSize     = 50;
    defaultConfig.numIter     = 6e3;
    defaultConfig.showProg    = true;
    defaultConfig.showResult  = true;
    defaultConfig.showWaitbar = false;   

    % Interpret user configuration(配置) inputs即对输入的数据的配置
    if ~nargin  %输入参数个数
        userConfig = struct();
    elseif isstruct(varargin{1})
        userConfig = varargin{1};
    else
        try
            userConfig = struct(varargin{:});
        catch
            error('Expected inputs are either a structure or parameter/value pairs');
        end
    end

    % Override(自动控制暂时失效) default(系统设定值) configuration(配置) with user inputs
    configStruct = get_config(defaultConfig,userConfig);

    % Extract(选取 configuration
    xy          = configStruct.xy;
    dmat        = configStruct.dmat;
    nSalesmen   = configStruct.nSalesmen;
    minTour     = configStruct.minTour;
    popSize     = configStruct.popSize;
    numIter     = configStruct.numIter;
    showProg    = configStruct.showProg;
    showResult  = configStruct.showResult;
    showWaitbar = configStruct.showWaitbar;
    %对输入数据的验证
    if isempty(dmat)
        nPoints = size(xy,1);
        a = meshgrid(1:nPoints);
        dmat = reshape(sqrt(sum((xy(a,:)-xy(a',:)).^2,2)),nPoints,nPoints);
    end

    % Verify Inputs 验证输入
    [N,dims] = size(xy);
    [nr,nc] = size(dmat);
    if N ~= nr || N ~= nc
        error('Invalid XY or DMAT inputs!')
    end
    n = N - 1; % Separate Start/End City

    % Sanity Checks(完整性检查)
    nSalesmen   = max(1,min(n,round(real(nSalesmen(1)))));
    minTour     = max(1,min(floor(n/nSalesmen),round(real(minTour(1)))));
    popSize     = max(8,8*ceil(popSize(1)/8));
    numIter     = max(1,round(real(numIter(1))));
    showProg    = logical(showProg(1));
    showResult  = logical(showResult(1));
    showWaitbar = logical(showWaitbar(1));

%%%%%%%%%%不懂%%%%%%%%%%    
    % Initializations for Route Break Point Selection 路径断点选择的初始化
    nBreaks = nSalesmen-1;
    dof = n - minTour*nSalesmen;          % degrees of freedom
    addto = ones(1,dof+1);
    for k = 2:nBreaks
        addto = cumsum(addto);
    end
    cumProb = cumsum(addto)/sum(addto);
%%%%%%%%%%%%%不懂%%%%%%%%%%%
    % Initialize初始化 the Populations
    popRoute = zeros(popSize,n);         % population of routes
    popBreak = zeros(popSize,nBreaks);   % population of breaks
    popRoute(1,:) = (1:n) + 1;
    popBreak(1,:) = rand_breaks();
    for k = 2:popSize
        popRoute(k,:) = randperm(n) + 1;   %randperm返回从1:n的无重复的随机数
        popBreak(k,:) = rand_breaks();     
    end

    % Select the Colors for the Plotted Routes    所画路径的颜色
    pclr = ~get(0,'DefaultAxesColor');
    clr = [1 0 0; 0 0 1; 0.67 0 1; 0 1 0; 1 0.5 0];
    if nSalesmen > 5
        clr = hsv(nSalesmen);
    end

    % Run the GA
    globalMin = Inf;
    totalDist = zeros(1,popSize);
    distHistory = zeros(1,numIter);
    tmpPopRoute = zeros(8,n);
    tmpPopBreak = zeros(8,nBreaks);
    newPopRoute = zeros(popSize,n);
    newPopBreak = zeros(popSize,nBreaks);
    if showProg
        figure('Name','MTSPF_GA | Current Best Solution','Numbertitle','off');
        hAx = gca;
    end
    if showWaitbar
        hWait = waitbar(0,'Searching for near-optimal solution ...');
    end
    for iter = 1:numIter
    % Evaluate Members of the Population    人口评估
        for p = 1:popSize
            d = 0;
            pRoute = popRoute(p,:);
            pBreak = popBreak(p,:);
            rng = [[1 pBreak+1];[pBreak n]]';
            for s = 1:nSalesmen
                d = d + dmat(1,pRoute(rng(s,1))); % Add Start Distance
                for k = rng(s,1):rng(s,2)-1
                    d = d + dmat(pRoute(k),pRoute(k+1));
                end
                d = d + dmat(pRoute(rng(s,2)),1); % Add End Distance
            end
            totalDist(p) = d;
        end

     % Find the Best Route in the Population
        [minDist,index] = min(totalDist);
        distHistory(iter) = minDist;
        if minDist < globalMin
            globalMin = minDist;
            optRoute = popRoute(index,:);
            optBreak = popBreak(index,:);
            rng = [[1 optBreak+1];[optBreak n]]';
            if showProg
     % Plot the Best Route   实时展示最优路径
                for s = 1:nSalesmen
                    rte = [1 optRoute(rng(s,1):rng(s,2)) 1];
                    if dims > 2, plot3(hAx,xy(rte,1),xy(rte,2),xy(rte,3),'.-','Color',clr(s,:));
                    else
                        plot(hAx,xy(rte,1),xy(rte,2),'.-','Color',clr(s,:)); 
                    end
                    hold(hAx,'on');
                end
                if dims > 2, plot3(hAx,xy(1,1),xy(1,2),xy(1,3),'o','Color',pclr);
                else
                    plot(hAx,xy(1,1),xy(1,2),'o','Color',pclr); 
                end
                title(hAx,sprintf('最短总路程= %1.4f',minDist));%iterion=%d,,iter
                for i=1:size(xy,1) %用这个循环
                    text(xy(i,1),xy(i,2),['',num2str(i-1)]);
                end
                hold(hAx,'off');
                drawnow;
                
            end
        end

      % Genetic Algorithm Operators
        randomOrder = randperm(popSize);
        for p = 8:8:popSize
            rtes = popRoute(randomOrder(p-7:p),:);
            brks = popBreak(randomOrder(p-7:p),:);
            dists = totalDist(randomOrder(p-7:p));
            [ignore,idx] = min(dists); %#ok
            bestOf8Route = rtes(idx,:);
            bestOf8Break = brks(idx,:);
            routeInsertionPoints = sort(ceil(n*rand(1,2)));
            I = routeInsertionPoints(1);
            J = routeInsertionPoints(2);
            for k = 1:8 % Generate New Solutions
                tmpPopRoute(k,:) = bestOf8Route;
                tmpPopBreak(k,:) = bestOf8Break;
                switch k
                    case 2 % Flip
                        tmpPopRoute(k,I:J) = tmpPopRoute(k,J:-1:I);
                    case 3 % Swap
                        tmpPopRoute(k,[I J]) = tmpPopRoute(k,[J I]);
                    case 4 % Slide
                        tmpPopRoute(k,I:J) = tmpPopRoute(k,[I+1:J I]);
                    case 5 % Modify Breaks
                        tmpPopBreak(k,:) = rand_breaks();
                    case 6 % Flip, Modify Breaks
                        tmpPopRoute(k,I:J) = tmpPopRoute(k,J:-1:I);
                        tmpPopBreak(k,:) = rand_breaks();
                    case 7 % Swap, Modify Breaks
                        tmpPopRoute(k,[I J]) = tmpPopRoute(k,[J I]);
                        tmpPopBreak(k,:) = rand_breaks();
                    case 8 % Slide, Modify Breaks
                        tmpPopRoute(k,I:J) = tmpPopRoute(k,[I+1:J I]);
                        tmpPopBreak(k,:) = rand_breaks();
                    otherwise % Do Nothing
                end
            end
            newPopRoute(p-7:p,:) = tmpPopRoute;
            newPopBreak(p-7:p,:) = tmpPopBreak;
        end
        popRoute = newPopRoute;
        popBreak = newPopBreak;

        % Update the waitbar
        if showWaitbar && ~mod(iter,ceil(numIter/325))
            waitbar(iter/numIter,hWait);
        end

    end
    if showWaitbar
        close(hWait);
    end

    if showResult
        % Plots     画图
        figure('Name','MTSPF_GA | Results','Numbertitle','off');
        subplot(2,2,1);
        if dims > 2, plot3(xy(:,1),xy(:,2),xy(:,3),'.','Color',pclr);
        else
            plot(xy(:,1),xy(:,2),'.','Color',pclr);
        end
        title('传感器位置');
        subplot(2,2,2);
        imagesc(dmat([1 optRoute],[1 optRoute]));
        title('距离矩阵');
        subplot(2,2,3);
        rng = [[1 optBreak+1];[optBreak n]]';
        for s = 1:nSalesmen
            rte = [1 optRoute(rng(s,1):rng(s,2)) 1];
            if dims > 2, plot3(xy(rte,1),xy(rte,2),xy(rte,3),'.-','Color',clr(s,:));
            else
                plot(xy(rte,1),xy(rte,2),'.-','Color',clr(s,:));
            end
            title(sprintf('最短总路程= %1.4f',minDist));
            hold on;
        end
        if dims > 2, plot3(xy(1,1),xy(1,2),xy(1,3),'o','Color',pclr);
        else plot(xy(1,1),xy(1,2),'o','Color',pclr); end
        subplot(2,2,4);
        plot(distHistory,'b','LineWidth',2);
        title('历史最优解');
        set(gca,'XLim',[0 numIter+1],'YLim',[0 1.1*max([1 distHistory])]);
    end

    % Return Output
    if nargout
        resultStruct = struct( ...
            'xy',          xy, ...
            'dmat',        dmat, ...
            'nSalesmen',   nSalesmen, ...
            'minTour',     minTour, ...
            'popSize',     popSize, ...
            'numIter',     numIter, ...
            'showProg',    showProg, ...
            'showResult',  showResult, ...
            'showWaitbar', showWaitbar, ...
            'optRoute',    optRoute, ...
            'optBreak',    optBreak, ...
            'minDist',     minDist);

        varargout = {resultStruct};
    end
function breaks = rand_breaks()
        if minTour == 1 % No Constraints on Breaks
            tmpBreaks = randperm(n-1);
            breaks = sort(tmpBreaks(1:nBreaks));
        else % Force Breaks to be at Least the Minimum Tour Length
            nAdjust = find(rand < cumProb,1)-1;
            spaces = ceil(nBreaks*rand(1,nAdjust));
            adjust = zeros(1,nBreaks);
            for kk = 1:nBreaks
                adjust(kk) = sum(spaces == kk);
            end
            breaks = minTour*(1:nBreaks) + cumsum(adjust);
        end
    end

end

四个旅行商时的最优路径四个旅行商时的最优路径

摘自: [link(https://blog.csdn.net/weixin_38130913/article/details/104471378?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param).]

  • 4
    点赞
  • 68
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值