필터 지우기
필터 지우기

What is the bug in this code with Integer Genetic algorithm solver?

조회 수: 2 (최근 30일)
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.

채택된 답변

Walter Roberson
Walter Roberson 2016년 9월 10일
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
Walter Roberson
Walter Roberson 2016년 9월 12일
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
Walter Roberson 2016년 9월 12일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Genetic Algorithm에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by