我正在尝试解决零和游戏,为玩家 I 找到最佳概率分布。为此,我使用 scipy linprog simplex 方法。
我看过一个例子,我需要改造这个游戏:
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.. 我做错了什么?
感谢您提供的任何帮助。