rectify error in genetic algorithm

2 views (last 30 days)
please do someone help me rectify this error...
i was using the genetic algorithm in matlab..
firstly i tried the followng code
rand('seed', 0);
FitnessFunction = @simple_fitness;
numberOfVariables = 50;
[x,fval] = ga(FitnessFunction,numberOfVariables);
function y = simple_fitness(x)
y = 100 * (x(1)^2 - x(2)) ^2 + (1 - x(1))^2;
and it worked fine...
then i tried giving input as
rand('seed', 0);
FitnessFunction = @simple_fitness;
numberOfVariables = 50;
A = rand(40,59);
b = ones(40,1);
[x,fval] = ga(FitnessFunction,numberOfVariables,A,b);
this worked for the first time, the second time i gave execute button it showed message as "didnt optimize..."
Third time also it got executed...
But then i changed the value of A and b to
A = rand(27,128);
b = ones(27,1);
then it didnt execute for the changed value... now it is not working for any values of A and b. It is showing error as
??? Error using ==> preProcessLinearConstr at 49
The number of columns in A must be the same as the length of X0.
Error in ==> gacommon at 79
[Iterate.x,Aineq,bineq,Aeq,beq,lb,ub,msg,exitFlag] = ...
Error in ==> ga at 269
[x,fval,exitFlag,output,population,scores,FitnessFcn,nvars,Aineq,bineq,Aeq,beq,lb,ub, ...
Error in ==> Untitled16 at 28
[x,fval] = ga(FitnessFunction,numberOfVariables,A,b);
i dont know why this error... it executed once, but now showing error... it gets executed for the case where A and b are not given... but in case we give values of A and b, the above error is obtained... Please do reply...
please can someone explain me what is the value of x in the first case...

Accepted Answer

Alan Weiss
Alan Weiss on 3 Jul 2014
I don't know what you are really trying to do, but whatever it is, you need to understand the syntax of linear constraints if you want to use them. As explained in Linear Inequality Constraints, A and b mean
A*x <= b
This is understood as matrix multiplication of A with a column vector of variables x. So the number of columns of A has to be the same as the number of variables x.
I, too, do not know why it executed when you have numberOfVariables = 50 and A with anything but 50 columns. You just got unlucky there, it probably won't happen again.
Alan Weiss
MATLAB mathematical toolbox documentation

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!