当前位置: 首页>>代码示例>>Python>>正文


Python DifferentialEvolutionSolver._constraint_violation_fn方法代码示例

本文整理汇总了Python中scipy.optimize._differentialevolution.DifferentialEvolutionSolver._constraint_violation_fn方法的典型用法代码示例。如果您正苦于以下问题:Python DifferentialEvolutionSolver._constraint_violation_fn方法的具体用法?Python DifferentialEvolutionSolver._constraint_violation_fn怎么用?Python DifferentialEvolutionSolver._constraint_violation_fn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在scipy.optimize._differentialevolution.DifferentialEvolutionSolver的用法示例。


在下文中一共展示了DifferentialEvolutionSolver._constraint_violation_fn方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_constraint_violation_fn

# 需要导入模块: from scipy.optimize._differentialevolution import DifferentialEvolutionSolver [as 别名]
# 或者: from scipy.optimize._differentialevolution.DifferentialEvolutionSolver import _constraint_violation_fn [as 别名]
    def test_constraint_violation_fn(self):
        def constr_f(x):
            return [x[0] + x[1]]

        def constr_f2(x):
            return [x[0]**2 + x[1], x[0] - x[1]]

        nlc = NonlinearConstraint(constr_f, -np.inf, 1.9)

        solver = DifferentialEvolutionSolver(rosen, [(0, 2), (0, 2)],
                                             constraints=(nlc))

        cv = solver._constraint_violation_fn([1.0, 1.0])
        assert_almost_equal(cv, 0.1)

        nlc2 = NonlinearConstraint(constr_f2, -np.inf, 1.8)
        solver = DifferentialEvolutionSolver(rosen, [(0, 2), (0, 2)],
                                             constraints=(nlc, nlc2))

        # for multiple constraints the constraint violations should
        # be concatenated.
        cv = solver._constraint_violation_fn([1.2, 1.])
        assert_almost_equal(cv, [0.3, 0.64, 0])

        cv = solver._constraint_violation_fn([2., 2.])
        assert_almost_equal(cv, [2.1, 4.2, 0])

        # should accept valid values
        cv = solver._constraint_violation_fn([0.5, 0.5])
        assert_almost_equal(cv, [0., 0., 0.])
开发者ID:anntzer,项目名称:scipy,代码行数:32,代码来源:test__differential_evolution.py


注:本文中的scipy.optimize._differentialevolution.DifferentialEvolutionSolver._constraint_violation_fn方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。