Guilin Sun
Ansys Employee

Lumerical has a built-in optimization based on particle-swarm method, and if you have API license you can use either Matlab or Python optimization.

In this example https://optics.ansys.com/hc/en-us/articles/360041613174-Blazed-grating the blaze grating is script with theta, and period. You can modify it, or use your own math to modify the script to create the desired grating. Please try.