LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

fireflies algorithm

Hello,

THis is my attempt to code the firefly algorithm in Labview.

I am based on this paper

http://arxiv.org/pdf/1003.1464v1.pdf

 

Begin
   1) Objective function: f(\mathbf{x}), \quad \mathbf{x}=(x_1,x_2,...,x_d) ;
   2) Generate an initial population of fireflies  \mathbf{x}_i \quad (i=1,2,\dots,n);.
   3) Formulate light intensity I so that it is associated with f(\mathbf{x})
      (for example, for maximization problems, I \propto f(\mathbf{x}) or simply I=f(\mathbf{x});
   4) Define absorption coefficient \gamma 
 
   While (t < MaxGeneration)
      for i = 1 : n (all n fireflies)
         for j = 1 : n (n fireflies)
            if (I_j>I_i ), 
               move firefly i towards j;
            end if 
            Vary attractiveness with distance r via  \exp(-\gamma \; r) ;
            Evaluate new solutions and update light intensity;
         end for j
      end for i
      Rank fireflies and find the current best;
   end while

   Post-processing the results and visualization;

end

 

 

\mathbf{x}_i^{t+1}=\mathbf{x}_i^t + \beta \exp[-\gamma r_{ij}^2] (\mathbf{x}_j^t - \mathbf{x}_i^t) +\alpha_t \boldsymbol{\epsilon}_t

 

Could anyone help me to fix the code because it is not working yet and I don't have any idea what I should modify to make it work.

I started by optimizing a simple function

f(x,y)=exp(-(x^2+y^2))

 

Thank you for your help.

Zied

0 Kudos
Message 1 of 12
(4,814 Views)

I'm not familiar with the algorthm and can't spend all day diving deep in to this code, but I took a glance and compared the algorithm in your post to the code and found some weird stuff. Here is the code with my comments added:

Bring Firefly Back.png

Cheers


--------,       Unofficial Forum Rules and Guidelines                                           ,--------

          '---   >The shortest distance between two nodes is a straight wire>   ---'


Message 2 of 12
(4,779 Views)

Thank you very much James. I tried to add more comments to the vi, I corrected  several things and I used the Auckley function for test. I expect to find a global maximum f(x)=0 at (0,0,0,..0).

Unfortunately the Vi fails to converge. I hope someone can help me to find the source of the error.

Thanks,

Zied

0 Kudos
Message 3 of 12
(4,664 Views)

I added to the vi a graph to show the fireflies in a 2d space. It is clear that the fireflies don't fly in all the space and miss the (0,0) coordinates. Is it related to the Levy flights that add the randomness to the space scan or an error in the code I still cannot find?

Thanks,

Zied

Message 4 of 12
(4,651 Views)

@ziedhosni wrote:

I added to the vi a graph to show the fireflies in a 2d space. It is clear that the fireflies don't fly in all the space and miss the (0,0) coordinates. Is it related to the Levy flights that add the randomness to the space scan or an error in the code I still cannot find?

Thanks,

Zied


hi,

 

did you take a look at "..\National Instruments\LabVIEW 20XX\examples\Mathematics\Optimization\Find Global Min on Surface.vi"?

 

 Start:

 0000.PNG

 

 

after 50 Iterations:

0026.PNG

Message 5 of 12
(4,577 Views)

Thank you very much. It is a useful vi but my objective is to write the fireflies algorithm with a levy flights distribution. The behaviour of the code changed when I added a Cauchy distribution as I did not find the Levy distribution. The results is a bit closer to what I expect.

Thanks,

Zied

0 Kudos
Message 6 of 12
(4,547 Views)

why do you think your code isn't wright?

what's the ouput you would expect supposed to look like?

 

You say your code is "not working" - but it does produces an ouput each time I run your vi.

 

 

Your "initial I" and "X initial" is quite randomly generated each time the .vi is started.

 

Let's use a fix input, to see if the algorithm generates the same output for the same input.

2015-11-05_fixed input.png

 

 

 

Ok, we get different ouputs for the same input.

 

2015-11-05_output_3runs.png

 

 

Is this what you would expect?

 

 

I didn't change anything at "formula (SubVI).vi"

 

2015-11-05_fixed input_core.png

 

 

 

Message 7 of 12
(4,517 Views)

THank you Alex. You helped me to make the vi more organised. In my previous version, I added the random generator according to the Cauchy distribution. This helped to improve the result. But I think it is still not enough. I expect to see 2 things.

1- The intensity (Ii) or at least the maximum which is the golbal maximum to increase as function of the generation until I reach 0 which is the real global maximum for the function (Ackley) I defined in the subVI.

2- In the plot of the Xi for 2 dimentional space (x and y for each Xi are the coordinates of each firefly). So I expect to see the point try to apporach from 0 after each generation until they fall in the global maximum (0,0).

You find attached the VI with the Cauchy distribution. Ideally, I would like to use the Levy distribution

Cheers,

Zied

0 Kudos
Message 8 of 12
(4,492 Views)

@ziedhosni wrote:

THank you Alex. You helped me to make the vi more organised. In my previous version, I added the random generator according to the Cauchy distribution. This helped to improve the result. But I think it is still not enough. I expect to see 2 things.

1- The intensity (Ii) or at least the maximum which is the golbal maximum to increase as function of the generation until I reach 0 which is the real global maximum for the function (Ackley) I defined in the subVI.

2- In the plot of the Xi for 2 dimentional space (x and y for each Xi are the coordinates of each firefly). So I expect to see the point try to apporach from 0 after each generation until they fall in the global maximum (0,0).

You find attached the VI with the Cauchy distribution. Ideally, I would like to use the Levy distribution

Cheers,

Zied


Please get rid of the "Build XY Graph"-Express.vi - I think, your LabView skills are advanced enough!

 

XY Graph 3 is easy to replace by the "bundle" function from the cluster palette:

 

2015-11-06_XY-graph3_usebundle.PNG

 

 

 

XY Graph 2 should be fairly easy to replace, but I have to look into detail, what's happening inside this other Express VI.

 

 

 

 

 

Message 9 of 12
(4,466 Views)

Ok, now understand what's going on in both XY Graphs, the "other" Express Vi is like an unlimited growing buffer which may be coded with the Insert Array.vi

0.PNG

 

1.PNG

 

 

 

I attached my version of "core algorithm.vi" & "formula (SubVI).vi" converted to LabView 2010, as I don't know your LabView Version.

 

 

 

So I expect to see the point try to apporach from 0 after each generation until they fall in the global maximum (0,0).

I'm sorry I don't understand. Isn't this happening already?

 

Ideally, I would like to use the Levy distribution

 According to wikipedia, the Levy distribution is a "[...] a special case of the inverse-gamma distribution."

Couldn't you use the Continous Inverse CDF.vi ?

 

 

Message 10 of 12
(4,452 Views)