下面的代码在生成场景后,会输出所有客户的货物需求种类及对应的需求重量。你可以直接运行整个代码,运行结束后在命令窗口中会显示所有客户的信息。
下面是完整代码,其中增加了输出所有客户需求货物种类及重量的部分:
%% 农村配送场景构建 - MATLAB实现
% 场景范围:[0,100]×[0,100]的二维平面,单位为公里
% 主要特征:
% 1. 中心仓库位于 [50,50]
% 2. 主干道:水平主干道位于 y = 20 和 y = 80;垂直主干道位于 x = 20 和 x = 80
% 3. 支道:从每条主干道随机延伸
% 4. 客户点:80% 分布在支道末端(偏远客户),20% 分布在主干道附近
% 5. 每个客户点生成货物需求信息,包括:
% 货物种类(1~15 的整数)
% 需求重量:0–5kg 占比 60%,5–10kg 占比 25%,10–15kg 占比 13%,15–20kg 占比 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 = length(horizontal_y) + length(vertical_x); % 总主干道数量=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%在支道末端)
num_product_types = 15; % 货物种类数量
% 重量分布参数:
% 0–5kg:占比 60%
% 5–10kg:占比 25%
% 10–15kg:占比 13%
% 15–20kg:占比 2%
weight_probs = [0.6, 0.25, 0.13, 0.02]; % 概率数组(总和为1)
% 对应的重量区间为:
% [0,5] , [5,10] , [10,15] , [15,20]
weight_ranges = [0, 5, 10, 15, 20];
% 无人车/无人机参数(此处仅作为信息保存)
vehicle_capacity = 100; % 无人车最大载重 (kg)
vehicle_range = 300; % 无人车最大行驶里程 (km)
drone_capacity = 5; % 无人机最大载重 (kg)
drone_range = 150; % 无人机最大航程 (km)
%% 2. 生成主干道
% 主干道包括两组:水平主干道和垂直主干道
main_roads = cell(num_main_roads, 1);
% 2.1 生成水平主干道(固定在 y = 20 和 y = 80)
for i = 1:length(horizontal_y)
y_pos = horizontal_y(i);
x_coords = linspace(0, scene_size, 100);
y_coords = y_pos * ones(1, 100);
main_roads{i} = [x_coords; y_coords];
end
% 2.2 生成垂直主干道(固定在 x = 20 和 x = 80)
for i = 1:length(vertical_x)
x_pos = vertical_x(i);
x_coords = x_pos * ones(1, 100);
y_coords = linspace(0, scene_size, 100);
main_roads{length(horizontal_y) + i} = [x_coords; y_coords];
end
%% 3. 生成支道
% 支道从每条主干道随机延伸
branch_roads = {};
for i = 1:num_main_roads
road = main_roads{i}; % 当前主干道 (2×100 矩阵)
% 假定主干道实际覆盖 scene_size 公里
road_length = scene_size;
num_branches = round(road_length * branch_density);
% 从当前主干道的 100 个采样点中随机抽取支道生成点
branch_indices = randperm(100, num_branches);
for j = 1:num_branches
if i <= length(horizontal_y)
% 对水平主干道:支道沿竖直方向延伸
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);
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);
branch_roads{end+1} = [x_coords; y_coords];
end
end
end
%% 4. 生成客户点
% 分为两类:
% (1) 偏远客户 (80%)—分布在支道末端
% (2) 主干道附近客户 (20%)—分布在主干道及其附近
customers = []; % 每行为 [x, y] 坐标
for i = 1:round(total_customers*remote_ratio)
% 随机选择一条支道
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);
% 确保客户点位于 [0, scene_size]
x = max(0, min(scene_size, x));
y = max(0, min(scene_size, y));
customers = [customers; x, y];
end
for i = round(total_customers*remote_ratio)+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(num_product_types, total_customers, 1);
% 同时生成需求重量 (kg)
customer_goods_weight = zeros(total_customers, 1);
% 根据以下概率分布生成:
% 0–5kg 占比 60%
% 5–10kg 占比 25%
% 10–15kg 占比 13%
% 15–20kg 占比 2%
for i = 1:total_customers
r = rand();
if r < 0.60
% 在 [0, 5]
customer_goods_weight(i) = 5 * rand();
elseif r < 0.60 + 0.25 % r < 0.85
% 在 [5, 10]
customer_goods_weight(i) = 5 + 5 * rand();
elseif r < 0.60 + 0.25 + 0.13 % r < 0.98
% 在 [10, 15]
customer_goods_weight(i) = 10 + 5 * rand();
else
% 在 [15, 20]
customer_goods_weight(i) = 15 + 5 * rand();
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所有客户需求货物信息:\n');
fprintf('客户\t货物种类\t需求重量 (kg)\n');
for i = 1:total_customers
fprintf('%d\t%d\t\t%.2f\n', i, customer_goods_type(i), customer_goods_weight(i));
end
%% 8. 输出场景基本信息
fprintf('\n场景生成完成!\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);
%% 9. 保存场景数据
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]。
- 主干道设置为2条水平道路(y=20 与 y=80)和2条垂直道路(x=20 与 x=80),共4条。
- 支道从主干道随机延伸;客户点总数设为100,其中80%为偏远客户。
- 货物种类定义为1~15;并使用给定的概率(60%、25%、13%、2%)生成需求重量,范围分别为[0,5]kg、[5,10]kg、[10,15]kg、[15,20]kg。
-
生成主干道 (第2部分):
- 分别生成水平主干道和垂直主干道,每条道路由100个点构成,以便后续插值使用。
-
生成支道 (第3部分):
- 遍历每条主干道,在随机选定的位置生成支道;水平主干道的支道沿竖直方向延伸,垂直主干道的支道沿水平方向延伸。
-
生成客户点 (第4部分):
- 偏远客户从支道末端生成(采用插值 t∈[0.9,1]);其他客户则从主干道上生成,并加上随机扰动(±5公里)。
-
生成货物需求信息 (第5部分):
- 为每个客户随机分配一个货物种类(1~15),并根据预设概率生成需求重量。
-
可视化 (第6部分):
- 绘制仓库(红色)、主干道(蓝色实线)和支道(绿色虚线);同时在每个客户点旁边标注其坐标(格式:(x, y))。
-
输出所有客户需求信息 (第7部分):
- 使用fprintf循环逐个输出每个客户的编号、货物种类以及需求重量到命令窗口。
-
输出场景基本信息 (第8部分):
- 输出仓库位置、主干道数量、支道数量、客户总数和偏远客户比例。
-
保存数据 (第9部分):
- 将所有生成的数据保存至MAT文件中,便于后续调用。
运行这段代码后,不仅会显示并绘制整个农村配送场景,还会在命令窗口中输出所有客户的货物需求信息(种类及重量)。