由于我是MATLAB中的优化工具箱的新手,我正在努力使用正确的代码来优化我的目标函数。一开始我有以下数据:
a = [3 3 1 1];
c = [2 5 5 10];
我的目标函数以及平等和不平等约束是:
F=∑(k=1..4){[0.2.*a(k)]+[0.16.*x_1(k)]+[b(k).*x_2 (k)]}
目标函数F
是来自k = 1..4
平等约束:
a(k)+x_1 (k)+x_2 (k)=c(k) AND d(k)=x_1 (k)+d(k-1)
不平等约束
x_1 (k)≤d(k)≤10
优化上述功能的变量是x1(k)
和x2(k)
。有一种方法我该怎么做?
答案 0 :(得分:1)
由于您的约束是线性的,因此您可以使用linear programming:
clear all;
n_k = 4; % number of k
n_v = 3; % number of variables
a = [3 3 1 1];
b = [0.19 0.19 0.19 0.19];
c = [2 5 5 10];
d0 = 0;
% Objective x1 x2 d
f = zeros(1, n_k * n_v);
f(1:n_v:end) = 0.16; % x1 coefficient
f(2:n_v:end) = b; % x2 coefficient (b(k))
% Equality 1
eq1 = [1 1 0]; % x1 + x2 + 0 * d = c(k) - a(k)
Aeq = zeros(2 * n_k, n_v * n_k);
for i = 1:1:n_k
Aeq(i, :) = cat(2, eq1, zeros(1, n_k * n_v - length(eq1)));
eq1 = cat(2, zeros(1, n_v), eq1);
end
eq2 = [1 1 0 -1]; % d(k-1) + x1(k) + 0 * x2(k) - d(k) = 0
Aeq(n_k + 1, :) = [1 0 -1 0 0 0 0 0 0 0 0 0];
Aeq(n_k + 2, :) = [0 0 1 1 0 -1 0 0 0 0 0 0];
Aeq(n_k + 3, :) = [0 0 0 0 0 1 1 0 -1 0 0 0];
Aeq(n_k + 4, :) = [0 0 0 0 0 0 0 0 1 1 0 -1];
beq = zeros(1, 8);
beq(1:n_k) = c - a;
beq(5) = -d0;
% Inequality
le1 = [1 0 -1];
A = zeros(n_k, n_v * n_k);
for i = 1:1:n_k
A(i, :) = cat(2, le1, zeros(1, n_k * n_v - length(le1)));
le1 = cat(2, zeros(1, n_v), le1);
end
b = zeros(1, 4);
ub = inf(1, n_k * n_v);
ub(3:n_v:end) = 10;
lb = -inf(1, n_k * n_v);
并解决:
X = linprog(f, A, b, Aeq, beq, lb, ub);
输出:
>> reshape(X, 3, 4)
ans =
5.8261 1.1680 -4.1562 7.1621
-6.8261 0.8320 8.1562 1.8379
5.8261 6.9941 2.8379 10.0000
您可以查看:
>> [X(1:3:end), X(2:3:end), transpose(a),
X(1:3:end) + X(2:3:end) + transpose(a), transpose(c)]
ans =
5.8261 -6.8261 3.0000 2.0000 2.0000
1.1680 0.8320 3.0000 5.0000 5.0000
-4.1562 8.1562 1.0000 5.0000 5.0000
7.1621 1.8379 1.0000 10.0000 10.0000
>> [X(3:3:end), X(1:3:end), X(3:3:end) - X(1:3:end), [0; X(3:3:11)]]
ans =
5.8261 5.8261 -0.0000 0
6.9941 1.1680 5.8261 5.8261
2.8379 -4.1562 6.9941 6.9941
10.0000 7.1621 2.8379 2.8379
>> [X(1:3:end), X(3:3:end)]
ans =
5.8261 5.8261
1.1680 6.9941
-4.1562 2.8379
7.1621 10.0000
旧答案(评论前)
% Objective a c d x1 x2
f = [.2 0 0 .16 .19];
f = repmat(f, 1, 4);
% Equality 1
eq1 = [1 -1 0 1 1]; % a c d x1 x1
eq2 = [1 0 0 0 0 -1 1 0]; % d(k-1) x1(k-1) x2(k-1) a(k) c(k) d(k) x1(k) x2(k)
Aeq = [cat(2, eq1, zeros(1, 15));
cat(2, zeros(1, 5), eq1, zeros(1, 10));
cat(2, zeros(1, 10), eq1, zeros(1, 5));
cat(2, zeros(1, 15), eq1);
cat(2, zeros(1, 2), eq2, zeros(1, 10));
cat(2, zeros(1, 7), eq2, zeros(1, 5));
cat(2, zeros(1, 12), eq2)];
beq = zeros(1, 7);
% Inequality
le1 = [0 0 -1 1 0];
A = [cat(2, le1, zeros(1, 15));
cat(2, zeros(1, 5), le1, zeros(1, 10));
cat(2, zeros(1, 10), le1, zeros(1, 5));
cat(2, zeros(1, 15), le1)];
b = zeros(1, 4);
ub = [inf inf 10 inf inf];
ub = repmat(ub, 1, 4);
lb = ones(1, 5) * (-inf);
lb = repmat(lb, 1, 4);
ub(3) = d0;
lb(3) = d0;
并解决:
linprog(f, A, b, Aeq, beq, lb, ub)
请注意,您的原始帖子中可能缺少某些信息,因为看起来这个问题是无限制的。