當前位置: 首頁>>代碼示例>>Python>>正文


Python optimize.basinhopping方法代碼示例

本文整理匯總了Python中scipy.optimize.basinhopping方法的典型用法代碼示例。如果您正苦於以下問題:Python optimize.basinhopping方法的具體用法?Python optimize.basinhopping怎麽用?Python optimize.basinhopping使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在scipy.optimize的用法示例。


在下文中一共展示了optimize.basinhopping方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_TypeError

# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import basinhopping [as 別名]
def test_TypeError(self):
        # test the TypeErrors are raised on bad input
        i = 1
        # if take_step is passed, it must be callable
        self.assertRaises(TypeError, basinhopping, func2d, self.x0[i],
                          take_step=1)
        # if accept_test is passed, it must be callable
        self.assertRaises(TypeError, basinhopping, func2d, self.x0[i],
                          accept_test=1)
        # accept_test must return bool or string "force_accept"

        def bad_accept_test1(*args, **kwargs):
            return 1

        def bad_accept_test2(*args, **kwargs):
            return "not force_accept"
        self.assertRaises(ValueError, basinhopping, func2d, self.x0[i],
                          minimizer_kwargs=self.kwargs,
                          accept_test=bad_accept_test1)
        self.assertRaises(ValueError, basinhopping, func2d, self.x0[i],
                          minimizer_kwargs=self.kwargs,
                          accept_test=bad_accept_test2) 
開發者ID:ktraunmueller,項目名稱:Computable,代碼行數:24,代碼來源:test__basinhopping.py

示例2: test_jac

# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import basinhopping [as 別名]
def test_jac(self):
        # test jacobian returned
        minimizer_kwargs = self.kwargs.copy()
        # BFGS returns a Jacobian
        minimizer_kwargs["method"] = "BFGS"

        res = basinhopping(func2d_easyderiv, [0.0, 0.0],
                           minimizer_kwargs=minimizer_kwargs, niter=self.niter,
                           disp=self.disp)

        assert_(hasattr(res.lowest_optimization_result, "jac"))

        # in this case, the jacobian is just [df/dx, df/dy]
        _, jacobian = func2d_easyderiv(res.x)
        assert_almost_equal(res.lowest_optimization_result.jac, jacobian,
                            self.tol) 
開發者ID:Relph1119,項目名稱:GraphicDesignPatternByPython,代碼行數:18,代碼來源:test__basinhopping.py

示例3: test_all_nograd_minimizers

# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import basinhopping [as 別名]
def test_all_nograd_minimizers(self):
        # test 2d minimizations without gradient.  Newton-CG requires jac=True,
        # so not included here.
        i = 1
        methods = ['CG', 'BFGS', 'L-BFGS-B', 'TNC', 'SLSQP',
                   'Nelder-Mead', 'Powell', 'COBYLA']
        minimizer_kwargs = copy.copy(self.kwargs_nograd)
        for method in methods:
            minimizer_kwargs["method"] = method
            res = basinhopping(func2d_nograd, self.x0[i],
                               minimizer_kwargs=minimizer_kwargs,
                               niter=self.niter, disp=self.disp)
            tol = self.tol
            if method == 'COBYLA':
                tol = 2
            assert_almost_equal(res.x, self.sol[i], decimal=tol) 
開發者ID:Relph1119,項目名稱:GraphicDesignPatternByPython,代碼行數:18,代碼來源:test__basinhopping.py

示例4: test_seed_reproducibility

# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import basinhopping [as 別名]
def test_seed_reproducibility(self):
        # seed should ensure reproducibility between runs
        minimizer_kwargs = {"method": "L-BFGS-B", "jac": True}

        f_1 = []

        def callback(x, f, accepted):
            f_1.append(f)

        basinhopping(func2d, [1.0, 1.0], minimizer_kwargs=minimizer_kwargs,
                     niter=10, callback=callback, seed=10)

        f_2 = []

        def callback2(x, f, accepted):
            f_2.append(f)

        basinhopping(func2d, [1.0, 1.0], minimizer_kwargs=minimizer_kwargs,
                     niter=10, callback=callback2, seed=10)
        assert_equal(np.array(f_1), np.array(f_2)) 
開發者ID:Relph1119,項目名稱:GraphicDesignPatternByPython,代碼行數:22,代碼來源:test__basinhopping.py

示例5: find_assignment

# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import basinhopping [as 別名]
def find_assignment(variable_handler, atoms, max_num_resets, tol, verbose=True):
    init = variable_handler.dict_to_vector()

    def func(vector):
        return sum(evaluate(atom, variable_handler.vector_to_dict(vector)).norm for atom in atoms)

    xs = []
    fs = []
    options = {'ftol': tol**2}
    minimizer_kwargs = {"method": "SLSQP", "options": options}
    for i in range(max_num_resets):
        result = basinhopping(func, init, minimizer_kwargs=minimizer_kwargs)
        if verbose:
            print("iteration %d:" % (i+1))
            print(result)
        xs.append(result.x)
        fs.append(result.fun)
        if result.fun < tol:
            break
        init = np.random.rand(len(init))

    min_idx = min(enumerate(fs), key=lambda pair: pair[1])[0]
    assignment = variable_handler.vector_to_dict(xs[min_idx])
    norm = fs[min_idx]
    return assignment, norm 
