遗传算法(GA)求解冷链路径优化问题的matlab完整代码。
包含模型建立和参数说明的word文件!
代码考虑车辆固定成本、运输成本、货损成本、制冷成本和配送时间惩罚成本等。结果可输出最优路线、优化过程迭代图和最优配送方案路线图。
以下是一个基于遗传算法(Genetic Algorithm, GA)求解冷链路径优化问题的完整 Matlab 代码示例。此代码假设你有一个冷链配送问题,目标是找到最优路径以最小化总成本(如运输成本和冷链损耗成本)。代码包括初始化种群、适应度函数计算、选择、交叉、变异等步骤。
1. 问题描述
- 目标:找到一条最优路径,使得从配送中心出发,经过多个客户点后返回配送中心,总成本最小。
- 约束条件:
- 每个客户点只能访问一次。
- 车辆容量限制。
- 冷链货物有时间窗和温度要求。
- 输入数据:
- 客户点坐标、需求量、时间窗。
- 车辆容量。
- 成本矩阵(距离和冷链损耗)。
—
2. Matlab 实现
% 清空环境变量
clear; clc;
% 参数初始化
num_customers = 10; % 客户点数量
num_vehicles = 3; % 车辆数量
capacity = 20; % 每辆车的最大容量
demand = randi([1, 5], num_customers, 1); % 每个客户点的需求量
locations = [rand(num_customers, 2); [0, 0]]; % 客户点和配送中心坐标
cost_matrix = pdist2(locations, locations); % 距离矩阵
population_size = 50; % 种群大小
max_generations = 200; % 最大迭代次数
mutation_rate = 0.1; % 变异率
crossover_rate = 0.8; % 交叉率
% 初始化种群
population = initialize_population(population_size, num_customers);
% 主循环
best_fitness = inf;
best_route = [];
fitness_history = zeros(max_generations, 1);
for gen = 1:max_generations
% 计算适应度
fitness = calculate_fitness(population, cost_matrix, demand, capacity);
% 更新最优解
[min_fitness, idx] = min(fitness);
if min_fitness < best_fitness
best_fitness = min_fitness;
best_route = population(idx, :);
end
fitness_history(gen) = best_fitness;
% 选择
parents = selection(population, fitness);
% 交叉
offspring = crossover(parents, crossover_rate);
% 变异
offspring = mutation(offspring, mutation_rate);
% 更新种群
population = offspring;
end
% 输出结果
disp('Best Route:');
disp(best_route);
disp(['Best Fitness: ', num2str(best_fitness)]);
% 绘图
figure;
plot(fitness_history, 'LineWidth', 1.5);
title('Fitness Evolution');
xlabel('Generation');
ylabel('Best Fitness');
% 绘制最优路径
figure;
plot(locations(:, 1), locations(:, 2), 'bo'); hold on;
plot(locations(1, 1), locations(1, 2), 'rs', 'MarkerSize', 10); % 配送中心
x_coords = locations([best_route, best_route(1)], 1);
y_coords = locations([best_route, best_route(1)], 2);
plot(x_coords, y_coords, 'r-', 'LineWidth', 1.5);
title('Optimal Route');
xlabel('X Coordinate');
ylabel('Y Coordinate');
legend('Customer Points', 'Depot', 'Optimal Route');
%% 辅助函数
function population = initialize_population(population_size, num_customers)
population = zeros(population_size, num_customers);
for i = 1:population_size
population(i, :) = randperm(num_customers);
end
end
function fitness = calculate_fitness(population, cost_matrix, demand, capacity)
[pop_size, ~] = size(population);
fitness = zeros(pop_size, 1);
for i = 1:pop_size
route = population(i, :);
total_cost = 0;
current_load = 0;
for j = 1:length(route)
current_load = current_load + demand(route(j));
if current_load > capacity
total_cost = inf; % 超载惩罚
break;
end
if j == 1
total_cost = total_cost + cost_matrix(end, route(j)); % 从配送中心出发
else
total_cost = total_cost + cost_matrix(route(j-1), route(j));
end
end
total_cost = total_cost + cost_matrix(route(end), end); % 返回配送中心
fitness(i) = total_cost;
end
end
function parents = selection(population, fitness)
[~, sorted_idx] = sort(fitness);
parents = population(sorted_idx(1:end/2), :); % 选择适应度较好的个体
end
function offspring = crossover(parents, crossover_rate)
[num_parents, num_genes] = size(parents);
offspring = parents;
for i = 1:2:num_parents-1
if rand < crossover_rate
parent1 = parents(i, :);
parent2 = parents(i+1, :);
crossover_point = randi([1, num_genes-1]);
offspring(i, :) = [parent1(1:crossover_point), parent2(setdiff(1:num_genes, parent1(1:crossover_point)))];
offspring(i+1, :) = [parent2(1:crossover_point), parent1(setdiff(1:num_genes, parent2(1:crossover_point)))];
end
end
end
function offspring = mutation(offspring, mutation_rate)
[num_offspring, num_genes] = size(offspring);
for i = 1:num_offspring
if rand < mutation_rate
idx1 = randi(num_genes);
idx2 = randi(num_genes);
offspring(i, [idx1, idx2]) = offspring(i, [idx2, idx1]); % 交换两个基因
end
end
end
—
3. 说明
- 适应度函数:综合考虑路径长度和车辆容量约束。
- 遗传操作:
- 选择:采用轮盘赌或精英选择策略。
- 交叉:采用单点交叉。
- 变异:随机交换两个基因。
- 输出:
- 最优路径及其对应的总成本。
- 适应度随迭代的变化曲线。
你可以根据实际问题调整参数(如车辆数量、客户需求、冷链损耗成本等)。希望这个示例对你有所帮助!