Create an interactive stopping criterion based on a figure closing function stopfun = stopifclosedfigure() Use on the options structure passed to a Manopt solver, e.g.: problem = ... % manopt problem structure with manifold, cost function, ... options.stopfun = @stopifclosedfigure; % add this option trustregions(problem, x0, options); % run this or any other solver This will create a figure. If this figure is closed at any time during the solver's execution, the solver will terminate gracefully and return its current iterate as soon as it gets to the point of evaluating the stopping criteria. Note: certain solvers (including trustregions) check stopping criteria only at outer iterations, not during inner iterations; hence, their may be a delay before actual termination. See also: statsfunhelper stopifdeletedfile
0001 function [stop, reason] = stopifclosedfigure(problem, x, info, last) %#ok<INUSL> 0002 % Create an interactive stopping criterion based on a figure closing 0003 % 0004 % function stopfun = stopifclosedfigure() 0005 % 0006 % Use on the options structure passed to a Manopt solver, e.g.: 0007 % 0008 % problem = ... % manopt problem structure with manifold, cost function, ... 0009 % options.stopfun = @stopifclosedfigure; % add this option 0010 % trustregions(problem, x0, options); % run this or any other solver 0011 % 0012 % This will create a figure. If this figure is closed at any time during 0013 % the solver's execution, the solver will terminate gracefully and return 0014 % its current iterate as soon as it gets to the point of evaluating the 0015 % stopping criteria. 0016 % 0017 % Note: certain solvers (including trustregions) check stopping criteria 0018 % only at outer iterations, not during inner iterations; hence, their may 0019 % be a delay before actual termination. 0020 % 0021 % See also: statsfunhelper stopifdeletedfile 0022 0023 % This file is part of Manopt: www.manopt.org. 0024 % Original author: Nicolas Boumal, Aug. 3, 2018. 0025 % Contributors: 0026 % Change log: 0027 0028 reason = 'Interactive stopping criterion: figure closed.'; 0029 0030 % Fix a likely unique figure id. 0031 figureid = 1465489213; 0032 0033 % If first iteration, create a figure to capture interaction. 0034 if last == 1 0035 h = figure(figureid); 0036 set(h, 'Name', 'Close to stop Manopt solver', 'NumberTitle', 'off'); 0037 text(0, 0, 'Close me to stop the Manopt solver.', 'FontSize', 16); 0038 axis tight; 0039 axis off; 0040 set(h, 'color', 'w'); 0041 drawnow(); 0042 end 0043 0044 % Call to drawnow() ensures that, if the user closed the figure, then 0045 % that information will have been refreshed. This may create small 0046 % delays, but on the other hand interactive stopping criteria are 0047 % mostly useful for costly problems where this overhead should be 0048 % marginal. 0049 drawnow(); 0050 if ~ishandle(figureid) % If the figure was closed, stop. 0051 stop = true; 0052 else 0053 stop = false; 0054 end 0055 0056 end