開發者ID:uwnlp,項目名稱:geosolver,代碼行數:27,代碼來源:numeric_solver.py

示例6: best_eer

# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import basinhopping [as 別名]
def best_eer(true_labels, predictions):

    def f_neg(threshold):
        ## Scipy tries to minimize the function
        return compute_eer(true_labels, predictions >= threshold)

    # Initialization of best threshold search
    thr_0 = [0.20] * 1 # binary class
    constraints = [(0.,1.)] * 1 # binary class
    def bounds(**kwargs):
        x = kwargs["x_new"]
        tmax = bool(np.all(x <= 1))
        tmin = bool(np.all(x >= 0))
        return tmax and tmin

    # Search using L-BFGS-B, the epsilon step must be big otherwise there is no gradient
    minimizer_kwargs = {"method": "L-BFGS-B",
                        "bounds":constraints,
                        "options":{
                            "eps": 0.05
                            }
                       }

    # We combine L-BFGS-B with Basinhopping for stochastic search with random steps
    logger.info("===> Searching optimal threshold for each label")
    start_time = timer()

    opt_output = basinhopping(f_neg, thr_0,
                                stepsize = 0.1,
                                minimizer_kwargs=minimizer_kwargs,
                                niter=10,
                                accept_test=bounds)

    end_time = timer()
    logger.info("===> Optimal threshold for each label:\n{}".format(opt_output.x))
    logger.info("Threshold found in: %s seconds" % (end_time - start_time))

    score = opt_output.fun
    return score, opt_output.x 
開發者ID:jefflai108,項目名稱:Attentive-Filtering-Network,代碼行數:41,代碼來源:v1_validation.py

示例7: best_eer

# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import basinhopping [as 別名]
def best_eer(val_scores, utt2len, utt2label, key_list):
    
    def f_neg(threshold):
        ## Scipy tries to minimize the function
        return utt_eer(val_scores, utt2len, utt2label, key_list, threshold)
    
    # Initialization of best threshold search
    thr_0 = [0.20] * 1 # binary class
    constraints = [(0.,1.)] * 1 # binary class
    def bounds(**kwargs):
        x = kwargs["x_new"]
        tmax = bool(np.all(x <= 1))
        tmin = bool(np.all(x >= 0))
        return tmax and tmin

    # Search using L-BFGS-B, the epsilon step must be big otherwise there is no gradient
    minimizer_kwargs = {"method": "L-BFGS-B",
                        "bounds":constraints,
                        "options":{
                            "eps": 0.05
                            }
                       }

    # We combine L-BFGS-B with Basinhopping for stochastic search with random steps
    logger.info("===> Searching optimal threshold for each label")
    start_time = timer()

    opt_output = basinhopping(f_neg, thr_0,
                                stepsize = 0.1,
                                minimizer_kwargs=minimizer_kwargs,
                                niter=10,
                                accept_test=bounds)

    end_time = timer()
    logger.info("===> Optimal threshold for each label:\n{}".format(opt_output.x))
    logger.info("Threshold found in: %s seconds" % (end_time - start_time))

    score = opt_output.fun
    return score, opt_output.x 
開發者ID:jefflai108,項目名稱:Attentive-Filtering-Network,代碼行數:41,代碼來源:v2_validation.py

示例8: _fit_basinhopping

# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import basinhopping [as 別名]
def _fit_basinhopping(f, score, start_params, fargs, kwargs, disp=True,
                          maxiter=100, callback=None, retall=False,
                          full_output=True, hess=None):
    if not 'basinhopping' in vars(optimize):
        msg = 'basinhopping solver is not available, use e.g. bfgs instead!'
        raise ValueError(msg)

    from copy import copy
    kwargs = copy(kwargs)
    niter = kwargs.setdefault('niter', 100)
    niter_success = kwargs.setdefault('niter_success', None)
    T = kwargs.setdefault('T', 1.0)
    stepsize = kwargs.setdefault('stepsize', 0.5)
    interval = kwargs.setdefault('interval', 50)
    minimizer_kwargs = kwargs.get('minimizer', {})
    minimizer_kwargs['args'] = fargs
    minimizer_kwargs['jac'] = score
    method = minimizer_kwargs.get('method', None)
    if method and method != 'L-BFGS-B': # l_bfgs_b doesn't take a hessian
        minimizer_kwargs['hess'] = hess

    retvals = optimize.basinhopping(f, start_params,
                                    minimizer_kwargs=minimizer_kwargs,
                                    niter=niter, niter_success=niter_success,
                                    T=T, stepsize=stepsize, disp=disp,
                                    callback=callback, interval=interval)
    if full_output:
        xopt, fopt, niter, fcalls = map(lambda x : getattr(retvals, x),
                                        ['x', 'fun', 'nit', 'nfev'])
        converged = 'completed successfully' in retvals.message[0]
        retvals = {'fopt': fopt, 'iterations': niter,
                   'fcalls': fcalls, 'converged': converged}

    else:
        xopt = retvals.x
        retvals = None

    return xopt, retvals 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:40,代碼來源:optimizer.py

