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