implicit none
integer(kind=4), parameter :: IB=4, RP=8
end module data_type
module data_Rosen
use data_type
implicit none
integer(kind=IB), parameter :: Dim_XC=10
end module data_Rosen
module data_HDE
use data_type
use data_Rosen
implicit none
integer(kind=IB), parameter :: NP=20, itermax=20000, strategy=6, &
refresh=500, iwrite=7
integer(kind=IB), dimension(3), parameter :: method=(/0, 1, 0/)
real(kind=RP), parameter :: VTR=-1.0e-4_RP, CR_XC=0.5_RP
real(kind=RP) :: F_XC=0.8_RP, F_CR=0.8_RP
real(kind=RP), dimension(Dim_XC), parameter :: XCmin=-10.0_RP, XCmax=10.0_RP
real(kind=RP), dimension(Dim_XC) :: bestmem_XC
integer(kind=IB) :: nfeval
real(kind=RP) :: bestval
end module data_HDE
program Rosen
use data_type
use data_Rosen
use data_HDE
implicit none
integer(kind=IB) :: i
integer (kind=IB), dimension(8) :: time
intrinsic date_and_time
external FTN
open(iwrite,file='Rosen.txt')
call date_and_time(values=time)
write(unit=iwrite, FMT=11) time(1:3), time(5:7)
call DE_Fortran90(FTN, Dim_XC, XCmin, XCmax, VTR, NP, itermax, F_XC,&握毁
CR_XC, strategy, refresh, iwrite, bestmem_XC, &
bestval, nfeval, F_CR, method)
write(iwrite,205) NP, nfeval, method(1:3)
write(iwrite,FMT=201) F_XC, CR_XC, F_CR
write(iwrite,FMT=200) bestval
do i=1,Dim_XC
write(iwrite,FMT=202) i,bestmem_XC(i)
end do
200 format(/2x, 'Bestval=', ES14.7)
201 format(2x, 'F_XC =',F6.3, 2x, 'CR_XC =', F6.3, 2x, '尘桥F_CR =', F6.3)
202 format(2x, 'best_XC(',I3,') =',ES14.7)
205 format(2x, 'NP=', I4, 4x, 'No. function call =', I9, &
/2x, 'mehtod(1:3) =',3I3)
call date_and_time(values=time)
write(unit=iwrite, FMT=10)time(1:3), time(5:7)
10 format(/1x, 'End of Program. Date:', I4, '/', I2,'/', I2, '段兄备, Time: ', I2,':',I2,':',I2)
11 format(1x, 'Beginning of Program. Date:', I4, '/', I2,'/', I2, ', Time: ', I2,':',I2,':',I2)
end program Rosen
subroutine FTN(X, objval)
use data_type
use data_Rosen
implicit none
real(kind=RP), dimension(Dim_XC), intent(in) :: X
real(kind=RP), intent(out) :: objval
integer(kind=IB) :: i
i=Dim_XC
objval=sum(100.0*(x(1:i-1)**2-x(2:i))**2+(1.0-x(1:i-1))**2)
end subroutine FTN
subroutine DE_Fortran90(obj, Dim_XC, XCmin, XCmax, VTR, NP, itermax, F_XC, &
CR_XC, strategy, refresh, iwrite, bestmem_XC, bestval, nfeval, &
F_CR, method)
!.......................................................................
!
! Differential Evolution for Optimal Control Problems
!
!.......................................................................
! This Fortran 90 program translates from the original MATLAB
! version of differential evolution (DE). This FORTRAN 90 code
! has been tested on Compaq Visual Fortran v6.1.
! Any users new to the DE are encouraged to read the article of Storn and Price.
!
! Refences:
! Storn, R., and Price, K.V., (1996). Minimizing the real function of the
! ICEC'96 contest by differential evolution. IEEE conf. on Evolutionary
! Comutation, 842-844.
!
! This Fortran 90 program written by Dr. Feng-Sheng Wang
! Department of Chemical Engineering, National Chung Cheng University,
! Chia-Yi 621, Taiwan, e-mail: chmfsw@ccunix.ccu.edu.tw
!.........................................................................
! obj : The user provided file for evlauting the objective function.
! subroutine obj(xc,fitness)
! where "xc" is the real decision parameter vector.(input)
! "fitness" is the fitness value.(output)
! Dim_XC : Dimension of the real decision parameters.
! XCmin(Dim_XC) : The lower bound of the real decision parameters.
! XCmax(Dim_XC) : The upper bound of the real decision parameters.
! VTR : The expected fitness value to reach.
! NP : Population size.
! itermax : The maximum number of iteration.
! F_XC : Mutation scaling factor for real decision parameters.
! CR_XC : Crossover factor for real decision parameters.
! strategy : The strategy of the mutation operations is used in HDE.
! refresh : The intermediate output will be produced after "refresh"
! iterations. No intermediate output will be produced if
! "refresh <1".
! iwrite : The unit specfier for writing to an external data file.
! bestmen_XC(Dim_XC) : The best real decision parameters.
! bestval : The best objective function.
! nfeval : The number of function call.
! method(1) = 0, Fixed mutation scaling factors (F_XC)
! = 1, Random mutation scaling factors F_XC=[0, 1]
! = 2, Random mutation scaling factors F_XC=[-1, 1]
! method(2) = 1, Random combined factor (F_CR) used for strategy = 6
! in the mutation operation
! = other, fixed combined factor provided by the user
! method(3) = 1, Saving results in a data file.
! = other, displaying results only.
use data_type, only : IB, RP
implicit none
integer(kind=IB), intent(in) :: NP, Dim_XC, itermax, strategy, &
iwrite, refresh
real(kind=RP), intent(in) :: VTR, CR_XC
real(kind=RP) :: F_XC, F_CR
real(kind=RP), dimension(Dim_XC), intent(in) :: XCmin, XCmax
real(kind=RP), dimension(Dim_XC), intent(inout) :: bestmem_XC
real(kind=RP), intent(out) :: bestval
integer(kind=IB), intent(out) :: nfeval
real(kind=RP), dimension(NP,Dim_XC) :: pop_XC, bm_XC, mui_XC, mpo_XC, &
popold_XC, rand_XC, ui_XC
integer(kind=IB) :: i, ibest, iter
integer(kind=IB), dimension(NP) :: rot, a1, a2, a3, a4, a5, rt
integer(kind=IB), dimension(4) :: ind
real(kind=RP) :: tempval
real(kind=RP), dimension(NP) :: val
real(kind=RP), dimension(Dim_XC) :: bestmemit_XC
real(kind=RP), dimension(Dim_XC) :: rand_C1
integer(kind=IB), dimension(3), intent(in) :: method
external obj
intrinsic max, min, random_number, mod, abs, any, all, maxloc
interface
function randperm(num)
use data_type, only : IB
implicit none
integer(kind=IB), intent(in) :: num
integer(kind=IB), dimension(num) :: randperm
end function randperm
end interface
!!-----Initialize a population --------------------------------------------!!
pop_XC=0.0_RP
do i=1,NP
call random_number(rand_C1)
pop_XC(i,:)=XCmin+rand_C1*(XCmax-XCmin)
end do
!!--------------------------------------------------------------------------!!
!!------Evaluate fitness functions and find the best member-----------------!!
val=0.0_RP
nfeval=0
ibest=1
call obj(pop_XC(1,:), val(1))
bestval=val(1)
nfeval=nfeval+1
do i=2,NP
call obj(pop_XC(i,:), val(i))
nfeval=nfeval+1
if (val(i) <bestval) then
ibest=i
bestval=val(i)
end if
end do
bestmemit_XC=pop_XC(ibest,:)
bestmem_XC=bestmemit_XC
!!--------------------------------------------------------------------------!!
bm_XC=0.0_RP
rot=(/(i,i=0,NP-1)/)
iter=1
!!------Perform evolutionary computation------------------------------------!!
do while (iter <= itermax)
popold_XC=pop_XC
!!------Mutation operation--------------------------------------------------!!
ind=randperm(4)
a1=randperm(NP)
rt=mod(rot+ind(1),NP)
a2=a1(rt+1)
rt=mod(rot+ind(2),NP)
a3=a2(rt+1)
rt=mod(rot+ind(3),NP)
a4=a3(rt+1)
rt=mod(rot+ind(4),NP)
a5=a4(rt+1)
bm_XC=spread(bestmemit_XC, DIM=1, NCOPIES=NP)
!----- Generating a random sacling factor--------------------------------!
select case (method(1))
case (1)
call random_number(F_XC)
case(2)
call random_number(F_XC)
F_XC=2.0_RP*F_XC-1.0_RP
end select
!---- select a mutation strategy-----------------------------------------!
select case (strategy)
case (1)
ui_XC=bm_XC+F_XC*(popold_XC(a1,:)-popold_XC(a2,:))
case default
ui_XC=popold_XC(a3,:)+F_XC*(popold_XC(a1,:)-popold_XC(a2,:))
case (3)
ui_XC=popold_XC+F_XC*(bm_XC-popold_XC+popold_XC(a1,:)-popold_XC(a2,:))
case (4)
ui_XC=bm_XC+F_XC*(popold_XC(a1,:)-popold_XC(a2,:)+popold_XC(a3,:)-popold_XC(a4,:))
case (5)
ui_XC=popold_XC(a5,:)+F_XC*(popold_XC(a1,:)-popold_XC(a2,:)+popold_XC(a3,:) &
-popold_XC(a4,:))
case (6) ! A linear crossover combination of bm_XC and popold_XC
if (method(2) == 1) call random_number(F_CR)
ui_XC=popold_XC+F_CR*(bm_XC-popold_XC)+F_XC*(popold_XC(a1,:)-popold_XC(a2,:))
end select
!!--------------------------------------------------------------------------!!
!!------Crossover operation-------------------------------------------------!!
call random_number(rand_XC)
mui_XC=0.0_RP
mpo_XC=0.0_RP
where (rand_XC <CR_XC)
mui_XC=1.0_RP
! mpo_XC=0.0_RP
elsewhere
! mui_XC=0.0_RP
mpo_XC=1.0_RP
end where
ui_XC=popold_XC*mpo_XC+ui_XC*mui_XC
!!--------------------------------------------------------------------------!!
!!------Evaluate fitness functions and find the best member-----------------!!
do i=1,NP
!!------Confine each of feasible individuals in the lower-upper bound-------!!
ui_XC(i,:)=max(min(ui_XC(i,:),XCmax),XCmin)
call obj(ui_XC(i,:), tempval)
nfeval=nfeval+1
if (tempval <val(i)) then
pop_XC(i,:)=ui_XC(i,:)
val(i)=tempval
if (tempval <bestval) then
bestval=tempval
bestmem_XC=ui_XC(i,:)
end if
end if
end do
bestmemit_XC=bestmem_XC
if( (refresh >0) .and. (mod(iter,refresh)==0)) then
if (method(3)==1) write(unit=iwrite,FMT=203) iter
write(unit=*, FMT=203) iter
do i=1,Dim_XC
if (method(3)==1) write(unit=iwrite, FMT=202) i, bestmem_XC(i)
write(*,FMT=202) i,bestmem_XC(i)
end do
if (method(3)==1) write(unit=iwrite, FMT=201) bestval
write(unit=*, FMT=201) bestval
end if
iter=iter+1
if ( bestval <= VTR .and. refresh >0) then
write(unit=iwrite, FMT=*) ' The best fitness is smaller than VTR'
write(unit=*, FMT=*) 'The best fitness is smaller than VTR'
exit
endif
end do
!!------end the evolutionary computation------------------------------!!
201 format(2x, 'bestval =', ES14.7, /)
202 format(5x, 'bestmem_XC(', I3, ') =', ES12.5)
203 format(2x, 'No. of iteration =', I8)
end subroutine DE_Fortran90
function randperm(num)
use data_type, only : IB, RP
implicit none
integer(kind=IB), intent(in) :: num
integer(kind=IB) :: number, i, j, k
integer(kind=IB), dimension(num) :: randperm
real(kind=RP), dimension(num) :: rand2
intrinsic random_number
call random_number(rand2)
do i=1,num
number=1
do j=1,num
if (rand2(i) >rand2(j)) then
number=number+1
end if
end do
do k=1,i-1
if (rand2(i) <= rand2(k) .and. rand2(i) >= rand2(k)) then
number=number+1
end if
end do
randperm(i)=number
end do
return
发现的几处错误:
1、适应度函数里面if a[i]=4改为if a(i)==4,类似的还有if b[i]=4。不需要多解释了吧?一个是数组注意和C语言风格区别,另一个是判断相等的符号问题。
2、适应度函数应返回列向量,在fit函数最后加一句:fitness=fitness(:)
3、选择的结果是种群规模减小,不能使用固定的出示规模20,应把适应度函数里面两处循环for i=1:20改为for i=1:size(x,1)。
4、主函数里面rein应为衡弊reins。
代码写到一个M文件中:
function zd%% 初始化遗传算法参数
%初始化参数
NIND=20
MAXGEN=100
NVAR=8
PRECI=1
GGAP=0.9% 进化代数,即迭代次数
% 种群规模
%% 初始化种群计算适应度值
% 初始化种群
FieldD=[rep(PRECI,[1,NVAR])rep([01],[1,NVAR])rep([1011],[1,NVAR])]
Chrom=crtbp(NIND,NVAR*PRECI)
ObjV=fit(bs2rv(Chrom,FieldD))
gen=0
while gen<MAXGEN
FitnV=ranking(ObjV)
SelCh=select('sus',Chrom,FitnV,GGAP)
SelCh=recombin('xovsp',SelCh,0.7)
SelCh=mut(SelCh,0.07)
ObjVSel=fit(bs2rv(SelCh,FieldD))
[Chrom ObjV]=reins(Chrom,SelCh,1,1,ObjV,ObjVSel)
gen=gen+1
%找最好的染色体
trace(gen,1)=min(ObjV)
trace(gen,2)=sum(ObjV)/length(ObjV)
end
plot(trace(:,1)) hold on
plot(trace(:,2)) grid
legend('average'蠢碰,'bestfitness')
function [fitness]=fit(x)
for 咐档族i=1:size(x,1)
i
%随机产生一个种群
if (x(i,6)*x(i,7)-x(i,8)*x(i,6))*(x(i,3)*x(i,2)-x(i,4)*x(i,1))==0
x(i,:)=unidrnd(2,1,8)-1
end%染色体的适应度
end
a=x(:,1)+x(:,2)+x(:,3)+x(:,4)
b=x(:,5)+x(:,6)+x(:,7)+x(:,8)
for i=1:size(x,1)
i
if a(i)==4
c=1
else
c=0
end
if b(i)==4
d=1
else
d=0
end
fitness(i)=c+d
end
fitness=fitness(:)
这个问题也困扰了我好久,终于解决了。给你个ga.m程序,新建m文件复制进去,再运行程序试试。%ga.m
function [x,endPop,bPop,traceInfo] = ga(bounds,evalFN,evalOps,startPop,opts,...
termFN,termOps,selectFN,selectOps,xOverFNs,xOverOps,mutFNs,mutOps)
% GA run a genetic algorithm
% function [x,endPop,bPop,traceInfo]=ga(bounds,evalFN,evalOps,startPop,opts,
% termFN,termOps,selectFN,selectOps,
% xOverFNs,xOverOps,mutFNs,mutOps)
%
% Output Arguments:
% x- the best solution found during the course of the run
% endPop - the final population
% bPop - a trace of the best population
% traceInfo- a matrix of best and means of the ga for each generation
%
% Input Arguments:
% bounds - a matrix of upper and lower bounds on the variables
% evalFN - the name of the evaluation .m function
% evalOps - options to pass to the evaluation function ([NULL])
% startPop - a matrix of solutions that can be initialized
% from initialize.m
% opts - [epsilon prob_ops display] change required to consider two
% solutions different, prob_ops 0 if you want to apply the
% genetic operators probabilisticly to each solution, 1 if
% you are supplying a deterministic number of operator
% applications and display is 1 to output progress 0 for
% quiet. ([1e-6 1 0])
% termFN - name of the .m termination function (['maxGenTerm'])
% termOps - options string to be passed to the termination function
% ([100]).
% selectFN - name of the .m selection function (['normGeomSelect'])
% selectOpts - options string to be passed to select after
% select(pop,#,opts) ([0.08])
% xOverFNS - a string containing blank seperated names of Xover.m
% files (['arithXover heuristicXover simpleXover'])
% xOverOps - A matrix of options to pass to Xover.m files with the
% first column being the number of that xOver to perform
% similiarly for mutation ([2 02 32 0])
% mutFNs - a string containing blank seperated names of mutation.m
% files (['boundaryMutation multiNonUnifMutation ...
% nonUnifMutation unifMutation'])
% mutOps - A matrix of options to pass to Xover.m files with the
% first column being the number of that xOver to perform
% similiarly for mutation ([4 0 06 100 34 100 34 0 0])
% Binary and Real-Valued Simulation Evolution for Matlab
% Copyright (C) 1996 C.R. Houck, J.A. Joines, M.G. Kay
%
% C.R. Houck, J.Joines, and M.Kay. A genetic algorithm for function
% optimization: A Matlab implementation. ACM Transactions on Mathmatical
% Software, Submitted 1996.
%
% This program is free softwareyou can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundationeither version 1, or (at your option)
% any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTYwithout even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details. A copy of the GNU
% General Public License can be obtained from the
% Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
%%$Log: ga.m,v $
%Revision 1.10 1996/02/02 15:03:00 jjoine
% Fixed the ordering of imput arguments in the comments to match
% the actual order in the ga function.
%
%Revision 1.9 1995/08/28 20:01:07 chouck
% Updated initialization parameters, updated mutation parameters to reflect
% b being the third option to the nonuniform mutations
%
%Revision 1.8 1995/08/10 12:59:49 jjoine
%Started Logfile to keep track of revisions
%
n=nargin
if n<2 | n==6 | n==10 | n==12
disp('Insufficient arguements')
end
if n<3 %Default evalation opts.
evalOps=[]
end
if n<5
opts = [1e-6 1 0]
end
if isempty(opts)
opts = [1e-6 1 0]
end
if any(evalFN<48) %Not using a .m file
if opts(2)==1 %Float ga
e1str=['x=c1c1(xZomeLength)=', evalFN '']
e2str=['x=c2c2(xZomeLength)=', evalFN '']
else %Binary ga
e1str=['x=b2f(endPop(j,:),bounds,bits)endPop(j,xZomeLength)=',...
evalFN '']
end
else %Are using a .m file
if opts(2)==1 %Float ga
e1str=['[c1 c1(xZomeLength)]=' evalFN '(c1,[gen evalOps])']
e2str=['[c2 c2(xZomeLength)]=' evalFN '(c2,[gen evalOps])']
else %Binary ga
e1str=['x=b2f(endPop(j,:),bounds,bits)[x v]=' evalFN ...
'(x,[gen evalOps])endPop(j,:)=[f2b(x,bounds,bits) v]']
end
end
if n<6 %Default termination information
termOps=[100]
termFN='maxGenTerm'
end
if n<12 %Default muatation information
if opts(2)==1 %Float GA
mutFNs=['boundaryMutation multiNonUnifMutation nonUnifMutation unifMutation']
mutOps=[4 0 06 termOps(1) 34 termOps(1) 34 0 0]
else %Binary GA
mutFNs=['binaryMutation']
mutOps=[0.05]
end
end
if n<10 %Default crossover information
if opts(2)==1 %Float GA
xOverFNs=['arithXover heuristicXover simpleXover']
xOverOps=[2 02 32 0]
else %Binary GA
xOverFNs=['simpleXover']
xOverOps=[0.6]
end
end
if n<9 %Default select opts only i.e. roullete wheel.
selectOps=[]
end
if n<8 %Default select info
selectFN=['normGeomSelect']
selectOps=[0.08]
end
if n<6 %Default termination information
termOps=[100]
termFN='maxGenTerm'
end
if n<4 %No starting population passed given
startPop=[]
end
if isempty(startPop) %Generate a population at random
%startPop=zeros(80,size(bounds,1)+1)
startPop=initializega(80,bounds,evalFN,evalOps,opts(1:2))
end
if opts(2)==0 %binary
bits=calcbits(bounds,opts(1))
end
xOverFNs=parse(xOverFNs)
mutFNs=parse(mutFNs)
xZomeLength = size(startPop,2) %Length of the xzome=numVars+fittness
numVar = xZomeLength-1 %Number of variables
popSize = size(startPop,1) %Number of individuals in the pop
endPop = zeros(popSize,xZomeLength)%A secondary population matrix
c1 = zeros(1,xZomeLength) %An individual
c2 = zeros(1,xZomeLength) %An individual
numXOvers= size(xOverFNs,1) %Number of Crossover operators
numMuts = size(mutFNs,1) %Number of Mutation operators
epsilon = opts(1)%Threshold for two fittness to differ
oval = max(startPop(:,xZomeLength))%Best value in start pop
bFoundIn = 1 %Number of times best has changed
done = 0 %Done with simulated evolution
gen = 1 %Current Generation Number
collectTrace = (nargout>3) %Should we collect info every gen
floatGA = opts(2)==1 %Probabilistic application of ops
display = opts(3)%Display progress
while(~done)
%Elitist Model
[bval,bindx] = max(startPop(:,xZomeLength))%Best of current pop
best = startPop(bindx,:)
if collectTrace
traceInfo(gen,1)=gen %current generation
traceInfo(gen,2)=startPop(bindx,xZomeLength) %Best fittness
traceInfo(gen,3)=mean(startPop(:,xZomeLength))%Avg fittness
traceInfo(gen,4)=std(startPop(:,xZomeLength))
end
if ( (abs(bval - oval)>epsilon) | (gen==1)) %If we have a new best sol
if display
fprintf(1,'\n%d %f\n',gen,bval) %Update the display
end
if floatGA
bPop(bFoundIn,:)=[gen startPop(bindx,:)]%Update bPop Matrix
else
bPop(bFoundIn,:)=[gen b2f(startPop(bindx,1:numVar),bounds,bits)...
startPop(bindx,xZomeLength)]
end
bFoundIn=bFoundIn+1 %Update number of changes
oval=bval %Update the best val
else
if display
fprintf(1,'%d ',gen) %Otherwise just update num gen
end
end
endPop = feval(selectFN,startPop,[gen selectOps])%Select
if floatGA %Running with the model where the parameters are numbers of ops
for i=1:numXOvers,
for j=1:xOverOps(i,1),
a = round(rand*(popSize-1)+1) %Pick a parent
b = round(rand*(popSize-1)+1) %Pick another parent
xN=deblank(xOverFNs(i,:)) %Get the name of crossover function
[c1 c2] = feval(xN,endPop(a,:),endPop(b,:),bounds,[gen xOverOps(i,:)])
if c1(1:numVar)==endPop(a,(1:numVar)) %Make sure we created a new
c1(xZomeLength)=endPop(a,xZomeLength)%solution before evaluating
elseif c1(1:numVar)==endPop(b,(1:numVar))
c1(xZomeLength)=endPop(b,xZomeLength)
else
%[c1(xZomeLength) c1] = feval(evalFN,c1,[gen evalOps])
eval(e1str)
end
if c2(1:numVar)==endPop(a,(1:numVar))
c2(xZomeLength)=endPop(a,xZomeLength)
elseif c2(1:numVar)==endPop(b,(1:numVar))
c2(xZomeLength)=endPop(b,xZomeLength)
else
%[c2(xZomeLength) c2] = feval(evalFN,c2,[gen evalOps])
eval(e2str)
end
endPop(a,:)=c1
endPop(b,:)=c2
end
end
for i=1:numMuts,
for j=1:mutOps(i,1),
a = round(rand*(popSize-1)+1)
c1 = feval(deblank(mutFNs(i,:)),endPop(a,:),bounds,[gen mutOps(i,:)])
if c1(1:numVar)==endPop(a,(1:numVar))
c1(xZomeLength)=endPop(a,xZomeLength)
else
%[c1(xZomeLength) c1] = feval(evalFN,c1,[gen evalOps])
eval(e1str)
end
endPop(a,:)=c1
end
end
else %We are running a probabilistic model of genetic operators
for i=1:numXOvers,
xN=deblank(xOverFNs(i,:)) %Get the name of crossover function
cp=find(rand(popSize,1)<xOverOps(i,1)==1)
if rem(size(cp,1),2) cp=cp(1:(size(cp,1)-1))end
cp=reshape(cp,size(cp,1)/2,2)
for j=1:size(cp,1)
a=cp(j,1)b=cp(j,2)
[endPop(a,:) endPop(b,:)] = feval(xN,endPop(a,:),endPop(b,:),...
bounds,[gen xOverOps(i,:)])
end
end
for i=1:numMuts
mN=deblank(mutFNs(i,:))
for j=1:popSize
endPop(j,:) = feval(mN,endPop(j,:),bounds,[gen mutOps(i,:)])
eval(e1str)
end
end
end
gen=gen+1
done=feval(termFN,[gen termOps],bPop,endPop)%See if the ga is done
startPop=endPop %Swap the populations
[bval,bindx] = min(startPop(:,xZomeLength))%Keep the best solution
startPop(bindx,:) = best %replace it with the worst
end
[bval,bindx] = max(startPop(:,xZomeLength))
if display
fprintf(1,'\n%d %f\n',gen,bval)
end
x=startPop(bindx,:)
if opts(2)==0 %binary
x=b2f(x,bounds,bits)
bPop(bFoundIn,:)=[gen b2f(startPop(bindx,1:numVar),bounds,bits)...
startPop(bindx,xZomeLength)]
else
bPop(bFoundIn,:)=[gen startPop(bindx,:)]
end
if collectTrace
traceInfo(gen,1)=gen %current generation
traceInfo(gen,2)=startPop(bindx,xZomeLength)%Best fittness
traceInfo(gen,3)=mean(startPop(:,xZomeLength))%Avg fittness
end
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)