当前位置: 首页>>代码示例>>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;未经允许,请勿转载。