蚁群算法matlab代码
蚁群算法是一种模拟蚂蚁觅食行为的优化算法,常用于解决组合优化问题。
matlabfunction [bestTour, minDistance] = antColonyOptimization(distanceMatrix, numAnts, numIterations, alpha, beta, rho) numCities = size(distanceMatrix, 1); % 初始化信息素矩阵 pheromoneMatrix = ones(numCities, numCities); % 迭代优化 for iter = 1:numIterations % 生成蚂蚁路径 antTours = generateAntTours(numAnts, pheromoneMatrix, distanceMatrix, alpha, beta); % 更新信息素 pheromoneMatrix = updatePheromones(pheromoneMatrix, antTours, distanceMatrix, rho); % 找到最短路径 [minDistance, bestTourIndex] = min(computeTourLength(antTours, distanceMatrix)); bestTour = antTours{bestTourIndex}; % 显示每次迭代的结果 fprintf('Iteration %d: Best Tour Length = %f\n', iter, minDistance); end end function antTours = generateAntTours(numAnts, pheromoneMatrix, distanceMatrix, alpha, beta) numCities = size(distanceMatrix, 1); antTours = cell(1, numAnts); for ant = 1:numAnts % 初始化蚂蚁的位置 currentCity = randi(numCities); antTour = zeros(1, numCities); antTour(1) = currentCity; % 生成蚂蚁路径 for step = 2:numCities probabilities = computeProbabilities(currentCity, antTour, pheromoneMatrix, distanceMatrix, alpha, beta); nextCity = selectNextCity(probabilities); antTour(step) = nextCity; currentCity = nextCity; end antTours{ant} = antTour; end end function probabilities = computeProbabilities(currentCity, antTour, pheromoneMatrix, distanceMatrix, alpha, beta) numCities = length(antTour); probabilities = zeros(1, numCities); visitedCities = antTour(antTour > 0); % 计算转移概率 for i = 1:numCities if ~ismember(i, visitedCities) pheromone = pheromoneMatrix(currentCity, i); distance = distanceMatrix(currentCity, i); probabilities(i) = pheromone^alpha * (1/distance)^beta; end end % 归一化概率 probabilities = probabilities / sum(probabilities); end function nextCity = selectNextCity(probabilities) cumulativeProbabilities = cumsum(probabilities); randomValue = rand(); nextCity = find(cumulativeProbabilities >= randomValue, 1); end function pheromoneMatrix = updatePheromones(pheromoneMatrix, antTours, distanceMatrix, rho) numAnts = length(antTours); pheromoneDelta = zeros(size(pheromoneMatrix)); % 计算每只蚂蚁留下的信息素 for ant = 1:numAnts tour = antTours{ant}; tourLength = computeTourLength({tour}, distanceMatrix); for i = 1:length(tour)-1 pheromoneDelta(tour(i), tour(i+1)) = pheromoneDelta(tour(i), tour(i+1)) + 1 / tourLength; pheromoneDelta(tour(i+1), tour(i)) = pheromoneDelta(tour(i+1), tour(i)) + 1 / tourLength; end end % 更新信息素矩阵 pheromoneMatrix = (1 - rho) * pheromoneMatrix + pheromoneDelta; end function tourLength = computeTourLength(antTours, distanceMatrix) numAnts = length(antTours); tourLength = zeros(1, numAnts); for ant = 1:numAnts tour = antTours{ant}; for i = 1:length(tour)-1 tourLength(ant) = tourLength(ant) + distanceMatrix(tour(i), tour(i+1)); end end end
matlab% 示例:解决旅行商问题 % 随机生成城市坐标 numCities = 10; cities = rand(numCities, 2); % 计算城市之间的距离矩阵 distanceMatrix = pdist2(cities, cities); % 设置算法参数 numAnts = 5; numIterations = 50; alpha = 1; beta = 2; rho = 0.5; % 调用蚁群算法 [bestTour, minDistance] = antColonyOptimization(distanceMatrix, numAnts, numIterations, alpha, beta, rho); % 显示结果 fprintf('Best Tour: %s\n', num2str(bestTour)); fprintf('Minimum Distance: %f\n', minDistance); % 绘制最佳路径 figure; plot(cities(:, 1), cities(:, 2), 'o', 'MarkerSize', 10, 'MarkerFaceColor', 'b'); hold on; plot(cities(bestTour, 1), cities(bestTour, 2), 'r', 'LineWidth', 2); title('Best Tour'); xlabel('X-coordinate'); ylabel('Y-coordinate'); legend('Cities', 'Best Tour'); hold off;
这个示例假设存在一个包含城市坐标的cities
矩阵,并且使用pdist2
函数计算城市之间的距离矩阵。算法的参数在调用时进行了设置,您可以根据问题的需要进行调整。
这个代码示例是为了演示蚁群算法的基本原理,实际应用中可能需要根据具体问题进行更多的调整和优化。