Skip to content

Commit

Permalink
Squash me
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob-Stevens-Haas committed Nov 8, 2023
1 parent 9907583 commit d436254
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
14 changes: 7 additions & 7 deletions pysindy/optimizers/constrained_sr3.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def _create_var_and_part_cost(
cost = cost + cp.norm2(np.ravel(self.thresholds) @ xi) ** 2
return xi, cost

def _update_coef_cvxpy(self, xi, cost, var_len, x, y, coef_sparse):
def _update_coef_cvxpy(self, xi, cost, var_len, coef_sparse, tol):
if self.use_constraints:
if self.inequality_constraints and self.equality_constraints:
# Process inequality constraints then equality constraints
Expand Down Expand Up @@ -304,22 +304,22 @@ def _update_coef_cvxpy(self, xi, cost, var_len, x, y, coef_sparse):
try:
prob.solve(
max_iter=self.max_iter,
eps_abs=self.tol,
eps_rel=self.tol,
eps_abs=tol,
eps_rel=tol,
verbose=self.verbose_cvxpy,
)
# Annoying error coming from L2 norm switching to use the ECOS
# solver, which uses "max_iters" instead of "max_iter", and
# similar semantic changes for the other variables.
except TypeError:
try:
prob.solve(abstol=self.tol, reltol=self.tol, verbose=self.verbose_cvxpy)
prob.solve(abstol=tol, reltol=tol, verbose=self.verbose_cvxpy)
except cp.error.SolverError:
print("Solver failed, setting coefs to zeros")
xi.value = np.zeros(coef_sparse.shape[0] * coef_sparse.shape[1])
xi.value = np.zeros(var_len)
except cp.error.SolverError:
print("Solver failed, setting coefs to zeros")
xi.value = np.zeros(coef_sparse.shape[0] * coef_sparse.shape[1])
xi.value = np.zeros(var_len)

if xi.value is None:
warnings.warn(
Expand Down Expand Up @@ -438,7 +438,7 @@ def _reduce(self, x, y):
var_len = coef_sparse.shape[0] * coef_sparse.shape[1]
xi, cost = self._create_var_and_part_cost(var_len, x_expanded, y)
coef_sparse = self._update_coef_cvxpy(
xi, cost, var_len, x_expanded, y, coef_sparse
xi, cost, var_len, coef_sparse, self.tol
)
objective_history.append(self._objective(x, y, 0, coef_full, coef_sparse))
else:
Expand Down
14 changes: 5 additions & 9 deletions pysindy/optimizers/trapping_sr3.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def _objective(self, x, y, coef_sparse, A, PW, q):
)
return 0.5 * np.sum(R2) + 0.5 * np.sum(A2) / self.eta + L1

def _solve_sparse_relax_and_split(self, xi, cost, var_len, coef_prev):
def _solve_sparse_relax_and_split(self, xi, cost, var_len, coef_prev, tol):
"""Solve coefficient update with CVXPY if threshold != 0"""
if self.use_constraints:
if self.inequality_constraints:
Expand All @@ -411,20 +411,16 @@ def _solve_sparse_relax_and_split(self, xi, cost, var_len, coef_prev):
try:
prob.solve(
max_iter=self.max_iter,
eps_abs=self.eps_solver,
eps_rel=self.eps_solver,
eps_abs=tol,
eps_rel=tol,
verbose=self.verbose_cvxpy,
)
# Annoying error coming from L2 norm switching to use the ECOS
# solver, which uses "max_iters" instead of "max_iter", and
# similar semantic changes for the other variables.
except TypeError:
try:
prob.solve(
abstol=self.eps_solver,
reltol=self.eps_solver,
verbose=self.verbose_cvxpy,
)
prob.solve(abstol=tol, reltol=tol, verbose=self.verbose_cvxpy)
except cp.error.SolverError:
print("Solver failed, setting coefs to zeros")
xi.value = np.zeros(var_len)
Expand Down Expand Up @@ -641,7 +637,7 @@ def _reduce(self, x, y):
)
# sparse relax_and_split
coef_sparse = self._solve_sparse_relax_and_split(
xi, cost, r * n_features, coef_prev
xi, cost, r * n_features, coef_prev, self.eps_solver
)
else:
pTp = np.dot(Pmatrix.T, Pmatrix)
Expand Down

0 comments on commit d436254

Please sign in to comment.