基于python的扩展Raftery-Markov链函数极小化

2024-06-14 08:12:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究扩展的Raftery模型,这是一个更一般的高阶Markov链模型,因为我需要求解下面的具有一定约束的线性规划模型。你知道吗

以下是需要最小化的(链接)线性规划函数:

受制于:

其中向量“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),这是不正确的。你知道吗

你能告诉我哪里出了问题吗?你知道吗


Tags: nonel1valuenparrayw1stateprint
1条回答
网友
1楼 · 发布于 2024-06-14 08:12:53

感谢您的评论和帮助!我自己能回答这个问题。以下是解决方案:

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)

相关问题 更多 >