java version of bat algorithm in matlab -


i have matlab code of bat algorithm , write java version of algorithm bat algorithm simple optimization algorithm finding minimum of function here matlab code , java version of code

my java version of algorithm can't find optimum result matlab version , can't find mistake in converting code matlab java

can me mistake?

import java.util.random;  public class bat {     private int n;     private float a, r;     private float qmin, qmax;     private int d;     private int nofgen;     private float fmin;     private int fminindex;     private float fnew;     private int loopcounter;      private float q[], v[][], sol[][], ul_bound[][], fitness[], s[][], best[];      private random myrand;      public bat(             int nbats,             float loudness,             float pulserate,             float minfreq,             float maxfreq,             int nofgeneration,             int dimension             )     {         n = nbats;         = loudness;         r = pulserate;         qmin = minfreq;         qmax = maxfreq;         nofgen = nofgeneration;         d = dimension;          s = new float[n][d];         best = new float[d];         ul_bound = new float[2][d];          //default bounds          for(int = 0 ;  < d ; i++)         {             ul_bound[0][i] = -10000;             ul_bound[1][i] =  10000;         }          loopcounter = 0;         myrand = new random();          q = new float[n];         for(int = 0 ; < n ; i++)             q[i] = 0;          v = new float[n][d];         for(int = 0 ; < n ; i++)             for(int j = 0 ; j < d ; j++)                 v[i][j] = 0;      }        public void intial()     {          sol = new float[n][d];         for(int = 0 ; < n ; i++)             for(int j = 0 ; j < d ; j++)             {                 float t = myrand.nextfloat();                 //(upper -lower)*rand + lower                 sol[i][j] = t * (ul_bound[1][j] - ul_bound[0][j]) + ul_bound[0][j];             }           fitness = new float[n];         for(int = 0 ; < n ; i++)             fitness[i] = function(sol[i]);          //finding fmin         fmin = fitness[0];         fminindex = 0;         for(int = 0 ; < n ; i++)         {             if (fitness[i] < fmin)             {                 fmin = fitness[i];                 fminindex = i;             }         }          //setting best         for(int j = 0 ; j < d ; j++)             best[j] = sol[fminindex][j];      }      public void start()     {          while(loopcounter < nofgen)         {              for(int = 0 ; < n ; i++)             {                  q[i] = qmin + (qmin - qmax)* myrand.nextfloat();                  for(int j = 0 ; j < d ; j++)                     v[i][j] = v[i][j] + (sol[i][j]-best[j])*q[i];                  for(int j = 0 ; j < d ; j++)                     s[i][j] = sol[i][j] + v[i][j];                   sol[i] = simplebounds(sol[i]);                  if(myrand.nextfloat() > r)                               for(int j = 0 ; j < d ; j++)                         s[i][j] =  (float) (best[j] + (.001 * myrand.nextfloat()) );                  fnew = function(s[i]);                  if(fnew <= fitness[i] && myrand.nextfloat() < a)                 {                     for(int j = 0 ; j < d ; j++)                         sol[i][j] = s[i][j];                      fitness[i] = fnew;                 }                  if(fnew <= fmin)                 {                     fmin = fnew;                     for(int j = 0 ; j < d ; j++)                         best[j] = s[i][j];                 }              }             loopcounter++;          }      }      public float[] simplebounds(float p[])     {         for(int = 0 ; < d ; i++)         {             if(p[i] < ul_bound[0][i])                 p[i] = ul_bound[0][i];              if(p[i] > ul_bound[1][i])                 p[i] = ul_bound[1][i];         }         return p;     }     float function(float p[])     {         // sphere function fmin=0 @ (0,0,...,0)         float sum = 0;         for(int = 0 ;  < p.length  ; i++)             sum = sum + p[i]*p[i];         return sum;      }     public float printresult()     {         system.out.println("after " + loopcounter + "repeats :");          for(int = 0 ; < d ; i++)             system.out.print(best[i] + ", ");          system.out.println ( "f(x)  = " + fmin);         return fmin;     }      public void set_ul_bound(int n, float l, float u)     {         if( n < d && n >= 0)         {             ul_bound[0][n] = l;             ul_bound[1][n] = u;         }     }        } 

and matlab versian

function [best,fmin,n_iter]=bat_algorithm(para) % display  bat_algorithm.m  % default parameters if nargin<1,  para=[20 1000 0.5 0.5];  end n=para(1);      % population size, typically 10 40 n_gen=para(2);  % number of generations a=para(3);      % loudness  (constant or decreasing) r=para(4);      % pulse rate (constant or decreasing) % frequency range determines scalings % should change these values if necessary qmin=0;         % frequency minimum qmax=2;         % frequency maximum % iteration parameters n_iter=0;       % total number of function evaluations % dimension of search variables d=5;           % number of dimensions  % lower limit/bounds/ vector lb=-3*ones(1,d); % upper limit/bounds/ vector ub=6*ones(1,d); % initializing arrays q=zeros(n,1);   % frequency v=zeros(n,d);   % velocities % initialize population/solutions i=1:n,   sol(i,:)=lb+(ub-lb).*rand(1,d);   fitness(i)=fun(sol(i,:)); end % find initial best solution [fmin,i]=min(fitness); best=sol(i,:);   t=1:n_gen,  % loop on bats/solutions         i=1:n,           q(i)=qmin+(qmin-qmax)*rand;           v(i,:)=v(i,:)+(sol(i,:)-best)*q(i);           s(i,:)=sol(i,:)+v(i,:);           % apply simple bounds/limits           sol(i,:)=simplebounds(sol(i,:),lb,ub);           % pulse rate           if rand>r           % factor 0.001 limits step sizes of random walks                s(i,:)=best+0.001*randn(1,d);           end       % evaluate new solutions            fnew=fun(s(i,:));      % update if solution improves, or not loud            if (fnew<=fitness(i)) & (rand<a) ,                 sol(i,:)=s(i,:);                 fitness(i)=fnew;            end            % update current best solution           if fnew<=fmin,                 best=s(i,:);                 fmin=fnew;           end         end         n_iter=n_iter+n; end % output/display disp(['number of evaluations: ',num2str(n_iter)]); disp(['best =',num2str(best),' fmin=',num2str(fmin)]);  % application of simple limits/bounds function s=simplebounds(s,lb,ub)   % apply lower bound vector   ns_tmp=s;   i=ns_tmp<lb;   ns_tmp(i)=lb(i);    % apply upper bound vector    j=ns_tmp>ub;   ns_tmp(j)=ub(j);   % update new move    s=ns_tmp;   function z=fun(u) % sphere function fmin=0 @ (0,0,...,0) z=sum(u.^2); %%%%% ============ end ==================================== 

  • the diff between 2 codes

in matlab code:

s(i,:)=best+0.001*randn(1,d);

randn=>standard normal distribution.

while in java code:

s[i][j] = (float) (best[j] + (.001 * myrand.nextfloat()) );

java.util.random.nextfloat()=>uniformly distributed float value between 0.0 , 1.0.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -