**You are now following this question**

- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.

# why ga become infeasible after increasing the size of variables

1 view (last 30 days)

Show older comments

Hi, I am trying to use ga to solve a mixed integer optimization problem. I have a problem that ga became infeasible after i enlarged the size of my varibles. For example, if the number of varibles are 80 or 150, ga runs very good. But when it comes to 252 variables, ga immediately becomes infeasible.In my problem, i set my number of variables to "N" which can be changed before the optimization.

Thank you!

##### 12 Comments

BOWEN LI
on 2 Sep 2019

Walter Roberson
on 2 Sep 2019

BOWEN LI
on 2 Sep 2019

Hi I'm sorry that my variables are setup in a relativley comlplicated way, but if you don't mind, is it possible for me to post my code here? The setup may seem complicated but the mechanisms of it is straight forward. Thanks so much for this!

totalcost912019 is my function file

thesis_code2 is the ga file.

revconstr is the unlinear constraint file

I have set up the paremeters to those which yield the infeasible outcome.

Walter Roberson
on 2 Sep 2019

Look at your code for constructing A4:

A4=[];

for t=1:N

for i=1:N

for j= i+1:N

A_temp=zeros(1,(N^2+N^3));

if A_temp((i+1+(t-1)*N):(j+(t-1)*N))==1

A_temp(((N^2)+(t-1)*(N^2)+(i-1)*N+j))=1;

else

A_temp(((N^2)+(t-1)*(N^2)+(i-1)*N+j))=0;

end

A4=[A4;A_temp];

end

end

end

You zero out all of A_temp every j iteration, so any changes to A_temp are going to be lost between rounds. Therefore A_temp(whatever) cannot == 1 in any position, so you never assign 1 to any A_temp position. Therefore the A4 that you build is going to be all 0. You are building N^3 rows of zeros to put on the non-linear constraints. That is not illegal, but it is a waste.

BOWEN LI
on 5 Sep 2019

Walter Roberson
on 5 Sep 2019

Edited: Walter Roberson
on 5 Sep 2019

Note that this does not explain why the constraints cannot be met.

When looked at the other linear constraints, it looked to me as if they could not be met, that they were internally inconsistent. However, I was not able to prove that in the time I spent.

BOWEN LI
on 6 Sep 2019

Walter Roberson
on 7 Sep 2019

I just had another glance at your code and noticed the section

for t=1:N

if t==1

qij(:,:,t)=qij0;

yi(:,:,t)=zeros(N);

sij(:,:,t)=zeros(N);

cc(t)=y(:,1,t).'*d*k;

else

qij(:,:,t)=(qij(:,:,t-1).*(1+r1).^t).*((1/8)*(1+sij(:,:,t-1).*r2).^t);

cc(t)=(y(:,1,t)-y(:,1,t-1)).'*d*k;%construction cost

end

%end

hb1(t)=sum(2*(1-yi(1,:,t))*d./vb)+sum((1-yi(1,:,t))*td)*ubc; %bus headway

hb2(t)=sum(sum((1-yi(:,:,t)).*qij(:,:,t)));

You are using the value of t after the end of the immediately proceeding for t=1:N loop. MATLAB does define a meaning for that: after a for loop, the loop index is left as the last value it was assigned in the loop. For example,

for K = 1 : 10

K = 11;

end

Here, K is initialized to 1 and the K=11 assignment is done. But for keeps internal track of the value of the loop variable and knows that it just did 1, and increments that internal counter to 2, finds that the 2 is within range, and assigns that 2 to K, making K=2, overwriting the K=11 value. Then when K=2, the loop assigns K=11 . But for is keeping track internally, increments its internal counter to 3, finds that 3 is in range, and assigns that 3 to K, making K=3, overwriting the K=11 that was just done. And so on. Eventually the internal counter is incremented to 10, and that is found to be within range, and K=10 is done. Then the loop body does K=11 because of the assignment there. The internal counter that was 10 is then incremented, and 11 is found to be outside of the loop bounds, and so the value of K is not updated, leaving the in-loop assignment K=11 as the current value of K.

So it is all well defined in MATLAB, but most people do not understand the details, and it is not a good idea to count on the details being clear to the readers (and to you, the author of the code.)

This matters because your %end is commented out, so the for t loop has not ended there. Your code continues on to

hb1(t)=sum(2*(1-yi(1,:,t))*d./vb)+sum((1-yi(1,:,t))*td)*ubc; %bus headway

hb2(t)=sum(sum((1-yi(:,:,t)).*qij(:,:,t)));

% hb(t)=2*sqrt(hb1(t)+hb2(t));

for t=1:N %bus headway requirement

if hb(t)==2*sqrt(hb1(t)+hb2(t))>=0.02

hb(t)=2*sqrt(hb1(t)+hb2(t));

else

hb(t)=0.02;

end

if hb(t)==2*sqrt(hb1(t)+hb2(t))>=nc*ct/max(max(qij(:,:,t)))

else

hb(t)=cb/max(max(qij(:,:,t)));

end

end

You are still inside the for t loop, and you start another for t loop. The effects are defined in MATLAB -- as far as MATLAB is concerned this is just like the K=11 case that I had in my example, where you are assigning something to the loop variable inside the loop. Because of the internal track it is keeping of each loop counter, MATLAB is able to handle the outer for t loop, but by now everyone is confused over what is supposed to be happening.

Then after the inner for t loop you continue to

hr1(t)=((2*yi(1,:,t)*d)/vr)+sum(sum((0.1+yi(1,:,t))*td))*nc*uc;

hr2(t)=sum(sum((0.1+y(:,:,t)).*qij(:,:,t)));

As described above, t will have been left the last value assigned by the inner for t=1:N loop, so t will equal N at that point, making those two lines equivalent to

hr1(N)=((2*yi(1,:,N)*d)/vr)+sum(sum((0.1+yi(1,:,N))*td))*nc*uc;

hr2(N)=sum(sum((0.1+y(:,:,N)).*qij(:,:,N)));

but is that really what you want there? Or do you want the t to be the t related to the outer for t loop?

BOWEN LI
on 7 Sep 2019

Hi Walter, thank you so much and I carefully read your answer. Thanks for your example and I figured out what your example says. Actually i want all "t" that changes simultaneously from 1:N.

for this part I try to define qij and cc(t) which i will use in my function.

if t==1

qij(:,:,t)=qij0;

yi(:,:,t)=zeros(N);

sij(:,:,t)=zeros(N);

cc(t)=y(:,1,t).'*d*k;

else

qij(:,:,t)=(qij(:,:,t-1).*((1+r1).^t)).*((1/(2*N))*(1+sij(:,:,t-1).*r2).^t);

cc(t)=(y(:,1,t)-y(:,1,t-1)).'*d*k;%construction cost

end

for this part I'm trying to set the rule for hb(t), as you see in the "if else" statement.

hb1(t)=sum(2*(1-yi(1,:,t))*d./vb)+sum((1-yi(1,:,t))*td)*ubc; %bus headway

hb2(t)=sum(sum((1-yi(:,:,t)).*qij(:,:,t)));

hb(t)=2*sqrt(hb1(t)+hb2(t));

%for t=1:N %bus headway requirement

if hb(t)==2*sqrt(hb1(t)+hb2(t))>=0.02

hb(t)=2*sqrt(hb1(t)+hb2(t));

else

hb(t)=0.02;

end

if hb(t)==2*sqrt(hb1(t)+hb2(t))>=nc*ct/max(max(qij(:,:,t)))

hb(t)=cb/max(max(qij(:,:,t)));

end

for this part I'm trying to set the rule for hr(t), as you see in the "if else" statement.

hr1(t)=((2*yi(1,:,t)*d)/vr)+sum(sum((0.1+yi(1,:,t))*td))*nc*uc;

hr2(t)=sum(sum((0.1+yi(:,:,t)).*qij(:,:,t)));

hr(t)=2*sqrt(hr1(t)/hr2(t));

%for t=1:N %rail headway requirement

if hr(t)==hr1(t)+hr2(t)>=0.02 %hours

hr(t)=2*sqrt(hr1(t)/hr2(t));

else

hr(t)=0.02;

end

if hr(t)==hr1(t)+hr2(t)>=nc*ct/max(max(qij(:,:,t)))

hr(t)=cb/max(max(qij(:,:,t)));

end

And this part is my equations about time (t), and my objective function is f(t) which is the sum of all previous equations.

cu(t)=sum(sum(qij(:,:,t).*(1-yi(:,:,t))))*(hb(t)/2)*(u/4)+sum(sum(qij(:,:,t).*yi(:,:,t)))*(hr(t)/2)*(u/4); %user wait cost

