Graph theory + linear programming modeling - vegetable supply plan

Record a modeling topic in school training

1 Problem description

Jiangping City is a small city with a population of less than 200,000 people. According to the vegetable planting situation in the city, three purchasing points are set up at the vegetable market (A), the urban-rural intersection (B) and the South Street intersection (C), and then distributed to 8 vegetable markets in the city from each purchasing point. The road conditions in the city, the distance of each road section (unit: 100m) and the specific locations of each purchase point and vegetable market ① to ⑧ are shown in the figure below.
insert image description here
According to the perennial situation, the daily purchase volumes of A, B, and C are 250, 200 and 180 respectively (unit: 100 kg). The daily demand of each vegetable market and the loss caused by the shortage of supply (yuan/100kg ) See Table 1.
insert image description here
Assume that the shipping fee for vegetables from the purchase point to each vegetable market is 2 yuan/(100kg.100m). Try to solve the following problems:

  1. Design a fixed-point supply plan from the purchase point to a vegetable market for the city, so as to minimize the expected shortage loss for vegetable transportation;
  2. If it is stipulated that the shortage in each vegetable market shall not exceed 25% of the demand, redesign the fixed-point supply plan;
  3. In order to meet the vegetable supply of urban residents, the leaders of the city plan to increase the planting area of ​​vegetables. How much of the increased vegetables should be supplied to the three purchasing points A, B, and C each day is the most economical and reasonable.

2 problem analysis

problem analysis

The purpose of the modeling of problem 1 is to design a fixed-point supply scheme from the purchase point to the vegetable market, so as to minimize the expected shortage loss for vegetable transportation. First, the question gives 3 acquisition points, and the daily acquisition volumes are 250, 200 and 180 (unit: 100 kg). Secondly, the question gives 8 vegetable markets, and the transportation fee for vegetables from the purchase point to the vegetable market is 2 yuan/(100kg.100m), and each purchase point to the vegetable market has multiple transportation routes to choose from. To minimize the loss of vegetable transportation, we need to design an optimal transportation plan. Finally, the problem gives the daily demand of 8 vegetable markets and the loss caused by supply shortage. In order to minimize the loss of supply shortage, we need to design an optimal supply plan.

Therefore, when designing a vegetable supply scheme, it should be divided into two sub-problems.

  1. Minimal transport loss: Vegetables need to be transported from 3 purchase points to 8 vegetable markets, and the distance of the road sections has been given. It can be easily figured out that since the transportation fee is unified at 2 yuan, the 24 shortest paths from 3 purchase points to 8 vegetable markets are the best transportation plan. It can be seen that the minimum transportation loss = shortest path × transportation quantity * transportation fee . Only the number of transfers is unknown.
  2. Minimum shortage loss: the total daily demand of the 8 vegetable markets is 700, while the total daily purchase volume of the 3 purchase points is only 630, which means that there will inevitably be a shortage. It can be seen that the minimum shortage loss = shortage quantity × unit shortage loss. Among them, only the shortage quantity is unknown, and the shortage quantity = demand quantity - dispatched quantity, so the final problem is still to find the dispatched quantity.

So minimum total loss = minimum dispatch loss + minimum shortage loss. Our purpose is to obtain the shortest path and dispatch quantity by establishing a model, and the path and dispatch quantity corresponding to the overall minimum loss obtained are the optimal supply scheme.

Analysis of problem two

The purpose of the modeling of problem 2 is to design a fixed-point supply plan from the purchase point to the vegetable market under the premise that the shortage of each vegetable market does not exceed 25% of the demand, so that the vegetable transportation and the expected shortage loss are minimized . After analysis, we can see that the second problem is to add a constraint on the basis of the first problem.

The additional constraints are: the supply of each vegetable market must not be less than 75% of the demand.

Question Three Analysis

The modeling purpose of problem 3 is to meet the vegetable supply demand of the vegetable market, to increase the vegetable production to make up for the shortage of supply, and to design a vegetable supply plan for increasing production at the purchase point, so that the vegetable supply to the vegetable market is the most economical and reasonable. After analysis, it can be seen that the third problem is based on the first problem, adding a constraint condition and modifying a constraint condition.

