优化输出在 python 中具有约束的输入

Optimizing input where output has constraints in python

我试图最小化非线性函数的输入(1 个变量),输出是概率。

例如,如果输入为 5,则输出概率为 40%,如果输入为 10,则概率变为 93%。计算概率的函数是确定性的。

现在我想最小化输入,使概率大于 80%。在 python 中使用 scipy 库是否有一种简单的方法来做到这一点?

以下有帮助吗?

import math
from scipy.optimize import minimize,NonlinearConstraint

def fmin(x):
# this is the function you are minimizing
# in your case this function just returns x
    return x

def fprob(x):
# this is the function defining your probability as a function of x
# this function is maximized at x=3, and its max value is 1
  return math.exp(-(x-3)*(x-3))

# these are your nonlinear constraints
# since you want to find input such that your probability is > 0.8
# the lower limit is 0.8. Since probabilty cannot be >1, upper limit is 1
nlc  = NonlinearConstraint(fprob,0.8,1)

# the zero is your initial guess
res = minimize(fmin,0,method='SLSQP',constraints=nlc)

# this is your answer
print(f'{res.x=}')