Particle Swarm Optimization in RCWA

Particle Swarm Optimization in RCWA


Introduction to Particle Swarm

What is particle swarm optimization?

Particle swarm optimization is an evolutionary algorithm for optimizing an arbitrary function. In this case, the function being optimized is any of the many outputs of the RCWA code we use (which is courtesy of the Podolskiy group at UMass Lowell). The default version of RCWA used in our particle swarms is Noah's 2022/2023 version, but if you understand what's going on feel free to change it.

The best and most common analogy for describing particle swarm optimization is a flock of birds. I've tried to add some of the regular PSO jargon in parenthesis to make it easier to translate later:

Basically, imagine you have a flock (swarm) of birds (particles) that are looking for a spot to land that has a ton of food (the best solution). The birds all start at the same height (the iterations) but at different x-y coordinates (the variables). Each bird starts by knowing it's best spot (personal best) for food, but they all move in random directions (velocities) looking for a better spot. They also move down by one meter (one iteration). At this point all of the birds know their own personal best spot for food, but they also know the flock's best position (group best). In future iterations, the birds move randomly, but the directions are weighted slightly to pull them to their personal bests and the group best. Eventually, the birds all converge on the best spot (which should be the swarm's optimal solution given enough iterations).

The idea for the code we use is based on a paper from Shokooh-Saremi and Magnusson (https://opg.optica.org/ol/fulltext.cfm?uri=ol-32-8-894&id=131251), where they optimize GMR filters.

Key Parameters

In particle swarm, there are a few key parameters. Some are particle specific, and some are important to the entire code.

  • Particle Specific:
    • Position
    • Velocity
    • Fitness (how good the solution is)
    • Personal best position
  • Swarm parameters
    • Cognitive rate (how fast we move to the personal best)
    • Social rate (how fast we move to the group best)
    • Inertial weight (how fast we keep moving int the same direction)
    • Upper and lower boundary conditions
    • Number of particles
    • Number of iterations of optimization
    • Group's best position
    • Group best fitness

If you keep these parameters in mind while working on the code, it should be pretty easy to get it to work.


An example of particle swarm optimization (from Wikipedia). Ephramac, CC BY-SA 4.0 <https://creativecommons.org/licenses/by-sa/4.0>, via Wikimedia Commons

How to use PSO

Setup the Files

The three files you need for PSO can be found on our group's Box folder. Check Noah's "Student Folder" for a file name Particle Swarm (or something like that). The files you need are: Particle.m, YourNameHere_PSO_Driver.m, YourNameHere_PSO_RCWA.m

When you are running the code, swap the "YourNameHere" with whatever you want to name your project. Put all of the files in the same folder

Additionally, you need all of the regular RCWA function files in the same folder as your PSO files.

Set the RCWA Parameters

At this point, go into the PSO_RCWA.m file and configure your structure. You do not need to specify wavelengths you are optimizing at, since the other files take care of that and we will do it later.

When you configure your structure, any parameter you want to sweep needs to be assigned as: variable(n) where "n" is an integer. This is because the variables stored in individual particles are passed into the RCWA code from the driver file.

Set the RCWA Output

Whatever data you want RCWA to spit out, you need to set at the end of the PSO_RCWA.m file. Set the variable ret_value equal to an array representing the output you want. For example:

ret_value = TMabs;

returns the total TM absorption of your structure as an array.

Configuring the Driver File

The driver file is the last part of the code that needs to be modified in order to run the swarm. There are a few things that need to be changed.

Firstly, you need to set the swarm size. The swarm_size parameter must be larger than the number of variables. Otherwise, you will only optimize over some of your variables. A larger swarm size also equates to a higher likelihood of finding good solutions. Recommended values are 15, 20, or 25 particles. I wouldn't do more than 30.

Next, set the number of iterations. More iterations means that while it will take longer to get a solution, the solution you find will be more likely to be the best result. It is recommended to use at least 100 iterations (unless you are running 2D simulations), but you can go much higher, especially in the cases that your structure is planar.

Then, set the wavelengths for visualization. If an optimized result looks better than the current best, your RCWA will simulate the full spectrum at the wavelengths given in this list to keep track during running. You can also choose to not use this.

If you aren't familiar with PSO, it's recommended that you don't touch the cognitive, social, or inertia weights.

Setting the boundary conditions is the next part. For each variable you set in the RCWA file, you need to set bounds on your parameter space. This is done sequentially in a matrix. Upperbounds should always be greater than lowerbounds or the optimization won't function and the solution may diverge to infinity.

p_upperbounds = [1]; %upper bounds based on the variables
p_lowerbounds = [0.5]; %lower bounds based on the variables

Then, you need to set the desired output values and the wavelength points of those values. This is done with the desvals and wlpoints variables. Basically, you say what values you want (i.e. 0.8,0.4,0 reflection) and the wavelength (5,8,9 µm). The lists would then look like this:

desvals = [0.8, 0.4, 0]; %Desired values (use common sense)
wlpoints = [5, 8, 9]; %Wavelengths of those values

These points tell the RCWA code what values to simulate, and they tell the fitness function what you should compare to. More points means that each individual point will be weighted less heavily, but you are less likely to run into weird features.

The last thing you need to do is replace three things in the main code body (all of them are the same, so use ctrl+F to find them). You need to replace YourNameHere_PSO_RCWA with whatever you named the RCWA file. After that, you should be done.


Running the Code and Interpreting the Output

Start the code by pressing "Run" for the driver file. It is recommended that you run the code on the group server so you can continue to use your laptop's full capabilities.

The PSO code will show a two things when it starts. It will show a waitbar detailing its progress initializing particles. It will also show a plot with the expected output shape (will be blank if you only optimize at one point). After the initialization step completes, the waitbar will get replaced by a second waitbar detailing the total PSO progress.

At this point, a second plot will appear detailing the fitness of the output over time. Lower fitness is better (its closer to what you want). This shows you how the optimization is proceeding, so you can choose to stop it early if the fitness seems okay. After the simulation starts the second iteration, you will also see the first plot update to show the best RCWA simulation every time the code finds a better result.

The command window will print data out at the end of every simulation iteration. This information is the best set of variables, the best fitness, and the various inertial, cognitive, and social rates. This is a good tool for pulling out the best parameters at the end of the simulation.

Output during initialization

Output during the first iteration (after initialization). Figure 2 not shown

Sample MATLAB Command Window output during running the optimization

Output during an early iteration. You can see the fitness decreasing as solutions start to get better.

Output during a much later iteration. At this point, the actual output is a much better match to the desired output.

Common Issues and Suggested Modifications

Waitbar does not close

Type into the command window:

delete(f)

This will delete the waitbar.


work in progress