线性优化会产生错误,但会提供正确的解决方案

时间:2019-07-11 11:31:25

标签: pandas optimization scipy

我有以下数据框:

Erlös   Kosten A    Kosten B
100     30  10
200     30  15
300     30  20
50      30  10
75      30  15
100     30  10
90      30  20

,我想借助Kosten A在目标列from scipy.optimize import linprog上进行线性优化 根据我的理解,linprog需要一个二维矩阵A,因此我定义了一个帮助向量x_1,目标向量x_0将替换列Kosten A

所以唯一的条件是每个行都有效:

df['Erlös']-df['Kosten A/x_0']-df['Kosten B'] >=0

x_0>=0 因此,我建立了以下优化:

def linear_optimization(data):
    #define c for x values: min -c *x
    c = [-len(df), 0]
    #define condition for x values Ax <= b
    b1 = df[['Erlös']].to_numpy()
    b2 = df[['Kosten B']].to_numpy()
    b = (b2-b1).T.tolist()[0]
    #define matrix A for condition
    A1 = np.array([-1]*len(df)).tolist()
    A2 = np.array([0]*len(df)).tolist()
    A = np.stack((A1, A2), axis=-1).tolist()
    #define condtions for x >=0
    x0_bounds = (0, None)
    x1_bounds = (0,0)
    #run linear optimization
    return linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds]), b, A, c

结果是:

fun: -1960.0
 message: 'Optimization failed. The problem appears to be unbounded.'
     nit: 8
   slack: array([190.,  95.,   0., 240., 220., 190., 210.,   0.])
  status: 3
 success: False
       x: array([280.,   0.])

向量x是正确的,但是为什么我会收到失败的按摩?

0 个答案:

没有答案