📄 ga.cpp
字号:
#include "matlib.h"
#pragma hdrstop
#include "ga.h"
#include "feval.h"
#include "initializega.h"
#include "calcbits.h"
#include "parse.h"
#include "b2f.h"
Mm ga(Mm bounds, Mm evalFN, Mm evalOps, Mm startPop, Mm opts, Mm termFN, Mm termOps, Mm selectFN, Mm selectOps, \
Mm xOverFNs, Mm xOverOps, Mm mutFNs, Mm mutOps, i_o_t, Mm& x__o, Mm& endPop__o, Mm& bPop__o, Mm& traceInfo__o) \
{
begin_scope
bounds.setname("bounds"); evalFN.setname("evalFN"); evalOps.setname("evalOps"); startPop.setname("startPop"); \
opts.setname("opts"); termFN.setname("termFN"); termOps.setname("termOps"); selectFN.setname("selectFN"); selectOps.setname( \
"selectOps"); xOverFNs.setname("xOverFNs"); xOverOps.setname("xOverOps"); mutFNs.setname("mutFNs"); mutOps.setname( \
"mutOps");
dMm(x); dMm(endPop); dMm(bPop); dMm(traceInfo); dMm(n); dMm(e1str); dMm(e2str); dMm(bits); dMm(xZomeLength); \
dMm(numVar); dMm(popSize); dMm(c1); dMm(c2); dMm(numXOvers); dMm(numMuts); dMm(epsilon); dMm(oval); dMm(bFoundIn) \
; dMm(done); dMm(gen); dMm(collectTrace); dMm(floatGA); dMm(display_); dMm(bval); dMm(bindx); dMm(best); dMm( \
i_); dMm(i_ga_v0); dMm(j_); dMm(j_ga_v1); dMm(a); dMm(b); dMm(xN); dMm(i_ga_v2); dMm(j_ga_v3); dMm(i_ga_v4); \
dMm(cp); dMm(j_ga_v5); dMm(lhs_0); dMm(lhs_1); dMm(i_ga_v6); dMm(mN); dMm(j_ga_v7);
call_stack_begin;
// nargin, nargout entry code
double old_nargin=nargin_val; if (!nargin_set) nargin_val=13.0;
nargin_set=0;
double old_nargout=nargout_val; if (!nargout_set) nargout_val=4.0;
nargout_set=0;
// translated code
// 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 0;2 3;2 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 0;6 100 3;4 100 3;4 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 software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 1, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without 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 (istrue(n<2.0)||istrue(n==6.0)||istrue(n==10.0)||istrue(n==12.0)) {
display( disp(TM("Insufficient arguements")) );
}
if (istrue(n<3.0)) {
//Default evalation opts.
evalOps = nop_M;
}
if (istrue(n<5.0)) {
opts = (BR(1e-06),1.0,0.0);
}
if (istrue(isempty(opts))) {
opts = (BR(1e-06),1.0,0.0);
}
if (istrue(any(evalFN<48.0))) {
//Not using a .m file
if (istrue(opts(2.0)==1.0)) {
//Float ga
e1str = (BR(TM("x=c1; c1(xZomeLength)=")),evalFN,TM(";"));
e2str = (BR(TM("x=c2; c2(xZomeLength)=")),evalFN,TM(";"));
} else {
//Binary ga
e1str = (BR(TM("x=b2f(endPop(j,:),bounds,bits); endPop(j,xZomeLength)=")),evalFN,TM(";"));
}
} else {
//Are using a .m file
if (istrue(opts(2.0)==1.0)) {
//Float ga
e1str = (BR(TM("[c1 c1(xZomeLength)]=")),evalFN,TM("(c1,[gen evalOps]);"));
e2str = (BR(TM("[c2 c2(xZomeLength)]=")),evalFN,TM("(c2,[gen evalOps]);"));
} else {
//Binary ga
e1str = (BR(TM("x=b2f(endPop(j,:),bounds,bits);[x v]=")),evalFN,TM("(x,[gen evalOps]); endPop(j,:)=[f2b(x,bounds,bits) v];") \
);
}
}
if (istrue(n<6.0)) {
//Default termination information
termOps = (BR(100.0));
termFN = TM("maxGenTerm");
}
if (istrue(n<12.0)) {
//Default muatation information
if (istrue(opts(2.0)==1.0)) {
//Float GA
mutFNs = (BR(TM("boundaryMutation multiNonUnifMutation nonUnifMutation unifMutation")));
mutOps = (BR(4.0),0.0,0.0,semi,
6.0,termOps(1.0),3.0,semi,
4.0,termOps(1.0),3.0,semi,
4.0,0.0,0.0);
} else {
//Binary GA
mutFNs = (BR(TM("binaryMutation")));
mutOps = (BR(0.05));
}
}
if (istrue(n<10.0)) {
//Default crossover information
if (istrue(opts(2.0)==1.0)) {
//Float GA
xOverFNs = (BR(TM("arithXover heuristicXover simpleXover")));
xOverOps = (BR(2.0),0.0,semi,
2.0,3.0,semi,
2.0,0.0);
} else {
//Binary GA
xOverFNs = (BR(TM("simpleXover")));
xOverOps = (BR(0.6));
}
}
if (istrue(n<9.0)) {
//Default select opts only i.e. roullete wheel.
selectOps = nop_M;
}
if (istrue(n<8.0)) {
//Default select info
selectFN = (BR(TM("normGeomSelect")));
selectOps = (BR(0.08));
}
if (istrue(n<6.0)) {
//Default termination information
termOps = (BR(100.0));
termFN = TM("maxGenTerm");
}
if (istrue(n<4.0)) {
//No starting population passed given
startPop = nop_M;
}
if (istrue(isempty(startPop))) {
//Generate a population at random
//startPop=zeros(80,size(bounds,1)+1);
startPop = initializega(80.0,bounds,evalFN,evalOps,opts(colon(1.0,1.0,2.0)));
}
if (istrue(opts(2.0)==0.0)) {
//binary
bits = calcbits(bounds,opts(1.0));
}
xOverFNs = parse(xOverFNs);
mutFNs = parse(mutFNs);
xZomeLength = size(startPop,2.0);
//Length of the xzome=numVars+fittness
numVar = xZomeLength-1.0;
//Number of variables
popSize = size(startPop,1.0);
//Number of individuals in the pop
endPop = zeros(popSize,xZomeLength);
//A secondary population matrix
c1 = zeros(1.0,xZomeLength);
//An individual
c2 = zeros(1.0,xZomeLength);
//An individual
numXOvers = size(xOverFNs,1.0);
//Number of Crossover operators
numMuts = size(mutFNs,1.0);
//Number of Mutation operators
epsilon = opts(1.0);
//Threshold for two fittness to differ
oval = max(startPop(c_p,xZomeLength));
//Best value in start pop
bFoundIn = 1.0;
//Number of times best has changed
done = 0.0;
//Done with simulated evolution
gen = 1.0;
//Current Generation Number
collectTrace = (nargout()>3.0);
//Should we collect info every gen
floatGA = opts(2.0)==1.0;
//Probabilistic application of ops
display_ = opts(3.0);
//Display progress
while (istrue(!done)) {
//Elitist Model
/*[bval,bindx] = */max(startPop(c_p,xZomeLength),i_o,bval,bindx);
//Best of current pop
best = startPop(bindx,c_p);
if (istrue(collectTrace)) {
traceInfo(gen,1.0) = gen;
//current generation
traceInfo(gen,2.0) = startPop(bindx,xZomeLength);
//Best fittness
traceInfo(gen,3.0) = mean(startPop(c_p,xZomeLength));
//Avg fittness
traceInfo(gen,4.0) = stdM(startPop(c_p,xZomeLength));
}
if (istrue((abs(bval-oval)>epsilon))||istrue((gen==1.0))) {
//If we have a new best sol
if (istrue(display_)) {
fprintf(1.0,TM("\\n%d %f\\n"),(CL(gen),bval));
//Update the display
}
if (istrue(floatGA)) {
bPop(bFoundIn,c_p) = (BR(gen),startPop(bindx,c_p));
//Update bPop Matrix
} else {
bPop(bFoundIn,c_p) = (BR(gen),b2f(startPop(bindx,colon(1.0,1.0,numVar)),bounds,bits),startPop(bindx,xZomeLength) \
);
}
bFoundIn = bFoundIn+1.0;
//Update number of changes
oval = bval;
//Update the best val
} else {
if (istrue(display_)) {
fprintf(1.0,TM("%d "),(CL(gen)));
//Otherwise just update num gen
}
}
endPop = feval(selectFN,(CL(startPop),(BR(gen),selectOps)));
//Select
if (istrue(floatGA)) {
//Running with the model where the parameters are numbers of ops
i_ga_v0 = colon(1.0,1.0,numXOvers); int i_ga_i0;
for (i_ga_i0=0;i_ga_i0<i_ga_v0.cols();i_ga_i0++) {
forelem(i_,i_ga_v0,i_ga_i0);
j_ga_v1 = colon(1.0,1.0,xOverOps(i_,1.0)); int j_ga_i1;
for (j_ga_i1=0;j_ga_i1<j_ga_v1.cols();j_ga_i1++) {
forelem(j_,j_ga_v1,j_ga_i1);
a = round(rand()*(popSize-1.0)+1.0);
//Pick a parent
b = round(rand()*(popSize-1.0)+1.0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -