我试图找到最大化我的和值的最佳组合,但它必须在两个特定的约束下,因此我假设线性规划将是最合适的
问题是这样的: 一些教育界活动希望聚集世界上最聪明的青少年学生。 每个州都对10万名学生进行了以下考试:“数学”、“英语”、“计算机”、“历史”、“物理”。。每次考试的分数都是0-100
每个州都被要求从接受测试的10万名学生中选出最好的10万名参加此次活动
您作为法国代表,被要求从您所在国家经过测试的10万名学生中选出前10万名学生。为此,您需要优化它们的最大值,以获得尽可能最好的总分
但有两个主要限制因素:
1-从总共10万名被选中的学生中,您需要分配特定的学生,这些学生将仅在上述5个科目中的1个特定科目上接受测试。 所需的分配是:[“数学”:4000,“英语”:3000,“计算机”:2000,“历史”:750,“物理”:250]
2-每个“考试科目”分数的权重必须不同。。举例来说:数学在历史上的价值超过97。 考题是:[“数学”:1.9,“英语”:1.7,“计算机”:1.5,“历史”:1.3,“物理”:1.1]
我的解决方案: 我试着将pulph(python)用作LP库,并正确地解决了这个问题,但运行了2个多小时。 你能找到更好(更快、更简单)的解决方法吗? 有一些NUMPY LP函数可以替代,也许会更快? 这应该是一个简单的优化问题,因为我把它做得太慢太复杂了。 --解决方案只需使用Python,请
例如,让我们从小范围来看同一个问题: 共有30名学生,您只需选择15名学生,就以下科目分配需求而言,这将为我们提供最佳组合。 所需的分配为-[“数学”:5,“英语”:4,“计算机”:3,“历史”:2,“物理”:1]
这是全部30名学生及其成绩:
运行算法后,输出解决方案为:
这是我对原始问题(10万名学生)的完整代码:
import pandas as pd
import numpy as np
import pulp as p
import time
t0=time.time()
demand = [4000, 3000, 2000, 750,250]
weight = [1.9,1.7, 1.5, 1.3, 1.1]
original_data= pd.read_csv('GRADE_100K.csv') #created simple csv file with random scores
data_c=original_data.copy()
data_c.index = np.arange(1, len(data_c)+1)
data_c.columns
data_c=data_c[['STUDENT_ID', 'MATH', 'ENGLISH', 'COMPUTERS', 'HISTORY','PHYSICS']]
#DataFrame Shape
m=data_c.shape[1]
n=data_c.shape[0]
data=[]
sublist=[]
for j in range(0,n):
for i in range(1,m):
sublist.append(data_c.iloc[j,i])
data.append(sublist)
sublist=[]
def _get_num_students(data):
return len(data)
def _get_num_subjects(data):
return len(data[0])
def _get_weighted_data(data, weight):
return [
[a*b for a, b in zip(row, weight)]
for row in data
]
data = _get_weighted_data(data, weight)
num_students = _get_num_students(data)
num_subjects = _get_num_subjects(data)
# Create a LP Minimization problem
Lp_prob = p.LpProblem('Problem', p.LpMaximize)
# Create problem Variables
variables_matrix = [[0 for i in range(num_subjects)] for j in range(num_students)]
for i in range(0, num_students):
for j in range(0, num_subjects):
variables_matrix[i][j] = p.LpVariable(f"X({i+1},{j+1})", 0, 1, cat='Integer')
df_d=pd.DataFrame(data=data)
df_v=pd.DataFrame(data=variables_matrix)
ml=df_d.mul(df_v)
ml['coeff'] = ml.sum(axis=1)
coefficients=ml['coeff'].tolist()
# DEALING WITH TARGET FUNCTION VALUE
suming=0
k=0
sumsum=[]
for z in range(len(coefficients)):
suming +=coefficients[z]
if z % 2000==0:
sumsum.append(suming)
suming=0
if z<2000:
sumsum.append(suming)
sumsuming=0
for s in range(len(sumsum)):
sumsuming=sumsuming+sumsum[s]
Lp_prob += sumsuming
# DEALING WITH the 2 CONSTRAINS
# 1-subject constraints
con1_suming=0
for e in range(num_subjects):
L=df_v.iloc[:,e].to_list()
for t in range(len(L)):
con1_suming +=L[t]
Lp_prob += con1_suming <= demand[e]
con1_suming=0
# 2- students constraints
con2_suming=0
for e in range(num_students):
L=df_v.iloc[e,:].to_list()
for t in range(len(L)):
con2_suming +=L[t]
Lp_prob += con2_suming <= 1
con2_suming=0
print("time taken for TARGET+CONSTRAINS %8.8f seconds" % (time.time()-t0) )
t1=time.time()
status = Lp_prob.solve() # Solver
print("time taken for SOLVER %8.8f seconds" % (time.time()-t1) ) # 632 SECONDS
print(p.LpStatus[status]) # The solution status
print(p.value(Lp_prob.objective))
df_v=pd.DataFrame(data=variables_matrix)
# Printing the final solution
lst=[]
val=[]
for i in range(0, num_students):
lst.append([p.value(variables_matrix[i][j]) for j in range(0, num_subjects)])
val.append([sum([p.value(variables_matrix[i][j]) for j in range(0, num_subjects)]),i])
ones_places=[]
for i in range (0, len(val)):
if val[i][0]==1:
ones_places.append(i+1)
len(ones_places)
data_once=data_c[data_c['STUDENT_ID'].isin(ones_places)]
IDs=[]
for i in range(len(ones_places)):
IDs.append(data_once['STUDENT_ID'].to_list()[i])
course=[]
sub_course=[]
for i in range(len(lst)):
j=0
sub_course='x'
while j<len(lst[i]):
if lst[i][j]==1:
sub_course=j
j=j+1
course.append(sub_course)
coures_ones=[]
for i in range(len(course)):
if course[i]!= 'x':
coures_ones.append(course[i])
# adding the COURSE name to the final table
# NUMBER OF DICTIONARY KEYS based on number of COURSES
col=original_data.columns.values[1:].tolist()
dic = {0:col[0], 1:col[1], 2:col[2], 3:col[3], 4:col[4]}
cc_name=[dic.get(n, n) for n in coures_ones]
one_c=[]
if len(IDs)==len(cc_name):
for i in range(len(IDs)):
one_c.append([IDs[i],cc_name[i]])
prob=[]
if len(IDs)==len(cc_name):
for i in range(len(IDs)):
prob.append([IDs[i],cc_name[i], data_once.iloc[i][one_c[i][1]]])
scoring_table = pd.DataFrame(prob,columns=['STUDENT_ID','COURES','SCORE'])
scoring_table.sort_values(by=['COURES', 'SCORE'], ascending=[False, False], inplace=True)
scoring_table.index = np.arange(1, len(scoring_table)+1)
print(scoring_table)
下面是关于我使用最小成本流的一些想法
我们用一个有向图来模拟这个问题,图中有4层,每一层与下一层完全相连
节点
第一层:一个节点
s
,它将是我们的源第二层:每个学生一个节点
第三层:每个主题一个节点
第四层:OA单节点
t
将成为我们的排水管边缘容量
第一->;第二:所有边的容量为1
第二->;第三:所有边的容量为1
第三->;第四:所有边缘都具有与必须分配给该科目的学生数量相对应的容量
边缘成本
第一->;第二:所有边的成本为0
第二->;第三:记住,这一层中的边缘将学生与主题联系起来。这些课程的费用将与学生在该科目上的加权分数成反比。
cost = -subject_weight*student_subject_score
第三->;第四:所有边的成本为0
然后我们要求从
s
到t
的流量等于我们必须选择的学生数量为什么这样做?
通过将第三层和第四层之间的所有边作为赋值,最小成本流问题的解决方案将与您的问题的解决方案相对应
每个学生最多可以选择一个主题,因为对应的节点只有一个传入边缘
每门学科都有确切的所需学生人数,因为我们必须为这门学科选择的学生人数对应着离校能力,我们必须充分利用这些边缘的能力,否则我们无法满足流动需求
MCF问题的最小解对应于问题的最大解,因为成本对应于它们给出的值
当您要求用python提供解决方案时,我用ortools实现了最小成本流问题。在我的colab笔记本上找到一个解决方案不到一秒钟。“长时间”需要的是提取溶液。但包括设置和解决方案提取,对于整个100000名学生的问题,我的运行时间仍然不到20秒
代码
在上面的代码中,用下面的列表理解(也不是每次迭代都使用列表查找)替换解决方案提取的for循环,可以显著提高运行时。但是出于可读性的原因,我也将这个旧的解决方案留在这里。这是新的:
下面的输出给出了新的更快实现的运行时
输出
请指出我可能犯的任何错误
我希望这有帮助
我想你已经接近了。这是一个相当标准的整数线性规划(ILP)分配问题。这会有点慢,因为问题的结构
你没有在你的帖子中说设置的故障是什么&;解决时间很短。我看到你在读一个文件并使用熊猫。我认为熊猫在优化问题上会很快变得笨重,但这只是个人喜好
我使用
cbc
解算器在pyomo
中对您的问题进行了编码,我很确定这与pulp
用于比较的解算器相同。(见下文)。我认为你有两个约束和一个双索引二进制决策变量如果我把它减少到10万名学生(没有松弛…只是一对一配对),它会在14秒内解决,以便进行比较。我的设置是一个5年的iMac,带有大量ram
与池中的10万名学生一起运行,在调用解算器之前,它将在大约25分钟内以10秒的“设置”时间解算。所以我不确定为什么你的编码要花2小时。如果你能分解你的求解时间,那会有帮助。其余的应该是微不足道的。我没有在输出中做太多的改动,但OBJ函数值980K似乎是合理的
其他想法:
如果您可以正确配置解算器选项,并将mip间距设置为0.05左右,那么如果您可以接受稍微非最佳的解决方案,应该可以加快速度。我只在像古罗比这样的付费解算器的解算器选项上有过不错的运气。我没有太多的运气与使用免费解决方案,YMMV
输出
相关问题 更多 >
编程相关推荐