1 function [opt_data,model] = grid_search(model, varargin)
2 %
function opt_data = detailed_grid_search(model, model_data);
4 % Function performing an optimization by grid search
using the detailed
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
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)
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
29 % Markus Dihlmann 03.02.2010
33 if strcmp(inputname(2),
'model_data')
34 model_data = varargin{1};
36 model.optimization.opt_mode =
'detailed';
37 elseif strcmp(inputname(2),
'reduced_data')
39 reduced_data = varargin{1};
40 model_data = varargin{2};
41 detailed_data = varargin{3};
42 model.optimization.opt_mode =
'reduced';
44 reduced_data = varargin{1};
45 model.optimization.opt_mode =
'reduced';
50 %Check required fields:
51 if isempty(model_data)
52 model_data = gen_model_data(model);
55 if isempty(model.mu_names)
56 error('No parameter names specified!');
59 %Total number of parameters defined in model
60 nbrParams = length(model.mu_names);
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!');
67 %if isempty(model.opt_method)
68 % error('No optimization method defined!');
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!');
76 % If no initial values are given, set them in the middle of the ranges
77 if isempty(model.optimization.init_params)
79 model.optimization.init_params(i) = (model.mu_ranges{i}(1)+model.mu_ranges{i}(2))/2;
84 if isempty(model.optimization.opt_params.grid_density)
85 model.optimization.opt_params.grid_density =3; %defautl value
88 nbrParams = length(model.mu_names);
89 nbrOptparams=0; %number of paramters to be optimized
91 if(size(model.optimization.opt_params.grid_density)==1)
92 %same density for all parameter dimensions
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);
101 nbrOptParams = ind-1; %number of parameters to be optimized
103 %
for each parameter dimension another density
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);
112 nbrOptParams = ind-1;
116 %Construct ParameterGrid-Matrix
117 limits=zeros(length(P),1);
118 for i=1:length(limits)
119 limits(i)=length(P{i});
122 for i=1:length(limits)
123 nbrCombinations=nbrCombinations * limits(i);
126 PM=zeros(nbrCombinations,nbrParams);
127 counter=ones(1,length(limits)+1);
133 if(model.optimization.params_to_optimize(i))
134 PM(j,i)=P{ind}(counter(ind));
137 PM(j,i)=model.optimization.init_params(i);
140 counter(1)=counter(1)+1;
141 for h=1:(length(counter)-1)
142 if(counter(h)>limits(h))
144 counter(h+1)=counter(h+1)+1;
148 if counter(length(counter))~=1
154 %performing a detailed simulation of all parameter sets given by the lines
156 output=zeros(length(PM(:,1)),1);
157 if strcmp(model.optimization.opt_mode ,
'reduced')
158 output_der = zeros(length(PM(:,1)),1);
161 for i=1:length(PM(:,1))
163 model = model.set_mu(model,PM(i,:));
166 disp('Actual parameter set:');
172 if strcmp(model.optimization.opt_mode,'detailed');
173 output(i) = model.optimization.objective_function(model,model_data);
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,:)';
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;
192 %where to reach this maximum value:
195 ind=search_pos(opt_data.max_output, output,ind)
197 Pmax=[Pmax;PM(ind,:)];
198 ind=search_pos(opt_data.max_output, output,ind+1);
201 opt_data.max_output_paramsets=Pmax;
206 function ind=search_pos(value,vec,start)
209 while ((~found)&&(ind<=length(vec)))
function r = verbose(level, message, messageId)
This function displays messages depending on a message-id and/or a level. Aditionally you can set/res...