The additional constraints are: in order to meet the vegetable supply demand of the vegetable market, the total purchase volume of the purchase point must not be less than the total demand of the vegetable market. The modified constraint is: the supply of each vegetable market is no longer greater than the demand, but must be equal to the demand.

3 question assumptions

Assumption 1: Suppose 1 unit of vegetables is 100kg, and 1 unit of distance is 100m.

Hypothesis 2: The transportation loss from the purchase point to the vegetable market is only related to the distance, and has nothing to do with the means of transportation and transportation costs. That is, the transportation cost of one unit of distance and one unit of vegetables is 2 yuan.

The remaining assumptions are to be supplemented. . .

4 Modeling and solution

variable specification

insert image description here

Question one

modeling

According to the data given in the question, it can be calculated from the acquisition point S i S_iSiTo the vegetable market A j A_jAjThe minimum dispatch loss cij c_{ij}cij, and then according to cij c_{ij}cijAnd the quantity of vegetables xij x_{ij} transportedxijAs well as the shortage loss of each vegetable market, a mathematical programming model is established to solve the minimum shortage loss and obtain the best transportation plan. It should be noted that although the problem can be decomposed into two sub-problems for processing, the final optimization must be processed as a comprehensive optimization problem, otherwise the global optimal solution cannot be obtained.

  1. Minimum transportation loss
    Firstly, construct the vegetable supply network weighted undirected graph G = (V, E, W), where V = {S1, S2, S3, A1,…,A8, B1,…,B4}={v1, … , v15}, a total of 15 vertices, W = ( ω ij \omega_{ij}ohij)15×15.
    ω ij = { dij , vi , vj are directly connected by a path+ ∞ , vi , vj are not directly connected by a path\omega_{ij}=\left\{ \begin{aligned} d_{ij} & , & v_i , there is a path between v_j and \\ +\infty & , & there is no path between v_i and v_j \end{aligned} \right.ohij={ dij+,,vi,vjdirectly connected by road _vi,vjThere is no direct connection between
    where dij d_{ij}dijvi , vj v_i,v_jvi,vjThe distance between them, the unit is 100m. Then apply the Dijkstra algorithm to obtain the shortest path between any two points, and extract the required acquisition point S i , ( i = 1 , 2 , 3 ) S_i,(i=1,2,3)Si,(i=1,2,3 )A j , ( j = 1 , 2 , . . . , 8 ) A_j,(j=1,2,...,8)Aj,(j=1,2,...,8 ) The shortest pathcij c_{ij}cij, forming a 3×8 shortest path matrix c. The minimum transportation loss of 1 unit of vegetables from the purchase point to the vegetable market can be obtained from c, the quantity of vegetables transferred from the purchase point to the vegetable market can be obtained from x, and the total minimum transportation loss can be obtained by multiplying the two. In the next step, we need to establish a mathematical programming model of the total loss and solve for x.
  2. Mathematical programming model of total loss
    a. Objective function
    The minimum transportation loss of 1 unit of vegetables from the purchase point to the vegetable market is
    ∑ i = 1 3 ∑ j = 1 8 2 cijxij \sum_{i=1}^{3}{\ sum_{j=1}^{8}2c_{ij}x_{ij}}i=13j=182 cijxij
    The total loss should not only consider the transportation loss caused by transportation, but also consider the loss caused by the lack of vegetables. After analysis, it can be seen that if the vegetables obtained by the vegetable market are less than the demand, the loss is the product of the unit shortage loss and the shortage quantity. The minimum shortage loss caused by vegetable shortage in vegetable market j is
    mj = zj ( yj − ∑ i = 1 3 xij ) m_j=z_j(y_j-\sum_{i=1}^{3}x_{ij})mj=zj(yji=13xij)
    Therefore, the total loss is the minimum dispatch loss + minimum shortage loss
    ∑ i = 1 3 ∑ j = 1 8 2 cijxij + ∑ j = 1 8 mj \sum_{i=1}^{3}{\sum_{j=1 }^{8}2c_{ij}x_{ij}}+\sum_{j=1}^{8}m_ji=13j=182 cijxij+j=18mj
    b. Constraints
    The total amount of vegetables dispatched by all vegetable markets should be equal to the total purchase amount of all purchase points
    ∑ i = 1 3 ∑ j = 1 8 xij = ∑ i = 1 3 pi \sum_{i=1}^{3 }{\sum_{j=1}^{8}x_{ij}}=\sum_{i=1}^{3}p_ii=13j=18xij=i=13pi
    The total amount of vegetables transported by each vegetable market is not greater than the daily demand of the vegetable market
    ∑ i = 1 3 xij ≤ ∑ j = 1 8 yj , j = 1 , 2 , . . . , 8 \sum_{i=1}^ {3}x_{ij}\le\sum_{j=1}^{8}y_{j},j=1,2,...,8i=13xijj=18yj,j=1,2,...,8
    The total amount of vegetables transported by all vegetable markets from one purchase point should be equal to the daily purchase amount of the purchase point
    ∑ j = 1 8 xij = pi , i = 1 , 2 , 3 \sum_{j=1}^{8} x_{ij}=p_i,i=1,2,3j=18xij=pi,i=1,2,3
    non-negative constraints
    xij ≥ 0 , i = 1 , 2 , 3 , j = 1 , 2 , . . . , 8 x_{ij}\ge0,i=1,2,3,j=1,2,.. .,8xij0,i=1,2,3,j=1,2,...,8

the code

clc, clear
% 蔬菜供应方案设计
% 自定义15个顶点名称
NN1 = strcat('S',cellstr(int2str([1:3]')));
NN2 = strcat('A',strtrim(cellstr((int2str([1:8]')))));
NN3 = strcat('B',strtrim(cellstr(int2str([1:4]'))));
NN=[NN1(:)',NN2(:)',NN3(:)'];
% 构建无向图G,图中有15个顶点,30条边
G = graph; G = addnode(G,NN);
L1 = {
    
    'S1','A1',4;'S1','A2',8;'S1','A6',6;'S1','B1',7;'S1','B2',4
'S2','A2',7;'S2','A3',7;'S2','B1',6;'S2','B4',11;'S3','A5',6
'S3','A7',5;'S3','A8',10;'S3','B3',8;'A1','A2',7;'A1','A6',5
'A2','B1',3;'A3','S1',8;'A3','A5',5;'A3','B1',5;'A3','B2',4
'A3','B4',6;'A4','B4',5;'A5','B2',7;'A5','B3',6;'A5','B4',3
'A6','B2',5;'A6','B3',7;'A7','A8',11;'A7','B3',10;'A8','B4',6};
G = addedge(G,L1(:,1),L1(:,2), cell2mat(L1(:,3)));
% 求所有点对之间的最短路径,返回一个15×15的矩阵
d = distances(G);
writematrix(d,'data2_1.xlsx')
% 根据最短路径求所有点对的最小运费
c1 = inf*ones(size(d));
c1(d==0)=0; 
ind=(d>0);
c1(ind)=2*ceil(d(ind));
writematrix(c1,'data2_1.xlsx','Sheet', 2)
% 提出3行8列的运费数据,即收购点S到菜市场A的1单位蔬菜的最小运费数据
c = c1(1:3,4:11); 
writematrix(c,'data2_1.xlsx','Sheet', 3)
h=plot(G,'EdgeLabel',G.Edges.Weight,'Layout', 'auto','NodeFontSize',12,'EdgeColor','k');

% y为菜市场每天需求,z为菜市场短缺1单位蔬菜的损失,p为收购点每天的收购量
y = [80, 70, 90, 80, 120, 70, 100, 90];
z = [10, 8, 5, 10, 10, 8, 5, 8];
p = [250, 200, 180]';

prob = optimproblem;
% 要求解的变量x
x = optimvar('x',3,8, 'LowerBound',0);
% 要求解的目标函数
obj = sum(sum(c.*x))+sum(z.*(y-sum(x)));
prob.Objective = obj;
% 约束条件
prob.Constraints.con1 = sum(sum(x))==sum(p);
prob.Constraints.con2 = sum(x) <= y;
prob.Constraints.con3 = sum(x,2) == p;
% 调用solve工具箱求解
[sol, fval, flag, out] = solve(prob)
% 输出最小损失
fval
% 输出方案x
sol.x
writematrix(sol.x,'data2_1.xlsx','Sheet', 4)

solve

Using MATLAB software to solve the above model, it is known from the calculation results that the minimum value of the total loss is 10280 yuan. The specific minimum transportation cost for 1 unit of vegetables is shown in the table below
insert image description here
The specific optimal supply plan is shown in the table below
insert image description here

question two

modeling

Question 2 adds a constraint on the basis of Question 1, that is, the supply of each vegetable market is not less than 75% of demand
∑ i = 1 3 xij ≥ 0.75 yj , j = 1 , 2 , . . . , 8 \sum_{i=1}^{3}x_{ij}\ge0.75y_{j},j=1,2,...,8i=13xij0 . 7 5 yj,j=1,2,...,8

the code

clc, clear
% 蔬菜供应方案设计
% 自定义15个顶点名称
NN1 = strcat('S',cellstr(int2str([1:3]')));
NN2 = strcat('A',strtrim(cellstr((int2str([1:8]')))));
NN3 = strcat('B',strtrim(cellstr(int2str([1:4]'))));
NN=[NN1(:)',NN2(:)',NN3(:)'];
% 构建无向图G,图中有15个顶点,30条边
G = graph; G = addnode(G,NN);
L1 = {
    
    'S1','A1',4;'S1','A2',8;'S1','A6',6;'S1','B1',7;'S1','B2',4
'S2','A2',7;'S2','A3',7;'S2','B1',6;'S2','B4',11;'S3','A5',6
'S3','A7',5;'S3','A8',10;'S3','B3',8;'A1','A2',7;'A1','A6',5
'A2','B1',3;'A3','S1',8;'A3','A5',5;'A3','B1',5;'A3','B2',4
'A3','B4',6;'A4','B4',5;'A5','B2',7;'A5','B3',6;'A5','B4',3
'A6','B2',5;'A6','B3',7;'A7','A8',11;'A7','B3',10;'A8','B4',6};
G = addedge(G,L1(:,1),L1(:,2), cell2mat(L1(:,3)));
% 求所有点对之间的最短路径,返回一个15×15的矩阵
d = distances(G);
writematrix(d,'data2_1.xlsx')
% 根据最短路径求所有点对的最小运费
c1 = inf*ones(size(d));
c1(d==0)=0; 
ind=(d>0);
c1(ind)=2*ceil(d(ind));
writematrix(c1,'data2_1.xlsx','Sheet', 2)
% 提出3行8列的运费数据,即收购点S到菜市场A的1单位蔬菜的最小运费数据
c = c1(1:3,4:11); 
writematrix(c,'data2_1.xlsx','Sheet', 3)
h=plot(G,'EdgeLabel',G.Edges.Weight,'Layout', 'auto','NodeFontSize',12,'EdgeColor','k');
%highlight(h,c,'LineWidth',2,'LineStyle','-') %最短路径虚线加粗

% y为菜市场每天需求,z为菜市场短缺1单位蔬菜的损失,p为收购点每天的收购量
y = [80, 70, 90, 80, 120, 70, 100, 90];
z = [10, 8, 5, 10, 10, 8, 5, 8];
p = [250, 200, 180]';

prob = optimproblem;
x = optimvar('x',3,8, 'LowerBound',0);
obj = sum(sum(c.*x))+sum(z.*(y-sum(x)));
prob.Objective = obj;
% 约束条件
prob.Constraints.con1 = sum(sum(x))==sum(p);
prob.Constraints.con2 = sum(x) <= y;
prob.Constraints.con3 = sum(x,2) == p;
prob.Constraints.con4 = sum(x) >= y*0.75;
[sol, fval, flag, out] = solve(prob)
fval
sol.x
writematrix(sol.x,'data2_1.xlsx','Sheet', 4)

solve

Using MATLAB software to solve the above model, it is known from the calculation results that the minimum value of the total loss of the second problem is 10520 yuan. The optimal transportation route remains unchanged, and the optimal supply plan for the specific problem 2 is as follows
insert image description here

question three

modeling

The third question is based on the first question, adding a constraint and modifying a constraint.
The additional constraints are: in order to meet the vegetable supply demand of the vegetable market, the total purchase volume of the purchase point must not be less than the total demand of the vegetable market
∑ i = 1 3 pi ≥ ∑ j = 1 8 yj \sum_{i=1} ^{3}p_{i}\ge\sum_{j=1}^{8}y_{j}i=13pij=18yj
The modified constraints are: the supply of each vegetable market is no longer greater than the demand, but must be equal to the demand
∑ i = 1 3 xij = ∑ j = 1 8 yj , j = 1 , 2 , . . . , 8 \sum_{i=1}^{3}x_{ij}=\sum_{j=1}^{8}y_{j},j=1,2,...,8i=13xij=j=18yj,j=1,2,...,8

the code

clc, clear
% 蔬菜供应方案设计
% 自定义15个顶点名称
NN1 = strcat('S',cellstr(int2str([1:3]')));
NN2 = strcat('A',strtrim(cellstr((int2str([1:8]')))));
NN3 = strcat('B',strtrim(cellstr(int2str([1:4]'))));
NN=[NN1(:)',NN2(:)',NN3(:)'];
% 构建无向图G,图中有15个顶点,30条边
G = graph; G = addnode(G,NN);
L1 = {
    
    'S1','A1',4;'S1','A2',8;'S1','A6',6;'S1','B1',7;'S1','B2',4
'S2','A2',7;'S2','A3',7;'S2','B1',6;'S2','B4',11;'S3','A5',6
'S3','A7',5;'S3','A8',10;'S3','B3',8;'A1','A2',7;'A1','A6',5
'A2','B1',3;'A3','S1',8;'A3','A5',5;'A3','B1',5;'A3','B2',4
'A3','B4',6;'A4','B4',5;'A5','B2',7;'A5','B3',6;'A5','B4',3
'A6','B2',5;'A6','B3',7;'A7','A8',11;'A7','B3',10;'A8','B4',6};
G = addedge(G,L1(:,1),L1(:,2), cell2mat(L1(:,3)));
% 求所有点对之间的最短路径,返回一个15×15的矩阵
d = distances(G);
writematrix(d,'data2_1.xlsx')
% 根据最短路径求所有点对的最小运费
c1 = inf*ones(size(d));
c1(d==0)=0; 
ind=(d>0);
c1(ind)=2*ceil(d(ind));
writematrix(c1,'data2_1.xlsx','Sheet', 2)
% 提出3行8列的运费数据,即收购点S到菜市场A的1单位蔬菜的最小运费数据
c = c1(1:3,4:11); 
writematrix(c,'data2_1.xlsx','Sheet', 3)
h=plot(G,'EdgeLabel',G.Edges.Weight,'Layout', 'auto','NodeFontSize',12,'EdgeColor','k');
%highlight(h,c,'LineWidth',2,'LineStyle','-') %最短路径虚线加粗

% y为菜市场每天需求,z为菜市场短缺1单位蔬菜的损失,p为收购点每天的收购量
y = [80, 70, 90, 80, 120, 70, 100, 90];
z = [10, 8, 5, 10, 10, 8, 5, 8];

prob = optimproblem;
% 要求解的变量多了一个p
x = optimvar('x',3,8, 'LowerBound',0);
p = optimvar('p',3,1, 'LowerBound',0);
obj = sum(sum(c.*x))+sum(z.*(y-sum(x)));
prob.Objective = obj;
% 约束条件
prob.Constraints.con1 = sum(sum(x))==sum(p);
prob.Constraints.con2 = sum(x) == y;
prob.Constraints.con3 = sum(x,2) == p;
prob.Constraints.con4 = sum(p) >= sum(y);
[sol, fval, flag, out] = solve(prob)
fval
% 输出x
sol.x
% 输出p
sol.p
writematrix(sol.x,'data2_1.xlsx','Sheet', 4)

solve

Using MATLAB software to solve the above model, it is known from the calculation results that the minimum value of the total loss of the third problem is 10200 yuan. The best transportation route remains unchanged, and the best supply plan for specific problem three is shown in the table below.
insert image description here

5 Analysis of results

This exercise studies the design of vegetable supply schemes from three vegetable purchasing points A, B, and C in Jiangping City to eight vegetable markets ① to ⑧ in the city, and uses Dijkstra algorithm to calculate the distance between each purchasing point and each vegetable market. For the shortest path, a linear programming model is established according to the subject information, and the model is solved for three problems under different constraints. The analysis of the solution results shows that the shortest path algorithm and linear programming model can effectively solve such problems.

Guess you like

Origin blog.csdn.net/weixin_46838605/article/details/125451766