import pandas as pd
from pulp import LpProblem, LpVariable, lpSum, LpMinimize, LpStatus
# Sample hospital data
data1 = {
'Hospital_ID': range(1, 11),
'Cost_Per_Nurse_Hour': [50, 55, 60, 45, 52, 58, 53, 49, 56, 54],
'Cost_Per_Doctor_Hour': [5000, 4500, 4000, 5500, 4800, 5100, 4700, 4900, 5200, 4600],
'Doctor_Hours': [100, 110, 95, 105, 108, 115, 98, 102, 107, 103],
'Nurse_Hours': [200, 190, 180, 210, 195, 205, 198, 215, 190, 200],
'Total_Patients': [150, 160, 145, 155, 148, 162, 149, 153, 158, 146],
'Hospital_Revenue': [80000, 75000, 82000, 78000, 79000, 81000, 79000, 80500, 79500, 81500],
}
data = pd.DataFrame(data1)
# Create the optimization problem
prob = LpProblem("Maximize_Margin_Labor", LpMinimize)
# Define decision variables
nurse_hours_ = LpVariable("Nurse_Hours",lowBound=0, cat='Continuous')
doctor_hours_ = LpVariable("Doctor_Hours",lowBound=0, cat='Continuous')
# Objective function (minimize labor cost/ revenue ratio)
total_labor_cost = nurse_hours_ * lpSum(data.loc[i,"Cost_Per_Nurse_Hour"] for i in
data.index) + doctor_hours_ * lpSum(data.loc[i,"Cost_Per_Doctor_Hour"] for i in data.index)
margin_labor = total_labor_cost / lpSum(data["Hospital_Revenue"])*100
prob += margin_labor
prob +=nurse_hours_>=190# minimum nurse hours observed
prob +=nurse_hours_<=215# maximum nurse hours observed
prob+=doctor_hours_>=95# minimum doctor hours observed
prob+=doctor_hours_<=115# maximum doctor hours observed
prob +=nurse_hours_-2.1*doctor_hours_<=0 #2.1 maximum observed Nurse Hours/Doctor Hours
prob +=nurse_hours_-1.73*doctor_hours_>=0 #1.73 minimum observed Nurse Hours/Doctor Hours
# Solve the problem
status = prob.solve()
# Print the results
print("Solver status:", LpStatus[status])
optimal_nurse_hours = int(nurse_hours_.value())
optimal_doctor_hours = int(doctor_hours_.value())
# Calculate labor cost and labor cost/revenue ratio for the optimal solutions
total_labor_cost = optimal_nurse_hours * lpSum(data["Cost_Per_Nurse_Hour"]) +
optimal_doctor_hours * lpSum(data["Cost_Per_Doctor_Hour"])
print("Optimal Nurse Hours:", optimal_nurse_hours)#190 minimum nurse hours observed
print("Optimal Doctor Hours:", optimal_doctor_hours)#95 minimum doctor hours observed
print("Margin Labor Percentage:", margin_labor_percentage, "%")
此代码给出的最佳解决方案是最小观测值。我怎样才能避免这种情况?我预计至少有一个解决方案与最小观察值不同。还可以添加哪些其他约束?谢谢。
我认为您没有问您真正想问的问题。 我假设您正在尝试以某种方式调查医院的生产力,但要做到这一点,您必须计算“每劳动力收入”或“每名患者的收入”等内容。 看这个例子:
import pandas as pd
# Sample hospital data
data1 = {
'Hospital_ID': range(1, 11),
'Cost_Per_Nurse_Hour': [50, 55, 60, 45, 52, 58, 53, 49, 56, 54],
'Cost_Per_Doctor_Hour': [5000, 4500, 4000, 5500, 4800, 5100, 4700, 4900, 5200, 4600],
'Doctor_Hours': [100, 110, 95, 105, 108, 115, 98, 102, 107, 103],
'Nurse_Hours': [200, 190, 180, 210, 195, 205, 198, 215, 190, 200],
'Total_Patients': [150, 160, 145, 155, 148, 162, 149, 153, 158, 146],
'Hospital_Revenue': [80000, 75000, 82000, 78000, 79000, 81000, 79000, 80500, 79500, 81500],
}
for i in range(10):
labor_cost = \
data1['Cost_Per_Nurse_Hour'][i] * data1['Nurse_Hours'][i] + \
data1['Cost_Per_Doctor_Hour'][i] * data1['Doctor_Hours'][i]
revenue_per_labor = data1['Hospital_Revenue'][i] / labor_cost
revenue_per_patient = data1['Hospital_Revenue'][i] / data1['Total_Patients'][i]
print(i+1,revenue_per_labor,revenue_per_patient)
输出:
1 0.1568627450980392 533.3333333333334
2 0.14838262934019192 468.75
3 0.2098259979529171 565.5172413793103
4 0.132890365448505 503.2258064516129
5 0.1494683467665645 533.7837837837837
6 0.1353632246528184 500.0
7 0.16769477004589317 530.2013422818792
8 0.15773952403813182 526.1437908496732
9 0.1402017494356659 503.1645569620253
10 0.16817994222038796 558.2191780821918
这说明医院 3 获得了最佳的每工时收入(到目前为止),并且也获得了最佳的每名患者收入比率。 真正的生产力衡量标准可能会以某种方式将这些结合起来。
只有当您知道自己想学什么时,这些工具才能为您提供帮助。