diff --git a/pysindy/optimizers/constrained_sr3.py b/pysindy/optimizers/constrained_sr3.py index 4631240bd..6d3112e53 100644 --- a/pysindy/optimizers/constrained_sr3.py +++ b/pysindy/optimizers/constrained_sr3.py @@ -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 @@ -304,8 +304,8 @@ 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 @@ -313,13 +313,13 @@ def _update_coef_cvxpy(self, xi, cost, var_len, x, y, coef_sparse): # 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( @@ -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: diff --git a/pysindy/optimizers/trapping_sr3.py b/pysindy/optimizers/trapping_sr3.py index ae3d33169..2aaddc421 100644 --- a/pysindy/optimizers/trapping_sr3.py +++ b/pysindy/optimizers/trapping_sr3.py @@ -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: @@ -411,8 +411,8 @@ 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 @@ -420,11 +420,7 @@ def _solve_sparse_relax_and_split(self, xi, cost, var_len, coef_prev): # 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) @@ -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)