Commit 9428007b authored by Stéphane Adjemian's avatar Stéphane Adjemian
Browse files

Added simpsa routines (new optimization algorithm)

parent b3b2cdfe
This diff is collapsed.
function o = SIMPSAGET(options,name,default,flag)
%SIMPSAGET Get SIMPSA OPTIONS parameters.
% VAL = SIMPSAGET(OPTIONS,'NAME') extracts the value of the named parameter
% from optimization options structure OPTIONS, returning an empty matrix if
% the parameter value is not specified in OPTIONS. It is sufficient to
% type only the leading characters that uniquely identify the
% parameter. Case is ignored for parameter names. [] is a valid OPTIONS
% argument.
%
% VAL = SIMPSAGET(OPTIONS,'NAME',DEFAULT) extracts the named parameter as
% above, but returns DEFAULT if the named parameter is not specified (is [])
% in OPTIONS. For example
%
% val = simpsaget(opts,'TolX',1e-4);
%
% returns val = 1e-4 if the TolX property is not specified in opts.
%
% See also SIMPSASET, SIMPSA
%
% Copyright (C) 2006 Brecht Donckels, BIOMATH, brecht.donckels@ugent.be
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
% USA.
% undocumented usage for fast access with no error checking
if (nargin == 4) && isequal(flag,'fast')
o = getknownfield(options,name,default);
return
end
if nargin < 2
error('MATLAB:odeget:NotEnoughInputs','Not enough input arguments.');
end
if nargin < 3
default = [];
end
if ~isempty(options) && ~isa(options,'struct')
error('MATLAB:odeget:Arg1NotODESETstruct',...
'First argument must be an options structure created with ODESET.');
end
if isempty(options)
o = default;
return;
end
Names = [
'TEMP_START '
'TEMP_END '
'COOL_RATE '
'INITIAL_ACCEPTANCE_RATIO '
'MIN_COOLING_FACTOR '
'MAX_ITER_TEMP_FIRST '
'MAX_ITER_TEMP_LAST '
'MAX_ITER_TEMP '
'MAX_ITER_TOTAL '
'MAX_TIME '
'MAX_FUN_EVALS '
'TOLX '
'TOLFUN '
'DISPLAY '
'OUTPUT_FCN '
];
names = lower(Names);
lowName = lower(name);
j = strmatch(lowName,names);
if isempty(j) % if no matches
error('MATLAB:odeget:InvalidPropName',...
['Unrecognized property name ''%s''. ' ...
'See ODESET for possibilities.'], name);
elseif length(j) > 1 % if more than one match
% Check for any exact matches (in case any names are subsets of others)
k = strmatch(lowName,names,'exact');
if length(k) == 1
j = k;
else
msg = sprintf('Ambiguous property name ''%s'' ', name);
msg = [msg '(' deblank(Names(j(1),:))];
for k = j(2:length(j))'
msg = [msg ', ' deblank(Names(k,:))];
end
msg = sprintf('%s).', msg);
error('MATLAB:odeget:AmbiguousPropName', msg);
end
end
if any(strcmp(fieldnames(options),deblank(Names(j,:))))
o = options.(deblank(Names(j,:)));
if isempty(o)
o = default;
end
else
o = default;
end
% --------------------------------------------------------------------------
function v = getknownfield(s, f, d)
%GETKNOWNFIELD Get field f from struct s, or else yield default d.
if isfield(s,f) % s could be empty.
v = subsref(s, struct('type','.','subs',f));
if isempty(v)
v = d;
end
else
v = d;
end
function options = simpsaset(varargin)
%SIMPSASET Create/alter simpsa optimization OPTIONS structure.
% OPTIONS = SIMPSASET('PARAM1',VALUE1,'PARAM2',VALUE2,...) creates an
% optimization options structure OPTIONS in which the named parameters have
% the specified values. Any unspecified parameters are set to [] (parameters
% with value [] indicate to use the default value for that parameter when
% OPTIONS is passed to the optimization function). It is sufficient to type
% only the leading characters that uniquely identify the parameter. Case is
% ignored for parameter names.
% NOTE: For values that are strings, the complete string is required.
%
% OPTIONS = SIMPSASET(OLDOPTS,'PARAM1',VALUE1,...) creates a copy of OLDOPTS
% with the named parameters altered with the specified values.
%
% OPTIONS = SIMPSASET(OLDOPTS,NEWOPTS) combines an existing options structure
% OLDOPTS with a new options structure NEWOPTS. Any parameters in NEWOPTS
% with non-empty values overwrite the corresponding old parameters in
% OLDOPTS.
%
% SIMPSASET with no input arguments and no output arguments displays all
% parameter names and their possible values, with defaults shown in {}
% when the default is the same for all functions that use that option -- use
% SIMPSASET(OPTIMFUNCTION) to see options for a specific function.
%
% OPTIONS = SIMPSASET (with no input arguments) creates an options structure
% OPTIONS where all the fields are set to [].
% Copyright (C) 2005 Henning Schmidt, FCC, henning@fcc.chalmers.se
% Copyright (C) 2006 Brecht Donckels, BIOMATH, brecht.donckels@ugent.be
% Copyright (C) 2013 Dynare Team.
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% Print out possible values of properties.
if (nargin == 0) && (nargout == 0)
fprintf(' TEMP_START: [ positive scalar ]\n');
fprintf(' TEMP_END: [ positive scalar ]\n');
fprintf(' COOL_RATE: [ positive scalar ]\n');
fprintf(' INITIAL_ACCEPTANCE_RATIO: [ positive scalar < 1 {0.95} ]\n');
fprintf(' MIN_COOLING_FACTOR: [ positive scalar < 1 {0.9}]\n');
fprintf(' MAX_ITER_TEMP_FIRST: [ positive scalar {100} ]\n');
fprintf(' MAX_ITER_TEMP_LAST: [ positive scalar {20} ]\n');
fprintf(' MAX_ITER_TOTAL: [ positive scalar {2500} ]\n');
fprintf(' MAX_TIME: [ positive scalar {2500} ]\n');
fprintf(' MAX_FUN_EVALS: [ positive scalar {2500} ]\n');
fprintf(' TOLX: [ positive scalar {1e-6} ]\n');
fprintf(' TOLFUN: [ positive scalar {1e-6} ]\n');
fprintf(' DISPLAY: [ ''iter'' or ''none'' {''iter''} ]\n');
fprintf(' OUTPUT_FCN: [ function_handle ]\n');
fprintf('\n');
return;
end
Names = [
'TEMP_START '
'TEMP_END '
'COOL_RATE '
'INITIAL_ACCEPTANCE_RATIO '
'MIN_COOLING_FACTOR '
'MAX_ITER_TEMP_FIRST '
'MAX_ITER_TEMP_LAST '
'MAX_ITER_TEMP '
'MAX_ITER_TOTAL '
'MAX_TIME '
'MAX_FUN_EVALS '
'TOLX '
'TOLFUN '
'DISPLAY '
'OUTPUT_FCN '
];
m = size(Names,1);
names = lower(Names);
% Combine all leading options structures o1, o2, ... in odeset(o1,o2,...).
options = [];
for j = 1:m
options.(deblank(Names(j,:))) = [];
end
i = 1;
while i <= nargin
arg = varargin{i};
if ischar(arg) % arg is an option name
break;
end
if ~isempty(arg) % [] is a valid options argument
if ~isa(arg,'struct')
error('MATLAB:odeset:NoPropNameOrStruct',...
['Expected argument %d to be a string property name ' ...
'or an options structure\ncreated with SIMANSET.'], i);
end
for j = 1:m
if any(strcmp(fieldnames(arg),deblank(Names(j,:))))
val = arg.(deblank(Names(j,:)));
else
val = [];
end
if ~isempty(val)
options.(deblank(Names(j,:))) = val;
end
end
end
i = i + 1;
end
% A finite state machine to parse name-value pairs.
if rem(nargin-i+1,2) ~= 0
error('MATLAB:odeset:ArgNameValueMismatch',...
'Arguments must occur in name-value pairs.');
end
expectval = 0; % start expecting a name, not a value
while i <= nargin
arg = varargin{i};
if ~expectval
if ~ischar(arg)
error('MATLAB:odeset:NoPropName',...
'Expected argument %d to be a string property name.', i);
end
lowArg = lower(arg);
j = strmatch(lowArg,names);
if isempty(j) % if no matches
error('MATLAB:odeset:InvalidPropName',...
'Unrecognized property name ''%s''.', arg);
elseif length(j) > 1 % if more than one match
% Check for any exact matches (in case any names are subsets of others)
k = strmatch(lowArg,names,'exact');
if length(k) == 1
j = k;
else
msg = sprintf('Ambiguous property name ''%s'' ', arg);
msg = [msg '(' deblank(Names(j(1),:))];
for k = j(2:length(j))'
msg = [msg ', ' deblank(Names(k,:))];
end
msg = sprintf('%s).', msg);
error('MATLAB:odeset:AmbiguousPropName', msg);
end
end
expectval = 1; % we expect a value next
else
options.(deblank(Names(j,:))) = arg;
expectval = 0;
end
i = i + 1;
end
if expectval
error('MATLAB:odeset:NoValueForProp',...
'Expected value for property ''%s''.', arg);
end
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment