This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathoptimization.py
393 lines (286 loc) · 14.2 KB
/
optimization.py
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# optimization.py
#
# Optimization classes for Filters
#
# Copyright (c) 2000-2008 Stephane Larouche.
#
# This file is part of OpenFilters.
#
# OpenFilters is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# OpenFilters is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
import math
import config
from moremath import interpolation
import targets
########################################################################
# #
# optimization_error #
# #
########################################################################
class optimization_error(Exception):
"""Exception class for optimization errors"""
def __init__(self, value = ""):
self.value = value
def __str__(self):
if self.value:
return "Optimization error: %s." % self.value
else:
return "Optimization error."
########################################################################
# #
# optimization #
# #
########################################################################
class optimization(object):
"""A generic optimization class
All optimization class derive from this class."""
######################################################################
# #
# __init__ #
# #
######################################################################
def __init__(self, filter, targets, parent = None):
"""Initialize an instance of the optimization class
This method takes 2 or 3 arguments:
filter the filter being optimized;
targets the targets used in the optimization;
parent (optional) the user interface used to do the
optimization.
If given, the parent must implement an update method taking two
arguments (working, status)."""
self.filter = filter
self.targets = targets
self.parent = parent
# Stop criteria.
self.max_iterations = 0
self.acceptable_chi_2 = 0.0
self.min_chi_2_change = 0.0
# Initial maximum number of iteration, used to increase the maximum
# number of iteration when the user wants to continue the
# refinement.
self.initial_max_iterations = self.max_iterations
# The evolution and quality of the fit.
self.max_iterations_reached = False
self.status = 0
self.chi_2 = 0.0
self.stop_criteria_met = False
# The number of iterations already done.
self.iteration = 0
# Is work currently being done and can it be stopped?
self.working = False
self.can_stop = False
# Should the optimization continue.
self.continue_optimization = False
######################################################################
# #
# set_parent #
# #
######################################################################
def set_parent(self, parent = None):
"""Set the parent
This method takes an optional argument:
parent (optional) the user interface used to do the
optimization.
If given, the parent must implement an update method taking two
arguments (working, status)."""
self.parent = parent
######################################################################
# #
# increase_max_iterations #
# #
######################################################################
def increase_max_iterations(self, increment = None):
"""Increase the maximum number of iterations
This method takes an optional argument:
increment (optional) the number of iterations to add.
By default, the number of iterations is increased by the original
number of arguments."""
if increment is None:
increment = self.initial_max_iterations
self.max_iterations += increment
self.max_iterations_reached = False
######################################################################
# #
# reset_iterations #
# #
######################################################################
def reset_iterations(self):
"""Reset the number of iterations"""
self.iteration = 0
self.max_iterations = self.initial_max_iterations
self.max_iterations_reached = False
self.stop_criteria_met = False
######################################################################
# #
# iterate_ #
# #
######################################################################
def iterate_(self):
"""A generic method to do one iteration
The derived class must define this method."""
raise NotImplementedError("Subclass must implement this method")
######################################################################
# #
# iterate #
# #
######################################################################
def iterate(self):
"""Perform one iteration
In addition to calling the derived class iterate_, this method
takes care of updating the user interface."""
self.working = True
self.can_stop = False
if self.parent:
self.parent.update(self.working, self.status)
self.iterate_()
self.working = False
if self.parent:
self.parent.update(self.working, self.status)
######################################################################
# #
# go #
# #
######################################################################
def go(self):
"""Optimize the filter
This method optimizes the filter until a stopping criteria is met
or the maximum number of iteration is reached.
In addition to calling the derived class iterate_ repeatedly, this
method takes care of updating the user interface."""
self.continue_optimization = True
self.working = True
self.can_stop = True
# Increase the number of iteration when asked to continue.
if self.max_iterations_reached:
self.increase_max_iterations()
if self.parent:
self.parent.update(self.working, self.status)
while self.continue_optimization:
self.iterate_()
if self.stop_criteria_met or self.max_iterations_reached:
break
if self.parent:
self.parent.update(self.working, self.status)
self.working = False
if self.parent:
self.parent.update(self.working, self.status)
######################################################################
# #
# stop #
# #
######################################################################
def stop(self):
"""Stop the optimization
This will stop the optimization after the current iteration."""
self.continue_optimization = False
######################################################################
# #
# get_iteration #
# #
######################################################################
def get_iteration(self):
"""Get the number of iterations
This method returns the number of iteration."""
return self.iteration
######################################################################
# #
# get_status #
# #
######################################################################
def get_status(self):
"""Get the status of the optimization
This method returns the status of the optimization."""
return self.status
######################################################################
# #
# get_chi_2 #
# #
######################################################################
def get_chi_2(self):
"""Get chi square
This method returns the chi square of the optimization."""
return self.chi_2
######################################################################
# #
# get_stop_criteria_met #
# #
######################################################################
def get_stop_criteria_met(self):
"""Get if a stop criteria was met
This method returns a boolean indicating if a stop criteria was med
during the optimization."""
return self.stop_criteria_met
######################################################################
# #
# get_max_iterations_reached #
# #
######################################################################
def get_max_iterations_reached(self):
"""Get if the maximum number of iteration was reached
This method returns a boolean indicating if the maximum number of
iteration was reached."""
return self.max_iterations_reached
######################################################################
# #
# get_working #
# #
######################################################################
def get_working(self):
"""Get if the optimization is working
This method returns a boolean indicating if the optimization is
working."""
return self.working
######################################################################
# #
# get_can_stop #
# #
######################################################################
def get_can_stop(self):
"""Get if it is possible to stop the current operation
This method returns a boolean indicating if it is possible to stop
the current operation. In most optimization method, it is possible
to stop between iterations, but not inside an iteration."""
return self.can_stop
######################################################################
# #
# copy_to_filter #
# #
######################################################################
def copy_to_filter(self):
"""Copy the optimized filter to the filter instance
Derived class must implement this method."""
raise NotImplementedError("Subclass must implement this method")
######################################################################
# #
# save_index_profile #
# #
######################################################################
def save_index_profile(self, file):
"""Save the index profile
This method takes one argument:
file the file in which to write.
Derived class must implement this method."""
raise NotImplementedError("Subclass must implement this method")
######################################################################
# #
# save_values #
# #
######################################################################
def save_values(self, file):
"""Save the current values of the optimized properties
This method takes one argument:
file the file in which to write.
Derived class must implement this method."""
raise NotImplementedError("Subclass must implement this method")