首页 文章

使用spicy.optimize.linprog进行线性编程

提问于
浏览
0

我正在尝试使用spicy.optimize.linprog函数来制定和解决线性编程问题 .

我想解决函数Ax = b受以下约束:

# A                      b   
-0.4866 x1 + 0.1632 x2 < 0  
 0.3211 x1 + 0.5485 x2 < 0  
-0.5670 x1 + 0.1099 x2 < 0   
-0.1070 x1 + 0.0545 x2 = 1   
-0.4379 x1 + 0.1465 x2 < 0   
 0.0220 x1 + 0.7960 x2 < 0  
-0.3673 x1 - 0.0494 x2 < 0

我输入了一个nx2矩阵 A 和nx1矩阵 b . 结果应该是向量x1和x2 . 这是我的输入数据 .

# Coefficients

A = [[-0.4866, 0.1632],   
     [0.3211, 0.5485],   
     [-0.5670, 0.1099],   
     [-0.1070, 0.0545],   
     [-0.4379, 0.1465],   
     [0.0220, 0.7960],  
     [-0.3673, -0.0494]]  

# Inequalities

b = [0, 0, 0, -1, 0, 0, 0]

我认为我的问题是如何制定 c ,将函数最小化以输入 linprog 函数 .

res = linprog(c, A, b)

1 回答

  • 0

    我能够通过以下方式解决上限:

    c = [1, 0, 0]
    
    A = [[-1, -0.486, 0.1632],
         [-1, 0.3211, 0.5485], 
         [-1, -0.5670, 0.1099], 
         [-1, -0.1070, 0.0545], 
         [-1, -0.4379, 0.1465], 
         [-1, 0.220, 0.7960],
         [-1,-0.3673, -0.0494]]
    
    b = [0, 0, 0, -1, 0, 0, 0]
    
    c0_bounds = (0, 1)
    x1_bounds = (-np.inf, np.inf)
    x2_bounds = (-np.inf, np.inf)
    
    res = linprog(c, A_ub=A, b_ub=b, bounds=(c0_bounds, x1_bounds, x2_bounds), options={"disp": True})
    

    但是现在如果我也想解决 lower bounds 怎么办?

相关问题