This page describes how to optimize a design using the graphical user interface. Optimizing the design is done using an advanced optimization algorithm, which requires running a large number of simulations. This can be much more efficient than running a parameter sweep, particularly if there is more than one parameter to optimize. For details in the optimization algorithm used, please see Particle Swarm Optimization. We will demonstrate how to use the optimization feature with a simple example. We will find the optimum thickness of an anti-reflection (AR) layer on silicon. The optimum thickness is the thickness that gives the minimum reflection at the wavelength of operation, in this case 500 nm.

## Optimization properties

- NAME: Parameter sweep name.

### Setup tab

**Optimization and configuration**

- ALGORITHM - PARTICLE SWARM: Use the Particle Swarm optimization method.
- ALGORITHM - USER DEFINED: Create your own User Defined optimization method. See examples and instructions in the associated files.
- TYPE: Specify if the result should be minimized or maximized.
- MAXIMUM GENERATIONS: The maximum number of generations to run. The total number of simulations to run is the product of Max generations and Generation size.
- GENERATION SIZE: The number of simulations per generation.
- RESET RANDOM GENERATOR: Enabling this option will reset the random number generator with a fixed seed. This ensures the same set of random numbers are used for each run of the optimization, which leads to consistent results between runs.
- TOLERANCE: Set a tolerance to stop the optimization early (ie. run less than the maximum number of generations. The tolerance algorithm is:
- if =0: Always run the specified number of generations.
- if >0: Always run the first 10% of generations. After that, stop the optimization if

$$\frac{\text{best_Result(lastGen)\ -\ best_Result(Last 10% of generations)}}{\text{best_Result(lastGen)}}\le \text{Tolerance}$$

Basically, this stops the optimization when the best_Result stops changing.

**Parameters**

- PARAMETER: Property to optimize.
- TYPE: Used to specify the type of units for the selected property (eg. length, time).
- MIN/MAX: The min/max value of the range of interest.
- UNIT: The unit of the parameter.
- ADD: Add another line to the parameters table.
- REMOVE: Remove a line from the parameters table.

**Figure of merit**

Multiple results can be collected, but one must be selected as the quantity to optimize. This result must be a single scalar number. Other results can be matrices.

- OPTIMIZE: Select a specific result to optimize (i.e. minimize or maximize this value). The selected result must be a single scalar value.
- NAME: A user specified name for the result. Will appear in the results dataset.
- RESULT: Result to collect.
- ADD: Add another line to the Figure of merit table.
- REMOVE: Remove a line from the Figure of merit table.
- SELECT TO OPTIMIZE: Make selected result the one to optimize.

### Advanced tab

**User defined algorithm tab**

- FIRST GENERATION SCRIPT: The First generation script can be used to define the initial parameter values for the initial generation of simulations. This script is not required if you want random starting values.
- NEXT GENERATION SCRIPT: This script is used to calculate the parameter values for the next generation, based on results from previous generations.
- TEST: Test the First and Next generation scripts without running any simulations.
- SCRIPT OUTPUT: Output messages from the scripts.

**Figure of merit script**

This tab allows users to calculate a custom figure of merit (FOM), rather than using a value obtained directly from a monitor or analysis object.

- USE FIGURE OF MERIT SCRIPT: Select this option to enable this feature.
- FIGURE OF MERIT SCRIPT: This script allows results selected in the 'Figure of merit' section of the SETUP tab to be post-processed into a final FOM that will be optimized. The script input arguments are the results selected in the 'Figure of merit' section of the SETUP tab. The script must calculate a variable named 'fom', which will be used as the FOM to optimize. This variable must be a single, scalar number.

For more advanced examples using user-defined algorithms, see the following attached files:

- optimization_shrinking_box.fsp (advanced User Defined 'shrinking box' algorithm)
- optimization_steepest_descent.fsp (advanced User Defined steepest descent algorithm)

Note: The figure of merit script is often difficult to debug. The debug_output flag can used to output a text file for this purpose - see example. It may be easier to use the model analysis group in cases where your FOM does not require nested sweeps, since this approach allows you to test your script directly. Where subsweeps are required you can continue to use the fom utility. In this example we look at unpolarized transmission and reflection, and require two polarizations. One subtlety with the optimization utility is that it will package the results with an extra dimension, even if no nested sweeps are defined. One can simply pinch the final dimension in the figure of merit script. To remove this singleton dimension, you can simply call pinch - Script command on the additional dimension.

## Creating the optimization project

1. Make sure you can see the Optimization and Sweeps window. This can be done by going to the menu View->Windows->Optimization and Sweeps, or by right clicking on the upper menu bar and making sure that Optimization and sweeps is selected.

2. Click the Add optimization button to create a new optimization task. Click the edit button to open the Edit optimization dialog window.

Note: Optimizations with nested sweeps

It may be necessary to use a nested parameter sweep within an optimization task. For example, to optimize a design for unpolarized light, it is necessary to sweep over both polarizations at each point in the optimization task. For an example, see the Nested sweeps page.

3. Once the optimizer object is open, add a parameter and browse the parameter pulldown menu. Select the "thickness" property of the AR structure.

4.Next, choose the Type to be length and set the min and max values to 10 nm and 150 nm respectively. The final result should look like the screenshot here.

5. Add a figure of merit. Select the power transmission 'T' from the Reflection monitor (R), as shown here.

6. Go to the Setup tab and choose the Particle Swarm algorithm. Choose to Minimize the figure of merit. Set the Maximum generations to 20 and the Generation size to 10 with a Tolerance of 0. The final settings should look like the screenshot here.

Click OK to accept all settings, and go back to the main Optimizations and Sweeps window.

## Running the optimization project

To run the optimization, right click on the new optimization object and choose Run, or simply click on the run button in the Optimizations and Sweeps window. The "Resume" option will restart the optimization from the last complete generation. For example if an incident stopped the optimization before it ended, this feature will allow to restart it from where it stopped instead of restarting the optimization from the start. Another case can be when the optimization ended normally after reaching the maximum number of generation, however the results haven't converged yet. At this scenario, the user can increase the number of generation and resume the optimization.

To distribute the optimization between several local computers, you must configure the computation Resources before running the optimization. Without additional configuration, all simulations will run on the local computer.

## Viewing the results

When the optimization runs, it automatically updates the following window which shows a plot of the best figure of merit as a function of generation. It also displays the best solution found with the corresponding parameters. Press "Stop Optimization" button to end the task early, which will keep all of the results calculated up to that point.

At the end of the task, you can zoom in on the figure of merit. We see that a reflection of less than 0.2% was achieved within 4 generations and that the optimum AR layer thickness is approximately 60nm.

Once the optimization has been run, you can view the results by right clicking on the optimization project. The best solution (optimized) project file will be saved as filename_optimization-parameter_optimization_best_solution.fileformat in the current working directory. Users can also right click on the optimization and choose "Apply best solutions" to apply the best solution to the current working file.

## Particle Swarm Optimization

Particle Swarm Optimization (PSO) is a population based stochastic optimization technique, inspired by the social behavior of flocks of birds or schools of fish [1,2], and has widely been used for various kinds of design optimization problems [2] including nanophotonic design [3-7]. In PSO, the potential solutions, called particles, are initialized at random positions, and then move within the parameter search space. The particles are subject to three forces as they move:

- Spring force towards the personal best position,
**p**, ever achieved by that individual particle - Spring force towards the global best position,
**g**, ever achieved by any particle. - A frictional force, proportional to the velocity.

The algorithm then follows these steps

- Set the number of particles N and initialize the positions
**x** - Evaluate figures of merit (FOM) and find
**p**and**g** - Calculate the new velocities
**v**for each particle based on the forces applied to the particle

\begin{equation}

\boldsymbol{v}_{\mathrm{t}}=\boldsymbol{v}_{\mathrm{t}-1}+\mathrm{c}_{1} \eta_{1}\left(\boldsymbol{p}_{\mathrm{t}-1}-\boldsymbol{x}_{\mathrm{t}-1}\right)+\mathrm{c}_{2} \eta_{2}\left(\boldsymbol{g}_{\mathrm{t}-1}-\boldsymbol{x}_{\mathrm{t}-1}\right)+(\omega-1) \boldsymbol{v}_{\mathrm{t}-1}\hspace{30pt}(1)

\end{equation}

- Update the positions
**x**of each particle based on the velocity

\begin{equation}

\boldsymbol{x}_{\mathrm{t}}=\boldsymbol{x}_{\mathrm{t}-1}+\boldsymbol{v}_{\mathrm{t}}\hspace{30pt}(2)

\end{equation}

- Repeat from step 2 until convergence is achieved

In Eq. (1), t is the iteration counter; c1 and c2 are the cognitive and social factors, respectively; ω is called the inertial weight; and η1 and η2 are random number between 0 and 1. Lumerical's PSO implementation uses default values of c1, c2 and ω that have shown to converge well in many test optimization problems for photonic design problems. A detailed description of the algorithm and the difference coefficients can be found in Refs. [1] or [2].

### Related publications

*J. Robinson and Y. Rahmat-Samii, "Particle swarm optimization in Electromagnetics," IEEE Trans. Antennas and Propagat.***52**, pp.397 - 407 (2004).*K. E. Parsopoulo and M N. Vrahatis, Particle swarm optimization and intelligence : advances and applications, Information Science Reference, 2010.**J. Pond and M. Kawano, “Virtual prototyping and optimization of novel solar cell designs”, Proc. SPIE 7750, 775028 (2010), DOI:10.1117/12.873114**M. Kawano and J. Pond, "Design Optimization of Photonic Crystal Organic Solar Cells using the FDTD method in Combination with Particle Swarm Optimization," 7th International Conference on Optics-photonics Design & Fabrication, Yokohama, Japan, 19S1-14, 2010.**J. G. Mutitu, S. Shi, C. Chen, T. Creazzo, A. Barnett, C. Honsberg and D. W. Prather, "Thin film silicon solar cell design based on photonic crystal and diffractive grating structures", Opt. Express 16, 5238, 2008**M.Shokooh-Saremi and R. Magnusson, "Leaky-mode resonant reflectors with extreme bandwidths," Opt. Lett.***35**, 1121, 2010.*R. Magnusson, M. Shokooh-Saremi,and E. G. Johnson, “Guided-mode resonant wave plates,” Opt. Lett.***35**, 2472, 2010.

### See also

Optimization scripting commands, Computation Resources, Run a parameter sweep, MATLAB driven optimization via Interoperability API