Hi @Ai Ping.Yow!
LocalOpt.Cycles
is not the number of cycles taken for optimization, it is the setting for the number of cycles. You can see this in line 5 of your example code:
LocalOpt.Cycles = ZOSAPI.Tools.Optimization.OptimizationCycles.Automatic
I suspect you are using Python 3.8 or older, together with Python.NET 2.5.2; in that case it makes sense that your code prints a 0. ZOSAPI.Tools.Optimization.OptimizationCycles.Automatic
is the first element of the ZOSAPI.Tools.Optimization.OptimizationCycles
enum, so it has index 0; printing an enum value while using Python.NET 2.5.2 will print its index.
Looking at the documentation, I unfortunately don't see a way to get the number of cycles used for optimization.
Thank you chaasjes! Yep, I am using Python 3.7.9.
I also looked through the documentation and wasn’t able to find a way. In that case, I would just use the elapsed time instead for my work.
There is no way to query the current or total cycles for an optimization, but you can come close with modifying your logic.
Rather than calling the blocking (sync) method of RunAndWaitForCompletion
, you can call Run and then put the LocalOpt.IsRunning
field as the condition of a while loop. Before the optimization, query & store the local merit function value. Then inside the while loop, constantly check if the MF has changed...if the value has changed, a new cycle has occurred.
If your system optimizes extremely fast, then you’re likely to miss a few cycles, but if you only have a few hundred cycles per second, you should be able to capture the number of cycles.
A proxy example of how to do this is in PythonStandalone_02_NSC_ray_trace.py around line 167.
Thank you @MichaelH ! It works based on your suggestion
MFvalue = F]
print('Initial Merit Function:', LocalOpt.InitialMeritFunction)
initMF = LocalOpt.InitialMeritFunction
MFvalue.append(initMF)
t = time.time()
LocalOpt.Run()
while LocalOpt.IsRunning:
currentValue = LocalOpt.CurrentMeritFunction
if MFvaluef-1] != currentValue:
print(f'Changed MF: {currentValue}')
MFvalue.append(currentValue)
LocalOpt.WaitForCompletion()
print('Final Merit Function:', LocalOpt.CurrentMeritFunction)
finalMF = LocalOpt.InitialMeritFunction
LocalOpt.Close()
elapsed = time.time() - t # Get the elapsed time
print('Time elapsed: ' + str(round(elapsed,3)) + 's')
print(f'Cycles: {len(MFvalue)}')