【问题标题】:Optimizing input where output has constraints in python在python中优化输出有约束的输入
【发布时间】:2020-10-02 20:00:41
【问题描述】:

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

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

现在我想最小化输入,使得概率大于 80% 。有没有使用 scipy 库在 python 中执行此操作的简单方法?

【问题讨论】:

    标签: python optimization scipy minimization nonlinear-functions


    【解决方案1】:

    以下有帮助吗?

    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=}')
    

    【讨论】:

      猜你喜欢
      • 2013-12-25
      • 2017-11-13
      • 2021-12-28
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 2014-11-18
      • 2019-05-22
      相关资源
      最近更新 更多