繁体   English   中英

约束在使用 Scipy 的优化中不起作用

[英]Constraints not working in Optimization using Scipy

我正在尝试解决利润最大化问题。 该公司有一些促销计划。 我制定了一个受一些约束的利润最大化目标函数。 我想做一个约束,说明公司不能同时运行 2 个以上的促销计划。 但是这种情况不起作用

这是我的代码:

from scipy.optimize import minimize
from math import floor

#Objective function
def objective(x):
    return -((-14.12*x[0]+1*floor(x[1])+2*floor(x[2])+4*floor(x[3]))*x[0] \ #total revenue (qty*price)
            -(-14.12*x[0]+1*floor(x[1])+2*floor(x[2])+4*floor(x[3]))*10000*0.05\ #Gold price cost
            -(-14.12*x[0]+1*floor(x[1])+2*floor(x[2])+4*floor(x[3]))*x[0]*0.1\ #Rebate cost
            -(-14.12*x[0]+1*floor(x[1])+2*floor(x[2])+4*floor(x[3])/15000*100000)) 

#Points constraint

#Constraints
def PriceConstraint(x):
    return x[0]-3000

def GoldCoinConstraint(x):
    return floor(x[1])-1

def PointsConstraint(x):
    return floor(x[2])-1

def ProgressiveRebateConstraint(x):
    return floor(x[3])-1

def CombinationConstraint(x):
    return 2-floor(x[1])+floor(x[2])+floor(x[3])

#Initial guesses
n=5
x0=np.zeros(5)
x0[0]=1
x0[1]=2
x0[2]=2
x0[3]=1
x0[4]=3

# show initial objective
print('Initial Objective: ' + str(objective(x0)))

# optimize
b = (0.0,1.0)
pricebound = (1,3000)
bnds = (pricebound, b, b, b,b)
con1= {'type':'ineq','fun':PriceConstraint}
con2= {'type':'ineq','fun':GoldCoinConstraint}
con3= {'type':'ineq','fun':PointsConstraint}
con4= {'type':'ineq','fun':ProgressiveRebateConstraint}
con5= {'type':'ineq','fun':CombinationConstraint}

cons = ([con1, con2, con3, con4, con5])

solution = minimize(objective,x0,method='SLSQP',\
                    bounds=bnds,constraints=cons)

x = solution.x

# show final objective
print('Final Objective: ' + str(objective(x)))

# print solution
print('Solution')
print('x1 = ' + str(x[0]))
print('x2 = ' + str(x[1]))
print('x3 = ' + str(x[2]))
print('x4 = ' + str(x[3]))

如您所见,我使用组合约束函数设置了营销方案数量不应超过 2 个的约束。 不知何故,它似​​乎不起作用? 我得到 x[1]、x[2] 和 x[3] 的输出为 1。

有人可以帮助我为什么这不起作用吗?

同样在不同的思路中,非线性优化中有类似影子价格的东西。 我知道它存在于线性规划中,但不确定非线性?

2-floor(x[1])+floor(x[2])+floor(x[3])

应该读

2-(floor(x[1])+floor(x[2])+floor(x[3]))

那并没有真正帮助你。 当你运行它时,你可能会得到类似的东西:

     fun: -976376.097853337
     jac: array([567.265625,   0.      ,   0.      ,   0.      ,   0.      ])
 message: 'Positive directional derivative for linesearch'
    nfev: 32
     nit: 7
    njev: 3
  status: 8
 success: False
       x: array([300.9,   1. ,   1. ,   1. ,   1. ])

您的floor函数使问题不可微。 SLSQP 只喜欢平滑(即可微分)的问题。

我认为您的问题实际上是一个混合整数二次规划问题。 所以我建议不要使用连续 NLP 求解器来解决这个问题,而是使用 MIQP 求解器。 这样你就不需要floor功能。 不幸的是 scipy 不包含 MIQP 求解器。


问:非线性优化中是否有类似影子价格的东西?

答:是的,对于连续问题,大多数 NLP 求解器都可以提供对偶或影子价格(scipy 求解器不会这样做)。 对偶不适用于离散问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM