Solved

Interact with Zemax while optimization is running - Matlab/Python

  • 25 October 2022
  • 1 reply
  • 122 views

Hey,

 

im trying to interact with OpticStudio via Matlab/Python while a global Optimization is running. All examples I have seen the optimization is limited by time. I would like to build another way to end the optimization than time. Like: end the optimization when Operand xy reached a value higher than whatever. 

Unfortunately the only possible way is to read values from operands before and after the optimization. Is time the only exit criteria?

 

Thanks for help, Lukas

icon

Best answer by Ethan 15 December 2022, 22:23

View original

1 reply

Userlevel 3
Badge +2

Hi @Lukas.Hani,

Could you stop the Global Optimization based on the overall Merit Function value, just as you would by using the OpticStudio GUI? If certain operands are the most critical, you can make sure to weight them more than less critical operands. In this way, the Merit Function value is more dependent on those operands. Here’s an example of how you might run the optimization and do a check for a certain threshold. The RunAndWaitWithTimeout function will return after a certain check interval (GlobalOptimTimeInSeconds), and this code will loop indefinitely until the threshold is reached or you manually stop it.

 

    % run global search

    GlobalOptimTimeInSeconds = 15;

    GlobalOpt = TheSystem.Tools.OpenGlobalOptimization();

    if ~isempty(GlobalOpt)

        GlobalOpt.Algorithm = ZOSAPI.Tools.Optimization.OptimizationAlgorithm.DampedLeastSquares;

        GlobalOpt.NumberOfCores = 8;

        fprintf('Global Optimization for %i seconds...\n', GlobalOptimTimeInSeconds);

        fprintf('Initial Merit Function %6.3%\n', GlobalOpt.InitialMeritFunction);

        GlobalOpt.NumberToSave = ZOSAPI.Tools.Optimization.OptimizationSaveCount.Save_10;

        while(GlobalOpt.CurrentMeritFunction(0) > threshold)

            GlobalOpt.RunAndWaitWithTimeout(GlobalOptimTimeInSeconds);

        end

        for j =1:10

            fprintf('%i: %6.3f\n', j, GlobalOpt.CurrentMeritFunction(j));

        end

        GlobalOpt.Cancel();

        GlobalOpt.WaitForCompletion();

        GlobalOpt.Close();

        end

    end

 

Let me know if this isn’t what you are looking for.

Best,
Ethan

Reply