示例9: setup_class

# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import basinhopping [as 別名]
def setup_class(cls):
        if not has_basinhopping:
            raise SkipTest("Skipped TestProbitBasinhopping since"
                           " basinhopping solver is not available")
        data = sm.datasets.spector.load()
        data.exog = sm.add_constant(data.exog, prepend=False)
        res2 = Spector()
        res2.probit()
        cls.res2 = res2
        fit = Probit(data.endog, data.exog).fit
        cls.res1 = fit(method="basinhopping", disp=0, niter=5,
                        minimizer={'method' : 'L-BFGS-B', 'tol' : 1e-8}) 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:14,代碼來源:test_discrete.py

示例10: max

# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import basinhopping [as 別名]
def max(self):
        # use method L-BFGS-B because the problem is smooth and bounded
        minimizer_kwargs = dict(method="L-BFGS-B", bounds=[(self.mus.min(), self.mus.max())])
        f = lambda x: -self.predict(x)
        res = basinhopping(f, self.mus.mean(), minimizer_kwargs=minimizer_kwargs)
        return res.x 
開發者ID:yihui-he,項目名稱:KL-Loss,代碼行數:8,代碼來源:gmm.py

示例11: test_1d_grad

# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import basinhopping [as 別名]
def test_1d_grad(self):
        # test 1d minimizations with gradient
        i = 0
        res = basinhopping(func1d, self.x0[i], minimizer_kwargs=self.kwargs,
                           niter=self.niter, disp=self.disp)
        assert_almost_equal(res.x, self.sol[i], self.tol) 
開發者ID:ktraunmueller,項目名稱:Computable,代碼行數:8,代碼來源:test__basinhopping.py

示例12: test_2d

# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import basinhopping [as 別名]
def test_2d(self):
        # test 2d minimizations with gradient
        i = 1
        res = basinhopping(func2d, self.x0[i], minimizer_kwargs=self.kwargs,
                           niter=self.niter, disp=self.disp)
        assert_almost_equal(res.x, self.sol[i], self.tol)
        self.assertTrue(res.nfev > 0) 
開發者ID:ktraunmueller,項目名稱:Computable,代碼行數:9,代碼來源:test__basinhopping.py

示例13: test_njev

# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import basinhopping [as 別名]
def test_njev(self):
        # test njev is returned correctly
        i = 1
        minimizer_kwargs = self.kwargs.copy()
        # L-BFGS-B doesn't use njev, but BFGS does
        minimizer_kwargs["method"] = "BFGS"
        res = basinhopping(func2d, self.x0[i],
                           minimizer_kwargs=minimizer_kwargs, niter=self.niter,
                           disp=self.disp)
        self.assertTrue(res.nfev > 0)
        self.assertEqual(res.nfev, res.njev) 
開發者ID:ktraunmueller,項目名稱:Computable,代碼行數:13,代碼來源:test__basinhopping.py

示例14: test_2d_nograd

# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import basinhopping [as 別名]
def test_2d_nograd(self):
        # test 2d minimizations without gradient
        i = 1
        res = basinhopping(func2d_nograd, self.x0[i],
                           minimizer_kwargs=self.kwargs_nograd,
                           niter=self.niter, disp=self.disp)
        assert_almost_equal(res.x, self.sol[i], self.tol) 
開發者ID:ktraunmueller,項目名稱:Computable,代碼行數:9,代碼來源:test__basinhopping.py

示例15: test_pass_takestep

# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import basinhopping [as 別名]
def test_pass_takestep(self):
        # test that passing a custom takestep works
        # also test that the stepsize is being adjusted
        takestep = MyTakeStep1()
        initial_step_size = takestep.stepsize
        i = 1
        res = basinhopping(func2d, self.x0[i], minimizer_kwargs=self.kwargs,
                           niter=self.niter, disp=self.disp,
                           take_step=takestep)
        assert_almost_equal(res.x, self.sol[i], self.tol)
        assert_(takestep.been_called)
        # make sure that the built in adaptive step size has been used
        assert_(initial_step_size != takestep.stepsize) 
開發者ID:ktraunmueller,項目名稱:Computable,代碼行數:15,代碼來源:test__basinhopping.py


注:本文中的scipy.optimize.basinhopping方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。