下面提供修改后的完整 MATLAB 代码示例。该代码在 [0,100]×[0,100](单位:公里)的区域内生成农村配送场景,仓库位置固定为 [50,50]。场景中主干道设定如下:
- 水平主干道位于 y = 20, 50, 80
- 垂直主干道位于 x = 20, 50, 80
另外,每个客户点生成时不标注客户编号,而是在客户点附近标注其坐标值。
每个客户点还随机生成一个货物需求信息,要求:
- 货物种类为 1~15 的随机整数
- 需求重量服从下面分布:
- 0–5 kg 占比 60%
- 5–10 kg 占比 25%
- 10–15 kg 占比 13%
- 15–20 kg 占比 2%
下面是代码及详细注释说明:
%% 农村配送场景构建 (单位:公里)
% 区域范围:[0,100]×[0,100]
% 仓库位置固定于 [50,50]
% 生成场景包括:
% 1. 主干道:水平主干道位于 y = 20, 50, 80;垂直主干道位于 x = 20, 50, 80
% 2. 支道:从每条主干道上随机延伸
% 3. 客户点:
% 80% 分布在支道末端(偏远客户)
% 20% 分布在主干道及其附近
% 4. 每个客户点生成一个货物需求信息,包括:
% 货物种类(1~15)
% 需求重量:0–5 kg 占比 60%,5–10 kg 占比 25%,10–15 kg 占比 13%,15–20 kg 占比 2%
%% 1. 参数设置
clear; clc; close all;
% 场景参数
scene_size = 100; % 区域边长(公里)
warehouse_loc = [50, 50]; % 仓库位置 (km)
% 主干道参数(水平主干道: y = 20, 50, 80;垂直主干道: x = 20, 50, 80)
horizontal_y = [20, 50, 80]; % 水平主干道的 y 坐标
vertical_x = [20, 50, 80]; % 垂直主干道的 x 坐标
num_main_roads_horizontal = length(horizontal_y); % 水平主干道数
num_main_roads_vertical = length(vertical_x); % 垂直主干道数
num_main_roads = num_main_roads_horizontal + num_main_roads_vertical; % 共 6 条主干道
main_road_width = 0.2; % 主干道宽度(公里)
% 支道参数
branch_length = 15; % 支道长度(公里)
branch_density = 0.1; % 支道密度:每公里产生的支道数
branch_width = 0.1; % 支道宽度(公里)
% 客户点参数
total_customers = 100; % 客户总数
remote_ratio = 0.8; % 偏远客户比例(80%在支道末端)
%% 2. 生成主干道
% 主干道分为两组:水平和垂直
main_roads = cell(num_main_roads, 1); % 初始化 cell 数组
% 2.1 生成水平主干道(设置在 y = 20, 50, 80)
for i = 1:num_main_roads_horizontal
y_pos = horizontal_y(i); % 当前水平主干道的 y 坐标
x_coords = linspace(0, scene_size, 100); % 沿 x 方向均匀取 100 个点
y_coords = y_pos * ones(1, 100); % 固定 y 坐标
main_roads{i} = [x_coords; y_coords]; % 存入 cell 数组
end
% 2.2 生成垂直主干道(设置在 x = 20, 50, 80)
for i = 1:num_main_roads_vertical
x_pos = vertical_x(i); % 当前垂直主干道的 x 坐标
x_coords = x_pos * ones(1, 100); % 固定 x 坐标
y_coords = linspace(0, scene_size, 100); % 沿 y 方向均匀取 100 个点
main_roads{num_main_roads_horizontal + i} = [x_coords; y_coords];
end
%% 3. 生成支道
% 支道从每条主干道随机延伸,模拟主要道路向内陆延伸的支路
branch_roads = {}; % 用 cell 数组保存所有支道
for i = 1:num_main_roads
road = main_roads{i}; % 当前主干道数据 (2×100矩阵)
% 假定主干道长度为 scene_size (100 公里)
road_length_temp = scene_size;
num_branches = round(road_length_temp * branch_density); % 计算支道数量
% 从主干道的 100 个采样点中随机抽出 num_branches 个索引
branch_indices = randperm(100, num_branches);
% 对于当前主干道的每个支道点生成支道数据
for j = 1:num_branches
if i <= num_main_roads_horizontal
% 水平主干道:支道沿竖直方向延伸
x_pos = road(1, branch_indices(j));
y_pos = road(2, branch_indices(j));
if rand() < 0.5
y_dir = 1; % 向上延伸
else
y_dir = -1; % 向下延伸
end
x_coords = linspace(x_pos, x_pos, 100); % x 坐标不变
y_coords = linspace(y_pos, y_pos + y_dir * branch_length, 100); % 线性延伸
branch_roads{end+1} = [x_coords; y_coords];
else
% 垂直主干道:支道沿水平方向延伸
x_pos = road(1, branch_indices(j));
y_pos = road(2, branch_indices(j));
if rand() < 0.5
x_dir = 1; % 向右延伸
else
x_dir = -1; % 向左延伸
end
x_coords = linspace(x_pos, x_pos + x_dir * branch_length, 100); % 线性延伸
y_coords = linspace(y_pos, y_pos, 100); % y 坐标不变
branch_roads{end+1} = [x_coords; y_coords];
end
end
end
%% 4. 生成客户点
% 客户点分为两类:
% ① 偏远客户(80%):生成在支道末端
% ② 主干道附近客户(20%):生成在主干道及其附近
customers = []; % 每一行存储一个客户的 [x, y]
demands = zeros(1, total_customers); % 示例需求量,仅作示例(可在后续使用中调整)
% 4.1 生成偏远客户 (80%)
num_remote = round(total_customers * remote_ratio);
for i = 1:num_remote
% 随机选择一条支道
branch_idx = randi(length(branch_roads));
branch_road = branch_roads{branch_idx};
% 在支道末端附近生成客户点:取 t ∈ [0.9, 1]
t = 0.9 + 0.1 * rand();
x = interp1(linspace(0, 1, 100), branch_road(1, :), t);
y = interp1(linspace(0, 1, 100), branch_road(2, :), t);
% 保证生成的点位于场景内
x = max(0, min(scene_size, x));
y = max(0, min(scene_size, y));
customers = [customers; x, y]; % 累计客户点
demands(i) = 1 + floor(3 * rand()); % 示例需求量(1~3单位)
end
% 4.2 生成主干道附近客户 (20%)
for i = num_remote+1:total_customers
% 随机选择一条主干道
main_idx = randi(num_main_roads);
main_road = main_roads{main_idx};
% 在主干道上生成客户点,取 t ∈ [0,1]
t = rand();
x = interp1(linspace(0, 1, 100), main_road(1, :), t);
y = interp1(linspace(0, 1, 100), main_road(2, :), t);
% 添加小随机偏移 (±5 公里)
x = x + (rand()-0.5) * 5;
y = y + (rand()-0.5) * 5;
% 保证客户点位于区域内
x = max(0, min(scene_size, x));
y = max(0, min(scene_size, y));
customers = [customers; x, y];
demands(i) = 3 + floor(7 * rand()); % 示例需求量(3~10单位)
end
%% 5. 为每个客户生成货物需求信息
% 为每个客户点分配一个货物种类(1~15 的随机整数)
customer_goods_type = randi(15, total_customers, 1);
customer_goods_weight = zeros(total_customers, 1); % 用于存储需求重量 (kg)
% 需求重量按照以下概率分布生成:
% 0–5 kg 占比 60%
% 5–10 kg 占比 25%
% 10–15 kg 占比 13%
% 15–20 kg 占比 2%
for i = 1:total_customers
r = rand();
if r < 0.60
customer_goods_weight(i) = 5 * rand(); % 在 [0,5] kg 内生成均匀随机数
elseif r < 0.60 + 0.25 % r < 0.85
customer_goods_weight(i) = 5 + 5 * rand(); % 在 [5,10] kg 内生成
elseif r < 0.60 + 0.25 + 0.13 % r < 0.98
customer_goods_weight(i) = 10 + 5 * rand(); % 在 [10,15] kg 内生成
else
customer_goods_weight(i) = 15 + 5 * rand(); % 在 [15,20] kg 内生成
end
end
%% 6. 可视化场景
figure('Name', '农村配送场景', 'Position', [100, 100, 1000, 800]);
hold on;
% 绘制仓库 (红色圆圈)
plot(warehouse_loc(1), warehouse_loc(2), 'ro', 'MarkerSize', 10, 'MarkerFaceColor', 'r');
text(warehouse_loc(1)+1, warehouse_loc(2)+1, '仓库', 'FontSize', 12);
% 绘制所有主干道 (蓝色实线)
for i = 1:num_main_roads
plot(main_roads{i}(1,:), main_roads{i}(2,:), 'b-', 'LineWidth', main_road_width*5);
end
% 绘制所有支道 (绿色虚线)
for i = 1:length(branch_roads)
plot(branch_roads{i}(1,:), branch_roads{i}(2,:), 'g--', 'LineWidth', branch_width*5);
end
% 绘制客户点 (散点图, 根据示例需求量着色)
scatter(customers(:,1), customers(:,2), 50, demands, 'filled');
colormap(jet);
colorbar;
title('农村配送场景:客户点分布、需求量及货物需求', 'FontSize', 14);
xlabel('X 坐标 (公里)', 'FontSize', 12);
ylabel('Y 坐标 (公里)', 'FontSize', 12);
axis([0 scene_size 0 scene_size]);
grid on;
% 在每个客户点旁标注其坐标(而非客户编号)
for i = 1:size(customers,1)
coord_str = sprintf('(%0.1f,%0.1f)', customers(i,1), customers(i,2));
text(customers(i,1)+0.5, customers(i,2)+0.5, coord_str, 'FontSize', 8);
end
hold off;
%% 7. 输出场景基本信息
fprintf('场景生成完成!\n');
fprintf('仓库位置: (%.2f, %.2f) km\n', warehouse_loc(1), warehouse_loc(2));
fprintf('主干道数量: %d\n', num_main_roads);
fprintf('支道数量: %d\n', length(branch_roads));
fprintf('客户总数: %d\n', total_customers);
fprintf('偏远客户比例: %.2f%%\n', remote_ratio * 100);
fprintf('每个客户均分配了一个货物种类和对应需求重量。\n');
% 示例输出前 5 个客户的货物需求信息
for i = 1:min(5, total_customers)
fprintf('客户 %d: 货物种类 %d, 需求重量 %.2f kg\n', i, customer_goods_type(i), customer_goods_weight(i));
end
%% 8. 保存场景数据
save('rural_delivery_scene.mat', ...
'warehouse_loc', 'main_roads', 'branch_roads', ...
'customers', 'demands', ...
'customer_goods_type', 'customer_goods_weight', ...
'total_customers', 'scene_size', ...
'main_road_width', 'branch_width', ...
'vehicle_capacity', 'vehicle_range', 'drone_capacity', 'drone_range');