Problem with Linear inequality constraints

Hello everyone,
I am trying to do an optimization using genetic algorithm solver. I have linear inequality constraints and i am having trouble properly defining them.
First of all, although my objective function gives not enough input argument error, it works(colud not solve this error i have tried everything). Next step is defining the constraints. I have several constraints, to be more exact including the domain constraints(2) i have 6.
My problem has variables and paremeters with (mostly) dimensions 36x36. I have choosed one constraint to test before proceeding with the rest of the constraints. i have choosed this one ;
sum(over j)(xij)<=1 for each i
what i did was;
i have created A matrix and b vector since this is an inequality constraint. In the A matrix i wrote 1's and 0's. in the b matrix i wrote 1 since that is what i desire.
But it does not assign any values to my variables (xij), and best solution returns as 0.
I will attch the file, if you can have a look i would be appreacited.
Thank you in advance.
Beyza.

10 comentarios

Torsten
Torsten el 30 de Mzo. de 2022
What was your objective function ?
What were possibly other constraints on the x_ij (e.g. only 0 or 1, positive or something else)
Azime Beyza Ari
Azime Beyza Ari el 30 de Mzo. de 2022
Editada: Azime Beyza Ari el 30 de Mzo. de 2022
my obj func is this;
function z = objectivefunc(xij,rij,ai)
xij = reshape(xij,size(rij));
z= sum(xij.*rij.*ai, 'all');
end
Xij has these constraints;
should be binary ( thus integer), row summation should be equal or less than 1, Also it takes place in other constriants.
Matt J
Matt J el 30 de Mzo. de 2022
Editada: Matt J el 30 de Mzo. de 2022
First of all, although my objective function gives not enough input argument error, it works(colud not solve this error i have tried everything)
Do you mean this?
If so, what is unclear? The error message is complaining that you are not providing input arguments and indeed, you are providing none. What are you trying to do in that line?
Azime Beyza Ari
Azime Beyza Ari el 30 de Mzo. de 2022
Editada: Azime Beyza Ari el 30 de Mzo. de 2022
Actually you recommended that line to me, in an another thread. here is the link.
At this point, i do not know what to do. What does it need me to do to provide input. rij and ai are in the workspace, xij is a decisison variable.
The line that is causing the error is the one I highlighted in blue. I did not tell you to put it there. You are invoking your objective function without passing in any xij. Did you mean to have,
objfun2(solution2)
Azime Beyza Ari
Azime Beyza Ari el 30 de Mzo. de 2022
ı fixed it and now it says this;
Get rid of this line
% Clear variables
clearvars objfun2 options2
Azime Beyza Ari
Azime Beyza Ari el 30 de Mzo. de 2022
I did not write that line. And can not delete it, it does not allow me.
Matt J
Matt J el 30 de Mzo. de 2022
Editada: Matt J el 30 de Mzo. de 2022
What are you trying to do? Are you just trying to see the objective function value? If so, just write,
objectiveValue
I did not write that line. And can not delete it, it does not allow me.
Just for reference, you can convert the Live Task to editable code using the drop down menu in the upper right corner of the task
Azime Beyza Ari
Azime Beyza Ari el 30 de Mzo. de 2022
Yes, Thank you!

Iniciar sesión para comentar.

 Respuesta aceptada

Matt J
Matt J el 30 de Mzo. de 2022
Editada: Matt J el 30 de Mzo. de 2022
sum(over j)(xij)<=1 for each i
This constraint can be written,
x=rand(36);
[m,n]=size(x);
A=kron(ones(1,n),speye(m)); b=ones(m,1);
You can check the equivalence as follows:
difference=sum(x,2) - A*x(:)
difference = 36×1
0 0 0 0 0 0 0 0 0 0

17 comentarios

