使用python的扩展Raftery Markov链函数最小化

时间:2018-09-01 04:14:09

标签: python numpy scipy markov-chains markov-models

我正在研究扩展的Raftery模型,这是一个更通用的高阶马尔可夫链模型,因为我需要解决以下带有某些约束的线性规划模型。

以下是(链接)线性编程功能,需要最小化:

受制于:

要在方程中求解矢量“ W”和“λ”。

Q和X分别是i步跃迁概率矩阵和稳态概率。

以下是我正在使用的示例:

import numpy as np

one_step_array = np.array([[0.12, 0.75, 0.12],
       [0.42, 0.14, 0.42],
       [0.75, 0.25, 0.0]])

two_step_array = np.array([[0.43, 0.23, 0.33],
       [0.43, 0.44, 0.11],
       [0.20, 0.59, 0.20]])

steady_state = np.array([0.38, 0.39, 0.21])

Q_Arr = np.vstack((np.matmul(one_step_array,steady_state),np.matmul(two_step_array,steady_state))).transpose()

from pulp import *

w1 = LpVariable("w1",0,None)
w2 = LpVariable("w2",0,None)
w3 = LpVariable("W3",0, None)
L1 = LpVariable("L1",0,None)
L2 = LpVariable("L2",0,None)

prob = LpProblem("Problem",LpMinimize)

prob += w1 >= steady_state[0] - Q_Arr[0][0]*L1 - Q_Arr[0][1]*L2
prob += w1 >= -steady_state[0] + Q_Arr[0][0]*L1 + Q_Arr[0][1]*L2

prob += w2 >= steady_state[1] - Q_Arr[1][0]*L1 - Q_Arr[1][1]*L2
prob += w2 >= -steady_state[1] + Q_Arr[1][0]*L1 + Q_Arr[1][1]*L2

prob += w3 >= steady_state[2] - Q_Arr[2][0]*L1 - Q_Arr[2][1]*L2
prob += w3 >= -steady_state[2] + Q_Arr[2][0]*L1 + Q_Arr[2][1]*L2

prob += w1 >= 0
prob += w2 >= 0
prob += w3 >= 0
prob += L1 >= 0
prob += L2 >= 0

prob += L1 + L2 == 1

prob += w1+w2+w3

status = prob.solve(GLPK(msg=0))
LpStatus[status]

print (value(w1))
print (value(w2))
print (value(w3))
print (value(L1))
print (value(L2))

结果是(λ1,λ2,w1,w2,w3)=(1,0,0.051,0.027,0.14)而不是(1,0,0.028,0.0071,0.0214)这是不正确的。

能否让我知道我要去哪里错了?

1 个答案:

答案 0 :(得分:0)

感谢您的审核和帮助!我自己回答了这个问题。解决方法如下:

from pulp import *


Weight_vec = []
Number_of_states = Q_Arr.shape[0]
for x in range(Number_of_states):
    Weight_vec.append('w'+str(x+1))

L1 = LpVariable("L1",0,100)
L2 = LpVariable("L2",0,100)

prob = LpProblem("Problem",LpMinimize)

for s in range(Number_of_states):
    Weight_vec[s] = LpVariable('w'+str(s+1),0,None)
count = 0

for row in Q_Arr:
    prob += steady_state[0] - row[0]*L1 - row[1]*L2 - Weight_vec[count] <= 0
    print (steady_state[0] - row[0]*L1 - row[1]*L2 - Weight_vec[count] <= 0)
    prob += - steady_state[0] + row[0]*L1 + row[1]*L2 - Weight_vec[count] <= 0
    print (- steady_state[0] + row[0]*L1 + row[1]*L2 - Weight_vec[count] <= 0)
    count = count + 1

prob += L1 >= 0
prob += L2 >= 0

prob += L1 + L2 == 1

for s in range(Number_of_states):
    prob += Weight_vec[s] >= 0

#objective
prob += sum(Weight_vec)

status = prob.solve(GLPK(msg=0))
LpStatus[status]

result = []

for s in range(Number_of_states):
    result.append(value(Weight_vec[s]))
result.append(value(L1))
result.append(value(L2))

print (result)