Home > manopt > tools > stopifdeletedfile.m

stopifdeletedfile

PURPOSE ^

Create an interactive stopping criterion based on the existence of a file

SYNOPSIS ^

function stopfun = stopifdeletedfile(filename)

DESCRIPTION ^

 Create an interactive stopping criterion based on the existence of a file

 function stopfun = stopifdeletedfile()
 function stopfun = stopifdeletedfile(filename)

 Use on the options structure passed to a Manopt solver, e.g.:

   problem = ... % manopt problem structure with manifold, cost function, ...
   options.stopfun = stopifdeletedfile(); % add this option
   trustregions(problem, x0, options); % run this or any other solver

 This will create a temporary file called MANOPT_DELETE_ME_TO_STOP_SOLVER
 in the present working directory. If this file is deleted 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
 stopping criteria. A different file name can also be specified using the
 input string filename (optional).

 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 stopifclosedfigure

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function stopfun = stopifdeletedfile(filename)
0002 % Create an interactive stopping criterion based on the existence of a file
0003 %
0004 % function stopfun = stopifdeletedfile()
0005 % function stopfun = stopifdeletedfile(filename)
0006 %
0007 % Use on the options structure passed to a Manopt solver, e.g.:
0008 %
0009 %   problem = ... % manopt problem structure with manifold, cost function, ...
0010 %   options.stopfun = stopifdeletedfile(); % add this option
0011 %   trustregions(problem, x0, options); % run this or any other solver
0012 %
0013 % This will create a temporary file called MANOPT_DELETE_ME_TO_STOP_SOLVER
0014 % in the present working directory. If this file is deleted at any time
0015 % during the solver's execution, the solver will terminate gracefully and
0016 % return its current iterate as soon as it gets to the point of evaluating
0017 % stopping criteria. A different file name can also be specified using the
0018 % input string filename (optional).
0019 %
0020 % Note: certain solvers (including trustregions) check stopping criteria
0021 % only at outer iterations, not during inner iterations; hence, their may
0022 % be a delay before actual termination.
0023 %
0024 % See also: statsfunhelper stopifclosedfigure
0025 
0026 % This file is part of Manopt: www.manopt.org.
0027 % Original author: Nicolas Boumal, Aug. 2, 2018.
0028 % Contributors:
0029 % Change log:
0030 
0031     % Default name for the temporary file.
0032     if ~exist('filename', 'var') || isempty(filename)
0033         filename = 'MANOPT_DELETE_ME_TO_STOP_SOLVER';
0034     end
0035 
0036     % Make sure the file exists, and release our handle on it.
0037     fid = fopen(filename, 'a');
0038     if fid >= 0
0039         fclose(fid);    
0040         % The stopping criterion is a function handle.
0041         stopfun = @checkcriterion;
0042     else
0043         warning('manopt:stopifdeletedfile', ...
0044               'Couldn''t create the file: no stopping criterion created.');
0045         stopfun = @(problem, x, info, last) false;
0046     end
0047 
0048 
0049     % The function is defined as a subfunction so that it has access to
0050     % filename without the need for an @() construct. This makes it easier
0051     % for Matlab to determine the number of output arguments of the
0052     % function handle @checkcriterion, which ultimately helps
0053     % stoppingcriterion determine how to call it.
0054     function [stop, reason] = checkcriterion(problem, x, info, last) %#ok<INUSD>
0055 
0056         reason = sprintf(['Interactive stopping criterion ' ...
0057                      '(file %s deleted). See options.stopfun.'], filename);
0058 
0059         % Try to access the file.
0060         fid = fopen(filename, 'r');
0061 
0062         % If we can't, it means the file no longer exists: stop the solver.
0063         % Otherwise, release our handle on the file to make sure it can be
0064         % deleted by another program.
0065         if fid < 0
0066             stop = true;
0067         else
0068             fclose(fid);
0069             stop = false;
0070         end
0071 
0072     end
0073     
0074 end

Generated on Fri 30-Sep-2022 13:18:25 by m2html © 2005