Genetic algorithm problem in matrix index
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
tahseen alshmary
le 30 Déc 2018
Commenté : tahseen alshmary
le 31 Déc 2018
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)
0 commentaires
Réponse acceptée
Stephan
le 30 Déc 2018
Modifié(e) : Stephan
le 30 Déc 2018
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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Solver Outputs and Iterative Display dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!