def test_unknown_options_or_solver(self): c = np.array([-3, -2]) A_ub = [[2, 1], [1, 1], [1, 0]] b_ub = [10, 8, 4] def f(c, A_ub=None, b_ub=None, A_eq=None, b_eq=None, bounds=None, options={}): linprog(c, A_ub, b_ub, A_eq, b_eq, bounds, method=self.method, options=options) _assert_warns(OptimizeWarning, f, c, A_ub=A_ub, b_ub=b_ub, options=dict(spam='42')) assert_raises(ValueError, linprog, c, A_ub=A_ub, b_ub=b_ub, method='ekki-ekki-ekki')
def test_warns_on_failure(self): # Set nsteps small to ensure failure solver = self._get_solver(f, jac) solver.set_integrator(self.solver_name, nsteps=1) ic = [1.0, 0.0] solver.set_initial_value(ic, 0.0) _assert_warns(UserWarning, solver.integrate, pi)
def test_warn_mixed_constraints(self): # warns about inefficiency of mixed equality/inequality constraints fun = lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2 + (x[2] - 0.75)**2 cons = NonlinearConstraint(lambda x: [x[0]**2 - x[1], x[1] - x[2]], [1.1, .8], [1.1, 1.4]) bnds = ((0, None), (0, None), (0, None)) with suppress_warnings() as sup: sup.filter(UserWarning, "delta_grad == 0.0") _assert_warns(OptimizeWarning, minimize, fun, (2, 0, 1), method=self.method, bounds=bnds, constraints=cons)
def test_unknown_options_or_solver(): c = np.array([-3,-2]) A_ub = [[2,1], [1,1], [1,0]] b_ub = [10,8,4] _assert_warns(OptimizeWarning, linprog, c, A_ub=A_ub, b_ub=b_ub, options=dict(spam='42')) assert_raises(ValueError, linprog, c, A_ub=A_ub, b_ub=b_ub, method='ekki-ekki-ekki')
def test_warn_ignored_options(self): # warns about constraint options being ignored fun = lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2 + (x[2] - 0.75)**2 x0 = (2, 0, 1) if self.method == "slsqp": bnds = ((0, None), (0, None), (0, None)) else: bnds = None cons = NonlinearConstraint(lambda x: x[0], 2, np.inf) res = minimize(fun, x0, method=self.method, bounds=bnds, constraints=cons) # no warnings without constraint options assert_allclose(res.fun, 1) cons = LinearConstraint([1, 0, 0], 2, np.inf) res = minimize(fun, x0, method=self.method, bounds=bnds, constraints=cons) # no warnings without constraint options assert_allclose(res.fun, 1) cons = [] cons.append( NonlinearConstraint(lambda x: x[0]**2, 2, np.inf, keep_feasible=True)) cons.append( NonlinearConstraint(lambda x: x[0]**2, 2, np.inf, hess=BFGS())) cons.append( NonlinearConstraint(lambda x: x[0]**2, 2, np.inf, finite_diff_jac_sparsity=42)) cons.append( NonlinearConstraint(lambda x: x[0]**2, 2, np.inf, finite_diff_rel_step=42)) cons.append(LinearConstraint([1, 0, 0], 2, np.inf, keep_feasible=True)) for con in cons: _assert_warns(OptimizeWarning, minimize, fun, x0, method=self.method, bounds=bnds, constraints=cons)
def test_unknown_options(self): c = np.array([-3, -2]) A_ub = [[2, 1], [1, 1], [1, 0]] b_ub = [10, 8, 4] def f(c, A_ub=None, b_ub=None, A_eq=None, b_eq=None, bounds=None, options={}): linprog(c, A_ub, b_ub, A_eq, b_eq, bounds, method=self.method, options=options) o = {key: self.options[key] for key in self.options} o['spam'] = 42 _assert_warns(OptimizeWarning, f, c, A_ub=A_ub, b_ub=b_ub, options=o)
def test_exactly_singular(self): A = np.array([[0, 0], [1j, 1j]]) B = np.asarray([[1, 1], [0, 0]]) for M in A, A.T, B, B.T: expected_warning = _matfuncs_inv_ssq.LogmExactlySingularWarning L, info = _assert_warns(expected_warning, logm, M, disp=False) E = expm(L) assert_allclose(E, M, atol=1e-14)
def test_warn_ignored_options(self): # warns about constraint options being ignored fun = lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2 + (x[2] - 0.75)**2 x0 = (2, 0, 1) if self.method == "slsqp": bnds = ((0, None), (0, None), (0, None)) else: bnds = None cons = NonlinearConstraint(lambda x: x[0], 2, np.inf) res = minimize(fun, x0, method=self.method, bounds=bnds, constraints=cons) # no warnings without constraint options assert_allclose(res.fun, 1) cons = LinearConstraint([1, 0, 0], 2, np.inf) res = minimize(fun, x0, method=self.method, bounds=bnds, constraints=cons) # no warnings without constraint options assert_allclose(res.fun, 1) cons = [] cons.append(NonlinearConstraint(lambda x: x[0]**2, 2, np.inf, keep_feasible=True)) cons.append(NonlinearConstraint(lambda x: x[0]**2, 2, np.inf, hess=BFGS())) cons.append(NonlinearConstraint(lambda x: x[0]**2, 2, np.inf, finite_diff_jac_sparsity=42)) cons.append(NonlinearConstraint(lambda x: x[0]**2, 2, np.inf, finite_diff_rel_step=42)) cons.append(LinearConstraint([1, 0, 0], 2, np.inf, keep_feasible=True)) for con in cons: _assert_warns(OptimizeWarning, minimize, fun, x0, method=self.method, bounds=bnds, constraints=cons)
def test_indeterminate_covariance(self): # Test that a warning is returned when pcov is indeterminate xdata = np.array([1, 2, 3, 4, 5, 6]) ydata = np.array([1, 2, 3, 4, 5.5, 6]) _assert_warns(OptimizeWarning, curve_fit, lambda x, a, b: a * x, xdata, ydata)
def test_nearly_singular(self): M = np.array([[1e-100]]) expected_warning = _matfuncs_inv_ssq.LogmNearlySingularWarning L, info = _assert_warns(expected_warning, logm, M, disp=False) E = expm(L) assert_allclose(E, M, atol=1e-14)
def test_indeterminate_covariance(self): # Test that a warning is returned when pcov is indeterminate xdata = np.array([1, 2, 3, 4, 5, 6]) ydata = np.array([1, 2, 3, 4, 5.5, 6]) _assert_warns(OptimizeWarning, curve_fit, lambda x, a, b: a*x, xdata, ydata)