简体   繁体   English

为什么scipy.optimize.linprog无法解决一个简单的问题

[英]Why scipy.optimize.linprog can't solve a simple problem

I need to write a function using scipy.optimize.linprog to solve a 3x3 matrix to find Nash equilibrium. 我需要使用scipy.optimize.linprog编写一个函数来解决3x3矩阵以找到Nash均衡。

The problem is defined as: 问题定义为:
1- x_i is the probability to select a row. 1- x_i是选择行的概率。
2- The column payoff is the the product of x_i with the corresponding value under col_j.(For example, the payoff of col_1 = 0 * x_1 - 1 * x_2 + 1 * x_3) 2-列支付是x_i与col_j下相应值的乘积(例如,col_1的支付= 0 * x_1 - 1 * x_2 + 1 * x_3)

This is a linear program problem and defined as below: 这是一个线性程序问题,定义如下:

 #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 

The solution should be: [0.33333, 0.33333, 0.33333] 解决方案应该是:[0.33333,0.33333,0.33333]

but when I run my code I get the error below: 但是当我运行我的代码时,我得到以下错误:

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

Below is my function and I don't know why it fails 下面是我的功能,我不知道它失败的原因

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

Your matrix A_eq and the vector b_eq are wrong. 您的矩阵A_eq和向量b_eq是错误的。 According to your optimization problem it should be: 根据您的优化问题,它应该是:

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.])

instead of 代替

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.])

Changing your function to 将您的功能更改为

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

gives me: 给我:

     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])

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

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