rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
detailed_grid_search.m
1 function [opt_data,model] = detailed_grid_search(model, model_data, detailed_data, reduced_data)
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 model.opt_method='detailed grid search';
32 
33 %Check required fields:
34 if isempty(model_data)
35  model_data = gen_model_data(model);
36 end;
37 
38 if isempty(model.mu_names)
39  error('No parameter names specified!');
40 end;
41 
42 %Total number of parameters defined in model
43 nbrParams = length(model.mu_names);
44 
45 %Continue check required fields
46 if (isempty(model.mu_ranges)||nbrParams~=length(model.mu_ranges))
47  error('not the right number of parameter ranges given!');
48 end;
49 
50 %if isempty(model.opt_method)
51 % error('No optimization method defined!');
52 %end;
53 
54 if (isempty(model.optimization.params_to_optimize)||nbrParams~=length(model.optimization.params_to_optimize))
55  error('It is not properly defined which parameters are set for optimization and which not!');
56 end;
57 
58 
59 % If no initial values are given, set them in the middle of the ranges
60 if isempty(model.optimization.init_params)
61  for i=1:nbrParams
62  model.optimization.init_params(i) = (model.mu_ranges{i}(1)+model.mu_ranges{i}(2))/2;
63  end;
64 end;
65 
66 %Creating search grid
67 if isempty(model.optimization.opt_params.grid_density)
68  model.optimization.opt_params.grid_density =3; %defautl value
69 end
70 
71 nbrParams = length(model.mu_names);
72 nbrOptparams=0; %number of paramters to be optimized
73 
74 if(size(model.optimization.opt_params.grid_density)==1)
75  %same density for all parameter dimensions
76  ind=1;
77  for i=1:nbrParams
78  if model.optimization.params_to_optimize(i)
79  stepd = (model.mu_ranges{i}(2)-model.mu_ranges{i}(1))/(model.optimization.opt_params.grid_density-1);
80  P{ind} = model.mu_ranges{i}(1):stepd:model.mu_ranges{i}(2);
81  ind = ind+1;
82  end
83  end
84  nbrOptParams = ind-1; %number of parameters to be optimized
85 else
86  %for each parameter dimension another density
87  ind=1;
88  for i=1:nbrParams
89  if model.optimization.params_to_optimize(i)
90  stepd = (model.mu_ranges{i}(2)-model.mu_ranges{i}(1))/(model.optimization.opt_params.grid_density(i)-1);
91  P{ind} = model.mu_ranges{i}(1):stepd:model.mu_ranges{i}(2);
92  ind = ind+1;
93  end
94  end
95  nbrOptParams = ind-1;
96 end
97 
98 
99 %Construct ParameterGrid-Matrix
100 limits=zeros(length(P),1);
101 for i=1:length(limits)
102  limits(i)=length(P{i});
103 end
104 nbrCombinations =1;
105 for i=1:length(limits)
106  nbrCombinations=nbrCombinations * limits(i);
107 end
108 
109 PM=zeros(nbrCombinations,nbrParams);
110 counter=ones(1,length(limits)+1);
111 stop=0;
112 j=1;
113 while(~stop)
114  ind=1;
115  for i=1:nbrParams
116  if(model.optimization.params_to_optimize(i))
117  PM(j,i)=P{ind}(counter(ind));
118  ind = ind+1;
119  else
120  PM(j,i)=model.optimization.init_params(i);
121  end;
122  end;
123  counter(1)=counter(1)+1;
124  for h=1:(length(counter)-1)
125  if(counter(h)>limits(h))
126  counter(h)=1;
127  counter(h+1)=counter(h+1)+1;
128  end
129 
130  end
131  if counter(length(counter))~=1
132  stop=1;
133  end
134  j=j+1;
135 end;
136 
137 %performing a detailed simulation of all parameter sets given by the lines
138 %of PM
139 output=zeros(length(PM(:,1)),1);
140 for i=1:length(PM(:,1))
141  %set_Parameters
142  model = model.set_mu(model,PM(i,:));
143 
144  if(model.verbose>=8)
145  disp('Actual parameter set:');
146  PM(i,:)
147  end;
148 
149 
150  %evaluate output
151  output(i) = model.optimization.objective_function(model,model_data);
152 
153 end;
154 
155 
156 opt_data = [];
157 opt_data.output = output;
158 opt_data.parameter_sets = PM;
159 opt_data.max_output = max(output);
160 
161 %where to reach this maximum value:
162 ind=1;
163 Pmax=[];
164 ind=search_pos(opt_data.max_output, output,ind)
165 while(ind>0)
166  Pmax=[Pmax;PM(ind,:)];
167  ind=search_pos(opt_data.max_output, output,ind+1);
168 end
169 
170 opt_data.max_output_paramsets=Pmax;
171 
172 
173 end
174 
175 function ind=search_pos(value,vec,start)
176  found=0;
177  ind=start;
178  while ((~found)&&(ind<=length(vec)))
179  if(vec(ind)==value)
180  found=1;
181  else
182  ind = ind+1;
183  end
184  end
185 
186  if(ind>length(vec))
187  ind=-1;
188  end
189 
190 end