# constrain variables to a set of values

4 views (last 30 days)
Omar Morsy on 6 Dec 2021
Commented: Matt J on 7 Dec 2021
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
Omar Morsy on 7 Dec 2021
I edited my question to clarify all the missing data

Matt J on 6 Dec 2021
Edited: Matt J on 6 Dec 2021
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;
Matt J on 7 Dec 2021
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

R2021a

### Community Treasure Hunt

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

Start Hunting!