comparison m-toolbox/classes/@smodel/addParameters.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % ADDPARAMETERS Add some parameters to the symbolic model (smodel) object
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: ADDPARAMETERS Add some parameters to the symbolic model
5 % (smodel) object. It is possible to set only the parameter
6 % name or to set the parameter names with their values.
7 % Exist the parameter-name in the object then replace this
8 % method the existing value.
9 %
10 % CALL: obj = obj.addParameters(key-value pairs);
11 % obj = obj.addParameters(plist);
12 % obj = obj.addParameters(two cell-arrays);
13 % obj = obj.addParameters(single cell-array of names);
14 %
15 % INPUTS: obj - a ltpda smodel object.
16 % key-value - A single key-value pair or a list of key-value
17 % pairs. Thereby it is important that the key is
18 % followed by the value.
19 % plist - Parameter list with values for the keys
20 % 'params' and 'values'. The values can be a
21 % single value or a cell array with multiple
22 % values.
23 % cell-arrays - Two cell-arrays with the first of the 'params'
24 % and the second with the 'values'.
25 %
26 % REMARK: This function will replace the parameter value if the
27 % parameter name already exist
28 %
29 % <a href="matlab:utils.helper.displayMethodInfo('smodel', 'addParameters')">Parameters Description</a>
30 %
31 % VERSION: $Id: addParameters.m,v 1.1 2011/04/17 15:48:24 ingo Exp $
32 %
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34
35 function varargout = addParameters(varargin)
36
37 % Check if this is a call from a class method
38 callerIsMethod = utils.helper.callerIsMethod;
39
40 if callerIsMethod
41 sm = varargin{1}; % smodel-object(s)
42 names = varargin{2}; % cell-array with parameter-names
43 values = varargin{3}; % call-array with parameter-values
44
45 else
46 % Check if this is a call for parameters
47 if utils.helper.isinfocall(varargin{:})
48 varargout{1} = getInfo(varargin{3});
49 return
50 end
51
52 import utils.const.*
53 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
54
55 % Collect input variable names
56 in_names = cell(size(varargin));
57 for ii = 1:nargin,in_names{ii} = inputname(ii);end
58
59 % Collect all smodel objects
60 [sm, sm_invars, rest] = utils.helper.collect_objects(varargin(:), 'smodel', in_names);
61 [pls, dummy, rest] = utils.helper.collect_objects(rest(:), 'plist');
62
63 names = {};
64 values = {};
65 %%% If the input PLIST have the keys 'params' and 'values' then use also
66 %%% the
67 if length(pls) == 1 && isa(pls, 'plist') && isparam(pls, 'params') && isparam(pls, 'values')
68 names = find(pls, 'params');
69 values = find(pls, 'values');
70 % Make sure that the param names and the values are cell-arrays
71 names = cellstr(names);
72 if ~iscell(values)
73 values = num2cell(values);
74 end
75 end
76
77 % Get the parameter names and the values from the input
78 nRest = numel(rest);
79 if nRest == 1 && iscell(rest{1})
80 % addParameters({'a', 'b'})
81 names = [names rest{1}];
82 elseif ~isempty(rest) && iscellstr(rest)
83 % addParameters('a', 'b', 'c')
84 names = [names rest];
85 elseif nRest == 2 && iscell(rest{1}) && isnumeric(rest{2})
86 % addParameters({'a', 'b'}, [1 2])
87 names = [names rest{1}];
88 values = [values num2cell(rest{2})];
89 elseif nRest == 2 && iscell(rest{1}) && iscell(rest{2})
90 % addParameters({'a', 'b'}, {1 2})
91 names = [names rest{1}];
92 values = [values rest{2}];
93 else
94 % addParameters('a', 1, 'b', 2, 'c', 3)
95 names = [names rest(1:2:nRest)];
96 values = [values rest(2:2:nRest)];
97 end
98
99 % Combine input plists and default PLIST
100 pls = combine(pls, getDefaultPlist());
101
102 end
103
104 % Check that we have the same number of param names as the number of values.
105 if ~isempty(values) && numel(names) ~= numel(values)
106 error('### Please specify for each parameter name [%d] one parameter value [%d]', numel(names), numel(values));
107 end
108
109 % Decide on a deep copy or a modify
110 sm = copy(sm, nargout);
111
112 % Loop over smodel objects
113 for oo=1:numel(sm)
114
115 % If the parameter values are not empty but the 'values' of the object
116 % then create default values for the object 'values'.
117 if ~isempty(values) && isempty(sm(oo).values)
118 sm(oo).values = cell(size(sm(oo).params));
119 end
120
121 for kk=1:numel(names)
122 % Look for the index we have to change
123 idx = strcmp(sm(oo).params, names{kk});
124
125 if any(idx)
126 % Change at the index the value
127 if ~isempty(values)
128 sm(oo).values{idx} = values{kk};
129 end
130 else
131 % Append the key-value pair
132 sm(oo).params = [sm(oo).params names(kk)];
133 if isempty(values) && ~isempty(sm(oo).values)
134 % Append a default value for the parameter names if there are
135 % already parameter values
136 sm(oo).values = [sm(oo).values {[]}];
137 elseif ~isempty(values)
138 sm(oo).values = [sm(oo).values values(kk)];
139 end
140 end
141 end
142
143 if ~callerIsMethod
144 plh = pls.pset('params', names);
145 plh.pset('values', values);
146 sm(oo).addHistory(getInfo('None'), plh, sm_invars(oo), sm(oo).hist);
147 end
148 end
149
150 % Set output
151 varargout = utils.helper.setoutputs(nargout, sm);
152 end
153
154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155 %
156 % FUNCTION: getInfo
157 %
158 % DESCRIPTION: Get Info Object
159 %
160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161
162 function ii = getInfo(varargin)
163 if nargin == 1 && strcmpi(varargin{1}, 'None')
164 sets = {};
165 pl = [];
166 else
167 sets = {'Default'};
168 pl = getDefaultPlist;
169 end
170 % Build info object
171 ii = minfo(mfilename, mfilename('class'), 'ltpda', utils.const.categories.helper, '$Id: addParameters.m,v 1.1 2011/04/17 15:48:24 ingo Exp $', sets, pl);
172 end
173
174 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175 %
176 % FUNCTION: getDefaultPlist
177 %
178 % DESCRIPTION: Get Default Plist
179 %
180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181
182 function plout = getDefaultPlist()
183 persistent pl;
184 if exist('pl', 'var')==0 || isempty(pl)
185 pl = buildplist();
186 end
187 plout = pl;
188 end
189
190 function pl = buildplist()
191 pl = plist();
192
193 % parameter names
194 p = param({'params', 'A cell-array with the parameter names.'}, paramValue.EMPTY_CELL);
195 pl.append(p);
196
197 % parameter values
198 p = param({'values', 'A cell-array with the parameter values.'}, paramValue.EMPTY_CELL);
199 pl.append(p);
200 end
201