This page describes how to generate and run an optimization analysis using script commands. The optimization of 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. The same example used in the section "Optimization" will be re-generated in this page. Using script commands to generate the optimization is a convenient way when users already have the parameters at their hands. For additional information and detailed implementation of the script commands, please see the "Measurement and optimization data" section. Please note that, all the script commands used in this example could also be applied to sweep and yield analysis.

To generate and run the optimization analysis using script commands, user can open the sweep_AR_coating_example_script.fsp file and follow the three steps listed below; or, open and run the script file optimization_AR_coating_example_script.lsf.

**In this article:**

## Creating the optimization analysis project

The following commands are used to generate and superficially define a new optimization named "thickness_optimization_script".

# add a new optimization and set basic properties

addsweep(1);

setsweep("optimization", "name", "thickness_optimization_script");

setsweep("thickness_optimization_script", "Type", "Minimize");

setsweep("thickness_optimization_script", "algorithm", "Particle Swarm");

setsweep("thickness_optimization_script", "maximum generations", 20);

setsweep("thickness_optimization_script", "generation size", 10);

setsweep("thickness_optimization_script", "tolerance", 0);

After the optimization is superficially generated, the parameters can then be defined and added to it. The following commands define the path, type, optimization window and unit of the parameter. And then add this parameter to the optimization "thickness_optimization_script".

# define the parameter thickness

para = struct;

para.Parameter = "::model::AR structure::thickness";

para.Type = "Length";

para.Min = 0.05e-6;

para.Max = 0.15e-6;

para.Units = "microns";

# add the parameter thickness to the optimization

addsweepparameter("thickness_optimization_script", para);

Then this optimization analysis will appear in the "Optimizations and Sweeps" tab as shown below.

The next step is to add the results that we want to optimize into the optimization. The commands listed below define and add the results "R" and "T" to the optimization object as shown in the optimization editing window below.

# define figure of merit

result_1 = struct;

result_1.Name = "R";

result_1.Result = "::model::R::T";

result_1.Optimize = true;

result_2 = struct;

result_2.Name = "T";

result_2.Result = "::model::T::T";

result_2.Optimize = false;

# add the figure of merits R & T to the optimization

addsweepresult("thickness_optimization_script", result_1);

addsweepresult("thickness_optimization_script", result_2);

Please note that, only the result R (reflection) is optimized to be minimum in this example. The optimization "thickness_optimization_script" is now completely defined as shown below.

## Running the optimization analysis

When the optimization runs, it automatically updates the Optimization status 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. The following command runs the optimization "thickness_optimization_script" and loads the results to it. After the optimization has been successfully run, the optimization process and the optimized resolution will be shown in the "Optimization Status" window as indicated below.

# run optimization

runsweep("thickness_optimization_script");

Close the "Optimization Status" window and user can find there are several results stored in the optimization as indicated below.

## Viewing the results

The optimization results could also be plotted by script commands. The commands below retrieve and plot the "parameter trend" result as an example.

# get & view the results - parameter value

R = getsweepresult("thickness_optimization_script", "parameter trend");

value = R.getattribute("parameter value");

gen = R.getparameter("generation");

plot(gen, value / 1e-9, "generation", "parameter value (nm)", "thickness");

And the reflection optimization trend is plotted as in the figure shown below. We can see that the optimization result and the sweep result from the previous section show great agreement with each other and the optimized thickness to get a minimum reflection is around 60 nm.