%% 农村配送场景构建 (单位:公里)
% 区域范围:[0,100]×[0,100]
% 仓库位置固定于 [50,50]
% 生成场景包括:
% 1. 主干道:水平主干道位于 y = 20, 80;垂直主干道位于 x = 20, 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, 80;垂直主干道: x = 20, 80)
horizontal_y = [20, 80]; % 水平主干道的 y 坐标
vertical_x = [20, 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; % 共 4 条主干道
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, 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, 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]
% 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]; % 累计客户点
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];
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, 'blue', 'filled');
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', ...
'customer_goods_type', 'customer_goods_weight', ...
'total_customers', 'scene_size', ...
'main_road_width', 'branch_width', ...
'vehicle_capacity', 'vehicle_range', 'drone_capacity', 'drone_range');
代码说明
-
参数设置 (部分 1)
- 区域范围定义为 [0,100]×[0,100] 公里;仓库位置为 [50,50]。
- 主干道参数:水平道路位于 y = 20, 80;垂直道路位于 x = 20, 80,共 4 条主干道。
- 支道、客户以及其它相关参数同前面设定。
-
生成主干道 (部分 2)
- 分别生成水平和垂直主干道,采用预设的水平坐标 horizontal_y 和垂直坐标 vertical_x;
- 每条道路由 100 个点构成,存入 cell 数组 main_roads 中。
-
生成支道 (部分 3)
- 遍历每条主干道,根据 branch_density 计算支道数量;
- 水平主干道的支道沿竖直方向延伸,垂直主干道的支道沿水平方向延伸。
-
生成客户点 (部分 4)
- 客户按 remote_ratio 分为两类:
- 偏远客户(80%)随机从支道上取,利用插值在支道末段 (t∈[0.9,1]) 生成客户点;
- 主干道客户(20%)随机从主干道取,加入 ±5 公里扰动。
-
生成货物需求信息 (部分 5)
- 为每个客户点随机分配一个货物种类(1~15 的整数)。
- 根据给定分布生成需求重量:
- 60% 概率在 [0,5] kg 内生成;
- 25% 在 [5,10] kg;
- 13% 在 [10,15] kg;
- 2% 在 [15,20] kg;
- 结果分别存入 customer_goods_type 和 customer_goods_weight 中。
-
可视化 (部分 6)
- 绘制仓库(红色圆圈)、主干道(蓝色实线)和支道(绿色虚线);
- 利用散点图显示客户点,统一使用蓝色(不根据需求量着色);
- 在每个客户点旁标注其坐标(采用 sprintf 格式化为 (x,y))。
-
输出与保存 (部分 7~8)
- 输出场景基本信息和部分客户货物需求信息到命令窗口;
- 将所有场景数据保存到 MAT 文件中,便于后续使用。
运行此代码后,你将在 MATLAB 中得到所描述的农村配送场景数据,既包含道路与客户点,又为每个客户生成了货物需求种类和重量信息。