Parametric sensitivities? #126
-
Is there a recommended method to determine the sensitivity of the objective to a parameter (gradient d_objective/d_parameter at the optimum)? I could get what I was after by making the parameter an opti variable, constraining it to equal the nominal value, and then after the optimization is solved, find the dual of that constraint. Is there a different recommended method that might work while leaving the parameter as an opti parameter? There may be LOTS of parameters of interest in an aircraft optimization, so I was looking for the "cleanest" method to generate these (perhaps even en masse). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi Ben, A good question! It's possible, but it's a bit convoluted and requires some hacking into some CasADi details where the documentation can be quite sparse. Here's an example that parametrically computes sensitivities for the sensitivities w.r.t. a parameter on the constrained Rosenbrock Problem import aerosandbox as asb
import aerosandbox.numpy as np
a = 1
b = 100
opti = asb.Opti() # set up an optimization environment
# Define optimization variables
x = opti.variable(init_guess=0)
y = opti.variable(init_guess=0)
r = opti.parameter(1)
# Define constraints
opti.subject_to(x ** 2 + y ** 2 <= r)
# Define objective
f = (a - x) ** 2 + b * (y - x ** 2) ** 2
opti.minimize(f)
# Optimize
sol = opti.solve()
# Compute sensitivities
import casadi as cas
f_func = opti.to_function('Z', [opti.x, opti.lam_g, r], [f])
dfdr_expr = cas.gradient(f_func(sol(opti.x), sol(opti.lam_g), r), r)
dfdr_func = cas.Function('dfdr', [r], [dfdr_expr])
sensitivity_to_r = dfdr_func(sol(r)) # Sensitivity of the objective to the parameter r, at r=1
print(sensitivity_to_r) # -0.121 You'll notice that this code runs IPOPT twice - however, the second time is initialized to correct primals and duals, so it converges much faster. I'm certain there's a way to avoid the second IPOPT "run", but I can't seem to get CasADi functions to play nice here at the moment. In practice, for these kinds of applications I usually just end up doing an In the future I'm hoping to entirely revamp the backend of the |
Beta Was this translation helpful? Give feedback.
Hi Ben,
A good question! It's possible, but it's a bit convoluted and requires some hacking into some CasADi details where the documentation can be quite sparse. Here's an example that parametrically computes sensitivities for the sensitivities w.r.t. a parameter on the constrained Rosenbrock Problem