www.gusucode.com > matlab的GA工具箱与vc混和实现遗传算法源码程序 > b2f.cpp

      #include "matlib.h"
  #pragma hdrstop
  
  #include "b2f.h"
  
  
  Mm b2f(Mm bval, Mm bounds, Mm bits) {
    begin_scope
    bval.setname("bval"); bounds.setname("bounds"); bits.setname("bits"); 
    dMm(fval); dMm(scale); dMm(numV); dMm(cs); dMm(i_); dMm(i_b2f_v0); dMm(a); 
    
    call_stack_begin;
    // nargin, nargout entry code
    double old_nargin=nargin_val; if (!nargin_set) nargin_val=3.0;
    nargin_set=0;
    double old_nargout=nargout_val; if (!nargout_set) nargout_val=1.0;
    nargout_set=0;
    
    // translated code
    
    // function [fval] = b2f(bval,bounds,bits)
    //
    // Return the float number corresponing to the binary representation of bval.
    //
    // fval   - the float representation of the number
    // bval   - the binary representation of the number
    // bounds - the bounds on the variables
    // bits   - the number of bits to represent each variable
    
    // 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.
    
    scale = rdivide(ctranspose((bounds(c_p,2.0)-bounds(c_p,1.0))),(power(2.0,bits)-1.0));
    //The range of the variables
    numV = size(bounds,1.0);
    cs = (BR(0.0),cumsum(bits));
    
    i_b2f_v0 = colon(1.0,1.0,numV); int i_b2f_i0;
    for (i_b2f_i0=0;i_b2f_i0<i_b2f_v0.cols();i_b2f_i0++) {
      forelem(i_,i_b2f_v0,i_b2f_i0);
      a = bval(colon((cs(i_)+1.0),1.0,cs(i_+1.0)));
      fval(i_) = sum(times(power(2.0,(colon(size(a,2.0)-1.0,-1.0,0.0))),a))*scale(i_)+bounds(i_,1.0);
    }
    call_stack_end;
    
    // nargin, nargout exit code
    nargin_val=old_nargin; nargout_val=old_nargout;
    
    // function exit code
    bval.setname(NULL); bounds.setname(NULL); bits.setname(NULL); 
    return fval;
    end_scope
  }