ci1(t)=((diag((1-yi(:,:,t))*qij(:,:,t))).'*(d+td))*(u/vb);

ci2(t)=((diag(yi(:,:,t)*qij(:,:,t))).'*(d+td)).*(u+vb);

ci(t)=ci1(t)+ci2(t);

rb(t)=2*(1-yi(1,:,t))*d/vb+2*sum((1-yi(1,:,t))*td);%bus round trip time

rr(t)=2*((yi(1,:,t)*d)/vr)+sum((yi(1,:,t))*td); %rail round trip time

cv1(t)=(rb(t)/hb(t))*ubc;

cv2(t)=(rr(t)/hr(t))*nc*uc;%vehicle operating speed

cv(t)=cv1(t)+cv2(t);

cm(t)=(yi(1,:,t)*d)*L;%maintenance cost

f(t)=cu(t)+ci(t)+cv(t)+cm(t)+cc(t);

lastly, i sum up all time periods of f(t) as

f = sum(f)*((1+rs)^-N);

I did confuse about how to use the "for loop" in my function structure. My objective function is f(t) which is about time (t) from 1 to N. And f(t) is composed by cu(t),ci(t),cv(t),cm(t), and cc(t). Before just add them up, I set some rules for some terms like hb(t), hr(t) which are used inside of the equations. So generally "t" i supposed to make it change simultaneously for each equation with respect to "t". So i changed my code that seperates these part from each other by set "for" loop for each of these parts as:

for t=1:N

if t==1

qij(:,:,t)=qij0;

yi(:,:,t)=zeros(N);

sij(:,:,t)=zeros(N);

cc(t)=y(:,1,t).'*d*k;

else

qij(:,:,t)=(qij(:,:,t-1).*((1+r1).^t)).*((1/(2*N))*(1+sij(:,:,t-1).*r2).^t);

cc(t)=(y(:,1,t)-y(:,1,t-1)).'*d*k;%construction cost

end

end

for t=1:N

hb1(t)=sum(2*(1-yi(1,:,t))*d./vb)+sum((1-yi(1,:,t))*td)*ubc; %bus headway

hb2(t)=sum(sum((1-yi(:,:,t)).*qij(:,:,t)));

hb(t)=2*sqrt(hb1(t)+hb2(t));

%for t=1:N %bus headway requirement

if hb(t)==2*sqrt(hb1(t)+hb2(t))>=0.02

hb(t)=2*sqrt(hb1(t)+hb2(t));

else

hb(t)=0.02;

end

if hb(t)==2*sqrt(hb1(t)+hb2(t))>=nc*ct/max(max(qij(:,:,t)))

hb(t)=cb/max(max(qij(:,:,t)));

end

end

for t=1:N

hr1(t)=((2*yi(1,:,t)*d)/vr)+sum(sum((0.1+yi(1,:,t))*td))*nc*uc;

hr2(t)=sum(sum((0.1+yi(:,:,t)).*qij(:,:,t)));

hr(t)=2*sqrt(hr1(t)/hr2(t));

%for t=1:N %rail headway requirement

if hr(t)==hr1(t)+hr2(t)>=0.02 %hours

hr(t)=2*sqrt(hr1(t)/hr2(t));

else

hr(t)=0.02;

end

if hr(t)==hr1(t)+hr2(t)>=nc*ct/max(max(qij(:,:,t)))

hr(t)=cb/max(max(qij(:,:,t)));

end

end

for t=1:N

cu(t)=sum(sum(qij(:,:,t).*(1-yi(:,:,t))))*(hb(t)/2)*(u/4)+sum(sum(qij(:,:,t).*yi(:,:,t)))*(hr(t)/2)*(u/4); %user wait cost

ci1(t)=((diag((1-yi(:,:,t))*qij(:,:,t))).'*(d+td))*(u/vb);

ci2(t)=((diag(yi(:,:,t)*qij(:,:,t))).'*(d+td)).*(u+vb);

ci(t)=ci1(t)+ci2(t);

rb(t)=2*(1-yi(1,:,t))*d/vb+2*sum((1-yi(1,:,t))*td);%bus round trip time

rr(t)=2*((yi(1,:,t)*d)/vr)+sum((yi(1,:,t))*td); %rail round trip time

cv1(t)=(rb(t)/hb(t))*ubc;

cv2(t)=(rr(t)/hr(t))*nc*uc;%vehicle operating speed

cv(t)=cv1(t)+cv2(t);

cm(t)=(yi(1,:,t)*d)*L;%maintenance cost

f(t)=cu(t)+ci(t)+cv(t)+cm(t)+cc(t);

end

f = sum(f)*((1+rs)^-N);

but i still got the similar result, infeasible solutions and complex fval number.

Thank you as always for your answer!

Walter Roberson
on 7 Sep 2019

Could you post your complete adjusted totalcost file?

My testing before strongly suggested that your linear constraints cannot be met, so it would not matter what your function calculated (provided the result was not infinite or nan) and you would still get told it was infeasible.

BOWEN LI
on 7 Sep 2019

Sure no problem. totalcost912019.m is the adjusted file.

I put my constraints in the thesis_code2 file.

My linear constriants basically says that:

A1: yi(: , :, t) - yi(:, :, t-1) >= 0 (t=2:N)

A1sub: sij(: , :, t) - sij(:, :, t-1) >= 0 (t=2:N)

A2: for each sij(:, :, t) matrix, the upper and lower half that symmetry to the diagonal are same

A3: negative of A2

A4: for each time period(t), if yi to yj are all "1", then sij is 1, 0 otherwise.

Since the constraints are created based on the paper I referenced which has A1 but not A2 A3 and A4. And based on my x outcome, at least A1 is met. However, A2 - A4, especially A4, should be met in my problem.

Thank you!

### Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

### Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)