当 objective 函数处于约束中时使用 fmincon MATLAB 求解优化
Solve optimization using fmincon MATLAB when objective function is in constraints
我要解决:
我使用以下 MATLAB 代码,但它不起作用。
有人可以指导我吗?
function f=objfun
f=-f;
function [c1,c2,c3]=constraint(x)
a1=1.1; a2=1.1; a3=1.1;
c1=f-log(a1)-log(x(1)/(x(1)+1));
c2=f-log(a2)-log(x(2)/(x(2)+1))-log(1-x(1));
c3=f-log(a3)-log(1-x(1))-log(1-x(2));
x0=[0.01;0.01];
[x,fval]=fmincon('objfun',x0,[],[],[],[],[0;0],[1;1],'constraint')
你需要稍微翻转一下问题。您正在尝试找到使 3 个 LHS 函数中的最小值最大的点 x
(即 (l_1,l_2)
)。所以,你可以用伪代码重写你的问题,
maximise, by varying x in [0,1] X [0,1]
min([log(a1)+log(x(1)/(x(1)+1)) ...
log(a2)+log(x(2)/(x(2)+1))+log(1-x(1)) ...
log(a3)+log(1-x(1))+log(1-x(2))])
由于Matlab有fmincon
,将其重写为最小化问题,
minimise, by varying x in [0,1] X [0,1]
max(-[log(a1)+log(x(1)/(x(1)+1)) ...
log(a2)+log(x(2)/(x(2)+1))+log(1-x(1)) ...
log(a3)+log(1-x(1))+log(1-x(2))])
所以实际代码是
F=@(x) max(-[log(a1)+log(x(1)/(x(1)+1)) ...
log(a2)+log(x(2)/(x(2)+1))+log(1-x(1)) ...
log(a3)+log(1-x(1))+log(1-x(2))])
[L,fval]=fmincon(F,[0.5 0.5])
哪个returns
L =
0.3383 0.6180
fval =
1.2800
也可以在convex optimization package CVX中用下面的MATLAB代码解决这个问题:
cvx_begin
variables T(1);
variables x1(1);
variables x2(1);
maximize(T)
subject to:
log(a1) + x1 - log_sum_exp([0, x1]) >= T;
log(a2) + x2 - log_sum_exp([0, x2]) + log(1 - exp(x1)) >= T;
log(a3) + log(1 - exp(x1)) + log(1 - exp(x2)) >= T;
x1 <= 0;
x2 <= 0;
cvx_end
l1 = exp(x1); l2 = exp(x2);
要使用 CVX,每个约束和 objective 函数都必须以使用 CVX 规则集证明是凸的方式编写。进行替换 x1 = log(l1)
和 x2 = log(l2)
可以做到这一点。注意:log_sum_exp([0,x1]) = log(exp(0) + exp(x1)) = log(1 + l1)
这也是 returns 答案:l1 = .3383,l2 = .6180,T = -1.2800
我要解决:
我使用以下 MATLAB 代码,但它不起作用。
有人可以指导我吗?
function f=objfun
f=-f;
function [c1,c2,c3]=constraint(x)
a1=1.1; a2=1.1; a3=1.1;
c1=f-log(a1)-log(x(1)/(x(1)+1));
c2=f-log(a2)-log(x(2)/(x(2)+1))-log(1-x(1));
c3=f-log(a3)-log(1-x(1))-log(1-x(2));
x0=[0.01;0.01];
[x,fval]=fmincon('objfun',x0,[],[],[],[],[0;0],[1;1],'constraint')
你需要稍微翻转一下问题。您正在尝试找到使 3 个 LHS 函数中的最小值最大的点 x
(即 (l_1,l_2)
)。所以,你可以用伪代码重写你的问题,
maximise, by varying x in [0,1] X [0,1]
min([log(a1)+log(x(1)/(x(1)+1)) ...
log(a2)+log(x(2)/(x(2)+1))+log(1-x(1)) ...
log(a3)+log(1-x(1))+log(1-x(2))])
由于Matlab有fmincon
,将其重写为最小化问题,
minimise, by varying x in [0,1] X [0,1]
max(-[log(a1)+log(x(1)/(x(1)+1)) ...
log(a2)+log(x(2)/(x(2)+1))+log(1-x(1)) ...
log(a3)+log(1-x(1))+log(1-x(2))])
所以实际代码是
F=@(x) max(-[log(a1)+log(x(1)/(x(1)+1)) ...
log(a2)+log(x(2)/(x(2)+1))+log(1-x(1)) ...
log(a3)+log(1-x(1))+log(1-x(2))])
[L,fval]=fmincon(F,[0.5 0.5])
哪个returns
L =
0.3383 0.6180
fval =
1.2800
也可以在convex optimization package CVX中用下面的MATLAB代码解决这个问题:
cvx_begin
variables T(1);
variables x1(1);
variables x2(1);
maximize(T)
subject to:
log(a1) + x1 - log_sum_exp([0, x1]) >= T;
log(a2) + x2 - log_sum_exp([0, x2]) + log(1 - exp(x1)) >= T;
log(a3) + log(1 - exp(x1)) + log(1 - exp(x2)) >= T;
x1 <= 0;
x2 <= 0;
cvx_end
l1 = exp(x1); l2 = exp(x2);
要使用 CVX,每个约束和 objective 函数都必须以使用 CVX 规则集证明是凸的方式编写。进行替换 x1 = log(l1)
和 x2 = log(l2)
可以做到这一点。注意:log_sum_exp([0,x1]) = log(exp(0) + exp(x1)) = log(1 + l1)
这也是 returns 答案:l1 = .3383,l2 = .6180,T = -1.2800