Fixed parameters

In this notebook we will show how to use fixed parameters. Therefore, we employ our Rosenbrock example. We define two problems, where for the first problem all parameters are optimized, and for the second we fix some of them to specified values.

Define problem

[1]:
import pypesto
import pypesto.visualize
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt

%matplotlib inline
[2]:
objective = pypesto.Objective(fun=sp.optimize.rosen,
                              grad=sp.optimize.rosen_der,
                              hess=sp.optimize.rosen_hess)

dim_full = 4
lb = -2 * np.ones((dim_full,1))
ub = 2 * np.ones((dim_full,1))

problem1 = pypesto.Problem(objective=objective, lb=lb, ub=ub)

x_fixed_indices = [1, 3]
x_fixed_vals = [1, 1]
problem2 = pypesto.Problem(objective=objective, lb=lb, ub=ub,
                           x_fixed_indices=x_fixed_indices,
                           x_fixed_vals=x_fixed_vals)

Optimize

[3]:
optimizer = pypesto.ScipyOptimizer()
n_starts = 10

result1 = pypesto.minimize(problem=problem1, optimizer=optimizer,
                           n_starts=n_starts)
result2 = pypesto.minimize(problem=problem2, optimizer=optimizer,
                           n_starts=n_starts)

Visualize

[4]:
fig, ax = plt.subplots()
pypesto.visualize.waterfall(result1, ax)
pypesto.visualize.waterfall(result2, ax)
[4]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f6ae7415eb8>
../_images/example_fixed_parameters_8_1.png
[5]:
result1.optimize_result.as_dataframe(['fval', 'x', 'grad'])
[5]:
fval grad x
0 3.927779e-15 [1.380082008367687e-06, -8.083573203926321e-08... [1.0000000049630953, 1.000000006500801, 1.0000...
1 7.810296e-14 [1.717501444180088e-06, -1.351913230799428e-06... [0.9999999523544961, 0.9999999001770131, 0.999...
2 8.211686e-14 [1.2722200973602453e-06, 2.6490319813334526e-0... [1.000000062824105, 1.0000001227817845, 1.0000...
3 6.675828e-13 [2.3108400838756338e-05, 5.154092104522114e-06... [1.0000000997998029, 1.0000001423276184, 1.000...
4 7.745611e-13 [-7.231244216666451e-06, 1.2069320871269854e-0... [1.0000001767517774, 1.0000003724654523, 1.000...
5 2.390803e-12 [-6.369931909390899e-06, 1.2558236812991624e-0... [0.9999996997057465, 0.9999994138349461, 0.999...
6 6.429238e-12 [7.387638091332483e-05, -5.3039113081590246e-0... [1.00000037493649, 1.0000005670569194, 1.00000...
7 3.157806e-10 [-0.0001412479254942318, -0.000317098067357588... [1.0000011490511884, 1.0000026569683544, 1.000...
8 3.701429e+00 [-9.476014543263744e-06, 2.2448530735408312e-0... [-0.7756612066524641, 0.6130963902374005, 0.38...
9 3.701429e+00 [1.2004479082783348e-05, -0.000416246476298010... [-0.7756557693277876, 0.6130880696435137, 0.38...
[6]:
result2.optimize_result.as_dataframe(['fval', 'x', 'grad'])
[6]:
fval grad x
0 1.034572e-18 [3.7571982860009024e-08, nan, 1.75952958872096... [1.0000000000468479, 1.0, 1.0000000000175602, ...
1 4.515451e-17 [-1.6843336988283393e-07, nan, -2.346168865413... [0.9999999997899833, 1.0, 0.9999999997658514, ...
2 1.788081e-16 [-5.347585997124525e-07, nan, -3.2423277395438... [0.9999999993332187, 1.0, 0.9999999999676414, ...
3 8.127003e-15 [-2.942953443493472e-06, nan, 2.33788272377160... [0.999999996330482, 1.0, 1.0000000023332163, 1.0]
4 7.530951e-14 [1.096951855484312e-05, nan, 7.631266543328272... [1.0000000136777036, 1.0, 1.0000000007616034, ...
5 3.989975e+00 [8.507160074167075e-07, nan, -1.00001459300255... [-0.9949747457536863, 1.0, 0.9999999990019814,...
6 3.989975e+00 [5.853680722367471e-07, nan, -4.80007196752967... [-0.9949747460895826, 1.0, 0.999999995209509, ...
7 3.989975e+00 [5.872636898551775e-06, nan, -2.24092759348412... [-0.9949747393965804, 1.0, 0.9999999977635453,...
8 3.989975e+00 [-9.41635691198428e-07, nan, -9.52827066229709... [-0.9949747480225729, 1.0, 0.9999999904907477,...
9 3.989975e+00 [3.688754127306737e-05, nan, -1.18522591456615... [-0.9949747001356986, 1.0, 0.9999999881713979,...