Skip to main content
Solved

Interact with Zemax while optimization is running - Matlab/Python

  • October 25, 2022
  • 1 reply
  • 171 views

Lukas.Hani

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

Best answer by Ethan

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

View original
Did this topic help you find an answer to your question?

1 reply

Ethan
En-Lightened
Forum|alt.badge.img+2
  • En-Lightened
  • 107 replies
  • Answer
  • December 15, 2022

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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings