【问题标题】:Linear Programming with summations in MatLab在 MatLab 中进行求和的线性规划
【发布时间】:2017-04-17 11:00:47
【问题描述】:

我正在尝试复制我在 Matlab 的一篇研究论文中发现的线性优化。我需要解决以下线性优化问题:

其中C1C2C3C4C5、w1 和 w2 是优先权重。

j 取自 1 到 12(一年中的 12 个月)。

以下限制适用:

I(j) 和 L(j) 是每月记录。

我使用 MatLab 编写了一个解决方案。这是我的代码(我对此很陌生,所以请原谅任何不好的编码!):

%set up the data for the year:
I = [72.6 26.0 23.2 20.4 15.2 22.0 40.9 45.2 38.7 41.4 142.2 116.8]
L = [1.6 1.3 0.8 0.6 0.6 0.6 1 1.5 1.8 1.8 1.8 2.0];
%set up the initial level:
S0 = 683
%set up the priority weightings
w2= 1;
w1 = 1.5;
C1 = 3;
C2 = 2;
C3 = 5;
C4 = 4;
C5 = -5;
%set up the constraint equation, lower bond and upper bound
A = [(C1*w1) C2 (C3*w2) (C4*w2) C5];
Aeq = [1 1 1 1 1];
lb = [70 0 0 0 0];
ub = [815 54.14 13.4 41.8 17345];
%set up a blank matrix to store the results
x=zeros(12,5);

%for each month calculate the optimum and store in the matrix
for j = 1:12
    Beq = [(I(j)+S0-L(j))];
    x(j,:) = linprog(-A,[],[],Aeq,Beq,lb,ub);
    S0 = x(j,1);
end

%output the result
opt = x

问题是当我将我的结果与研究论文进行比较时,我发现我得到了不同的结果。最终我意识到我正在寻找每个月的最佳值,而不是全年的全局最佳值。我一直在网上搜索如何找到全年的最佳值(即优化求和函数),但我找不到任何东西。谁能指出我正确的方向?

【问题讨论】:

  • matlab的linprog中没有求和。它完全基于矩阵。对于某些问题,这不是问题,而对于其他问题,这简直就是噩梦(参见here)。在您的情况下,您需要建立一个大矩阵,每一列对应一个变量,每一行对应一个约束。
  • @ErwinKalvelagen 非常感谢您的回复。我用你的例子来解决我的问题。

标签: matlab optimization


【解决方案1】:

感谢@ErwinKalvelagen,我能够解决我的问题。这是我未来对其他人的解决方案:

%set up the data for the year:
I = [72.6; 26.0; 23.2; 20.4; 15.2; 22.0; 40.9; 45.2; 38.7; 41.4; 142.2; 116.8;];
I = I*1; %allows the inflow to be scaled to test the model for a dry or wet year
L = [1.6; 1.3; 0.8; 0.6; 0.6; 0.6; 1; 1.5; 1.8; 1.8; 1.8; 2.0;];

%set up the priority weightings
w1 = 2;
w2= 50;
C1 = 3;
C2 = 2;
C3 = 5;
C4 = 4;
C5 = -5;

%set up the constraint equation, lower bond and upper bound
A = [(C1*w1) C2 (C3*w2) (C4*w2) C5 (C1*w1) C2 (C3*w2) (C4*w2) C5 (C1*w1) C2 (C3*w2) (C4*w2) C5 (C1*w1) C2 (C3*w2) (C4*w2) C5 (C1*w1) C2 (C3*w2) (C4*w2) C5 (C1*w1) C2 (C3*w2) (C4*w2) C5 (C1*w1) C2 (C3*w2) (C4*w2) C5 (C1*w1) C2 (C3*w2) (C4*w2) C5 (C1*w1) C2 (C3*w2) (C4*w2) C5 (C1*w1) C2 (C3*w2) (C4*w2) C5 (C1*w1) C2 (C3*w2) (C4*w2) C5 (C1*w1) C2 (C3*w2) (C4*w2) C5];

%set up spare matrix for Aeq
Aeq = zeros(12,60);

% Populate Aeq 
% first the positive portions of the monthly data
row = 1;
coloumn = 1;
for counter = 1:12
    for counter = 1:5
        Aeq(coloumn,row)=1;
        row = row + 1;
    end
    coloumn = coloumn+1;
end
% then the -S0 for each month
Aeq(1, 56)=-1;
coloumn = 1;
for row = 2:12
    Aeq(row,coloumn)=-1;
    coloumn = coloumn+5;
end

%populate Beq
Beq = I-L;

%populate the lower and upper bounds
Smin = 70;
Smax_1_9 = 731.2;
Smax_10_12 = 673.2
QDmin = 0
QDmax = 54.14
Rmin = 0;
Rmax = 13.4;
RRmin = 0;
RRmax = 41.8;
SPILLmin = 0;
SPILLmax = 17345;
%first the lower bound
lbmonthly = [Smin; QDmin; Rmin; RRmin; SPILLmin;];
lb = vertcat(lbmonthly,lbmonthly,lbmonthly,lbmonthly,lbmonthly,lbmonthly,lbmonthly,lbmonthly,lbmonthly,lbmonthly,lbmonthly,lbmonthly);
%second the upper bound
ubmonthly_1_9 = [Smax_1_9; QDmax; Rmax; RRmax; SPILLmax;];
ubmonthly_10_12 = [Smax_10_12; QDmax; Rmax; RRmax; SPILLmax;];
ub = vertcat(ubmonthly_1_9, ubmonthly_1_9, ubmonthly_1_9, ubmonthly_1_9, ubmonthly_1_9, ubmonthly_1_9, ubmonthly_1_9, ubmonthly_1_9, ubmonthly_1_9, ubmonthly_10_12, ubmonthly_10_12, ubmonthly_10_12);

%find the optimal
opt = linprog(-A,[],[],Aeq,Beq,lb,ub);

%output the result as a matrix
opt = reshape(opt,5,12)'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多