【问题标题】:Why scipy.optimize.linprog can't solve a simple problem为什么 scipy.optimize.linprog 不能解决一个简单的问题
【发布时间】:2019-08-28 12:04:33
【问题描述】:

我需要使用 scipy.optimize.linprog 编写一个函数来求解 3x3 矩阵以找到纳什均衡。

问题定义为:
1- x_i 是选择一行的概率。
2- 列payoff是x_i与col_j下对应值的乘积。(例如col_1的payoff = 0 * x_1 - 1 * x_2 + 1 * x_3)

这是一个线性规划问题,定义如下:

 #The matrix to solve  
    col_1 col_2 col_3  
x_1 [[0.0,  1.0, -1.0],  
x_2 [-1.0,  0.0,  1.0],   
x_3 [ 1.0, -1.0,  0.0]]

maximize rows payoff:      
    (0 + 1 -1)X_1 + (-1 + 0 + 1)X_2 + (1 - 1 + 0)X_3

subject to:                    
  0-x_2+x_3=x_1+0-x_3 ---> -x_1-x_2+2x_3=0 #Payoff col_1 = Payoff col_2    
  0-x_2+x_3=-x_1+x_2+0 ---> x_1-2x_2+x_3=0 #Payoff col_1 = Payoff col_3   
  0<=x_1<=1 #Probability bounds of x_1  
  0<=x_2<=1 #Probability bounds of x_2  
  0<=x_3<=1 #Probability bounds of x_3  
  x_1+x_2+x_3=1  #Sum of probabilities of all rows 

解应该是:[0.33333, 0.33333, 0.33333]

但是当我运行我的代码时,我收到以下错误:

 message: 'Optimization failed. Unable to find a feasible starting point.'
     nit: 2
  status: 2
 success: False
       x: nan

下面是我的函数,不知道为什么会失败

def solve_Mixed_NE_LP(X):
    num_of_rows = X.shape[0]
    num_of_columns = X.shape[1]
    c = np.sum(X, axis=1).T #objective to maximize
    b_eq = np.array([])
    A_eq = None
    bounds = []

    #Probabilities bounds: 0 <= x_i <= 1
    for i in range(num_of_rows):
        bounds.append((0.,1.))

    #Total rows selection probabilities must sum to 1
    b_eq = np.append(b_eq, np.array([1]))
    A_eq = np.array([[1 for i in range(num_of_rows)]]).T


    XT = X.T
    for i in range(1,num_of_columns):
        b_eq = np.append(b_eq, np.array([0]))
        constraint = XT[0,:] - XT[i,:]
        constraint = np.array([constraint]).T
        A_eq = np.hstack((A_eq, constraint))


    return optimize.linprog(c=c, A_ub=None, b_ub=None, A_eq=A_eq, b_eq=b_eq, bounds=bounds, method='simplex')

【问题讨论】:

    标签: python scipy mathematical-optimization linear-programming


    【解决方案1】:

    你的矩阵 A_eq 和向量 b_eq 是错误的。根据您的优化问题应该是:

    In [21]: A_eq                                                                                        
    Out[21]: 
    array([[-1, -1,  2],
           [ 1, -2,  1],
           [ 1,  1,  1]])
    In [22]: b_eq                                                                                        
    Out[22]: array([0., 0., 1.])
    

    而不是

    In [25]: A_eq                                                                                        
    Out[25]: 
    array([[ 1., -1.,  1.],
           [ 1., -1., -2.],
           [ 1.,  2.,  1.]])
    
    In [26]: b_eq                                                                                        
    Out[26]: array([1., 0., 0.])
    

    将您的功能更改为

    def solve_Mixed_NE_LP(X):
        num_of_rows = X.shape[0]
        num_of_columns = X.shape[1]
        c = np.sum(X, axis=1).T #objective to maximize
    
        #Probabilities bounds: 0 <= x_i <= 1
        bounds = [(0,1) for i in range(num_of_rows)]
    
        #Total rows selection probabilities must sum to 1
        A_eq = np.zeros(X.shape)
        b_eq = np.zeros(num_of_rows)
        A_eq[-1,:] = np.ones(num_of_columns)
        b_eq[-1] = 1
        for i in range(num_of_rows-1):
            A_eq[i, :] = X[:, 0] - X[:, i+1]
    
    
        return optimize.linprog(c=c, A_ub=None, b_ub=None, A_eq=A_eq, b_eq=b_eq, bounds=bounds, method='simplex')
    

    给我:

         con: array([0., 0., 0.])
         fun: 0.0
     message: 'Optimization terminated successfully.'
         nit: 6
       slack: array([], dtype=float64)
      status: 0
     success: True
           x: array([0.33333333, 0.33333333, 0.33333333])
    

    【讨论】:

    • 我删除了约束上的转置并使用 vstack 而不是 hstack 并且它有效。感谢代码提示
    猜你喜欢
    • 1970-01-01
    • 2013-02-03
    • 2022-07-27
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多