# 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.optimize as optimize
import pypesto.visualize as visualize
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt

%matplotlib inline

[2]:

objective = pypesto.Objective(fun=sp.optimize.rosen,
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¶

[3]:

optimizer = optimize.ScipyOptimizer()
n_starts = 10

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


## Visualize¶

[4]:

fig, ax = plt.subplots()
visualize.waterfall(result1, ax)
visualize.waterfall(result2, ax)
visualize.parameters(result1)
visualize.parameters(result2)
visualize.parameters(result2, parameter_indices='all')

[4]:

<matplotlib.axes._subplots.AxesSubplot at 0x7ff3da236a20>

[5]:

result1.optimize_result.as_dataframe(['fval', 'x', 'grad'])

[5]:

0 2.563931e-14 [0.9999999859217336, 0.9999999812160436, 0.999... [-3.7771869883630873e-06, 3.2004378806360524e-...
1 4.103854e-14 [1.0000000033181213, 1.00000001070042, 1.00000... [-1.6190347383411735e-06, 5.768553691118231e-0...
2 2.430040e-13 [0.9999999979980921, 0.9999999872750013, 0.999... [3.4844693764909735e-06, 4.6873211372756083e-0...
3 2.993261e-12 [1.0000000655628785, 1.0000002137366326, 1.000... [-3.291322500031286e-05, 6.600823794056182e-07...
4 3.028019e-11 [1.0000002263273202, 0.9999999457510741, 1.000... [0.00020321414758544783, -0.000184783444508992...
5 1.504857e-10 [1.0000008747306284, 1.000001813929941, 1.0000... [-2.4037728901340504e-05, -1.168240814877157e-...
6 3.713657e-10 [1.0000011952242212, 1.000001771893066, 1.0000... [0.00024981346612303615, 0.0001962351003382311...
7 4.012393e-10 [0.9999986079063197, 0.9999988670990364, 0.999... [-0.000663297051531534, 0.000537723456872972, ...
8 5.247717e-10 [1.000000368254703, 1.0000009022274876, 0.9999... [-6.555069341760695e-05, 0.0009407121705420637...
9 3.930839e+00 [-0.9620510415103535, 0.9357394330313418, 0.88... [-1.109923131625834e-06, 5.109232684041842e-06...
[6]:

result2.optimize_result.as_dataframe(['fval', 'x', 'grad'])

[6]: