You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Genetic Algorithm problem: initial population size
12 views (last 30 days)
Show older comments
when I ran my optimization using ga, I got the following error: Any suggestions will be appreciated.
Error using validate (line 201) Incorrect size of InitialPopulation.
Error in gacommon (line 72) [options,nvars,FitnessFcn,NonconFcn] = validate(options,type,nvars,fun,nonlcon,user_options);
Error in ga (line 319) [x,fval,exitFlag,output,population,scores,FitnessFcn,nvars,Aineq,bineq,Aeq,beq,lb,ub, ...
Answers (1)
Alan Weiss
on 25 Jul 2014
Did you give an initial population? Did it match the number of variables that you passed to GA?
For more detailed help, please show us your options and your call to GA. And perhaps tell us how many variables your fitness funtion is expecting.
Alan Weiss
MATLAB mathematical toolbox documentation
15 Comments
roja chigiti
on 12 Apr 2018
Edited: Walter Roberson
on 12 Apr 2018
i am also having the same issue
si is an java.lang.string with a value of 88001102177383048688338133739104972920397361352570410883073005159324252420948
i wanted to send this as my initialpopulation
its showing the following errors
Error using validate (line 286)
Invalid value for OPTIONS parameter InitialPopulationMatrix.
Error in gacommon (line 65)
[options,nvars,FitnessFcn,NonconFcn] = validate(options,type,nvars,fun,nonlcon,user_options);
Error in ga (line 336)
NonconFcn,options,Iterate,type] = gacommon(nvars,fun,Aineq,bineq,Aeq,beq,lb,ub, ...
Error in temp (line 142)
[chromosome,~,~,~,~,~]=ga(fitnessfcn,nVars,options) ;
if i am converting si to num using str2num its showing following error
Error using validate (line 294)
Incorrect size of InitialPopulationMatrix.
Error in gacommon (line 65)
[options,nvars,FitnessFcn,NonconFcn] = validate(options,type,nvars,fun,nonlcon,user_options);
Error in ga (line 336)
NonconFcn,options,Iterate,type] = gacommon(nvars,fun,Aineq,bineq,Aeq,beq,lb,ub, ...
Error in temp (line 142)
[chromosome,~,~,~,~,~]=ga(fitnessfcn,nVars,options) ;
roja chigiti
on 13 Apr 2018
Edited: Walter Roberson
on 13 Apr 2018
options = gaoptimset('InitialPopulation', si,...
'PopulationSize',200,...
'Generations',100,...
'PopulationType', 'bitstring',...
'SelectionFcn',{@selectiontournament,tournamentSize},...
'MutationFcn',{@mutationuniform, 0.01},...
'CrossoverFcn', {@crossoverarithmetic,0.8},...
'EliteCount',0.05*1,...
'StallGenLimit',100,...
'PlotFcns',{@gaplotbestf},...
'Display', 'iter');
following is my fitness function: pop is the initial population which i am sending
function [FitVal] = FitFunc()
pop=getGlobalx;
le= length(pop);
FeatIndex = find(pop==1); %Feature Index
NumFeat = numel(FeatIndex);
FitVal= NumFeat/ le * 0.01;
end
Walter Roberson
on 13 Apr 2018
Is it correct that getGlobalx is going to return a copy of si that you originally set?
Your find: are you hoping to match the digit '1' in the population? So your fitness returns the fraction of '1'? And since you are minimizing, the lowest possible would be when there were no '1'?
Why are you ignoring the input to the function? How can the fitness function return anything different between invocations?
roja chigiti
on 13 Apr 2018
Edited: roja chigiti
on 13 Apr 2018
getGlobalX returns copy of si
i dont understand what you are asking i dont know how initial population is getting passed inside fitfunc so i internally passed value if not needed then do guide me for that. following is what i am trying to do if there is another way to do this please do tell me.
import java.security.*;
import java.lang.*;
import java.math.*;
md = MessageDigest.getInstance('SHA-256');
hash = md.digest(uint8(all));
si=double(reshape(hash, 1, []));;
%selection of best feature using genetic algorithm
tournamentSize=2;
options = gaoptimset('InitialPopulation',si,...
'PopulationSize',200,...
'Generations',100,...
'PopulationType', 'bitstring',...
'SelectionFcn',{@selectiontournament,tournamentSize},...
'MutationFcn',{@mutationuniform, 0.01},...
'CrossoverFcn', {@crossoverarithmetic,0.8},...
'EliteCount',0.05*1,...
'StallGenLimit',100,...
'PlotFcns',{@gaplotbestf},...
'Display', 'iter');
rng('Shuffle','v5normal')
fitnessfcn = @FitFunc;
nVars = 1;
[chromosome,~,~,~,~,~]=ga(fitnessfcn,nVars,options) ;
Best_chromosome = chromosome; % Best Chromosome
Feat_Index = find(Best_chromosome==1); % Index of Chromosome
end
function [FitVal] = FitFunc(Population)
le= length(Population);
FeatIndex = find(Population==1); %Feature Index
NumFeat = numel(FeatIndex);
FitVal= NumFeat/ le * 0.01;
end
Walter Roberson
on 13 Apr 2018
Your code
function [FitVal] = FitFunc(Population)
le= length(Population);
FeatIndex = find(Population==1); %Feature Index
NumFeat = numel(FeatIndex);
FitVal= NumFeat/ le * 0.01;
end
can be optimized to
function FitVal = FitFunc(Population)
FitVal = mean(Population == 1) * 0.01;
end
This will not change the effect of your function: it should just run faster.
Note that the effect of using this function will be to cross-over and mutate until no elements of Population are equal to 1, until you run out of iterations. Is that the intention ? A form of randomization applied to the population?
Note that your initial population, a java.lang.string with a value of '88001102177383048688338133739104972920397361352570410883073005159324252420948'
is not equal to numeric 1, so the initial test will find a fitness of 0 and nothing will be able to improve that.
Best_chromosome = chromosome; % Best Chromosome
Feat_Index = find(Best_chromosome==1); % Index of Chromosome
If the optimization went well, then
FeatIndex = find(Population==1); %Feature Index
will be minimized, so ideally none of the elements in the population will be equal to 1, which makes it odd that you have
Feat_Index = find(Best_chromosome==1); % Index of Chromosome
You are searching for something that, if the optimization went well, will not be present.
options = gaoptimset('InitialPopulation',si,...
You will need to change that to
options = gaoptimset('InitialPopulation', char(si), ...
roja chigiti
on 14 Apr 2018
Edited: roja chigiti
on 14 Apr 2018
thank you for taking your time.. as per your suggestion i tried to change si to char(si) but its showing error as follow Error using strip (line 46) First argument must be a string array, character vector, or cell array of character vectors.
Error in gaoptimset (line 345) arg = strip(arg);
Error in temp (line 104) options = gaoptimset('InitialPopulation',char(si),...
attached file is what i am trying to do
Walter Roberson
on 14 Apr 2018
Your actual java.lang.String in si is a column vector, each element of which is certain to be one of '0' through '9' because it is constructed from a BigInteger. But you are declaring your population to be bitstring, about which it is documented,
"Bit string ('bitstring') — Use this option if the individuals in the population have components that are 0 or 1."
gaoptimiset is complaining because the char() of the column vector java.lang.String is producing a character array, but gaoptimset expects either character row vectors or cell array of character vectors when the population is not numeric. That can be handled by cellstr(char(si))
The validation that the inputs are numeric is failing, because you do not have numeric inputs.
And then when you get into your fitness function, you compare your population members to numeric 1. If you are expecting characters then why do you compare to numeric 1 ?
I really have to wonder what you are doing. You have a bit-level hash; if you want to fiddle with bits then why not pass in a row vector version of the bits as the x to be worked on? But even then I can't tell what you think you are optimizing on. If you are trying to optimize to have as few digit '1' as possible, then why not replace them all with random '0' or '2' to '9' ?
roja chigiti
on 14 Apr 2018
when i am converting si to bit string and passing population type as bit string it showing error as Error using validate (line 286) Invalid value for OPTIONS parameter InitialPopulationMatrix.
Error in gacommon (line 65) [options,nvars,FitnessFcn,NonconFcn] = validate(options,type,nvars,fun,nonlcon,user_options);
Error in ga (line 336) NonconFcn,options,Iterate,type] = gacommon(nvars,fun,Aineq,bineq,Aeq,beq,lb,ub, ...
Error in temp (line 121) [chromosome,~,~,~,~,~]=ga(fitnessfcn,nVars,options) ;
Walter Roberson
on 14 Apr 2018
How are you converting si to bit string? And why are you converting si to bitstring, when your si is the printable version of the BigInteger that is the encoding of the int8 values that are in hash? If you want bits then why not directly convert hash to bits, such as by
reshape( (dec2bin(typecast(hash, 'uint8'),8) - '0').', 1, [])
roja chigiti
on 14 Apr 2018
Edited: roja chigiti
on 14 Apr 2018
vi=dec2bin(hex2dec(num2hex(str2double(si))));
i am converting like this
when i am converting hash value to bits and passing it generates error of Incorrect size of initialPopulationMatrix
Walter Roberson
on 14 Apr 2018
I have no idea why you are using hex2dec() of num2hex() of the doubles. Especially since str2double() on your column vector would end up trying to convert '88001102177383048688338133739104972920397361352570410883073005159324252420948' to a double, getting 8.8001102177383e+76 and losing almost all of the bits of the input.
The code I gave preserves all of the bits.
roja chigiti
on 14 Apr 2018
yes sir i tried to pass hash as bits earlier but it showing error of incorrect size of InitialPopulationMatrix that's why i tried to do other way
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)