rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
grid_search.m
1 function [opt_data,model] = grid_search(model, varargin)
2 % function opt_data = detailed_grid_search(model, model_data);
3 %
4 % Function performing an optimization by grid search using the detailed
5 % simulations.
6 %
7 %Required fields of model:
8 %model.mu_names: names of the parameters to be optimized
9 %model.mu_ranges: value ranges for optimization
10 %model.optimization.init_params: marks which parameters should be optimized
11 %model.optimization.opt_params.grid_density: Density of the search grid. F.Ex. "3"
12 % means using 3 parameters per dimension
13 %model.optimitzation.params_to_optimize: Flag vector indicating which
14 % variables to opimize
15 %
16 %used variables in Function:
17 % P : cell-array where the used parameters are stored.
18 % P{1} = Vector of values for first parameter
19 % PM : Parameter matrix (each row defines a Parameter set for a
20 % detailed simulation)
21 %
22 %Generated fields of opt_data:
23 % output: sequence of outputvalues obtained during optimization
24 % parameter_sets: sequence of corresponding parameter sets to output sequence
25 % max_output: maximal output value
26 % max_output_paramsets: paramset for which the maximal parameter was
27 % obtained
28 %
29 % Markus Dihlmann 03.02.2010
30 
31 
32 
33 if strcmp(inputname(2),'model_data')
34  model_data = varargin{1};
35  detailed_data = [];
36  model.optimization.opt_mode = 'detailed';
37 elseif strcmp(inputname(2),'reduced_data')
38  if nargin >1
39  reduced_data = varargin{1};
40  model_data = varargin{2};
41  detailed_data = varargin{3};
42  model.optimization.opt_mode = 'reduced';
43  else
44  reduced_data = varargin{1};
45  model.optimization.opt_mode = 'reduced';
46  end
47 end
48 
49 
50 %Check required fields:
51 if isempty(model_data)
52  model_data = gen_model_data(model);
53 end;
54 
55 if isempty(model.mu_names)
56  error('No parameter names specified!');
57 end;
58 
59 %Total number of parameters defined in model
60 nbrParams = length(model.mu_names);
61 
62 %Continue check required fields
63 if (isempty(model.mu_ranges)||nbrParams~=length(model.mu_ranges))
64  error('not the right number of parameter ranges given!');
65 end;
66 
67 %if isempty(model.opt_method)
68 % error('No optimization method defined!');
69 %end;
70 
71 if (isempty(model.optimization.params_to_optimize)||nbrParams~=length(model.optimization.params_to_optimize))
72  error('It is not properly defined which parameters are set for optimization and which not!');
73 end;
74 
75 
76 % If no initial values are given, set them in the middle of the ranges
77 if isempty(model.optimization.init_params)
78  for i=1:nbrParams
79  model.optimization.init_params(i) = (model.mu_ranges{i}(1)+model.mu_ranges{i}(2))/2;
80  end;
81 end;
82 
83 %Creating search grid
84 if isempty(model.optimization.opt_params.grid_density)
85  model.optimization.opt_params.grid_density =3; %defautl value
86 end
87 
88 nbrParams = length(model.mu_names);
89 nbrOptparams=0; %number of paramters to be optimized
90 
91 if(size(model.optimization.opt_params.grid_density)==1)
92  %same density for all parameter dimensions
93  ind=1;
94  for i=1:nbrParams
95  if model.optimization.params_to_optimize(i)
96  stepd = (model.mu_ranges{i}(2)-model.mu_ranges{i}(1))/(model.optimization.opt_params.grid_density-1);
97  P{ind} = model.mu_ranges{i}(1):stepd:model.mu_ranges{i}(2);
98  ind = ind+1;
99  end
100  end
101  nbrOptParams = ind-1; %number of parameters to be optimized
102 else
103  %for each parameter dimension another density
104  ind=1;
105  for i=1:nbrParams
106  if model.optimization.params_to_optimize(i)
107  stepd = (model.mu_ranges{i}(2)-model.mu_ranges{i}(1))/(model.optimization.opt_params.grid_density(i)-1);
108  P{ind} = model.mu_ranges{i}(1):stepd:model.mu_ranges{i}(2);
109  ind = ind+1;
110  end
111  end
112  nbrOptParams = ind-1;
113 end
114 
115 
116 %Construct ParameterGrid-Matrix
117 limits=zeros(length(P),1);
118 for i=1:length(limits)
119  limits(i)=length(P{i});
120 end
121 nbrCombinations =1;
122 for i=1:length(limits)
123  nbrCombinations=nbrCombinations * limits(i);
124 end
125 
126 PM=zeros(nbrCombinations,nbrParams);
127 counter=ones(1,length(limits)+1);
128 stop=0;
129 j=1;
130 while(~stop)
131  ind=1;
132  for i=1:nbrParams
133  if(model.optimization.params_to_optimize(i))
134  PM(j,i)=P{ind}(counter(ind));
135  ind = ind+1;
136  else
137  PM(j,i)=model.optimization.init_params(i);
138  end;
139  end;
140  counter(1)=counter(1)+1;
141  for h=1:(length(counter)-1)
142  if(counter(h)>limits(h))
143  counter(h)=1;
144  counter(h+1)=counter(h+1)+1;
145  end
146 
147  end
148  if counter(length(counter))~=1
149  stop=1;
150  end
151  j=j+1;
152 end;
153 
154 %performing a detailed simulation of all parameter sets given by the lines
155 %of PM
156 output=zeros(length(PM(:,1)),1);
157 if strcmp(model.optimization.opt_mode ,'reduced')
158  output_der = zeros(length(PM(:,1)),1);
159 end
160 
161 for i=1:length(PM(:,1))
162  %set_Parameters
163  model = model.set_mu(model,PM(i,:));
164 
165  if(model.verbose>=8)
166  disp('Actual parameter set:');
167  PM(i,:)
168  end;
169 
170 
171  %evaluate output
172  if strcmp(model.optimization.opt_mode,'detailed');
173  output(i) = model.optimization.objective_function(model,model_data);
174  else
175  sim_data = lin_evol_rb_derivative_simulation(model, reduced_data);
176  sim_data = rb_reconstruction_derivative(model, model_data, detailed_data, sim_data);
177  nt=length(sim_data.output);
178  output(i) = sim_data.output(nt);
179  output_der(i) = sim_data.output_derivative(nt,:) * sim_data.output_derivative(nt,:)';
180  end
181 end;
182 
183 
184 opt_data = [];
185 opt_data.output = output;
186 opt_data.parameter_sets = PM;
187 opt_data.max_output = max(output);
188 if strcmp(model.optimization.opt_mode ,'reduced')
189  opt_data.output_der = output_der;
190 end
191 
192 %where to reach this maximum value:
193 ind=1;
194 Pmax=[];
195 ind=search_pos(opt_data.max_output, output,ind)
196 while(ind>0)
197  Pmax=[Pmax;PM(ind,:)];
198  ind=search_pos(opt_data.max_output, output,ind+1);
199 end
200 
201 opt_data.max_output_paramsets=Pmax;
202 
203 
204 end
205 
206 function ind=search_pos(value,vec,start)
207  found=0;
208  ind=start;
209  while ((~found)&&(ind<=length(vec)))
210  if(vec(ind)==value)
211  found=1;
212  else
213  ind = ind+1;
214  end
215  end
216 
217  if(ind>length(vec))
218  ind=-1;
219  end
220 
221 end
function r = verbose(level, message, messageId)
This function displays messages depending on a message-id and/or a level. Aditionally you can set/res...
Definition: verbose.m:17