MATLAB Answers

Vectorized fitness function for ga

3 views (last 30 days)
Emiliano Rosso
Emiliano Rosso on 3 Sep 2015
Edited: Alan Weiss on 4 Sep 2015
I'm not very experienced in vectorization and I have not found something of interesting on internet.I've built a fitness function for genetic algorithm but I did not understand very well if I have to vectorize all components (as in the second code) or if it's sufficient to vectorize function's input "variables".(variables is composed by 8200 integer variables).Initial population is composed by 100 components. In both cases I have errors:
for code 1 :
Subscripted assignment dimension mismatch.
Error in GeneticPattern (line 27) tempvariables(i,j)=variables(:,j+(82.*(i-1)));
code 1
function my_y=GeneticPattern(variables)
global inputs;global fval;global Nvar;global LB;global UB;global IntCon;global option;
global final_pop;global exitflag;global GAoutput;global indicatorrange;global inputrange;
global tempvariables;global inputstemp;global classi;global Nneuron;global targets; global tr;
global GAfilter;global GAPOP;
[m,n]=size(inputs);
nimp=1:n;
nset=1:100;
nvar=1:82;
nindy=1:5;
nforex=1:6;
nprice=1:11;
npart=1:33;
tempvariables(nset,nvar)=0;
inputstemp(nset,npart,nimp)=0;
classi(nset,nindy,nimp)=0;
cutindy=[20,15,10,5,3,1];
%tempvariables(:,nset,nvar)=variables(:,nvar+(82.*(nset-1)));
for i=1:numel(nset)
for j=1:numel(nvar)
tempvariables(i,j)=variables(:,j+(82.*(i-1))); %line 27
end
end
if tempvariables(nset,2+(nindy-1).*3)==1
if inputs(33.*6+nindy,nimp)<=indicatorrange(1+(nindy-1).*2)+((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./11).*tempvariables(nset,1+(nindy-1).*3)
classi(nset,nindy,nimp)=1;
end
end
if tempvariables(nset,2+(nindy-1).*3)==2
if inputs(33.*6+nindy,nimp)>=indicatorrange(1+(nindy-1).*2)+((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./11).*tempvariables(nset,1+(nindy-1).*3)
classi(nset,nindy,nimp)=1;
end
end
if tempvariables(nset,2+(nindy-1).*3)==3 & tempvariables(nset,3+(nindy-1).*3)~=5
if inputs(33.*6+nindy,nimp)>=indicatorrange(1+(nindy-1).*2) && inputs(33.*6+nindy,nimp)<=indicatorrange(2+(nindy-1).*2)
if inputs(33.*6+nindy,nimp)>=indicatorrange(1+(nindy-1).*2)+((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./11).*tempvariables(nset,1+(nindy-1).*3)...
-((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./cutindy(tempvariables(nset,3+(nindy-1).*3)))./2 && ...
inputs(33.*6+nindy,nimp)<indicatorrange(1+(nindy-1).*2)+((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./11).*tempvariables(nset,1+(nindy-1).*3)...
+((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./cutindy(tempvariables(nset,3+(nindy-1).*3)))./2
classi(nset,nindy,nimp)=1;
end
end
if inputs(33.*6+nindy,nimp)<indicatorrange(1+(nindy-1).*2) && ...
indicatorrange(1+(nindy-1).*2)+((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./11).*tempvariables(nset,1+(nindy-1).*3)...
-((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./cutindy(tempvariables(nset,3+(nindy-1).*3)))./2 ...
<indicatorrange(1+(nindy-1).*2)
classi(nset,nindy,nimp)=1;
end
if inputs(33.*6+nindy,nimp)>indicatorrange(2+(nindy-1).*2) && ...
indicatorrange(1+(nindy-1).*2)+((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./11).*tempvariables(nset,1+(nindy-1).*3)...
+((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./cutindy(tempvariables(nset,3+(nindy-1).*3)))./2 ...
>indicatorrange(2+(nindy-1).*2)
classi(nset,nindy,nimp)=1;
end
end
if tempvariables(nset,2+(nindy-1).*3)==3 & tempvariables(nset,3+(nindy-1).*3)==5
classi(nset,nindy,nimp)=1;
end
if classi(nset,1,nimp)==1 & classi(nset,2,nimp)==1 & classi(nset,3,nimp)==1 & classi(nset,4,nimp)==1 & classi(nset,5,nimp)==1
if tempvariables(nset,18+(npart-1).*2)~=5
if inputs(npart+(33.*tempvariables(:,nset,16)-1),nimp)<=inputrange(npart+(33.*tempvariables(nset,16)-1),2) && ...
inputs(npart+(33.*tempvariables(:,nset,16)-1),nimp)>=inputrange(npart+(33.*tempvariables(nset,16)-1),1)
if inputs(npart+(33.*tempvariables(:,nset,16)-1),nimp)<inputrange(npart+(33.*tempvariables(nset,16)-1),1)+(inputrange(npart+(33.*tempvariables(nset,16)-1),2)-inputrange(npart+(33.*tempvariables(nset,16)-1),1))./11.*tempvariables(nset,17+(npart-1).*2)...
+ (inputrange(npart+(33.*tempvariables(nset,16)-1),2)-inputrange(npart+(33.*tempvariables(nset,16)-1),1))./cutindy(tempvariables(nset,18+(npart-1).*2))./2 &&...
inputs(npart+(33.*tempvariables(nset,16)-1),nimp)>=inputrange(npart+(33.*tempvariables(nset,16)-1),1)+(inputrange(npart+(33.*tempvariables(nset,16)-1),2)-inputrange(npart+(33.*tempvariables(nset,16)-1),1))./11.*tempvariables(nset,17+(npart-1).*2)...
-(inputrange(npart+(33.*tempvariables(nset,16)-1),2)-inputrange(npart+(33.*tempvariables(nset,16)-1),1))./cutindy(tempvariables(nset,18+(npart-1).*2))./2
inputstemp(nset,npart,nimp)=1-(abs(inputs(npart+(33.*tempvariables(nset,16)-1),nimp)-(inputrange(npart+(33.*tempvariables(nset,16)-1),1)+(inputrange(npart+(33.*tempvariables(nset,16)-1),2)-inputrange(npart+(33.*tempvariables(nset,16)-1),1))./11.*tempvariables(nset,17+(npart-1).*2))))...
./((inputrange(npart+(33.*tempvariables(nset,16)-1),1)+(inputrange(npart+(33.*tempvariables(nset,16)-1),2)-inputrange(npart+(33.*tempvariables(nset,16)-1),1))./11.*tempvariables(nset,17+(npart-1).*2))...
-abs((inputrange(npart+(33.*tempvariables(nset,16)-1),1)+(inputrange(npart+(33.*tempvariables(nset,16)-1),2)-inputrange(npart+(33.*tempvariables(nset,16)-1),1))./11.*tempvariables(nset,17+(npart-1).*2))-(inputrange(npart+(33.*tempvariables(nset,16)-1),2)-inputrange(npart+(33.*tempvariables(nset,16)-1),1))./cutindy(tempvariables(nset,18+(npart-1).*2))./2));
end
end
if inputs(npart+(33.*tempvariables(nset,16)-1),nimp)<inputrange(npart+(33.*tempvariables(nset,16)-1),1) && ...
inputrange(npart+(33.*tempvariables(nset,16)-1),1)+(inputrange(npart+(33.*tempvariables(nset,16)-1),2)-inputrange(npart+(33.*tempvariables(:,nset,16)-1),1))./11.*tempvariables(nset,17+(npart-1).*2)...
-(inputrange(npart+(33.*tempvariables(nset,16)-1),2)-inputrange(npart+(33.*tempvariables(nset,16)-1),1))./cutindy(tempvariables(nset,18+(npart-1).*2))./2 ...
<inputrange(npart+(33.*tempvariables(nset,16)-1),1)
inputstemp(nset,npart,nimp)=1;
end
if inputs(npart+(33.*tempvariables(nset,16)-1),nimp)>inputrange(npart+(33.*tempvariables(nset,16)-1),2) && ...
inputrange(npart+(33.*tempvariables(nset,16)-1),1)+(inputrange(npart+(33.*tempvariables(nset,16)-1),2)-inputrange(npart+(33.*tempvariables(nset,16)-1),1))./11.*tempvariables(nset,17+(npart-1).*2)...
+ (inputrange(npart+(33.*tempvariables(nset,16)-1),2)-inputrange(npart+(33.*tempvariables(nset,16)-1),1))./cutindy(tempvariables(nset,18+(npart-1).*2))./2 ...
>inputrange(npart+(33.*tempvariables(nset,16)-1),2)
inputstemp(nset,npart,nimp)=1;
end
else
inputstemp(nset,npart,nimp)=1;
end
end
STDinputs(nset,nimp)=0;
AVinputs(nset,nimp)=0;
STDinputs(nset,nimp)=std(inputstemp,0,2);
AVinputs(nset,nimp)=mean(inputstemp,2);
inputstemp2(nset,nimp)=0;
inputstemp2(nset,nimp)=AVinputs(nset,nimp)-STDinputs(nset,nimp);
if GAfilter{1,1}(1,1)~=0
if inputstemp2(nset,nimp)<GAfilter{1,1}(1,1)
inputstemp2(nset,nimp)=0;
end
end
clear('l','k','i','j','n','m','nimp','nset','nvar','nindy','nforex','nprice','npart','cutindy','mycut','STDinputs','AVinputs');
myneuron=double(Nneuron{1,1}(1,1));
mynet=fitnet(myneuron(1,1));
mynet.trainFcn= 'trainlm';
mynet.biasConnect = [1;1];
mynet.inputs{1}.processFcns = {};
mynet.outputs{2}.processFcns = {};
mynet.divideFcn = 'divideblock';
mynet.divideParam.trainRatio = 80/100;
mynet.divideParam.valRatio = 10/100;
mynet.divideParam.testRatio = 10/100;
mynet.trainParam.showWindow = false;
mynet.trainParam.showCommandLine = false;
mynet.trainParam.epochs=100;
mynet.layers{1}.transferFcn = 'tansig';
mynet.layers{2}.transferFcn = 'tansig';
mynet.efficiency.memoryReduction=1;
[mynet,tr]=train(mynet,inputstemp2,targets);
if tr.best_vperf>=tr.best_tperf
my_y=tr.best_vperf;
else
my_y=tr.best_tperf;
end
clear('myneuron','inputstemp2','mynet');
end
for code 2:
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in GeneticPattern (line 29) tempvariables(:,i,j)=variables(:,j+(82.*(i-1)));
code 2
function my_y=GeneticPattern(variables)
global inputs;global fval;global Nvar;global LB;global UB;global IntCon;global option;
global final_pop;global exitflag;global GAoutput;global indicatorrange;global inputrange;
global tempvariables;global inputstemp;global classi;global Nneuron;global targets; global tr;
global GAfilter;global GAPOP;global myvariables;
[m,n]=size(inputs);
nimp1=1:n;
nset1=1:100;
nvar=1:82;
nindy1=1:5;
nforex=1:6;
nprice=1:11;
npart1=1:33;
tempvariables(:,nset1,nvar)=0;
inputstemp(:,nset1,npart1,nimp1)=0;
classi(:,nset1,nindy1,nimp1)=0;
cutindy=[20,15,10,5,3,1];
%tempvariables(:,nset,nvar)=variables(:,nvar+(82.*(nset-1)));
%myvariables=variables;
for i=1:numel(nset1)
for j=1:numel(nvar)
tempvariables(:,i,j)=variables(:,j+(82.*(i-1))); %line 29
end
end
for nset=1:100
for nindy=1:5
for nimp=1:n
if tempvariables(:,nset,2+(nindy-1).*3)==1
if inputs(33.*6+nindy,nimp)<=indicatorrange(1+(nindy-1).*2)+((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./11).*tempvariables(:,nset,1+(nindy-1).*3)
classi(:,nset,nindy,nimp)=1;
end
end
if tempvariables(:,nset,2+(nindy-1).*3)==2
if inputs(33.*6+nindy,nimp)>=indicatorrange(1+(nindy-1).*2)+((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./11).*tempvariables(:,nset,1+(nindy-1).*3)
classi(:,nset,nindy,nimp)=1;
end
end
if tempvariables(:,nset,2+(nindy-1).*3)==3 & tempvariables(:,nset,3+(nindy-1).*3)~=5
if inputs(33.*6+nindy,nimp)>=indicatorrange(1+(nindy-1).*2) && inputs(33.*6+nindy,nimp)<=indicatorrange(2+(nindy-1).*2)
if inputs(33.*6+nindy,nimp)>=indicatorrange(1+(nindy-1).*2)+((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./11).*tempvariables(:,nset,1+(nindy-1).*3)...
-((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./cutindy(tempvariables(:,nset,3+(nindy-1).*3)))./2 && ...
inputs(33.*6+nindy,nimp)<indicatorrange(1+(nindy-1).*2)+((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./11).*tempvariables(:,nset,1+(nindy-1).*3)...
+((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./cutindy(tempvariables(:,nset,3+(nindy-1).*3)))./2
classi(:,nset,nindy,nimp)=1;
end
end
if inputs(33.*6+nindy,nimp)<indicatorrange(1+(nindy-1).*2) && ...
indicatorrange(1+(nindy-1).*2)+((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./11).*tempvariables(:,nset,1+(nindy-1).*3)...
-((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./cutindy(tempvariables(:,nset,3+(nindy-1).*3)))./2 ...
<indicatorrange(1+(nindy-1).*2)
classi(:,nset,nindy,nimp)=1;
end
if inputs(33.*6+nindy,nimp)>indicatorrange(2+(nindy-1).*2) && ...
indicatorrange(1+(nindy-1).*2)+((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./11).*tempvariables(:,nset,1+(nindy-1).*3)...
+((indicatorrange(2+(nindy-1).*2)-indicatorrange(1+(nindy-1).*2))./cutindy(tempvariables(:,nset,3+(nindy-1).*3)))./2 ...
>indicatorrange(2+(nindy-1).*2)
classi(:,nset,nindy,nimp)=1;
end
end
if tempvariables(:,nset,2+(nindy-1).*3)==3 & tempvariables(:,nset,3+(nindy-1).*3)==5
classi(:,nset,nindy,nimp)=1;
end
end
end
end
for nset=1:100
for npart=1:33
for nimp=1:n
if classi(:,nset,1,nimp)==1 & classi(:,nset,2,nimp)==1 & classi(:,nset,3,nimp)==1 & classi(:,nset,4,nimp)==1 & classi(:,nset,5,nimp)==1
if tempvariables(:,nset,18+(npart-1).*2)~=5
if inputs(npart+(33.*tempvariables(:,nset,16)-1),nimp)<=inputrange(npart+(33.*tempvariables(:,nset,16)-1),2) && ...
inputs(npart+(33.*tempvariables(:,nset,16)-1),nimp)>=inputrange(npart+(33.*tempvariables(:,nset,16)-1),1)
if inputs(npart+(33.*tempvariables(:,nset,16)-1),nimp)<inputrange(npart+(33.*tempvariables(:,nset,16)-1),1)+(inputrange(npart+(33.*tempvariables(:,nset,16)-1),2)-inputrange(npart+(33.*tempvariables(:,nset,16)-1),1))./11.*tempvariables(:,nset,17+(npart-1).*2)...
+ (inputrange(npart+(33.*tempvariables(:,nset,16)-1),2)-inputrange(npart+(33.*tempvariables(:,nset,16)-1),1))./cutindy(tempvariables(:,nset,18+(npart-1).*2))./2 &&...
inputs(npart+(33.*tempvariables(:,nset,16)-1),nimp)>=inputrange(npart+(33.*tempvariables(:,nset,16)-1),1)+(inputrange(npart+(33.*tempvariables(:,nset,16)-1),2)-inputrange(npart+(33.*tempvariables(:,nset,16)-1),1))./11.*tempvariables(:,nset,17+(npart-1).*2)...
-(inputrange(npart+(33.*tempvariables(:,nset,16)-1),2)-inputrange(npart+(33.*tempvariables(:,nset,16)-1),1))./cutindy(tempvariables(:,nset,18+(npart-1).*2))./2
inputstemp(:,nset,npart,nimp)=1-(abs(inputs(npart+(33.*tempvariables(:,nset,16)-1),nimp)-(inputrange(npart+(33.*tempvariables(:,nset,16)-1),1)+(inputrange(npart+(33.*tempvariables(:,nset,16)-1),2)-inputrange(npart+(33.*tempvariables(:,nset,16)-1),1))./11.*tempvariables(:,nset,17+(npart-1).*2))))...
./((inputrange(npart+(33.*tempvariables(:,nset,16)-1),1)+(inputrange(npart+(33.*tempvariables(:,nset,16)-1),2)-inputrange(npart+(33.*tempvariables(:,nset,16)-1),1))./11.*tempvariables(:,nset,17+(npart-1).*2))...
-abs((inputrange(npart+(33.*tempvariables(:,nset,16)-1),1)+(inputrange(npart+(33.*tempvariables(:,nset,16)-1),2)-inputrange(npart+(33.*tempvariables(:,nset,16)-1),1))./11.*tempvariables(:,nset,17+(npart-1).*2))-(inputrange(npart+(33.*tempvariables(:,nset,16)-1),2)-inputrange(npart+(33.*tempvariables(:,nset,16)-1),1))./cutindy(tempvariables(:,nset,18+(npart-1).*2))./2));
end
end
if inputs(npart+(33.*tempvariables(:,nset,16)-1),nimp)<inputrange(npart+(33.*tempvariables(:,nset,16)-1),1) && ...
inputrange(npart+(33.*tempvariables(:,nset,16)-1),1)+(inputrange(npart+(33.*tempvariables(:,nset,16)-1),2)-inputrange(npart+(33.*tempvariables(:,nset,16)-1),1))./11.*tempvariables(:,nset,17+(npart-1).*2)...
-(inputrange(npart+(33.*tempvariables(:,nset,16)-1),2)-inputrange(npart+(33.*tempvariables(:,nset,16)-1),1))./cutindy(tempvariables(:,nset,18+(npart-1).*2))./2 ...
<inputrange(npart+(33.*tempvariables(:,nset,16)-1),1)
inputstemp(:,nset,npart,nimp)=1;
end
if inputs(npart+(33.*tempvariables(:,nset,16)-1),nimp)>inputrange(npart+(33.*tempvariables(:,nset,16)-1),2) && ...
inputrange(npart+(33.*tempvariables(:,nset,16)-1),1)+(inputrange(npart+(33.*tempvariables(:,nset,16)-1),2)-inputrange(npart+(33.*tempvariables(:,nset,16)-1),1))./11.*tempvariables(:,nset,17+(npart-1).*2)...
+ (inputrange(npart+(33.*tempvariables(:,nset,16)-1),2)-inputrange(npart+(33.*tempvariables(:,nset,16)-1),1))./cutindy(tempvariables(:,nset,18+(npart-1).*2))./2 ...
>inputrange(npart+(33.*tempvariables(:,nset,16)-1),2)
inputstemp(:,nset,npart,nimp)=1;
end
else
inputstemp(:,nset,npart,nimp)=1;
end
end
end
end
end
STDinputs(:,nset1,nimp1)=0;
AVinputs(:,nset1,nimp1)=0;
STDinputs(:,nset1,nimp1)=std(inputstemp,0,3);
AVinputs(:,nset1,nimp1)=mean(inputstemp,3);
inputstemp2(:,nset1,nimp1)=0;
inputstemp2(:,nset1,nimp1)=AVinputs(:,nset1,nimp1)-STDinputs(:,nset1,nimp1);
if GAfilter{1,1}(1,1)~=0
if inputstemp2(:,nset1,nimp1)<GAfilter{1,1}(1,1)
inputstemp2(:,nset1,nimp1)=0;
end
end
for i=1:100
for l=1:n
myinputsvect(:).myinputs(i,l)= inputstemp2(:,i,l);
end
end
clear('l','k','i','j','n','m','nimp','nset','nvar','nindy','nforex','nprice','npart','cutindy','mycut','STDinputs','AVinputs');
clear('nset1','nindy1','nimp1','npart1','inputstemp2');
myneuron=double(Nneuron{1,1}(1,1));
myvect(:).mynet=fitnet(myneuron(1,1));
myvect(:).mynet.trainFcn= 'trainlm';
myvect(:).mynet.biasConnect = [1;1];
myvect(:).mynet.inputs{1}.processFcns = {};
myvect(:).mynet.outputs{2}.processFcns = {};
myvect(:).mynet.divideFcn = 'divideblock';
myvect(:).mynet.divideParam.trainRatio = 80/100;
myvect(:).mynet.divideParam.valRatio = 10/100;
myvect(:).mynet.divideParam.testRatio = 10/100;
myvect(:).mynet.trainParam.showWindow = false;
myvect(:).mynet.trainParam.showCommandLine = false;
myvect(:).mynet.trainParam.epochs=100;
myvect(:).mynet.layers{1}.transferFcn = 'tansig';
myvect(:).mynet.layers{2}.transferFcn = 'tansig';
myvect(:).mynet.efficiency.memoryReduction=1;
[myvect(:).mynet,mytr(:).tr]=train(myvect(:).mynet,myinputsvect(:).myinputs,targets);
if mytr(:).tr.best_vperf>=mytr(:).tr.best_tperf
my_y=mytr(:).tr.best_vperf;
else
my_y=mytr(:).tr.best_tperf;
end
clear('myneuron');
end
Somebody can explain me what's wrong? Thanks

Accepted Answer

Alan Weiss
Alan Weiss on 3 Sep 2015
It is difficult to look at your code and figure out what you are trying to do. In "code 1" you have the line
tempvariables(i,j)=variables(:,j+(82.*(i-1))); %line 27
What do you think that tempvariables(i,j) should be here? i and j have particular values, so tempvariables(i,j) should have just one value.
If you are trying to set a whole piece of a matrix at once, you have to index that piece properly, and ensure that the left and right sides of the assignment statement have the same number of elements and orientation.
For example, if you have a matrix A and a matrix B, and you want to set column 5 of matrix A to equal column 4 of matrix B plus 6 times column 7 of matrix B, you could write
A(:,5) = B(:,4) + 6*B(:,7);
Similarly, if you want to do the same with rows of the matrices, you would write
A(5,:) = B(4,:) + 6*B(7,:);
I hope that this helps. For more information, see the colon operator reference page.
Alan Weiss
MATLAB mathematical toolbox documentation
  2 Comments
Alan Weiss
Alan Weiss on 4 Sep 2015
I am not sure that I understand your problem. As the documentation states, for ga you vectorize the fitness function by having it accept a matrix with an arbitrary number of rows, and return a column vector giving the fitness function for each row. Each row represents one individual, and the matrix represents the entire population.
Generally, you use the colon operator : to represent the list of rows (individuals) in the matrix (population). For example,
fun = x(:,1).^2 - x(:,2).^3
is a vectorized form of the fitness function x(1)^2 - x(2)^3.
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!