Dear sir
i have problem with this code . can any one help me and thanks advance
error
Index exceeds matrix dimensions.
Error in Untitled3 (line 47)
CTI1 = A(1,1)*x(1)+A(1,6)*x(6)
the code
%To minimize our fitness function using the ga function, we need to pass in a function handle to the fitness function as well as specifying the number of variables as the second argument. Lower and upper bounds are provided as LB and UB respectively. In addition, we also need to pass in a function handle to the nonlinear constraint function.
tic
clear
clc
ObjectiveFunction = @simple_fitness;
nvars = 14; % Number of variables
LB = [0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05]; % Lower bound for (x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14)
UB = [1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 ]; % Upper bound for (x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14)
A=zeros(20,14); b=zeros(20,1);
A(1,1)=+2.174; A(1,6)=-1.777; b(1)=-0.3; %first row
A(2,1)=-3.555; A(2,2)=+1.159; b(2)=-0.3; %second row
A(3,2)=+1.159; A(3,7)=-2.137; b(3)=-0.3; %third row
A(4,2)=-1.272; A(4,3)=+2.880; b(4)=-0.3; %fourth row
A(5,3)=-3.593; A(5,4)=+3.043; b(5)=-0.3; %fifth row
A(6,4)=-3.818; A(6,5)=+1.402; b(6)=-0.3; %sixth row
A(7,5)=-1.650; A(7,6)=+1.519; b(7)=-0.3; %seventh row
A(8,5)=-1.650; A(8,7)=+1.606; b(8)=-0.3; %eighth row
A(9,6)=+1.519; A(9,14)=-2.143; b(9)=-0.3; %nighth row
A(10,7)=+1.606; A(10,13)=-2.629; b(10)=-0.3; %tenth row
A(11,7)=-2.137; A(11,8)=+1.806; b(11)=-0.3; %eleventh row
A(12,8)=+1.806; A(12,9)=-1.622; b(12)=-0.3; %tweleventh row
A(13,9)=+1.948; A(13,10)=-3.605; b(13)=-0.3; %thirteenth row
A(14,10)=+2.753; A(14,11)=-3.935; b(14)=-0.3; %fourteenth row
A(15,11)=+3.105; A(15,12)=-1.287; b(15)=-0.3; %fiveteenth row
A(16,12)=+1.181; A(16,13)=-2.629; b(16)=-0.3; %sixteenth row
A(17,12)=+1.181; A(17,14)=-2.143; b(17)=-0.3; %seventennth row
A(18,8)=-2.232; A(18,13)=+1.816; b(18)=-0.3; %eighteenth row
A(19,1)=-3.555; A(19,14)=+1.608; b(19)=-0.3; %nighnteenth row
A(20,9)=-1.622; A(20,14)=+1.608; b(20)=-0.3; %twinty row
Aeq=[];
beq=[];
nonlcon=[];
IntCon=[];
rng default % For reproducibility
options=gaoptimset('plotfcns',{'gaplotgenealogy','gaplotselection', 'gaplotbestf' ,@gaplotscorediversity});
options= gaoptimset(options,'Tolfun',1e-6,'TolCon',1e-3,'stallGenlimit',50);
options= gaoptimset(options,'MutationFcn',{@mutationadaptfeasible});
options=gaoptimset(options,'PopulationSize',200);
options=gaoptimset(options,'PopulationType','doubleVector');
options=gaoptimset(options,'CrossoverFcn',{@crossoverarithmetic});
options=gaoptimset(options,'Display', 'iter');
[x,fval,exitflag,output,population,scores] = ga(ObjectiveFunction,nvars,A,b,Aeq,beq,LB,UB,nonlcon,IntCon,options)
toc
CTI1 = A(1,1)*x(1)+A(1,6)*x(6)
CTI2 = A(2,1)*x(1)+ A(2,2)*x(2)
CTI3 = A(3,2)*x(2)+ A(3,7)*x(7)
CTI4 = A(4,2)*x(2)+ A(4,3)*x(3)
CTI5 = A(5,3)*x(3)+ A(5,4)*x(4)
CTI6 = A(6,4)*x(4)+ A(6,5)*x(5)
CTI7 = A(7,5)*x(5)+ A(7,6)*x(6)
CTI8 = A(8,5)*x(5)+ A(8,7)*x(7)
CTI9 = A(9,6)*x(6)+ A(9,14)*x(14)
CT10 = A(10,7)*x(7)+ A(10,13)*x(13)
CTI11= A(11,7)*x(7)+ A(11,8)*x(8)
CTI12= A(12,8)*x(8)+ A(12,9)*x(9)
CTI13= A(13,9)*x(9)+ A(13,10)*x(10)
CTI14= A(14,10)*x(10)+ A(14,11)*x(11)
CTI15= A(15,11)*x(11)+ A(15,12)*x(12)
CTI16= A(16,12)*x(12)+ A(16,13)*x(13)
CTI17= A(17,12)*x(12)+ A(17,14)*x(14)
CTI18= A(18,8)*x(8)+ A(18,13)*x(13)
CTI19= A(19,1)*x(1)+ A(19,14)*x(14)
CTI20= A(20,9)*x(9)+ A(20,14)*x(14)

 채택된 답변

Stephan
Stephan 2018년 12월 30일
편집: Stephan 2018년 12월 30일

0 개 추천

Hi,
you are using the simple_fitness function, which is an implementation of the Rosenbrock function. This function is a built in example function and has a number of variables = 2. How do you expect your code to run on 14 variables with this function?
Maybe you did write your own fitness function, which is accidently named like the built in one. If so, rename the .m-file you created and provide the function together with the other code if there are still more issues.
Best regards
Stephan

댓글 수: 9

tahseen alshmary
tahseen alshmary 2018년 12월 30일
my function is
function y = simple_fitness(x)
y = 2.174*x(1)+1.159*x(2)+2.880*x(3)+3.043*x(4)+1.402*x(5)+1.519*x(6)+1.606*x(7)+1.806*x(8)+1.363*x(9)+2.753*x(10)+3.105*x(11)+1.181*x(12)+1.816*x(13)+1.608*x(14);
end
Stephan
Stephan 2018년 12월 30일
편집: Stephan 2018년 12월 30일
This is a linear objective function with linear constraints - use linprog (and rename your objective function)
tahseen alshmary
tahseen alshmary 2018년 12월 30일
hi stephan
i know its linear objective function , but i need solve it with genetic algorithm .
previously i solve one same equation but different in constrain value . it is work normally
Stephan
Stephan 2018년 12월 30일
편집: Stephan 2018년 12월 30일
Hi,
your function does not have a feasible solution under the given conditions - i checked this with linprog:
lb = 0.05*ones(1,14); % Lower bound for (x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14)
ub = 1.1*ones(1,14); % Upper bound for (x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14)
A=zeros(20,14); b=zeros(20,1);
A(1,1)=+2.174; A(1,6)=-1.777; b(1)=-0.3; %first row
A(2,1)=-3.555; A(2,2)=+1.159; b(2)=-0.3; %second row
A(3,2)=+1.159; A(3,7)=-2.137; b(3)=-0.3; %third row
A(4,2)=-1.272; A(4,3)=+2.880; b(4)=-0.3; %fourth row
A(5,3)=-3.593; A(5,4)=+3.043; b(5)=-0.3; %fifth row
A(6,4)=-3.818; A(6,5)=+1.402; b(6)=-0.3; %sixth row
A(7,5)=-1.650; A(7,6)=+1.519; b(7)=-0.3; %seventh row
A(8,5)=-1.650; A(8,7)=+1.606; b(8)=-0.3; %eighth row
A(9,6)=+1.519; A(9,14)=-2.143; b(9)=-0.3; %nighth row
A(10,7)=+1.606; A(10,13)=-2.629; b(10)=-0.3; %tenth row
A(11,7)=-2.137; A(11,8)=+1.806; b(11)=-0.3; %eleventh row
A(12,8)=+1.806; A(12,9)=-1.622; b(12)=-0.3; %tweleventh row
A(13,9)=+1.948; A(13,10)=-3.605; b(13)=-0.3; %thirteenth row
A(14,10)=+2.753; A(14,11)=-3.935; b(14)=-0.3; %fourteenth row
A(15,11)=+3.105; A(15,12)=-1.287; b(15)=-0.3; %fiveteenth row
A(16,12)=+1.181; A(16,13)=-2.629; b(16)=-0.3; %sixteenth row
A(17,12)=+1.181; A(17,14)=-2.143; b(17)=-0.3; %seventennth row
A(18,8)=-2.232; A(18,13)=+1.816; b(18)=-0.3; %eighteenth row
A(19,1)=-3.555; A(19,14)=+1.608; b(19)=-0.3; %nighnteenth row
A(20,9)=-1.622; A(20,14)=+1.608; b(20)=-0.3; %twinty row
Aeq=[];
beq=[];
f = [2.174 1.159 2.880 3.043 1.402 1.519 1.606 1.806 1.363 2.753 3.105 1.181 1.816 1.608];
[x,fval,exitflag,output,lambda] = linprog(@fitness,A,b,Aeq,beq,lb,ub)
Result is:
>> test
No feasible solution found.
Linprog stopped because no point satisfies the constraints.
x =
[]
fval =
[]
exitflag =
-2
output =
struct with fields:
iterations: 0
message: 'No feasible solution found.↵↵Linprog stopped because no point satisfies the constraints.'
algorithm: 'dual-simplex'
constrviolation: []
firstorderopt: []
lambda =
[]
So you can use every solver you want - either there is an issue in your constraints, your bounds or in your objective function - one thing is for sure: there is no solution to this problem in the given form . So if you fix the problem (whatever it is), i think also ga should find some (nearly) optimal solution. Since ga doesnt find anything useful you get an empty x and in the result you get the error you have, because you reference to an empty x, which causes an error.
Thats it.
tahseen alshmary
tahseen alshmary 2018년 12월 30일
okay thanks
do you know how can i solve this problem
Stephan
Stephan 2018년 12월 30일
편집: Stephan 2018년 12월 30일
Set the upper bounds to:
ub = 2*ones(1,14);
linprog finds an optimal solution under this conditions , so ga should also find a good solution.
With solving this problem, you have a running code without error messages.
If this was helpful for you, please make sure to accept my answer.
tahseen alshmary
tahseen alshmary 2018년 12월 30일
yes thank you very much
but the best fuction (total value very high )=22.054
must be less than 10
but i have another idea make nonlinear constraint fuction will find solution
many ,many thanks for you
Stephan
Stephan 2018년 12월 30일
편집: Stephan 2018년 12월 30일
i wish you success, please accept useful answers.
tahseen alshmary
tahseen alshmary 2018년 12월 31일
thanks you so much

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

추가 답변 (0개)

카테고리

태그

질문:

2018년 12월 30일

댓글:

2018년 12월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by