gusucode.com > matlab的GA工具箱与vc混和实现遗传算法源码程序 > initializega.cpp
#include "matlib.h" #pragma hdrstop #include "initializega.h" #include "calcbits.h" Mm initializega(Mm num, Mm bounds, Mm evalFN, Mm evalOps, Mm options) { begin_scope num.setname("num"); bounds.setname("bounds"); evalFN.setname("evalFN"); evalOps.setname("evalOps"); options.setname( \ "options"); dMm(pop); dMm(estr); dMm(numVars); dMm(rng); dMm(xZomeLength); dMm(bits); dMm(i_); dMm(i_initializega_v0); call_stack_begin; // nargin, nargout entry code double old_nargin=nargin_val; if (!nargin_set) nargin_val=5.0; nargin_set=0; double old_nargout=nargout_val; if (!nargout_set) nargout_val=1.0; nargout_set=0; // translated code // function [pop]=initializega(populationSize, variableBounds,evalFN, // evalOps,options) // initializega creates a matrix of random numbers with // a number of rows equal to the populationSize and a number // columns equal to the number of rows in bounds plus 1 for // the f(x) value which is found by applying the evalFN. // This is used by the ga to create the population if it // is not supplied. // // pop - the initial, evaluated, random population // populatoinSize - the size of the population, i.e. the number to create // variableBounds - a matrix which contains the bounds of each variable, i.e. // [var1_high var1_low; var2_high var2_low; ....] // evalFN - the evaluation fn, usually the name of the .m file for // evaluation // evalOps - any options to be passed to the eval function defaults [] // options - options to the initialize function, ie. // [type prec] where eps is the epsilon value // and the second option is 1 for float and 0 for binary, // prec is the precision of the variables defaults [1e-6 1] // Binary and Real-Valued Simulation Evolution for Matlab GAOT V2 // Copyright (C) 1998 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. if (istrue(nargin()<5.0)) { options = (BR(1e-06),1.0); } if (istrue(nargin()<4.0)) { evalOps = nop_M; } if (istrue(any(evalFN<48.0))) { //Not a .m file if (istrue(options(2.0)==1.0)) { //Float GA estr = (BR(TM("x=pop(i,1); pop(i,xZomeLength)=")),evalFN,TM(";")); } else { //Binary GA estr = (BR(TM("x=b2f(pop(i,:),bounds,bits); pop(i,xZomeLength)=")),evalFN,TM(";")); } } else { //A .m file if (istrue(options(2.0)==1.0)) { //Float GA estr = (BR(TM("[ pop(i,:) pop(i,xZomeLength)]=")),evalFN,TM("(pop(i,:),[0 evalOps]);")); } else { //Binary GA estr = (BR(TM("x=b2f(pop(i,:),bounds,bits);[x v]=")),evalFN,TM("(x,[0 evalOps]); pop(i,:)=[f2b(x,bounds,bits) v];") \ ); } } numVars = size(bounds,1.0); //Number of variables rng = ctranspose((bounds(c_p,2.0)-bounds(c_p,1.0))); //The variable ranges' if (istrue(options(2.0)==1.0)) { //Float GA xZomeLength = numVars+1.0; //Length of string is numVar + fit pop = zeros(num,xZomeLength); //Allocate the new population pop(c_p,colon(1.0,1.0,numVars)) = times((ones(num,1.0)*rng),(rand(num,numVars)))+(ones(num,1.0)*ctranspose( \ bounds(c_p,1.0))); } else { //Binary GA bits = calcbits(bounds,options(1.0)); xZomeLength = sum(bits)+1.0; //Length of string is numVar + fit pop = round(rand(num,sum(bits)+1.0)); } i_initializega_v0 = colon(1.0,1.0,num); int i_initializega_i0; for (i_initializega_i0=0;i_initializega_i0<i_initializega_v0.cols();i_initializega_i0++) { forelem(i_,i_initializega_v0,i_initializega_i0); eval(estr); } call_stack_end; // nargin, nargout exit code nargin_val=old_nargin; nargout_val=old_nargout; // function exit code num.setname(NULL); bounds.setname(NULL); evalFN.setname(NULL); evalOps.setname(NULL); options.setname(NULL); \ return pop; end_scope } Mm initializega() { begin_scope double old_nargin=nargin_val; nargin_val=0.0; nargin_set=1; dMm(num); dMm(bounds); dMm(evalFN); dMm(evalOps); dMm(options); dMm(ret1a); ret1a=initializega(num, bounds, evalFN, evalOps, options); nargin_val=old_nargin; return ret1a; end_scope } Mm initializega(Mm num) { begin_scope double old_nargin=nargin_val; nargin_val=1.0; nargin_set=1; dMm(bounds); dMm(evalFN); dMm(evalOps); dMm(options); dMm(ret1a); ret1a=initializega(num, bounds, evalFN, evalOps, options); nargin_val=old_nargin; return ret1a; end_scope } Mm initializega(Mm num, Mm bounds) { begin_scope double old_nargin=nargin_val; nargin_val=2.0; nargin_set=1; dMm(evalFN); dMm(evalOps); dMm(options); dMm(ret1a); ret1a=initializega(num, bounds, evalFN, evalOps, options); nargin_val=old_nargin; return ret1a; end_scope } Mm initializega(Mm num, Mm bounds, Mm evalFN) { begin_scope double old_nargin=nargin_val; nargin_val=3.0; nargin_set=1; dMm(evalOps); dMm(options); dMm(ret1a); ret1a=initializega(num, bounds, evalFN, evalOps, options); nargin_val=old_nargin; return ret1a; end_scope } Mm initializega(Mm num, Mm bounds, Mm evalFN, Mm evalOps) { begin_scope double old_nargin=nargin_val; nargin_val=4.0; nargin_set=1; dMm(options); dMm(ret1a); ret1a=initializega(num, bounds, evalFN, evalOps, options); nargin_val=old_nargin; return ret1a; end_scope }