-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconjugate_min.c
215 lines (180 loc) · 6.98 KB
/
conjugate_min.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <volume_io/internal_volume_io.h>
#include <conjugate_grad.h>
#include <conjugate_min.h>
#include <line_min_prototypes.h>
#define DEFAULT_N_RESTARTS 2
struct conjugate_min_struct
{
int n_parameters;
Real *parameters;
Real *derivative;
Real *test_parameters;
Real current_value;
Real (*function) ( Real [], void * );
void (*deriv_function) ( Real [], void *, Real [] );
void *function_data;
Real line_min_range_tolerance;
Real line_min_domain_tolerance;
Real termination_range_tolerance;
Real termination_domain_tolerance;
int n_iterations;
int max_iterations;
int n_restarts;
int max_restarts;
conjugate_grad conjugate_dir_info;
};
public conjugate_min conjugate_min_initialize(
int n_parameters,
Real initial[],
Real (*function) ( Real [], void * ),
void (*deriv_function) ( Real [], void *, Real [] ),
void *function_data,
Real termination_range_tolerance,
Real termination_domain_tolerance,
Real line_min_range_tolerance,
Real line_min_domain_tolerance,
int max_iterations,
int max_restarts,
Real *current_value )
{
int parm;
conjugate_min conj;
ALLOC( conj, 1 );
conj->conjugate_dir_info = initialize_conjugate_gradient( n_parameters );
conj->n_parameters = n_parameters;
if( conj->n_parameters > 0 )
{
ALLOC( conj->parameters, n_parameters );
ALLOC( conj->derivative, n_parameters );
ALLOC( conj->test_parameters, n_parameters );
}
for_less( parm, 0, n_parameters )
conj->parameters[parm] = initial[parm];
conj->function = function;
conj->deriv_function = deriv_function;
conj->function_data = function_data;
conj->termination_range_tolerance = termination_range_tolerance;
conj->termination_domain_tolerance = termination_domain_tolerance;
conj->line_min_range_tolerance = line_min_range_tolerance;
conj->line_min_domain_tolerance = line_min_domain_tolerance;
conj->n_iterations = 0;
conj->max_iterations = max_iterations;
conj->n_restarts = 0;
if( max_restarts < 0 )
conj->max_restarts = DEFAULT_N_RESTARTS;
else
conj->max_restarts = max_restarts;
conj->current_value = (*conj->function) ( conj->parameters,
conj->function_data );
if( current_value != NULL )
*current_value = conj->current_value;
return( conj );
}
public BOOLEAN conjugate_min_do_one_iteration(
conjugate_min conj,
Real *resulting_value )
{
BOOLEAN success;
Real new_value, max_movement;
if( conj->max_iterations >= 0 && conj->n_iterations >= conj->max_iterations)
return( FALSE );
++conj->n_iterations;
(*conj->deriv_function) ( conj->parameters, conj->function_data,
conj->derivative );
success = get_conjugate_unit_direction( conj->conjugate_dir_info,
conj->derivative,
conj->derivative );
if( success )
{
new_value = minimize_along_line( conj->n_parameters,
conj->parameters,
conj->derivative,
conj->test_parameters,
conj->function,
conj->function_data,
conj->line_min_range_tolerance,
conj->line_min_domain_tolerance,
conj->current_value,
&max_movement );
if( conj->current_value - new_value <=
conj->termination_range_tolerance &&
max_movement <= conj->termination_domain_tolerance )
{
success = FALSE;
}
conj->current_value = new_value;
}
if( !success && conj->n_restarts < conj->max_restarts )
{
reinitialize_conjugate_gradient( conj->conjugate_dir_info );
success = TRUE;
++conj->n_restarts;
}
if( resulting_value != NULL )
*resulting_value = conj->current_value;
return( success );
}
public void conjugate_min_get_current_position(
conjugate_min conj,
Real parameters[] )
{
int parm;
for_less( parm, 0, conj->n_parameters )
parameters[parm] = conj->parameters[parm];
}
public int conjugate_min_get_n_iterations(
conjugate_min conj )
{
return( conj->n_iterations );
}
public void conjugate_min_print_iteration_info(
conjugate_min conj )
{
print( "Iter %5d: Value: %30.16g N restarts: %2d\n",
conj->n_iterations, conj->current_value, conj->n_restarts );
}
public void conjugate_min_terminate(
conjugate_min conj )
{
if( conj->n_parameters > 0 )
{
FREE( conj->parameters );
FREE( conj->derivative );
FREE( conj->test_parameters );
}
delete_conjugate_gradient( conj->conjugate_dir_info );
}
public Real conjugate_minimize_function(
int n_parameters,
Real initial[],
Real (*function) ( Real [], void * ),
void (*deriv_function) ( Real [], void *, Real [] ),
void *function_data,
Real termination_range_tolerance,
Real termination_domain_tolerance,
Real line_min_range_tolerance,
Real line_min_domain_tolerance,
int max_iterations,
int max_restarts,
Real solution[] )
{
Real value;
conjugate_min conj;
conj = conjugate_min_initialize( n_parameters, initial,
function, deriv_function,
function_data,
termination_range_tolerance,
termination_domain_tolerance,
line_min_range_tolerance,
line_min_domain_tolerance,
max_iterations,
max_restarts, &value );
conjugate_min_print_iteration_info( conj );
while( conjugate_min_do_one_iteration( conj, &value ) )
{
conjugate_min_print_iteration_info( conj );
}
conjugate_min_get_current_position( conj, solution );
conjugate_min_terminate( conj );
return( value );
}