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.

[1]:
# install if not done yet
# %pip install pypesto --quiet

Define the two problems

[2]:
import matplotlib.pyplot as plt
import numpy as np
import scipy as sp

import pypesto
import pypesto.optimize as optimize
import pypesto.visualize as visualize

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

dim_full = 5
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

[4]:
optimizer = optimize.ScipyOptimizer()
n_starts = 10

result1 = optimize.minimize(
    problem=problem1, optimizer=optimizer, n_starts=n_starts, filename=None
)
result2 = optimize.minimize(
    problem=problem2, optimizer=optimizer, n_starts=n_starts, filename=None
)

Visualize

[5]:
fig, ax = plt.subplots()
visualize.waterfall(result1, ax)
visualize.waterfall(result2, ax)
visualize.parameters(result1)
visualize.parameters(result2)
visualize.parameters(result2, parameter_indices="all");
../_images/example_fixed_parameters_9_0.png
../_images/example_fixed_parameters_9_1.png
../_images/example_fixed_parameters_9_2.png
../_images/example_fixed_parameters_9_3.png
[6]:
result1.optimize_result.as_dataframe(["fval", "x", "grad"])
[6]:
fval x grad
0 1.087686e-13 [1.000000003681747, 1.0000000179802533, 1.0000... [-4.239340310444613e-06, 3.5127430372472925e-0...
1 1.129672e-13 [1.0000000149431127, 1.000000027980478, 1.0000... [7.921853496075773e-07, 1.1450881537186398e-05...
2 6.629000e-13 [0.9999999290745738, 0.9999999059622436, 0.999... [-1.9267085912715815e-05, -9.947610829709145e-...
3 1.462313e-11 [1.0000001849918025, 1.0000004372911107, 1.000... [-2.6553009925117194e-05, -6.984728391741192e-...
4 3.465962e-11 [0.9999999458472647, 0.9999997675286915, 0.999... [4.9558028172361256e-05, 0.0001026185071983223...
5 5.020502e-11 [1.0000004403293112, 1.000000624315949, 1.0000... [0.00010341785072354713, 0.0001506732319428299...
6 1.767208e-10 [1.0000005978282906, 1.0000016040450035, 1.000... [-0.0001621596669565902, -9.472559655459499e-0...
7 3.930839e+00 [-0.9620509939419464, 0.9357393603977466, 0.88... [6.061550525515713e-06, 1.9379957616472154e-07...
8 3.930839e+00 [-0.9620509992603287, 0.9357393591605636, 0.88... [1.6586041877708624e-06, -2.778132248693055e-0...
9 3.930839e+00 [-0.9620500006701921, 0.9357378820650448, 0.88... [0.00017055538213917032, 9.742115611999225e-07...
[7]:
result2.optimize_result.as_dataframe(["fval", "x", "grad"])
[7]:
fval x grad
0 3.250661e-17 [0.9999999999491841, 1.0, 1.000000000116344, 1... [-4.0754357442262286e-08, nan, 1.1657673361060...
1 1.817616e-16 [0.9999999993633873, 1.0, 0.9999999999906579, ... [-5.105633461654692e-07, nan, -9.3607668282670...
2 2.855117e-14 [0.9999999997788883, 1.0, 0.9999999925796897, ... [-1.773315709438736e-07, nan, -7.4351508315965...
3 4.560022e-14 [1.0000000084932499, 1.0, 1.0000000021728896, ... [6.811586462406269e-06, nan, 2.177235429059635...
4 5.380211e-14 [1.000000000526084, 1.0, 1.0000000070019677, 1... [4.219194205117625e-07, nan, 7.015971646904344...
5 3.989975e+00 [-0.9949747459449094, 1.0, 0.9999999987420566,... [6.996554779981068e-07, nan, -1.26045930166601...
6 3.989975e+00 [-0.9949747383039872, 1.0, 1.0000000022981061,... [6.73575243670399e-06, nan, 2.302702366347522e...
7 3.989975e+00 [-0.9949747431404307, 1.0, 0.9999999942746612,... [2.9151087379553076e-06, nan, -5.7367894099379...
8 3.989975e+00 [-0.9949747475764179, 1.0, 1.0000000259371473,... [-5.891867571605758e-07, nan, 2.59890224180793...
9 3.989975e+00 [-0.9949746252496621, 1.0, 1.0000000092670884,... [9.604522572725926e-05, nan, 9.285622675528115...