Particle Swarm Optimization in RCWA
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 variablesp_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.
