constrain variables to a set of values

1 次查看(过去 30 天)
I have two questions.
My objective finction is L*A (dot product).
I want to optimze my variables which are in matrix (A). But the elemnts of the output of optimizing A should be one value from a set of specific values.
For example: I want to set the output values of A (after the optimization) to be one of the following values { 1:37}.
1) how can I set that the elemnts of the output matrix (A) to be one of the values from the givien set?
And one more thing. I have a ready function (pfile) which I want to use in the optimization problem. That function works as follow:
[w, a, x] = ASU(A)
I get 3 outputs from that function and I would like to minimize my objective function subjected to the output (a) and (x) =0.
2) how can I use the output of the given ASU function as contraints to the optimization problem?
The problem is linear and I am using solver-based.
L is 345x1
A is 1x345
w,a and x are 1x1
ASU accepts only a 1x345 matrix.
If I multiple L*A by a constant (density) it will give me w which is the objective function that I want to minimize
Thanks
  5 个评论
Omar Morsy
Omar Morsy 2021-12-7
编辑:Omar Morsy 2021-12-7
It is a dot product of L and A.
I forgot to clarify that.
Omar Morsy
Omar Morsy 2021-12-7
I edited my question to clarify all the missing data

请先登录,再进行评论。

回答(1 个)

Matt J
Matt J 2021-12-6
编辑:Matt J 2021-12-6
The first thing I recommend is that you use ASU to compute L and the equality constraints in matrix form. You can do that by downloading func2mat,
M=func2mat(@fun,zeros(345,1))
L=M(1,:);
Aeq=M(2:3,:);
function out=fun(A)
[w,a,x]=ASU(A);
out=[w;a;x];
end
Now you make the change of variables A=Z*[1.6; 2.1; 2.2; 17.2] where Z is a 345x4 unknown binary matrix satisfying sum(Z,2)=1. You then solve for Z with intlinprog,
v=[1.6; 2.1; 2.2; 17.2];
f=kron(v',L);
Aeq=kron(v', Aeq); %a=0 and x=0
beq=[0;0];
Aeq=[Aeq; kron([1,1,1,1], speye(345))]; %sum(Z,2)=1
beq(3:347)=1;
lb=zeros(345,4);
Z=intlinprog(f,1:numel(lb),[],[],Aeq,beq,lb,lb+1);
A=Z*v;
  15 个评论
Matt J
Matt J 2021-12-7
If it truly is linear, then this should fix it.
A1=ones(1,345);
wax1=fun(A1);
M=func2mat( @(A)fun(A+1) - wax1 , A1);
L=M(1,:);
Aeq=M(2:3,:);
function out=fun(A)
[w,a,x]=ASU(A);
out=[w;a;x];
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Nonlinear Optimization 的更多信息

产品


版本

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by