本文整理汇总了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.])