# genetic algorithm - reg

조회 수: 7(최근 30일)
I want to use genetic algorithm for inventory classification. Number of variables is 6. All the variables must lie between 0 and 1. If we add first 4 variables, the sum must be 1. Also 5 th variable must be greater than 6 th variable. My doubt is whether the Fitness function should return a single scalar value or the fitness function should return all the fitness values of the population. I think the GA can be solved using the toolbox of GA in MATLAB. I tried to run GA in optimtool toolbox. The population size is 50. The fitness function is InvGAClassifyFitnessFunc(Position, classGA1,ClassDM!) where Position is the chromosome with the above properties. I have written the inequalities as Aineq=[0,0,0,0,-1,1] Bineq=0;
Aeq as [1,1,1,1,0,0] and Beq as ;
lb as [0,0,0,0,0,0] and ub as [1,1,1,1,1,1];
creation function as constraint-dependent. Population type is double.Scaling function as Rank. Selection function as 'Roulette'.Mutation function as 'Adaptive feasible'. Crossover function as 'Constraint dependent'. All other options as default.
When i ran this problem in MATLAB it is giving error " Not enough input arguments".

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

### 채택된 답변

Walter Roberson 11 Dec 2018
Your fitness function will be passed a single population member in the form of a vector . It should return aa scalar . ga will call it as many times as needed to pass in all the population members .
Your fitness function appears to require 3 inputs needing classGA1 and ClassDM1 as the second and third parameters . ga only passes a single parameter to the fitness function . You probably need to paramaterize the fitness function
ff = @(Position) InvGAClassifyFitnessFunc(Position, classGA1, ClassDM1 )
and then pass ff as the fitness function .

#### 댓글 수: 3

I wrote the following code to run genetic algorithm . The best fitness and mean fitness are remaining same at 1 through out the generations. Where did i made the mistake.
[ Position ] = InvClassifyGAFunc2(4,68);
[classGA1 ] = @(Position)InvClassifyGAWeightedSumFunc(Position);
classGA=char(68,1);
ClassDM=cell(68,1);
classGA1=char(68,1);
ClassDM1=cell(68,1);
ClassDM2=char(68,1);
for i=1:68
ClassDM1{i,1}=Data.ClassDM{i+3,1};
ClassDM1{i,1}=ClassDM1{i,1};
end
ClassDM1=@(i)ClassDM(i,1);
ff = @(Position) InvClassifyGAFitnessFunc(Position, classGA1, ClassDM1 ) ;
A=[0,0,0,0,-1,1];
b=;
Aeq=[1,1,1,1,0,0];
beq=;
lb=[0,0,0,0,0,0];
ub=[1,1,1,1,1,1];
options= gaoptimset('PlotFcn',@gaplotbestf);
[x,fval]=ga(ff,6,A,b,Aeq,beq,lb,ub,[],options);
Walter Roberson 11 Dec 2018
Note: char(68,1) does not reserve a 68 x 1 array of characters. char(68,1) is the same as
[char(68); char(1)]
which takes the character that is assigned position 68, namely 'D', and put below it the character assigned position 1, namely https://www.fileformat.info/info/unicode/char/0001/index.htm SOH, Start Of Heading.
Walter Roberson 11 Dec 2018
It would be easier if we had your source code to work with.
for i=1:68
ClassDM1{i,1}=Data.ClassDM{i+3,1};
ClassDM1{i,1}=ClassDM1{i,1};
end
Okay so the first ClassDM1{i,1} assignment extracts some data from Data.ClassDM. But then the second one overwrites what was just assigned... overwrites it with itself ??
ClassDM1=@(i)ClassDM(i,1);
and that overwrites the cell array just built up, with a function handle to an anonymous function that extracts one cell (not the contents of the cell) from a variable that is still only initialized to cell(68,1) ?
And the function handle is being passed as a parameter to InvClassifyGAFitnessFunc ??

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

R2015b

### Community Treasure Hunt

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

Start Hunting!