-
Notifications
You must be signed in to change notification settings - Fork 28
/
optimize.py
564 lines (454 loc) · 17.4 KB
/
optimize.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
"""Functional wrapper around the pygmo, nlopt and scipy libraries."""
import json
from collections import namedtuple
from multiprocessing import Event
from multiprocessing import Pool
from multiprocessing import Process
from multiprocessing import Queue
from pathlib import Path
from warnings import simplefilter
import numpy as np
import pandas as pd
import pygmo as pg
from scipy.optimize import minimize as scipy_minimize
from estimagic.dashboard.server_functions import run_server
from estimagic.optimization.process_arguments import process_optimization_arguments
from estimagic.optimization.process_constraints import process_constraints
from estimagic.optimization.reparametrize import reparametrize_from_internal
from estimagic.optimization.reparametrize import reparametrize_to_internal
from estimagic.optimization.utilities import index_element_to_string
from estimagic.optimization.utilities import propose_algorithms
QueueEntry = namedtuple("QueueEntry", ["iteration", "params", "fitness"])
def maximize(
criterion,
params,
algorithm,
criterion_kwargs=None,
constraints=None,
general_options=None,
algo_options=None,
dashboard=False,
db_options=None,
):
"""
Maximize *criterion* using *algorithm* subject to *pc* and bounds.
Args:
criterion (function):
Python function that takes a pandas DataFrame with parameters as the first
argument and returns a scalar floating point value.
params (pd.DataFrame):
See :ref:`params`.
algorithm (str):
specifies the optimization algorithm. See :ref:`list_of_algorithms`.
criterion_kwargs (dict):
additional keyword arguments for criterion
constraints (list):
list with constraint dictionaries. See for details.
general_options (dict):
additional configurations for the optimization.
algo_options (dict):
algorithm specific configurations for the optimization.
dashboard (bool):
whether to create and show a dashboard.
db_options (dict):
dictionary with kwargs to be supplied to the run_server function.
"""
def neg_criterion(params, **criterion_kwargs):
return -criterion(params, **criterion_kwargs)
# identify the criterion function as belongig to a maximization problem
if general_options is None:
general_options = {"_maximization": True}
else:
general_options["_maximization"] = True
res_dict, params = minimize(
neg_criterion,
params=params,
algorithm=algorithm,
criterion_kwargs=criterion_kwargs,
constraints=constraints,
general_options=general_options,
algo_options=algo_options,
dashboard=dashboard,
db_options=db_options,
)
res_dict["fun"] = -res_dict["fun"]
return res_dict, params
def minimize(
criterion,
params,
algorithm,
criterion_kwargs=None,
constraints=None,
general_options=None,
algo_options=None,
dashboard=False,
db_options=None,
):
"""Minimize *criterion* using *algorithm* subject to *constraints* and bounds.
Run several optimizations if called by lists of inputs.
Args:
criterion (function or list of functions):
Python function that takes a pandas DataFrame with parameters as the first
argument and returns a scalar floating point value.
params (pd.DataFrame or list of pd.DataFrames):
See :ref:`params`.
algorithm (str or list of strings):
specifies the optimization algorithm. See :ref:`list_of_algorithms`.
criterion_kwargs (dict or list of dicts):
additional keyword arguments for criterion
constraints (list or list of lists):
list with constraint dictionaries. See for details.
general_options (dict):
additional configurations for the optimization
algo_options (dict or list of dicts):
algorithm specific configurations for the optimization
dashboard (bool):
whether to create and show a dashboard
db_options (dict):
dictionary with kwargs to be supplied to the run_server function.
"""
arguments = process_optimization_arguments(
criterion=criterion,
params=params,
algorithm=algorithm,
criterion_kwargs=criterion_kwargs,
constraints=constraints,
general_options=general_options,
algo_options=algo_options,
dashboard=dashboard,
db_options=db_options,
)
if len(arguments) == 1:
# Run only one optimization
arguments = arguments[0]
result = _single_minimize(**arguments)
else:
# Run multiple optimizations
if dashboard:
raise NotImplementedError(
"Dashboard cannot be used for multiple optimizations, yet."
)
# set up multiprocessing
if "n_cores" not in arguments[0]["general_options"]:
raise ValueError(
"n_cores need to be specified in general_options"
+ " if multiple optimizations should be run."
)
n_cores = arguments[0]["general_options"]["n_cores"]
pool = Pool(processes=n_cores)
result = pool.map(_one_argument_single_minimize, arguments)
return result
def _single_minimize(
criterion,
params,
algorithm,
criterion_kwargs,
constraints,
general_options,
algo_options,
dashboard,
db_options,
):
"""Minimize * criterion * using * algorithm * subject to * constraints * and bounds.
Only one minimization.
Args:
criterion (function):
Python function that takes a pandas DataFrame with parameters as the first
argument and returns a scalar floating point value.
params (pd.DataFrame):
See :ref:`params`.
algorithm (str):
specifies the optimization algorithm. See :ref:`list_of_algorithms`.
criterion_kwargs (dict):
additional keyword arguments for criterion
constraints (list):
list with constraint dictionaries. See for details.
general_options (dict):
additional configurations for the optimization
algo_options (dict):
algorithm specific configurations for the optimization
dashboard (bool):
whether to create and show a dashboard
db_options (dict):
dictionary with kwargs to be supplied to the run_server function.
"""
simplefilter(action="ignore", category=pd.errors.PerformanceWarning)
params = _process_params(params)
fitness_factor = -1 if general_options.get("_maximization", False) else 1
fitness_eval = fitness_factor * criterion(params, **criterion_kwargs)
constraints, params = process_constraints(constraints, params)
internal_params = reparametrize_to_internal(params, constraints)
queue = Queue() if dashboard else None
if dashboard:
stop_signal = Event()
outer_server_process = Process(
target=run_server,
kwargs={
"queue": queue,
"db_options": db_options,
"start_param_df": params,
"start_fitness": fitness_eval,
"stop_signal": stop_signal,
},
daemon=False,
)
outer_server_process.start()
result = _internal_minimize(
criterion=criterion,
criterion_kwargs=criterion_kwargs,
params=params,
internal_params=internal_params,
constraints=constraints,
algorithm=algorithm,
algo_options=algo_options,
general_options=general_options,
queue=queue,
fitness_factor=fitness_factor,
)
if dashboard:
stop_signal.set()
outer_server_process.terminate()
return result
def _one_argument_single_minimize(kwargs):
"""Wrapper for single_minimize used for multiprocessing.
"""
return _single_minimize(**kwargs)
def _internal_minimize(
criterion,
criterion_kwargs,
params,
internal_params,
constraints,
algorithm,
algo_options,
general_options,
queue,
fitness_factor,
):
"""Create the internal criterion function and minimize it.
Args:
criterion (function):
Python function that takes a pandas DataFrame with parameters as the first
argument and returns a scalar floating point value.
criterion_kwargs (dict):
additional keyword arguments for criterion
params (pd.DataFrame):
See :ref:`params`.
internal_params (DataFrame):
See :ref:`params`.
constraints (list):
list with constraint dictionaries. See for details.
algorithm (str):
specifies the optimization algorithm. See :ref:`list_of_algorithms`.
algo_options (dict):
algorithm specific configurations for the optimization
general_options (dict):
additional configurations for the optimization
queue (Queue):
queue to which the fitness evaluations and params DataFrames are supplied.
fitness_factor (float):
multiplicative factor for the fitness displayed in the dashboard.
Set to -1 for maximizations to plot the fitness that is being maximized.
"""
internal_criterion = create_internal_criterion(
criterion=criterion,
params=params,
constraints=constraints,
criterion_kwargs=criterion_kwargs,
queue=queue,
fitness_factor=fitness_factor,
)
current_dir_path = Path(__file__).resolve().parent
with open(current_dir_path / "algo_dict.json") as j:
algos = json.load(j)
origin, algo_name = algorithm.split("_", 1)
try:
assert algo_name in algos[origin], "Invalid algorithm requested: {}".format(
algorithm
)
except (AssertionError, KeyError):
proposals = propose_algorithms(algorithm, algos)
raise NotImplementedError(
f"{algorithm} is not a valid choice. Did you mean one of {proposals}?"
)
if origin == "pygmo" and algorithm != "pygmo_simulated_annealing":
assert (
"popsize" in algo_options
), f"For genetic optimizers like {algo_name}, popsize is mandatory."
assert (
"gen" in algo_options
), f"For genetic optimizers like {algo_name}, gen is mandatory."
if origin in ["nlopt", "pygmo"]:
prob = _create_problem(internal_criterion, params)
algo = _create_algorithm(algo_name, algo_options, origin)
pop = _create_population(prob, algo_options, internal_params)
evolved = algo.evolve(pop)
result = _process_results(evolved, params, internal_params, constraints, origin)
elif origin == "scipy":
bounds = _get_scipy_bounds(params)
minimized = scipy_minimize(
internal_criterion,
internal_params,
method=algo_name,
bounds=bounds,
options=algo_options,
)
result = _process_results(
minimized, params, internal_params, constraints, origin
)
else:
raise ValueError("Invalid algorithm requested.")
return result
def create_internal_criterion(
criterion, params, constraints, criterion_kwargs, queue, fitness_factor
):
"""Create the internal criterion function.
Args:
criterion (function):
Python function that takes a pandas DataFrame with parameters as the first
argument and returns a scalar floating point value.
params (pd.DataFrame):
See :ref:`params`.
constraints (list):
list with constraint dictionaries. See for details.
criterion_kwargs (dict):
additional keyword arguments for criterion
queue (Queue):
queue to which the fitness evaluations and params DataFrames are supplied.
fitness_factor (float):
multiplicative factor for the fitness displayed in the dashboard.
Set to -1 for maximizations to plot the fitness that is being maximized.
Returns:
internal_criterion (function):
function that takes an internal_params DataFrame as only argument.
It calls the original criterion function after the necessary
reparametrizations and passes the results to the dashboard queue if given
before returning the fitness evaluation.
"""
c = np.ones(1, dtype=int)
def internal_criterion(x, counter=c):
p = reparametrize_from_internal(
internal=x,
fixed_values=params["_internal_fixed_value"].to_numpy(),
pre_replacements=params["_pre_replacements"].to_numpy().astype(int),
processed_constraints=constraints,
post_replacements=params["_post_replacements"].to_numpy().astype(int),
processed_params=params,
)
fitness_eval = criterion(p, **criterion_kwargs)
if queue is not None:
queue.put(
QueueEntry(
iteration=counter[0],
params=p,
fitness=fitness_factor * fitness_eval,
)
)
counter += 1
return fitness_eval
return internal_criterion
def _process_params(params):
assert (
not params.index.duplicated().any()
), "No duplicates allowed in the index of params."
params = params.copy()
if "lower" not in params.columns:
params["lower"] = -np.inf
else:
params["lower"].fillna(-np.inf)
if "upper" not in params.columns:
params["upper"] = np.inf
else:
params["upper"].fillna(np.inf)
if "group" not in params.columns:
params["group"] = "All Parameters"
if "name" not in params.columns:
names = [index_element_to_string(tup) for tup in params.index]
params["name"] = names
assert "_fixed" not in params.columns, "Invalid column name _fixed in params_df."
invalid_names = ["_fixed_value", "_is_fixed_to_value", "_is_fixed_to_other"]
invalid_present_columns = []
for col in params.columns:
if col in invalid_names or col.startswith("_internal"):
invalid_present_columns.append(col)
if len(invalid_present_columns) > 0:
msg = (
"Column names starting with '_internal' and as well as any other of the "
f"following columns are not allowed in params:\n{invalid_names}."
f"This is violated for:\n{invalid_present_columns}."
)
raise ValueError(msg)
return params
def _get_scipy_bounds(params):
params = params.query("_internal_free")
unprocessed_bounds = params[["lower", "upper"]].to_numpy().tolist()
bounds = []
for lower, upper in unprocessed_bounds:
bounds.append((_convert_bound(lower), _convert_bound(upper)))
return bounds
def _convert_bound(x):
if np.isfinite(x):
return x
else:
return None
def _create_problem(internal_criterion, params):
params = params.query("_internal_free")
class Problem:
def fitness(self, x):
return [internal_criterion(x)]
def get_bounds(self):
lb = params["_internal_lower"].to_numpy()
ub = params["_internal_upper"].to_numpy()
return lb, ub
return Problem()
def _create_algorithm(algo_name, algo_options, origin):
"""Create a pygmo algorithm."""
if origin == "nlopt":
algo = pg.algorithm(pg.nlopt(solver=algo_name))
for option, val in algo_options.items():
setattr(algo.extract(pg.nlopt), option, val)
elif origin == "pygmo":
pygmo_uda = getattr(pg, algo_name)
algo_options = algo_options.copy()
if "popsize" in algo_options:
del algo_options["popsize"]
algo = pg.algorithm(pygmo_uda(**algo_options))
return algo
def _create_population(problem, algo_options, internal_params):
"""Create a pygmo population object.
Todo:
- constrain random initial values to be in some bounds
- remove hardcoded seed
"""
popsize = algo_options.copy().pop("popsize", 1)
pop = pg.population(problem, size=popsize - 1, seed=5471)
pop.push_back(internal_params)
return pop
def _process_results(res, params, internal_params, constraints, origin):
"""Convert optimization results into json serializable dictionary.
Args:
res: Result from numerical optimizer.
params (DataFrame): See :ref:`params`.
internal_params (DataFrame): See :ref:`params`.
constraints (list): constraints for the optimization
origin (str): takes the values "pygmo", "nlopt", "scipy"
"""
if origin == "scipy":
res_dict = {}
res_dict.update(res)
for key, value in res_dict.items():
if isinstance(value, np.ndarray):
res_dict[key] = value.tolist()
x = res.x
elif origin in ["pygmo", "nlopt"]:
x = res.champion_x
res_dict = {"fun": res.champion_f[0]}
params = reparametrize_from_internal(
internal=x,
fixed_values=params["_internal_fixed_value"].to_numpy(),
pre_replacements=params["_pre_replacements"].to_numpy().astype(int),
processed_constraints=constraints,
post_replacements=params["_post_replacements"].to_numpy().astype(int),
processed_params=params,
)
return res_dict, params