蚁群算法(matlab代码)

function ant_colony_optimization_tsp()
    % 初始化城市坐标
    cities = [0, 0; 1, 5; 2, 3; 5, 2; 6, 6];
    num_cities = size(cities, 1); % 城市数量
    num_ants = 20; % 蚂蚁数量
    max_iterations = 100; % 最大迭代次数
    
    % 初始化参数
    alpha = 1; % 信息素重要程度
    beta = 5; % 启发函数重要程度
    rho = 0.1; % 信息素挥发系数
    Q = 100; % 常量,用于更新信息素
    
    % 初始化信息素矩阵和距离矩阵
    pheromone = ones(num_cities); % 信息素矩阵,初始值为1
    distance = zeros(num_cities); % 距离矩阵,初始值为0
    for i = 1:num_cities
        for j = 1:num_cities
            if i ~= j
                distance(i,j) = sqrt(sum((cities(i,:) - cities(j,:)).^2)); % 计算城市间的欧几里得距离
            else
                distance(i,j) = inf; % 城市到自身的距离为无穷大
            end
        end
    end
    
    % 主循环
    best_length = inf; % 初始化最优路径长度为无穷大
    best_route = []; % 初始化最优路径为空
    
    figure; % 创建图形窗口
    for iter = 1:max_iterations
        routes = zeros(num_ants, num_cities); % 存储每只蚂蚁的路径
        route_lengths = zeros(num_ants, 1); % 存储每只蚂蚁路径的长度
        
        % 每只蚂蚁构建解
        for k = 1:num_ants
            route = zeros(1, num_cities); % 初始化蚂蚁的路径
            route(1) = randi(num_cities); % 随机选择起点
            for step = 2:num_cities
                current_city = route(step-1); % 当前所在城市
                probabilities = zeros(1, num_cities); % 初始化选择概率
                for j = 1:num_cities
                    if ~ismember(j, route(1:step-1)) % 如果城市未被访问过
                        probabilities(j) = (pheromone(current_city, j)^alpha) * (1/distance(current_city, j)^beta); % 计算选择概率probabilities(j)
                    end
                end
                probabilities = probabilities / sum(probabilities); % 归一化选择概率
                next_city = randsample(1:num_cities, 1, true, probabilities); % 根据概率选择下一个城市,轮盘赌
                route(step) = next_city; % 更新路径
            end
            routes(k, :) = route; % 记录蚂蚁的路径
            route_lengths(k) = sum(arrayfun(@(x, y) distance(x, y), route, [route(2:end), route(1)])); % 计算路径总长度
        end
        
        % 更新最优解
        [min_length, min_index] = min(route_lengths); % 找到最短路径及其索引
        if min_length < best_length % 如果找到更短的路径
            best_length = min_length; % 更新最短路径长度
            best_route = routes(min_index, :); % 更新最优路径
        end
        
        % 更新信息素
        pheromone = pheromone * (1 - rho); % 信息素挥发
        for k = 1:num_ants
            for step = 1:num_cities
                from = routes(k, step); % 路径中的当前城市
                to = routes(k, mod(step, num_cities) + 1); % 路径中的下一个城市,mod使形成环路
                pheromone(from, to) = pheromone(from, to) + Q / route_lengths(k); % 更新信息素
                pheromone(to, from) = pheromone(to, from) + Q / route_lengths(k); % 对称更新信息素
            end
        end
        
        % 绘制当前最优路径
        clf; % 清除当前图形
        plot(cities(:,1), cities(:,2), 'bo'); % 绘制城市,cities(:,1) 和 cities(:,2) 分别表示所有城市的 x 坐标和 y 坐标。
        hold on;
        best_route_cities = [best_route, best_route(1)]; % 最优路径的城市序列,将最优路径的起点城市添加到路径的末尾,使得路径形成一个环
        for i = 1:num_cities
            from = best_route_cities(i); % 当前城市,获取当前城市的索引
            to = best_route_cities(i+1); % 下一个城市,获取下一个城市的索引
            % 绘制路径线段
            quiver(cities(from,1), cities(from,2), cities(to,1)-cities(from,1), cities(to,2)-cities(from,2), 0, 'Color', [0.5 0.75 1], 'MaxHeadSize', 0.2); % 使用箭头绘制路径
        end
        
        % 标注起点和终点
        start_city = best_route(1); % 起点
        end_city = best_route(end); % 终点
        plot(cities(start_city, 1), cities(start_city, 2), 'go', 'MarkerSize', 8, 'MarkerFaceColor', 'g'); % 起点标绿
        plot(cities(end_city, 1), cities(end_city, 2), 'ro', 'MarkerSize', 8, 'MarkerFaceColor', 'r'); % 终点标红
        text(cities(start_city,1), cities(start_city,2), '起点', 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'left'); % 标注起点文字
        text(cities(end_city,1), cities(end_city,2), '终点', 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'left'); % 标注终点文字
        
        % 标注城市编号
        for i = 1:num_cities
            text(cities(i,1), cities(i,2), sprintf('City %d', i), 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'right'); % 标注城市编号
        end
        
        title(sprintf('Iteration %d: Best length = %.2f', iter, best_length)); % 标题显示当前迭代次数和最优路径长度
        xlabel('X'); % X轴标签
        ylabel('Y'); % Y轴标签
        grid on; % 显示网格
        hold off;
        pause(0.1); % 暂停0.1秒以显示图像
    end
    
    % 输出最终结果
    fprintf('Best route: '); % 打印最优路径
    disp(best_route); % 显示最优路径
    fprintf('Best length: %.2f\n', best_length); % 打印最优路径长度
end

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值