What is the bug in this code with Integer Genetic algorithm solver?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Example Code:
F = [1 2 3 4 5 6 7 8 9];
H = [1 2 3 4 5 6 7 8 9];
K = [9 8 7 6 5 4 3 2 1];
S = @(x)[H(1:x(1)-1).'-K(1:x(1)-1).';
H(x(1):x(2)-1).'-K(x(1):x(2)-1).'-F(3:6).';
H(x(2):9).'-K(x(2):9).'];
A=ga(S,2,[1,-1;-1,1],[-4,4],[],[],[2,3],[8,9],[],[1,2]);
The errors that are displayed alternatively upon solving this:
Error using "-" Matrix dimensions must agree.
Subscripted assignment dimension mismatch.
Index exceeds matrix dimensions.
I dont find any examples on ga for handling array of values with indices being constrained to integers. So someone please shed some light on this.
Thanks in advance.
Accepted Answer
Walter Roberson
on 10 Sep 2016
You have
H(x(1):x(2)).'-K(x(1):x(2)).'-F(3:6).'
x(1) can be in the range 2 to 8, and x(2) can be 3 to 9 according to the LB and UB constraints. The LB and UB constraints are the only ones guaranteed to be satisfied at all times (others might only be satisfied at generation boundaries.) So x(1):x(2) can range from 8:3 (length 0) to 2:9 (length 8). But that expression, H(x(1):x(2)).'-K(x(1):x(2)).', which can be length anywhere from 0 to 8, must match the size of F(3:6) (length 4).
If your constraints are intended to enforce that x(2) = x(1) + 3 (so that F(3:6) will match on length) then rewrite your formula into one variable, substituting x(1)+3 for each occurrence of x(2).
The variable-length array extractions in your S formula are suspicious.
It appears to me that you have also neglected a fundamental fact: the objective function for ga() must return a scalar unless you have options with 'UseVectorized' set to true. You are returning a vector.
If you are wanting to do a pareto front calculation then you need to be using gamultiobj()
7 Comments
Thanigaivel Raja T
on 11 Sep 2016
Edited: Thanigaivel Raja T
on 12 Sep 2016
Thank you so much. I have reformulated the objective function and bounds based on your insightful suggestion to reduce the problem to a single variable by substituting x(2)-1=x(1)+3.
S1 = @(x)[H(1:x(1)-1).'-K(1:x(1)-1).';
H(x(1):x(1)+3).'-K(x(1):x(1)+3).'-F(3:6).';
H(x(1)+4:9).'-K(x(1)+4:9).'];
A = ga(S1,1,[],[],[],[],2,5,[],1);
Upon solving, again I got:
Subscripted assignment dimension mismatch.
Upon turning Vectorized option on, I got:
When 'Vectorized' is 'on', your fitness function must return a vector of length equal
to the size of the population.
What I intended to find is value of x(1) for which:
- the sum of elements of the vector returned by the objective S1 is minimum, i.e
for S1(5) returning a vector [-8;-6;-4;-2;-3;-2;-1;0;6;8], the sum of the elements returned by it is the minimum.
So I again reformulated the objective as:
S2 = @(x) sum([H(1:x(1)-1).'-K(1:x(1)-1).';
H(x(1):x(1)+3).'-K(x(1):x(1)+3).'-F(3:6).';
H(x(1)+4:9).'-K(x(1)+4:9).']);
Now that the objective returns a scalar value, I don't get errors. Am I right in doing, what I intend to do?
Thanigaivel Raja T
on 11 Sep 2016
Edited: Thanigaivel Raja T
on 11 Sep 2016
Also from expression
A = ga(S1,1,[],[],[],[],2,5,[],1);
the bounds for x(1) are 2<=x(1)<=5 which was selected based on the size of H and K being 1*9 and S1 expression.
But upon evaluating S1 I get a vector, even at x(1)=6 and x(1)=1(beyond which the 'index exceeds matrix dimensions' error appears). For example, I get S1(6)=[-8;-6;-4;-2;0;-1;0;1;2]. And from S1 expression, S1(6) is:
S1(x(1)=6) = [H(1:6-1).'-K(1:6-1).';
H(6:6+3).'-K(6:6+3).'-F(3:6).';
H(6+4:9).'-K(6+4:9).'],
I don't understand this because how could K(6+4:9) i.e, K(10:9) be possible. Upon checking value of K(10:9), I see
>> K=(10:9)
K =
Empty matrix: 1-by-0
Similar applies to S1(1)(how is K(1:0) possible?). So what's happening here? Should my bounds be lb=2,ub=5 or lb=1,ub=6?
Walter Roberson
on 11 Sep 2016
In your last line for S, you added 4 to x(1) instead of adding 3.
Your ub must be such that adding 3 does not go out of range.
I am not sure exactly what you are doing, but it looks to me as if you should probably be using linear programming.
For that matter, you should probably just loop over all the valid x(1) as you only have such a small number of possibilities and only one variable.
Thanigaivel Raja T
on 12 Sep 2016
Edited: Thanigaivel Raja T
on 12 Sep 2016
Thank you again Sir.
Actually, from the question you can see that x(1):x(2)-1 should be equal to 3:6
So I had substituted x(2)-1=x(1)+3.(rather than x(2)=x(1)+3)
It means that ub should be such that adding 4 does not go out of range.
So ub= 9-4 = 5 which I have set as the upper bound. Hence for any manual evaluation of the objective function(S1) above ub=5, I should get the error "index exceeds matrix dimensions". But I get some answer(vector) till x1=6 i.e S1(6), which I have given the in the previous comment. You can check it in matlab workspace.
That why I am confused on how it is possible and hence what should be the upper bound?
Thanigaivel Raja T
on 12 Sep 2016
Edited: Thanigaivel Raja T
on 12 Sep 2016
Nextly, coming to the method to be used. I thought when the variable in the problem is an index, the problem is non-linear. Hence I went for integer GA solver. Am I wrong in my perception?
Walter Roberson
on 12 Sep 2016
You had
H(x(2):9).'-K(x(2):9).'
but x(2) = x(1)+3 so that line becomes
H(x(1)+3:9).'-K(x(1)+3:9).'
but you instead coded
H(x(1)+4:9).'-K(x(1)+4:9).'
Walter Roberson
on 12 Sep 2016
You only have a small number of valid x(1) values, with only 2, 3, 4, 5, and 6 being valid (it looks like to me.) There is no point in using ga() for something like that: just loop over all of the values calling your objective function and finding the minimum.
More Answers (0)
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
See Also
on 10 Sep 2016
on 12 Sep 2016
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!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)