GA Optimization using matrix parameters and variables

Hello
I am trying to optimize a mixed integer opt problem using ga. I have parameters anad variables as matrices. First of all, is it possible to optimize this using ga? If it is possible, what is wrong with my codes?
My fitness function to minimize:
function y=tour(u)
x1=u(1);
x2=u(2);
x3=u(3);
x4=u(4);
x5=u(5);
x6=u(6);
z1=u(7);
...
w=repmat(600,7,1);
d=repmat(200,6,1);
f=repmat(400,6,1);
y=dot(w(:),sum(x1))+dot(x1(:)*5.77*10^-5,b1(:))+dot(x2(:)*5.77*10^-5,b2(:))+dot(x3(:)*5.77*10^-5,b3(:))+dot(x4(:)*5.77*10^-5,b4(:))+dot(x5(:)*5.77*10^-5,b5(:))+dot(x6(:)*5.77*10^-5,b6(:))+dot(d(:),sum(x1))+dot(d,sum(x1,1))+dot(f(:),z1(:))-0.093*sum(x2(:))-0.37*sum(x3(:))-0.06*sum(x4(:))-0.87*sum(x5(:));
end
I have already written all b1 to b6 parameters above.
I have my constraints as below:
function [c,ceq]= mycons(u)
x1=u(1);
x2=u(2);
x3=u(3);
x4=u(4);
x5=u(5);
x6=u(6);
z1=u(7);
z2=u(8);
z3=u(9);
z4=u(10);
y1=u(11);
y2=u(12);
y3=u(13);
y4=u(14);
y5=u(15);
y6=u(16);
yk=u(17);
yp=u(18);
yl=u(19);
...%Here I have written also all parameters as matrix (a,sal1,etc.).
c=[sum(x1,2).'-a; a-sum(x1,2).'; sum(x1,1).'-y1; y1-sum(x1,1).'; sum(x2,2).'-y2; y2-sum(x2,2).'; sum(x3,2).'-y3; y3-sum(x3,2).'; sum(x4,2).'-y4; y4-sum(x4,2).'; sum(x5,2).'-y5; y5-sum(x5,2).'; sum(x6,2).'-y6; y6-sum(x6,2).'; sum(x2,1).'-yk; yk-sum(x2,1).'; sum(x3,1).'-yp; yp-sum(x3,1).'; sum(x4,1).'-yl; y1-sum(x4,1).'; y1*50.576-y2; y2-y1*50.576; y1*33.226-y3; y3-y1*33.226; y1*7.516-y4; y4-y1*7.516; y1*8.662-y5; y5-y1*8.662; y1*0.02-y6; y6-y1*0.02; sal1*z1-y1; y1-syu1*z1; sal2*z2-yk; yk-syu2*z2; sal3*z3-yp; yp-syu3*z3; sal4*z4-yl; yl-syu4*z4; sum(z1)-6; sum(z2)-6; sum(z3)-8; sum(z4)-3; -x1; -x2; -x3; -x4; -x5; -x6; -y1; -y2; -y3; -y4; -y5; -y6; -yk; -yp; -yl];
ceq=[];
My main code:
ObjFcn=@tour;
nvars=19;
lx1=zeros(7,6);
lx1=(lx1(:))';
lx2=zeros(6,6);
lx2=(lx2(:))';
lx3=zeros(6,8);
lx3=(lx3(:))';
lx4=zeros(6,3);
...continuing so on
uy5=repmat(26780,6,1);
uy5=(uy5(:))';
uy6=repmat(460,6,1);
uy6=(uy6(:))';
uyk=repmat(157150,6,1);
uyk=(uyk(:))';
uyp=repmat(102980,8,1);
uyp=(uyp(:))';
uyl=repmat(23294,3,1);
uyl=(uyl(:))';
LB=[lx1 lx2 lx3 lx4 lx5 lx6 lz1 lz2 lz3 lz4 ly1 ly2 ly3 ly4 ly5 ly6 lyk lyp lyl];
UB=[ux1 ux2 ux3 ux4 ux5 ux6 uz1 uz2 uz3 uz4 uy1 uy2 uy3 uy4 uy5 uy6 uyk uyp uyl];
ConsFcn=@mycons;
[u,fval]=ga(ObjFcn,nvars,[],[],[],[],LB,UB,ConsFcn);
Thanks for your help

Answers (1)

Matt J
Matt J on 11 May 2016
Edited: Matt J on 11 May 2016
First of all, is it possible to optimize this using ga?
Possible, yes, but probably more reliable to use intlinprog() instead. The objective and constraints all look linear.
If it is possible, what is wrong with my codes?
You haven't described the bad behavior you are seeing, so there is no reason for us to think anything is wrong with it. However, what looks strange are things like this,
function [c,ceq]= mycons(u)
x1=u(1);
x2=u(2);
x3=u(3);
etc...
Here, I assume x1, x2, etc... are scalars. This has to be the case unless unless u is non-numeric, which it should not be. But if they are scalars, then matrix-manipulation expressions later in the code like sum(x1,2).' are pointless.

3 Comments

I have a solution to this MILP on LINGO. But I want just to compare results.
x1,x2,... and also all y and z variables are matrices as written in my main code (lx1, lx2,..., uy1, uy2,uyp, etc. and here the prefix "l-" and "u-" for lower and upper bounds).
I'm getting this error when I run main code:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in mymain (line 99)
LB=[lx1 lx2 lx3 lx4 lx5 lx6 lz1 lz2 lz3 lz4 ly1 ly2 ly3 ly4 ly5 ly6 lyk lyp lyl];"
I vectorised all variables for lb and ub in my main code. Do I have to vectorise all matrices in my fitness and constraint functions?
x1,x2,... and also all y and z variables are matrices as written in my main code
That is not possible based on the code you have posted. A statement like x1=u(1) cannot result in a non-scalar matrix x1 if u is also a numeric matrix.
I'm getting this error when I run main code:
lx1 lx2 lx3, etc... must all have the same number of rows for this statement to work.
Thanks. I will check my codes.

Sign in to comment.

Asked:

on 10 May 2016

Commented:

on 11 May 2016

Community Treasure Hunt

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

Start Hunting!