Python scipy.optimize.OptimizeWarning() Examples
The following are 15
code examples of scipy.optimize.OptimizeWarning().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
scipy.optimize
, or try the search function
.
Example #1
Source File: test_linprog.py From GraphicDesignPatternByPython with MIT License | 6 votes |
def test_network_flow_limited_capacity(self): # A network flow problem with supply and demand at nodes # and with costs and capacities along directed edges. # http://blog.sommer-forst.de/2013/04/10/ cost = [2, 2, 1, 3, 1] bounds = [ [0, 4], [0, 2], [0, 2], [0, 3], [0, 5]] n, p = -1, 1 A_eq = [ [n, n, 0, 0, 0], [p, 0, n, n, 0], [0, p, p, 0, n], [0, 0, 0, p, p]] b_eq = [-4, 0, 0, 4] if self.method == "simplex": # Including the callback here ensures the solution can be # calculated correctly, even when phase 1 terminated # with some of the artificial variables as pivots # (i.e. basis[:m] contains elements corresponding to # the artificial variables) res = linprog(c=cost, A_eq=A_eq, b_eq=b_eq, bounds=bounds, method=self.method, options=self.options, callback=lambda x, **kwargs: None) else: with suppress_warnings() as sup: sup.filter(RuntimeWarning, "scipy.linalg.solve\nIll...") sup.filter(OptimizeWarning, "A_eq does not appear...") sup.filter(OptimizeWarning, "Solving system with option...") res = linprog(c=cost, A_eq=A_eq, b_eq=b_eq, bounds=bounds, method=self.method, options=self.options) _assert_success(res, desired_fun=14)
Example #2
Source File: test_linprog.py From GraphicDesignPatternByPython with MIT License | 6 votes |
def test_enzo_example_b(self): # rescued from https://github.com/scipy/scipy/pull/218 c = [2.8, 6.3, 10.8, -2.8, -6.3, -10.8] A_eq = [[-1, -1, -1, 0, 0, 0], [0, 0, 0, 1, 1, 1], [1, 0, 0, 1, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 1, 0, 0, 1]] b_eq = [-0.5, 0.4, 0.3, 0.3, 0.3] if self.method == "simplex": # Including the callback here ensures the solution can be # calculated correctly. res = linprog(c=c, A_eq=A_eq, b_eq=b_eq, method=self.method, options=self.options, callback=lambda x, **kwargs: None) else: with suppress_warnings() as sup: sup.filter(OptimizeWarning, "A_eq does not appear...") res = linprog(c=c, A_eq=A_eq, b_eq=b_eq, method=self.method, options=self.options) _assert_success(res, desired_fun=-1.77, desired_x=[0.3, 0.2, 0.0, 0.0, 0.1, 0.3])
Example #3
Source File: test_generation.py From penaltymodel with Apache License 2.0 | 6 votes |
def test_linprog_optimizewarning(self, dummy_linprog): """The linear program sometimes throws OptimizeWarning for matrices that are not full row rank. In such a case, penaltymodel-lp should give up and let a more sophisticated penaltymodel deal with the problem""" # Note: I'm using mock because it's difficult to think of a small ising # system that is not full row rank. (i.e. need to consider linear states, # quadratic states, offset and gap coefficients when building # the non-full-row-rank matrix). dummy_linprog.return_value = OptimizeWarning # Placeholder problem nodes = ['r', 'a', 'n', 'd', 'o', 'm'] values = {(1, 1, 1, 1, 1, 1), (1, 1, 0, 0, 0, 0)} with self.assertRaises(ValueError): lp.generate_bqm(nx.complete_graph(nodes), values, nodes, catch_warnings=False)
Example #4
Source File: skater.py From region with BSD 3-Clause "New" or "Revised" License | 6 votes |
def make_cut(self, in_node, out_node, score, MSF=None): """ make a cut on the MSF inplace, provided the in_node, out_node, MSF, and score. in_node: int, ID of the source node for the edge to be cut out_node: int, ID of the destination node for the edge to be cut score: float, the value of the score being cut. if the score is infinite, the cut is not made. MSF: the spanning forest to use when making the cut. If not provided, uses the defualt tree in self.minimum_spanning_forest_ """ if MSF is None: MSF = self.minimum_spanning_forest_ if np.isfinite(score): MSF[in_node, out_node] = 0 MSF.eliminate_zeros() return (MSF, *cg.connected_components(MSF, directed=False)) raise OptimizeWarning('Score of the ({},{}) cut is inf, the quorum is likely not met!')
Example #5
Source File: test_linprog.py From GraphicDesignPatternByPython with MIT License | 5 votes |
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')
Example #6
Source File: test_linprog.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_remove_redundancy_infeasibility(self): m, n = 10, 10 c = np.random.rand(n) A0 = np.random.rand(m, n) b0 = np.random.rand(m) A0[-1, :] = 2 * A0[-2, :] b0[-1] *= -1 with suppress_warnings() as sup: sup.filter(OptimizeWarning, "A_eq does not appear...") res = linprog(c, A_eq=A0, b_eq=b0, method=self.method, options=self.options) _assert_infeasible(res)
Example #7
Source File: test_linprog.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_magic_square_bug_7044(self): # test linprog with a problem with a rank-deficient A_eq matrix A, b, c, N = magic_square(3) with suppress_warnings() as sup: sup.filter(OptimizeWarning, "A_eq does not appear...") res = linprog(c, A_eq=A, b_eq=b, bounds=(0, 1), method=self.method, options=self.options) _assert_success(res, desired_fun=1.730550597)
Example #8
Source File: test_linprog.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_bug_8664(self): # Weak test. Ideally should _detect infeasibility_ for all options. c = [4] A_ub = [[2], [5]] b_ub = [4, 4] A_eq = [[0], [-8], [9]] b_eq = [3, 2, 10] with suppress_warnings() as sup: sup.filter(RuntimeWarning) sup.filter(OptimizeWarning, "Solving system with option...") o = {key: self.options[key] for key in self.options} o["presolve"] = False res = linprog(c, A_ub, b_ub, A_eq, b_eq, options=o, method=self.method) assert_(not res.success, "incorrectly reported success")
Example #9
Source File: test_linprog.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_alternate_initial_point(self): # Test with a rather large problem (400 variables, # 40 constraints) generated by https://gist.github.com/denis-bz/8647461 # use "improved" initial point A, b, c = lpgen_2d(20, 20) with suppress_warnings() as sup: sup.filter(RuntimeWarning, "scipy.linalg.solve\nIll...") sup.filter(OptimizeWarning, "Solving system with option...") res = linprog(c, A_ub=A, b_ub=b, method=self.method, options={"ip": True, "disp": True}) # ip code is independent of sparse/dense _assert_success(res, desired_fun=-64.049494229)
Example #10
Source File: test_linprog.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_magic_square_sparse_no_presolve(self): # test linprog with a problem with a rank-deficient A_eq matrix A, b, c, N = magic_square(3) with suppress_warnings() as sup: sup.filter(MatrixRankWarning, "Matrix is exactly singular") sup.filter(OptimizeWarning, "Solving system with option...") o = {key: self.options[key] for key in self.options} o["presolve"] = False res = linprog(c, A_eq=A, b_eq=b, bounds=(0, 1), options=o, method=self.method) _assert_success(res, desired_fun=1.730550597)
Example #11
Source File: test_linprog.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_sparse_solve_options(self): A, b, c, N = magic_square(3) with suppress_warnings() as sup: sup.filter(OptimizeWarning, "A_eq does not appear...") sup.filter(OptimizeWarning, "Invalid permc_spec option") o = {key: self.options[key] for key in self.options} permc_specs = ('NATURAL', 'MMD_ATA', 'MMD_AT_PLUS_A', 'COLAMD', 'ekki-ekki-ekki') for permc_spec in permc_specs: o["permc_spec"] = permc_spec res = linprog(c, A_eq=A, b_eq=b, bounds=(0, 1), method=self.method, options=o) _assert_success(res, desired_fun=1.730550597)
Example #12
Source File: test_minpack.py From GraphicDesignPatternByPython with MIT License | 4 votes |
def test_pcov(self): xdata = np.array([0, 1, 2, 3, 4, 5]) ydata = np.array([1, 1, 5, 7, 8, 12]) sigma = np.array([1, 2, 1, 2, 1, 2]) def f(x, a, b): return a*x + b for method in ['lm', 'trf', 'dogbox']: popt, pcov = curve_fit(f, xdata, ydata, p0=[2, 0], sigma=sigma, method=method) perr_scaled = np.sqrt(np.diag(pcov)) assert_allclose(perr_scaled, [0.20659803, 0.57204404], rtol=1e-3) popt, pcov = curve_fit(f, xdata, ydata, p0=[2, 0], sigma=3*sigma, method=method) perr_scaled = np.sqrt(np.diag(pcov)) assert_allclose(perr_scaled, [0.20659803, 0.57204404], rtol=1e-3) popt, pcov = curve_fit(f, xdata, ydata, p0=[2, 0], sigma=sigma, absolute_sigma=True, method=method) perr = np.sqrt(np.diag(pcov)) assert_allclose(perr, [0.30714756, 0.85045308], rtol=1e-3) popt, pcov = curve_fit(f, xdata, ydata, p0=[2, 0], sigma=3*sigma, absolute_sigma=True, method=method) perr = np.sqrt(np.diag(pcov)) assert_allclose(perr, [3*0.30714756, 3*0.85045308], rtol=1e-3) # infinite variances def f_flat(x, a, b): return a*x pcov_expected = np.array([np.inf]*4).reshape(2, 2) with suppress_warnings() as sup: sup.filter(OptimizeWarning, "Covariance of the parameters could not be estimated") popt, pcov = curve_fit(f_flat, xdata, ydata, p0=[2, 0], sigma=sigma) popt1, pcov1 = curve_fit(f, xdata[:2], ydata[:2], p0=[2, 0]) assert_(pcov.shape == (2, 2)) assert_array_equal(pcov, pcov_expected) assert_(pcov1.shape == (2, 2)) assert_array_equal(pcov1, pcov_expected)
Example #13
Source File: elastic.py From Elastic with GNU General Public License v3.0 | 4 votes |
def get_BM_EOS(cryst, systems): """Calculate Birch-Murnaghan Equation of State for the crystal. The B-M equation of state is defined by: .. math:: P(V)= \\frac{B_0}{B'_0}\\left[ \\left({\\frac{V}{V_0}}\\right)^{-B'_0} - 1 \\right] It's coefficients are estimated using n single-point structures ganerated from the crystal (cryst) by the scan_volumes function between two relative volumes. The BM EOS is fitted to the computed points by least squares method. The returned value is a list of fitted parameters: :math:`V_0, B_0, B_0'` if the fit succeded. If the fitting fails the ``RuntimeError('Calculation failed')`` is raised. The data from the calculation and fit is stored in the bm_eos and pv members of cryst for future reference. You have to provide properly optimized structures in cryst and systems list. :param cryst: Atoms object, basic structure :param systems: A list of calculated structures :returns: tuple of EOS parameters :math:`V_0, B_0, B_0'`. """ pvdat = array([[r.get_volume(), get_pressure(r.get_stress()), norm(r.get_cell()[:, 0]), norm(r.get_cell()[:, 1]), norm(r.get_cell()[:, 2])] for r in systems]).T # Estimate the initial guess assuming b0p=1 # Limiting volumes v1 = min(pvdat[0]) v2 = max(pvdat[0]) # The pressure is falling with the growing volume p2 = min(pvdat[1]) p1 = max(pvdat[1]) b0 = (p1*v1-p2*v2)/(v2-v1) v0 = v1*(p1+b0)/b0 # Initial guess p0 = [v0, b0, 1] # Fitting try : p1, succ = optimize.curve_fit(BMEOS, pvdat[0], pvdat[1], p0) except (ValueError, RuntimeError, optimize.OptimizeWarning) as ex: raise RuntimeError('Calculation failed') cryst.bm_eos = p1 cryst.pv = pvdat return cryst.bm_eos
Example #14
Source File: interp.py From sporco with BSD 3-Clause "New" or "Revised" License | 4 votes |
def lstabsdev(A, b): r"""Least absolute deviations (LAD) linear regression. Solve the linear regression problem .. math:: \mathrm{argmin}_\mathbf{x} \; \left\| A \mathbf{x} - \mathbf{b} \right\|_1 \;\;. The interface is similar to that of :func:`numpy.linalg.lstsq` in that `np.linalg.lstsq(A, b)` solves the same linear regression problem, but with a least squares rather than a least absolute deviations objective. Unlike :func:`numpy.linalg.lstsq`, `b` is required to be a 1-d array. The solution is obtained via `mapping to a linear program <https://stats.stackexchange.com/a/12564>`__. Parameters ---------- A : (M, N) array_like Regression coefficient matrix b : (M,) array_like Regression ordinate / dependent variable Returns ------- x : (N,) ndarray Least absolute deviations solution """ M, N = A.shape c = np.zeros((M + N,)) c[0:M] = 1.0 I = np.identity(M) A_ub = np.hstack((np.vstack((-I, -I)), np.vstack((-A, A)))) b_ub = np.hstack((-b, b)) with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=sco.OptimizeWarning) res = sco.linprog(c, A_ub, b_ub) if res.success is False: raise ValueError('scipy.optimize.linprog failed with status %d' % res.status) return res.x[M:]
Example #15
Source File: interp.py From sporco with BSD 3-Clause "New" or "Revised" License | 4 votes |
def lstmaxdev(A, b): r"""Least maximum deviation (least maximum error) linear regression. Solve the linear regression problem .. math:: \mathrm{argmin}_\mathbf{x} \; \left\| A \mathbf{x} - \mathbf{b} \right\|_{\infty} \;\;. The interface is similar to that of :func:`numpy.linalg.lstsq` in that `np.linalg.lstsq(A, b)` solves the same linear regression problem, but with a least squares rather than a least maximum error objective. Unlike :func:`numpy.linalg.lstsq`, `b` is required to be a 1-d array. The solution is obtained via `mapping to a linear program <https://stats.stackexchange.com/a/12564>`__. Parameters ---------- A : (M, N) array_like Regression coefficient matrix b : (M,) array_like Regression ordinate / dependent variable Returns ------- x : (N,) ndarray Least maximum deviation solution """ M, N = A.shape c = np.zeros((N + 1,)) c[0] = 1.0 one = np.ones((M, 1)) A_ub = np.hstack((np.vstack((-one, -one)), np.vstack((-A, A)))) b_ub = np.hstack((-b, b)) with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=sco.OptimizeWarning) res = sco.linprog(c, A_ub, b_ub) if res.success is False: raise ValueError('scipy.optimize.linprog failed with status %d' % res.status) return res.x[1:]