rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
stepsize_dichotomie.m
1 function [model,t_opt,nr_fct,output]=stepsize_dichotomie(model, model_data, output, x, d);
2 %function [model,t_opt,nr_fct,output]=stepsize_dichotomie(model, model_data, output, x, d)
3 %
4 % Function calculating a stepsize following a Dichotomie-algorithm
5 %
6 %Required fields of model:
7 % model.optimization.objective_function: function to be optimized
8 % model.optimization.tol: tolerance for exactness of the stepsize
9 % x: initial value
10 % d: search direction
11 %
12 %
13 %Output variables
14 % t_opt: optimal value for stepsize t
15 % nr_fct: number of function calls needed during process
16 %
17 %
18 % Oliver Zeeb 25.05.2010%
19 
20 if(model.verbose>=8)
21  disp('entered stepsize_dichotomie')
22 end
23 
24 if model.optimization.min_or_max == 'min'
25  min_or_max=1;
26 elseif model.optimization.min_or_max == 'max'
27  min_or_max=-1;
28 else disp('maximize or minimize? model.optimization.min_or_max not properly defined')
29  return
30 end
31 
32 
33 % Initializing
34 tol = model.optimization.tol;
35 nr_fct = 0; %counter for the number of function calls
36 t = 1; %Initial stepsize
37 
38 
39 h_t = calc_h(model,model_data,x,t,d)*min_or_max; nr_fct=nr_fct+1;
40 h_2_t = calc_h(model,model_data,x,2*t,d)*min_or_max; nr_fct=nr_fct+1;
41 h_0 = calc_h(model,model_data,x,0,d)*min_or_max; nr_fct=nr_fct+1;
42 
43 if isempty(output) %first component, before mu is changed. i.e. value of the functional with init_params, saves one simulation in gradient_opt
44  output=[output,h_0];
45 end
46 
47 %
48 % Phase 1
49 % Surrounding the minimum
50 %
51 if(model.verbose>=8)
52  disp('in stepsize_dichotomie, Phase 1')
53 end
54 
55 %debug
56 max_iter_phase_1=20;
57 iter_phase_1=0;
58 %end debug
59 
60 
61 if h_t >= h_0
62  while (h_t >= h_0)
63  t = t/2;
64  h_t = calc_h(model,model_data,x,t,d)*min_or_max;
65  nr_fct=nr_fct+1;
66  %debug
67  iter_phase_1=iter_phase_1+1
68  if iter_phase_1 == max_iter_phase_1
69  break
70  end
71  %end debug
72  end
73  a=0;
74  b=t;
75  c=2*t;
76 else %h(t) < h_0
77  while (h_2_t <= h_t)
78  h_t = h_2_t;
79  t = 2*t;
80  h_2_t = calc_h(model,model_data,x,t,d)*min_or_max;
81  nr_fct=nr_fct+1;
82  %debug
83  %iter_phase_1=iter_phase_1+1
84  %if iter_phase_1 == max_iter_phase_1
85  % break
86  %end
87  %end debug
88 
89 
90  end
91  a=0;
92  b=t/2;
93  c=t;
94 end
95 
96 %
97 % Phase2
98 % Finding the minimum
99 %
100 if(model.verbose>=8)
101  disp('in stepsize_dichotomie, Phase 2')
102 end
103 
104 
105 %debug
106 iter_phase_2=0;
107 %end debug
108 
109 h_b = calc_h(model,model_data,x,b,d)*min_or_max; nr_fct=nr_fct+1;
110 while (c-a) >= tol
111  nr_fct=nr_fct+1; %checking the following if condition needs a function call
112  if calc_h(model,model_data,x,(a+b)/2,d)*min_or_max < h_b
113  c=b;
114  b=(a+b)/2;
115  h_b=calc_h(model,model_data,x,b,d)*min_or_max; nr_fct=nr_fct+1;
116  elseif calc_h(model,model_data,x,(b+c)/2,d)*min_or_max < h_b
117  nr_fct=nr_fct+1; % elseif-condition was just checked, that needed a function call
118  a=b;
119  b=(b+c)/2;
120  h_b=calc_h(model,model_data,x,b,d)*min_or_max; nr_fct=nr_fct+1;
121  else % h(b) <= min{h((a+b)/2), h((b+c)/2)}
122  nr_fct=nr_fct+1; % elseif-condition was just checked, that needed a function call
123  a=(a+b)/2;
124  c=(b+c)/2;
125  end
126 
127  %debug
128  iter_phase_2=iter_phase_2+1
129  if mod(iter_phase_2,50) == 0
130  zeit_vergangen_phase_2=toc;
131  keyboard
132  end
133  %end debug
134 
135 
136 end
137 
138 %setting output data
139 t_opt=(a+c)/2;
140 func_t_opt=calc_h(model,model_data,x,t_opt,d)*min_or_max;
141 output=[output;func_t_opt];
142 model=set_mu_to_optimize(model,x,t_opt,d);
143 
144 if(model.verbose>=8)
145  disp('leaving stepsize_dichotomie')
146 end
147 
148 
149 %calculation of function h(t), i.e. function f(x+t*d)
150 function h_t=calc_h(model,model_data,x,t,d)
151 model_t_d=set_mu_to_optimize(model,x,t,d);
152 h_t=model.optimization.objective_function(model_t_d,model_data);
153 
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
function model = set_mu_to_optimize(model, x, varargin)
Funcion sets the parameters that are to be optimized to the values given by x Also usable for optimiz...