scipy linprog单纯形麻烦

时间:2019-06-02 15:17:30

标签: game-theory simplex

我正在尝试求解零和游戏,以找到玩家I的最佳概率分布。为此,我正在使用scipy linprog单纯形法。

我看过一个例子,我需要改造这个游戏:

G=np.array([
[ 0  2 -3  0]
[-2  0  0  3]
[ 3  0  0 -4]
[ 0 -3  4  0]])

进入线性优化问题:

Maximize           z
Subject to:               2*x2 - 3*x3        + z <= 0
                  -2*x1 +             + 3*x4 + z <= 0
                   3*x1 +             - 4*x4 + z <= 0
                        - 3*x2 + 4*x3        + z <= 0
with              x1 + x2 + x3 + x4 = 1

这是我的实际代码:

def simplex(G):
    (n,m) = np.shape(G)

    A_ub = np.transpose(G)
    # we add an artificial variable to maximize, present in all inequalities
    A_ub = np.append(A_ub, np.ones((m,1)), axis = 1)
    # all inequalities should be inferior to 0
    b_ub = np.zeros(m)

    # the sum of all variables except the artificial one should be equal to one
    A_eq = np.ones((1,n+1))
    A_eq[0][n] = 0
    b_eq = np.ones(1)

    c = np.zeros(n + 1)
    # -1 to maximize the artificial variable we're going to add
    c[n] = -1

    res = linprog(c, A_ub=A_ub, b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, bounds=(0,None))

    return (res.x[:-1], res.fun)

这是我得到的分布: [5.87042987e-01 1.77606350e-10 2.79082859e-10 4.12957014e-01] 总计不超过1,但我希望 [0 0.6 0.4 0]

我正在尝试使用6或7行(还有变量)的大型游戏,它甚至不等于1。我做错了什么?

感谢您可以提供的任何帮助。

2 个答案:

答案 0 :(得分:2)

(我假设播放器1(行播放器)正在最大化,播放器2(列播放器)正在最小化。)

在此游戏的纳什均衡中,玩家1的策略是任何[0, x2, x3, 0]4/7 <= x2 <= 3/5x2 + x3 = 1的游戏。

在您的代码中,您缺少不等式约束-G.T x + z <= 0的负号。 尝试以下代码:

def simplex(G, method='simplex'):
    (n,m) = np.shape(G)

    A_ub = -np.transpose(G)  # negative sign added
    # we add an artificial variable to maximize, present in all inequalities
    A_ub = np.append(A_ub, np.ones((m,1)), axis = 1)
    # all inequalities should be inferior to 0
    b_ub = np.zeros(m)

    # the sum of all variables except the artificial one should be equal to one
    A_eq = np.ones((1,n+1))
    A_eq[0][n] = 0
    b_eq = np.ones(1)

    c = np.zeros(n + 1)
    # -1 to maximize the artificial variable we're going to add
    c[n] = -1

    res = linprog(c, A_ub=A_ub, b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, bounds=(0,None),
                  method=method)  # `method` option added

    return (res.x[:-1], res.fun)

使用单纯形方法:

simplex(G, method='simplex')
(array([0.        , 0.57142857, 0.42857143, 0.        ]), 0.0)
# 4/7 = 0.5714285...

使用内点法:

simplex(G, method='interior-point')
(array([1.77606350e-10, 5.87042987e-01, 4.12957014e-01, 2.79082859e-10]),
 -9.369597151936987e-10)
# 4/7 < 5.87042987e-01 < 3/5

使用修改后的单纯形方法:

simplex(G, method='revised simplex')
(array([0. , 0.6, 0.4, 0. ]), 0.0)
# 3/5 = 0.6

(与SciPy v1.3.0一起运行)

答案 1 :(得分:0)

自从找到解决方案以来,我还没有更新过帖子。我建议不要使用Scipy linprog函数,如果您对线性编程了解不多,则记录不好,而且我发现它在许多示例中都不精确且不一致(而且我确实尝试添加负号,如oyamad)。

我切换到PuLP python库,并且从一开始就获得一致的结果没有问题。