Skip to main content

I want to start and optimization over the ZOS-API and output some status values (more than the merrit function itself) during the run. It seams that the variable variables are not updated during the optimization but only when the optimizsation ends or is canceled. It seams there is no API interface like in the GUI (“Auto Update”) for the optimizers and even in the GUI this button seams to be more gui related than belonging to a file (at least it is not saved within the file).

Amnybody has an Idea how to solve this ? Any chances for a feature update in Zemax ?

Hi @Michael Baus,

 

Its not clear to me what you want to do, but you can choose the number of Cylces the optimizer does. You could have a loop that runs the optimizer multiple time for a single cycle, and output your status values every time. It might be substantially slower though.

If you clarify what those status values should be, there might be a more straightforward solution.

Take care,

 

David


Thanks for this fast reply. Basically this is what I wanted to achieve.

However I think this might not work. I tested this and did not give any improvement for me (DLS). I guessed it was the dynamic factors which get kind of reseted each time I restart the optimization. Can you confirm this or should I try to re-do this experiment to verify (and maybe debug) my last results ?


Hi @Michael Baus,

 

Unless I don’t understand what you are trying to do, it seems to work for me. Just to clarify, here is what I did:

  1. Open the Double Gauss 28 degree field.zos sample file (located in your {Documents}\Zemax\Samples\Sequential\Objectives)
  2. Change Thickness of Surface 11 to 10.0 mm
  3. Create a Default Merit Function for Spot, 5 Rings, 6 Arms
  4. Run the following Python snippet in an Interactive Extension

This is the Python snippet:

print("Initial Thickness 11: " + str(TheSystem.LDE.GetSurfaceAt(11).Thickness))

optimizer = TheSystem.Tools.OpenLocalOptimization()

optimizer.Cycles = ZOSAPI.Tools.Optimization.OptimizationCycles.Fixed_1_Cycle

for ii in range(5):
optimizer.RunAndWaitForCompletion()

print("Thickness 11 after " + str(ii+1) + " cycles: " + str(TheSystem.LDE.GetSurfaceAt(11).Thickness))

optimizer.Close()

The result is as follows:

Initial Thickness 11: 10.0
Thickness 11 after 1 cycles: 22.66618965704847
Thickness 11 after 2 cycles: 23.515186898413912
Thickness 11 after 3 cycles: 26.406466800926346
Thickness 11 after 4 cycles: 28.248605578148826
Thickness 11 after 5 cycles: 29.572361986256745

Let me know if I’m missing something. Again, I’d like to reiterate that if you tell us what you’ll want to calculate for each iteration, there might be a better way to do this.

Take care,


David


Hey David,

This is only a partial solution.  Running 1 optimization with 10 cycles could produce a different result than running 10 optimizations with 1 cycle:

Feature Request: Plot of Merit Function Value During Optimization | Zemax Community

Zemax needs to modify both the ZOS-API and how optimization is handled in OpticStudio for us to have a robust solution. 

If you want to monitor performance during an optimization, you can use the Run command (rather than RunAndWaitForCompletion).  This will start a tool in async mode so the rest of your code continues and you can monitor the values as needed (you cannot, or rather should not, change the system with a tool open):

# once you open a tool, there is a "snapshot" of TheSystem that is operated on
tool = zos.TheSystem.Tools.OpenLocalOptimization()
tool.Cycles = zos.ZOSAPI.Tools.Optimization.OptimizationCycles.Infinite;

# run for 10 seconds and output the variables
print('Surf 1 radius : %6.3f' % zos.TheSystem.LDE.GetSurfaceAt(1).Radius);
print('will start tool in async (non-blocking) mode')

# run optimization in async mode
tool.Run()
loop = 0;

# run optimization for 5 seconds
while (loop < 250):
print('Surf 1 radius : %6.3f (%.2f)' % (zos.TheSystem.LDE.GetSurfaceAt(1).Radius, loop * 0.01));
time.sleep(0.01);
loop = loop + 1;

# always close your tool
tool.Close();

Note that any output to the screen or querying data will most likely slow down your code vs using the native GUI.  I get an optimized system in 1.22s with the GUI and 1.51s with the Python code.


Thanks for pointing this out @MichaelH, it makes a lot of sense.

Take care,

 

David


Exactly this might be my problem. I have a quite complex system and this single cycle did not improve the merrit function the same way as multi cycle optimization. It is nearly stuck after the first 1 or 2 steps. Thanks MichaelH for helping !

Just monitoring the merrit value would work with the workaround of polling the “.Status()” of the optimization which gives from time to time the current merrit value. However I want to check the change of the variable parameters during the optimization and they are not updated in the “snapshot” where the optimization runs.

 

Beside a clean fix / feature improve, I could live for the moment if at least the same button as in the GUI (“Auto Update”)  would be implemented in the API. It seams that this updates in the GUI at least every 5s the internal data (LensData & MultiConfigurations) which I could then poll.

BR,

MichaelB


Any news from the Ansys Team how / when this topic could be solved ?


Hi Michael,

Please may I take a different direction, and ask why you want this data? What will you do with it if you could get it?

  • Mark

Dear Mark,

 

I have a team working on optimization of complex optical system (>>10 runs with >>12h per optimization run, >>10k merrits, >50 variables) and I asked them about the optimization flow. In the beginning I could not believe that Zemax is not outputting any usable information about the optimization flow with traceable parameters. So we decided to try to get our own interface to Zemax providing this information (using Matlab and ZOS API, see some example outputs below as 1] and 2]). This allowed us to understand which parameters are critical in the optimization and where we could maybe tune manually to improve runtime/performance. In general it works now but due to the fact that we have to cancel the optimization after each step the results are significantly different to those without this additional feature (and it takes even much longer).

BR,

Michael

>1] Example of merrit function over optimization step and time

 

p2] example of paramters over optimization steps

 


Reply