我使用以下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')
答案 0 :(得分:3)
你需要稍微解决问题。您正在尝试找到使{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])
返回
L =
0.3383 0.6180
fval =
1.2800
答案 1 :(得分:0)
还可以使用以下MATLAB代码在convex optimization package CVX中解决此问题:
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,每个约束和目标函数必须使用CVX的规则集以可证明凸的方式编写。替换x1 = log(l1)
和x2 = log(l2)
允许人们这样做。请注意:log_sum_exp([0,x1]) = log(exp(0) + exp(x1)) = log(1 + l1)
这也会返回答案:l1 = .3383,l2 = .6180,T = -1.2800