Skip to content

Commit

Permalink
add fallback method to swedmdc
Browse files Browse the repository at this point in the history
add clipping to sensitivities
  • Loading branch information
EthanJamesLew committed Apr 19, 2024
1 parent 305d8cf commit 96a40cf
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions autokoopman/estimator/koopman.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def swdmdc(X, Xp, U, r, Js, W):
K = cp.Variable((n_obs, n_obs + n_inps))

# SW-eDMD objective
weights_obj = np.vstack([(np.abs(J) @ w) for J, w in zip(Js, W)]).T
weights_obj = np.vstack([(np.clip(np.abs(J), 0.0, 1.0) @ w) for J, w in zip(Js, W)]).T
P = sf * cp.multiply(weights_obj, Yp.T - K @ Y.T)
# add regularization
objective = cp.Minimize(cp.sum_squares(P) + 1E-4 * 1.0 / (n_obs**2) * cp.norm(K, "fro"))
Expand All @@ -97,15 +97,15 @@ def swdmdc(X, Xp, U, r, Js, W):
prob = cp.Problem(objective, constraints)

# solve for the SW-eDMD Koopman operator
_ = prob.solve(solver=cp.CLARABEL)
#_ = prob.solve(solver=cp.ECOS)

# backup case
if K.value is None:
# give a warning about the optimization failure
try:
_ = prob.solve(solver=cp.CLARABEL)
#_ = prob.solve(solver=cp.ECOS)
if K.value is None:
raise Exception("SW-eDMD (cvxpy) Optimization failed to converge.")
except:
warnings.warn("SW-eDMD (cvxpy) Optimization failed to converge. Switching to unweighted DMDc.")
return dmdc(X, Xp, U, r)

return dmdc(X, Xp, U, r)
# get the transformation
Atilde = K.value
return Atilde[:, :state_size], Atilde[:, state_size:]
Expand Down

0 comments on commit 96a40cf

Please sign in to comment.