using genetic algorithm for optimization

i have this error message
??? Subscripted assignment dimension mismatch.
Error in ==> fcnvectorizer at 14
y(i,:) = feval(fun,(pop(i,:)));
Error in ==> makeState at 47
Score =
fcnvectorizer(state.Population(initScoreProvided+1:end,:),FitnessFcn,1,options.SerialUserFcn);
Error in ==> gaunc at 41
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ==> ga at 279
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in ==> main at 11
[x,fval,exitflag,output,final_pop]=ga(FitnessFcn,nvar,options);
Caused by:
Failure in user-supplied fitness function evaluation. GA cannot continue.

 Respuesta aceptada

Alan Weiss
Alan Weiss el 29 de Mzo. de 2013

0 votos

Apparently there is an error in your fitness function. If it isn't too long, please post it so we can see what might be the problem.
And please include your gaoptimset calls and your function call to ga (e.g., x = ga(@main,4,A,b) )
Alan Weiss
MATLAB mathematical toolbox documentation

Más respuestas (3)

Walter Roberson
Walter Roberson el 29 de Mzo. de 2013
Your fitness function is returning your input values x_pop as its first return value. It should not be doing that. The documentation indicates,
fitnessfcn
Handle to the fitness function. The fitness function should accept a row vector of length nvars and return a scalar value.
When the 'Vectorized' option is 'on', fitnessfcn should accept a pop-by-nvars matrix, where pop is the current population size. In this case fitnessfcn should return a vector the same length as pop containing the fitness function values. fitnessfcn should not assume any particular size for pop, since ga can pass a single member of a population even in a vectorized calculation.
So your fitnessfcn should only be returning a single variable, not two variables, and (unless you have Vectorized turned on) return a scalar such as fx_val .

1 comentario

mado
mado el 29 de Mzo. de 2013
do you mean that i should write it function [ fx_val]=PID_objfun_ITAE(x_pop,options)

Iniciar sesión para comentar.

mado
mado el 29 de Mzo. de 2013
Editada: Walter Roberson el 29 de Mzo. de 2013
this is the fitness function code
function [x_pop, fx_val]=PID_objfun_ITAE(x_pop,options)
KP=x_pop(1);
KI=x_pop(2);
simopt = simset('solver','ode45','SrcWorkspace','Current','DstWorkspace','Current'); % Initialize sim options
[tout,xout,yout] = sim('pid_INC_GA',[0 .1],simopt);
t =0:0.1:30;
for i=1:301
error(i) = (abs(26.3-yout(i)))*t(i);
error=error*error';
ISE=sum(error);
end
sys_overshoot=max(yout)-26.3; % compute the overshoot
alpha=10;beta=1;
fx_val=ISE*beta+sys_overshoot*alpha;

9 comentarios

mado
mado el 29 de Mzo. de 2013
Editada: mado el 29 de Mzo. de 2013
and this is the main
%initialize genetic
nvar=2;
FitnessFcn = @PID_objfun_ITAE
options=gaoptimset('populationsize',30,'Generations',100,....
'SelectionFcn', @selectionroulette,'EliteCount',6)
%options=gaoptimset('Vectorize','on'); %fasten time %options = gaoptimset('InitialPop', final_pop); %start algorithm %______________________________________
[x,fval,exitflag,output,final_pop]=ga(FitnessFcn,nvar,options);
fprintf('Genetic Algorithm values : Kd = %7.5f Kp = %7.5f Ki = %7.4f ',x(1),x(2),x(3));
Alan Weiss
Alan Weiss el 29 de Mzo. de 2013
Please format your answers so that we can more easily read them. Use the {}Code button.
Alan Weiss
MATLAB mathematical toolbox documentation
mado
mado el 29 de Mzo. de 2013
can't do more ,excuse me
Walter Roberson
Walter Roberson el 29 de Mzo. de 2013
Please put a breakpoint in your PID_objfun_ITAE function at the last line, and check size(ISE) and size(sys_overshoot) . If sys_overshoot turns out not to be a scalar, please check size(yout)
mado
mado el 29 de Mzo. de 2013
size(yout) =1000 and error still there what do you mean by breakpoint
Walter Roberson
Walter Roberson el 29 de Mzo. de 2013
Note: every iteration of your "for" loop, you are overwriting error and ISE. After your instruction error=error*error'; then "error" is being replaced by a scalar. It is unlikely that that is what you want. Perhaps you want that instruction and the ISE assignment to be after the "for i" loop.
mado
mado el 29 de Mzo. de 2013
ok i know i put it outside for loop but where is the error here?
Put a breakpoint at the fx_val assignment. Run the program. When it stops, show us
size(ISE)
size(sys_overshoot)
size(ISE*beta+sys_overshoot*alpha)
If those values look reasonable, use the command
dbcont
to continue. See if you again stop at the same line, or if you get an error first. If you again stop at the same line then the problem is not triggered immediately so you should test those sizes and continue executing until you see something odd.
My speculation is that at some point an empty matrix is being generated.
mado
mado el 29 de Mzo. de 2013
size of them all are [1 1] and i typed dbcont it gives the error message directly

Iniciar sesión para comentar.

mado
mado el 30 de Mzo. de 2013

0 votos

i make the function return one variable fx-val but the program is iterating as it is in a loop and restart its work without stopping i don't think its the for loop of error cause i removed it and it gives error message this is a part of it "??? Error using ==> PID_objfun_ITAE at 28 Error due to multiple causes.
Error in ==> validate>@(x)fitness(x,FitnessFcnArgs{:}) at 136 fitness = @(x) fitness(x,FitnessFcnArgs{:});

Etiquetas

Preguntada:

el 29 de Mzo. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by