genetic algorithm tool (Bit string)

%first way%
function g =myfitnesstest(X)
s=0;
if (X(1)==1)
s=s+1;
end
if (X(2)==1)
s=s+2;
end
if (X(3)==1)
s=s+4;
end
if (X(4)==1)
s=s+8;
end
if (X(5)==1)
s=s+16;
end
LAMDA=s;
TYPE=1;
ITER=1000;
R1=Routing3a3(TYPE,LAMDA,ITER);
g=-R1;
end
---------
%second way%
function g =myfitness2(X)
LAMDA=X(1)+X(2)*2+X(3)*4+X(4)*8
TYPE=1;
ITER=1000;
R1=Routing3a(TYPE,LAMDA,ITER);
g=-R1;
end
------ when i use this (first way) the matlab run it very very slowly. when i use (second way) the matlab run with out stopping with many solution with repeating. i want way to represent the value of X with bit string type to find the optimal solution (by using Matlab gatool )
[EDITED, Jan, code formatted]

回答(1 个)

I can't try right now, but I am fairly certain that your first few if statements could be substituted by
s = 2.^(0:4) *X(:);
only this would be muuuuch faster, that should explain why your way 1 is so slow.
Of course we don't know what Routing3a does, so hard to help you if there's an error in there.
And lastly, it appears to me that
g=(R1);
while(g>=99)
g
break
end
is nothing but
if R1>=99
R1
end
so what is that line supposed to be doing?

7 个评论

sorry first way just like that ( i removed the second part of first way ) and the Routing3a is function of my project It works well correct and quick. i want to to put the value of* x* 1 to 16 but in bit string way with out any point (ex... 1.5 or 2.3 ..etc) just real number 1..2..3.....16 with out repeating any number. the first way work but very very slow. (2 or 3 hours). the second way work very fast but with repeating and with out stopping . Routing3a return for me value of R1
for example(if LAMDA =1 R1=13.5, LAMDA=2 R1=27.32 ......LAMDA=8 R1=96.21 , LAMDA=9 R1=100 when the value of LAMDA Higher than 9 (10 or 11 ...etc) the value of R1= 100.
i want to use ga(tool) to select best value of LAMDA
So you are saying you want to maximize R1 with respect to LAMBDA? If LAMDA indeed varies only from 1 to 16, why don't you just try all 16 and pick the best?
What does your call to ga look like? Would something like
ga(@(b) -Routing3a(1, 1+b*2.^(0:3), 1000), 4)
work? My impression is you are complicating your problem a lot...
mohammed is discussing this exact same topic in other threads. He only has 16 possible inputs, so a "for" loop or "arrayfun" would be the obvious way to proceed, but for reasons I do not understand, he feels he must use ga() to evaluate those possibilities, ideally only one time each. He does not have continuous inputs such as ga() would normally provide and his version of MATLAB does not have the possibility of using IntCon. Note that in the ga() call you suggest here, ga() is going to choose continuous values for the vector "b".
This is not the first time I have seen someone say they are required to use ga() to minimize over a fixed (and usually not very big) set of possibilities, when arrayfun() or the like would be much more natural.
Details, mohammed, details!
??? Error using ==> mtimes Inner matrix dimensions must agree.
Error in ==> @(b)-Routing3a(1,1+b*2.^(0:3),1000)
Error in ==> validate>@(x)fitness(x,FitnessFcnArgs{:}) at 136 fitness = @(x) fitness(x,FitnessFcnArgs{:});
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 291 [x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Caused by: Failure in user-supplied fitness function evaluation. GA cannot continue.

请先登录,再进行评论。

类别

Community Treasure Hunt

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

Start Hunting!

Translated by