Azime Beyza Ari
Azime Beyza Ari el 30 de Mzo. de 2022
This is the constraint.
I failed to understand the code you provided. Can you please explain.
Matt J
Matt J el 30 de Mzo. de 2022
Editada: Matt J el 30 de Mzo. de 2022
i have created A matrix and b vector since this is an inequality constraint. In the A matrix i wrote 1's and 0's.
sum(x,2) and A*x(:) give the same result. This is the A matrix you want, not the one in your current code.
Azime Beyza Ari
Azime Beyza Ari el 30 de Mzo. de 2022
Yes but that constriant is not my only constraint
Torsten
Torsten el 30 de Mzo. de 2022
Azime is a beginner in MATLAB programming.
I think it is more error-prone if she tries to use commands like
A=kron(ones(1,n),speye(m))
than setting up a simple example manually.
Although it is of course much more efficient and safe if one knows how it works out.
It is your only constraint, apart from the bounds, and the way you have built it currently is wrong. Also, I now notice that you have set Intcon=nvars. If you want all variables to be integer constrained, you should have Intcon=1:nvars in your call to ga.
IntCon=1:nvars;
[solution2,objectiveValue] = ga(objfun2,nvars,A,b,[],[],zeros(nvars,1),...
ones(nvars,1),[],IntCon,options2);
Azime Beyza Ari
Azime Beyza Ari el 30 de Mzo. de 2022
Editada: Azime Beyza Ari el 30 de Mzo. de 2022
This is the model i am working on.
Yes i want them to be binary so 1 and 0.
Matt J
Matt J el 30 de Mzo. de 2022
Editada: Matt J el 30 de Mzo. de 2022
@Torsten
I think it is more error-prone if she tries to use commands like A=kron(ones(1,n),speye(m)) than setting up a simple example manually.
Simple examples fine, but this A matrix will be 36 x 1296 with 1296 non-zeros. That is a lot of data entry.
Azime Beyza Ari
Azime Beyza Ari el 30 de Mzo. de 2022
it is actually bigger than 36x1296. Since there is summation operations on constraints i expect it to be something like 120x1296
Matt J
Matt J el 30 de Mzo. de 2022
Editada: Matt J el 30 de Mzo. de 2022
As an alternative to using kron(), you can use the problem-based framework to set up the constraints. This might be more intuitive,
x=optimvar('xij',[36,36],'Type','integer','LowerBound',0,'UpperBound',1);
con.sumrows=(sum(x,2)<=1);
However, because the problem-based framework cannot currently invoke ga(), you will need to interface to it by some other means. For this, I recommend downloading prob2matrices(),
whereupon you can do,
p=prob2matrices({x},'Constraints', con);
[solution2,objectiveValue] = ga(objfun2,numel,p.A,p.b,[],[],p.lb,...
p.ub,[],p.IntCon,options2);
Azime Beyza Ari
Azime Beyza Ari el 30 de Mzo. de 2022
After this how can i add other canstraints? like i said i have 3 other constraints. Is this a solution to the scenerio where i only have 1 constraint?
Matt J
Matt J el 30 de Mzo. de 2022
Editada: Matt J el 30 de Mzo. de 2022
Yes, what I've shown so far is just for the constraints on the row-sums of x. I encourage you to read more about the problem-based optimization framework, but briefly, you can include more variables and constraints using the same kind of natural mathematical description, e.g.,
x=optimvar('x',[36,36],'Type','integer','LowerBound',0,'UpperBound',1);
y=optimvar('y',[36,1]);
con.sumxrows=(sum(x,2)<=1);
con.sumy=(sum(y)<=p);
...
You will find that it is almost like writing out your problem in print form as you did here.
One last remark. Your problem looks like a mixed integer linear program (linear objective, linear and integer constraints). Therefore, ga() is the wrong tool for this, unless you are just testing ga() for educational purposes. The most suitable way to solve a problem like you've shown is with solve() (which will call intlinprog) and that would require no File Exchange downloads.
Torsten
Torsten el 30 de Mzo. de 2022
You should start with this example. It will show you step by step how to set up a problem like yours.
Azime Beyza Ari
Azime Beyza Ari el 30 de Mzo. de 2022
Thank you, I believe this will help me further.
(I must use ga to solve this, i know if i used any other solver the work would be easier but this is a must. Thank you for your recommendation.)
Torsten
Torsten el 30 de Mzo. de 2022
It would not be easier with a different solver since in all MATLAB solvers for optimization, you would have to set up A and b. That's an advantage: Once you have A and b set up correctly, you can switch between ga and intlinprog.
Azime Beyza Ari
Azime Beyza Ari el 30 de Mzo. de 2022
So it all ends with how i set up A, b, and (of course) objfunc. Which all three i am currently failing to do properly.
Matt J
Matt J el 30 de Mzo. de 2022
Editada: Matt J el 30 de Mzo. de 2022
It would not be easier with a different solver since in all MATLAB solvers for optimization, you would have to set up A and b.
No, if you were to use intlinprog, but through the problem-based workflow, there would be no need to explicitly create A and b. This would be done for you internally by optimproblem().
So it all ends with how i set up A, b, and (of course) objfunc Which all three i am currently failing to do properly.
So far, nothing has been found to be incorrect in your objective function.
Torsten
Torsten el 30 de Mzo. de 2022
Editada: Torsten el 30 de Mzo. de 2022
It would not be easier with a different solver since in all MATLAB solvers for optimization, you would have to set up A and b.
I should have added: if you use the solver-based approach.
The problem-based approach looks simple, but I have the feeling that one looses too much control over the problem. Checking how settings are internally translated (kind of debugging possibility) is impossible (at least as far as I can see). But as so many things: it's a matter of taste.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 30 de Mzo. de 2022

Editada:

el 30 de Mzo. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by