Integer constrained optimization using the "ga" (genetic algorithm) solver of MATLAB - can anyone help?
Ältere Kommentare anzeigen
I tried to do mono-objective linear optimization subject to linear equality and inequality constraints and over binary decision variables (o or 1) using the "ga" solver of MATLAB. MATLAB optimization "ga" toolbox did not help, because many constraints are violated and not satisfied. However i have transformed the equality constraints to inequality constraints using a tolerance. Does anybody have an explanation or an idea to overcome this problem?
Akzeptierte Antwort
Weitere Antworten (3)
imed NASRI
am 3 Nov. 2013
0 Stimmen
I tried to do mono-objective linear optimization subject to linear equality and inequality constraints and over binary decision variables (o or 1) using the "ga" solver of MATLAB. MATLAB optimization "ga" toolbox did not help,
BINTPROG would be the more appropriate solver, if the objective and all constraints are linear.
6 Kommentare
imed NASRI
am 3 Nov. 2013
Matt J
am 3 Nov. 2013
If you must use ga(), you could try reformulating the equality constraints with some slack, e.g. instead of x1+x2=d, you would have
TolCon=1e-6;
slack=TolCon*100;
x1+x2 <=d+slack
-x1-x2<=-d+slack
This is not the same as what you are doing already.
imed NASRI
am 3 Nov. 2013
Matt J
am 3 Nov. 2013
Well, you'll have to convince your supervisors to use BINTPROG...
imed NASRI
am 3 Nov. 2013
Matt J
am 3 Nov. 2013
I don't think you can trick ga() into thinking that it is bintprog()...
Another thing you could try is to eliminate the equality constraints by solving for some variables in terms of the others. For example, if you had the single equality constraint
x1+x2+x3=d
then you could eliminate both the constraint and x3 from the problem by replacing x3 everywhere by d-x1-x2. The same kind of thing could be done with systems of inequality constraints.
Then you would be left with inequality constraints only, which ga() can handle.
73 Kommentare
imed NASRI
am 4 Nov. 2013
Bearbeitet: imed NASRI
am 4 Nov. 2013
imed NASRI
am 4 Nov. 2013
If your equality constraints can be partitioned
[A1, A2]*[x1;x2]=b
where A2 is a square nonsingular matrix, then x2 can be eliminated and written in terms of x1 by re-arranging and solving:
x2 = A2\(b-A1*x1) = (A2\b) - (A2\A1)*x1
Below is a function that can help make such partitions,
function [Xsub,idx]=licols(X,tol)
%Extract a linearly independent set of columns of a given matrix X
%
% [Xsub,idx]=licols(X)
%
%in:
%
% X: The given input matrix
% tol: A rank estimation tolerance. Default=1e-10
%
%out:
%
% Xsub: The extracted columns of X
% idx: The indices (into X) of the extracted columns
if ~nnz(X) %X has no non-zeros and hence no independent columns
Xsub=[]; idx=[];
return
end
if nargin<2, tol=1e-10; end
[Q, R, E] = qr(X,0);
if ~isvector(R)
diagr = abs(diag(R));
else
diagr = R(1);
end
%Rank estimation
r = find(diagr >= tol*diagr(1), 1, 'last'); %rank estimation
idx=sort(E(1:r));
Xsub=X(:,idx);
imed NASRI
am 4 Nov. 2013
imed NASRI
am 4 Nov. 2013
Bearbeitet: imed NASRI
am 4 Nov. 2013
imed NASRI
am 4 Nov. 2013
I think that it can be a good idea to overcome the problem of equality constraints for a small problem. But i deal with a big problem (100.000) constraints and 40.200 variables.
Do you mean 100000 equality constraints? It sounds wrong that you have more equality constraints than variables. This means that either you have redundant equations (which you can therefore get rid of) or the solution to the linear equalities is unique, making the problem trivial.
Yes, I have passed them as nonlinear constraints [c,ceq] and it works.
Would the matrix of coefficients be sparse? If so, it's not a good idea to disguise linear constraints as nonlinear ones. The solvers benefit from knowing which constraints are linear and which are not. I wonder vaguely whether that could explain why slack didn't work earlier.
If the linear equations are sparse, you can convert them to matrices by evaluating the equations at the columns of speye(N);
N=42000;
E=speye(N);
columns=cell(1,N);
for i=1:4N
columns{i}=evaulate_equations(E(:,i))
end
matrix=cell2mat(columns);
That's what I would advise you to do. Of course, it would be better to abandon symbolic equation manipulation altogether. It doesn't make obvious sense to work this way when everything is linear. Linear equations is what the numerical side of MATLAB does best.
imed NASRI
am 4 Nov. 2013
Bearbeitet: imed NASRI
am 4 Nov. 2013
Matt J
am 4 Nov. 2013
N=10;
E=speye(N,N+1);
columns=cell(1,N);
beq=-evaluate_equations(E(:,N+1));
for i=1:N
columns{i}=evaluate_equations(E(:,i))+ beq;
end
Aeq=cell2mat(columns);
full(Aeq),
full(beq),
function F=evaluate_equations(x)
F(1)=2*x(1)+x(2)-10;
F(2)=x(9)+3*x(10)-7;
F=F(:); %column vector
imed NASRI
am 4 Nov. 2013
What did u mean by "equations are sparse?"
In MATLAB, a matrix which contains mostly zeros can be held in sparse form. This conserves memory and speeds up lots of different kinds of manipulations. E.g.,
>> A=eye(5000); Asparse=sparse(A); whos A Asparse
Name Size Bytes Class Attributes
A 5000x5000 200000000 double
Asparse 5000x5000 120008 double sparse
See "doc sparse" for more info.
It attaches a file that contains some equations of my problem. Can u show me please how to convert them to matrices?
See the example in my last Comment. There, evaluate_equations() takes the place of whatever you use to evaluate the left hand side of the equalities.
imed NASRI
am 5 Nov. 2013
Bearbeitet: imed NASRI
am 5 Nov. 2013
Matt J
am 5 Nov. 2013
N is the number of unknowns.
imed NASRI
am 5 Nov. 2013
Matt J
am 5 Nov. 2013
Probably because the mfile you put the code in doesn't have
function in=f(out)
at the top.
imed NASRI
am 5 Nov. 2013
imed NASRI
am 5 Nov. 2013
Bearbeitet: imed NASRI
am 5 Nov. 2013
imed NASRI
am 5 Nov. 2013
imed NASRI
am 5 Nov. 2013
Matt J
am 5 Nov. 2013
Once ga has solved for x1,x2 you can recover x3 from the equation
x3=d-x1-x2
imed NASRI
am 5 Nov. 2013
Bearbeitet: imed NASRI
am 5 Nov. 2013
imed NASRI
am 5 Nov. 2013
Bearbeitet: imed NASRI
am 5 Nov. 2013
No, not manually. We talked about making the matrix partition Aeq=[A1,A2] and using it to derive a matrix expression for the eliminated variables,
x_elim = (A2\beq)-(A2\A1)*x_keep
= c-D*x_keep
This elimination formula can be substituted into your existing expressions and used to derive new ones. These operations can be done using MATLAB matrix manipulation expressions independent of the problem dimension.
For example, the linear inequalities can be similarly partitioned with Aineq=[A3,A4] leading to
[A3, A4]*[x_keep; x_elim] <=bineq
Substituting the variable elimination formula leads to
bineq >= A3*x_keep +A4*x_elim
= A3*x_keep +A4*(c-D*x_keep)
= (A3-A4*D)*x_keep +A4*c
Rearranging this leads to reduced inequalities
(A3-A4*D)*x_keep <= (bineq - A4*c)
I.e., your new Aineq is (A3-A4*D) while the vector (bineq-A4*c) replaces your original bineq.
imed NASRI
am 6 Nov. 2013
Matt J
am 6 Nov. 2013
There's no reason to be applying licols() to Aineq. It is Aeq that is used to eliminate variables. Therefore, it is the partitioning of Aeq that determines the partitioning of everything else.
imed NASRI
am 6 Nov. 2013
Matt J
am 6 Nov. 2013
Yes, but that partitioning is dictated by licols(Aeq), not licols(Aineq). Once you've used licols(Aeq) to partition x into x_elim and x_keep, you now want to rewrite
Aineq*x<=bineq
in terms of the same x_elim and x_keep.
Note that licols has additional output arguments telling you the indices of the matrix columns that belong to x_elim. That info is reusable as you reformulate both your inequality constraints and any nonlinear constraints you might add later.
imed NASRI
am 6 Nov. 2013
imed NASRI
am 6 Nov. 2013
Matt J
am 6 Nov. 2013
A2=Aeq(:,idx)
x_elim=x(idx);
Indicies not in idx correspond to x_keep and A1.
imed NASRI
am 6 Nov. 2013
imed NASRI
am 6 Nov. 2013
Matt J
am 6 Nov. 2013
lb and ub give additional inequality constraints. They can be re-expressed by adding rows to Aineq.
imed NASRI
am 6 Nov. 2013
Bearbeitet: imed NASRI
am 6 Nov. 2013
imed NASRI
am 6 Nov. 2013
Bearbeitet: imed NASRI
am 6 Nov. 2013
frankly, I do not see how to reduce automatically the problem and then find x_elim and x_keep just by manipulating matrices in Matlab.
What about everything we discussed yesterday?!?! How can you not see it, when you've already been shown matrix equations for it?
Anyway, let me be clear. This whole reformulation exercise has only been to accomodate your supervisor's peculiar obsession with GA. The true recommendable way to solve the problem is bintprog.
imed NASRI
am 6 Nov. 2013
Matt J
am 6 Nov. 2013
As I said before, the output idx from licols tells you the indices to be eliminated. Therefore
idx_keep = setdiff(1:30,idx);
u_keep=u(idx_keep);
imed NASRI
am 6 Nov. 2013
Bearbeitet: imed NASRI
am 6 Nov. 2013
My problem is how to change automatically the indices in the new reduced vector u to have the new formulation as follow:
You aren't using ga to solve for u anymore. You are also not writing your functions in terms of
[u(1);u(2);u(3);u(4);u(5);u(19);u(20);u(24);u(27)].'
You are supposed to be writing them in terms of
u_keep = [u_keep(1),...,u_keep(9)].'
Furthermore, in your code, you don't need to index either u_keep(i) or u(i) individually, because you now have matrices to represent the objective and constraints. You should be calling ga() using the syntax
x = ga(fitnessfcn,9,A,b,Aeq,beq,LB,UB)
where A,b,Aeq,beq are the reduced matrices and LB,UB are bounds on u_keep that we've been talking about.
You shouldn't have to use the nonlcon nonlinear constraint arguments anymore, but even if you were to do so, you wouldn't write individual equations for F(i). You would write vector/matrix expressions for F, and you would write them in terms of u_keep, not u:
x = ga(fitnessfcn,9,[],[],[],[],LB,UB,...
@(u_keep) constraints(u_keep,Aeq,beq,Aineq,bineq) )
function [Fineq, F]=constraints(ukeep, Aeq,beq,Aineq,bineq)
F=Aeq*u_keep-beq;
Fineq=Aineq*u_keep-bineq;
end
The only indexing you need to do is at the very end when you want to translate ga's solution for u_keep back into the full u in the original formulation. For that, you would do
u=zeros(30,1);
u(idx_keep)= u_keep;
u(idx)=c-D*u_keep; %the elimination equation
imed NASRI
am 6 Nov. 2013
Matt J
am 6 Nov. 2013
Hurray!!
imed NASRI
am 8 Nov. 2013
Bearbeitet: imed NASRI
am 8 Nov. 2013
imed NASRI
am 8 Nov. 2013
Bearbeitet: imed NASRI
am 8 Nov. 2013
To conclude, it's not a good idea to eliminate equality constraints because they can be violated !!!!!
No, you overlooked part of what I told you earlier. You must use the reduction formula to convert your upper and lower bound inequalities in addition to your original Aineq, bineq inequalities.
So for example, in your original 3-variable formulation above, you originally have the bound inequality
x3>=0
When you apply the reduction formula you obtain
1-x1-x2>=0
or equivalently
x1+x2<=1
This must be appended to the reduced Aineq, bineq. If it is, the algorithm will not be allowed to accept x1=x2=1 as a solution, since it clearly violates the above inequality.
imed NASRI
am 9 Nov. 2013
If ga() couldn't find a point x_keep that satisfies the constraints, the exitflag and exit messages would tell you that a feasible point was not found. In that case, it's always possible that your constraints are not feasible. For example, the following constraint set is non-empty on the reals, but empty on the integers
x1>=0
x2>=0
x1+x2<=.5
If a feasible x_keep point was found, but the constraints on x_elim are still violated, then you have a coding error.
imed NASRI
am 9 Nov. 2013
imed NASRI
am 9 Nov. 2013
Bearbeitet: imed NASRI
am 9 Nov. 2013
Matt J
am 10 Nov. 2013
Then maybe a feasible solution does not exist. You can always check by solving the problem (as originally formulated) using bintprog.
imed NASRI
am 10 Nov. 2013
A feasible solution exists because ga() finds a feasible solution in some runs.
Meaning that you are not changing the problem data at all between runs? You are running ga() multiple times with the exact same initial population in the exact some way on the exact same problem?
LINGO finds quickly an optimal solution that satisfied all constraints.
You should take the LINGO solution and check if it satisfies the reduced constraints as well. If it does not, you have a coding error in the reduction step. The reduced problem is equivalent to the original problem.
If the LINGO solution does satisfy both the original constraints and the reduced constraints, then it confirms what I think we suspected all along: GA is not the most robust solver you can use for these problems. You can report that to your supervisor, who insists on using it.
Regardless, you might try including the LINGO solution in the Initial Population when running GA. It could be just an initialization issue.
imed NASRI
am 10 Nov. 2013
imed NASRI
am 10 Nov. 2013
Bearbeitet: imed NASRI
am 10 Nov. 2013
imed NASRI
am 13 Nov. 2013
Bearbeitet: imed NASRI
am 13 Nov. 2013
imed NASRI
am 13 Nov. 2013
imed NASRI
am 13 Nov. 2013
Bearbeitet: imed NASRI
am 13 Nov. 2013
i have noticed that when i add the optimal solution of LINGO to the intial population and when i set EliteCount to 0, the ga() can not find a solution.
EliteCount is supposed to be a positive integer less than the population size. Otherwise, no population members survive to the next generation.
This function is not suitable
I'd be curious to know what advantage your supervisor thought it would have over bintprog (or LINGO).
imed NASRI
am 13 Nov. 2013
imed NASRI
am 13 Nov. 2013
Bearbeitet: imed NASRI
am 13 Nov. 2013
Matt J
am 13 Nov. 2013
.p files are encrypted mfiles. TMW wants to keep their contents proprietary.
Why compare with GA, though? Why even expect it to be better than bintprog/LINGO?
imed NASRI
am 13 Nov. 2013
suvrat
am 26 Jan. 2014
So finally you concluded mixed integer linear programming with equality constrain and inequality constrain can't be solved by ga in matlab? problem is due to equality constrain? if remove eqaulty constrain will it work? secondly binary integer programming with inequality constrain only no eqaulity can be solved by ga.is it will be better than lingo? please help my. thank you.
@suvrat,
No, the conclusion of imed's findings is that GA does not robustly solve binary linear programs even when only inequality constraints are present. More general mixed integer and non-binary problems were never looked at.
I don't have the Global Optimization Toolbox, so I have no way of verifying imed's findings. However, to my understanding, GA is a stochastic algorithm. If so, it makes sense in my mind that it has some probability of failure, in contrast to bintprog/LINGO
Vitor Ribeiro
am 10 Sep. 2014
In fact, after some simulations, with GA working in same parameters as I initially state, I can see that when it finds a feasible solution GA rapidly learn and apparently converge because it is evaluating more feasible solutions around the 1st one it reach.
In many literature there are results showing that GA is a really good choice for solving problems of topology optimization with his basic heuristic formulation. I'm not certain about the mean code of GA function but I am convinced that it respects the heuristic core.
I'm not saying that GA in matlab works better then any other optimization function cause I do not have the knowledge for that. I don't know if BINTPROG or even INTLINPROG works better. I am interested in see documentation explaining how it works before seeing how it's used. If you can provide some link it would be really helpful.
After I can see how it works this functions, about how the solutions are generated or even search space composed by solutions are really searched by this algorithms, if they have an heuristic base, I would be able to choose research more in literature and probably understand which works better before I apply it in my problem indiscriminately.
Thank you all for your answers. I really appreciate each one.
Matt J
am 22 Jun. 2015
I've only now realized that there is a pitfall with the variable elimination approach. When using the equality constraints alone to eliminate variables, there is nothing ensuring that the eliminated variables will end up being integers. :-(
Kategorien
Mehr zu Solver Outputs and Iterative Display finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!