线性规划最大值优化

2024-09-30 23:39:21 发布

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

我试图找到最大化我的和值的最佳组合,但它必须在两个特定的约束下,因此我假设线性规划将是最合适的

问题是这样的: 一些教育界活动希望聚集世界上最聪明的青少年学生。 每个州都对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名学生及其成绩:

enter image description here

运行算法后,输出解决方案为:

enter image description here

这是我对原始问题(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)

Tags: infordatalentimeonesrange学生
2条回答

下面是关于我使用最小成本流的一些想法

我们用一个有向图来模拟这个问题,图中有4层,每一层与下一层完全相连

节点

  • 第一层:一个节点s,它将是我们的源

  • 第二层:每个学生一个节点

  • 第三层:每个主题一个节点

  • 第四层:OA单节点t将成为我们的排水管

边缘容量

  • 第一->;第二:所有边的容量为1

  • 第二->;第三:所有边的容量为1

  • 第三->;第四:所有边缘都具有与必须分配给该科目的学生数量相对应的容量

边缘成本

  • 第一->;第二:所有边的成本为0

  • 第二->;第三:记住,这一层中的边缘将学生与主题联系起来。这些课程的费用将与学生在该科目上的加权分数成反比。 cost = -subject_weight*student_subject_score

  • 第三->;第四:所有边的成本为0

然后我们要求从st的流量等于我们必须选择的学生数量

为什么这样做?

通过将第三层和第四层之间的所有边作为赋值,最小成本流问题的解决方案将与您的问题的解决方案相对应

每个学生最多可以选择一个主题,因为对应的节点只有一个传入边缘

每门学科都有确切的所需学生人数,因为我们必须为这门学科选择的学生人数对应着离校能力,我们必须充分利用这些边缘的能力,否则我们无法满足流动需求

MCF问题的最小解对应于问题的最大解,因为成本对应于它们给出的值

当您要求用python提供解决方案时,我用ortools实现了最小成本流问题。在我的colab笔记本上找到一个解决方案不到一秒钟。“长时间”需要的是提取溶液。但包括设置和解决方案提取,对于整个100000名学生的问题,我的运行时间仍然不到20秒

代码

# imports
from ortools.graph import pywrapgraph
import numpy as np
import pandas as pd
import time
t_start = time.time()

# setting given problem parameters
num_students = 100000
subjects = ['MATH', 'ENGLISH', 'COMPUTERS', 'HISTORY','PHYSICS']
num_subjects = len(subjects)
demand = [4000, 3000, 2000, 750, 250]
weight = [1.9,1.7, 1.5, 1.3, 1.1]

# generating student scores
student_scores_raw = np.random.randint(101, size=(num_students, num_subjects))

# setting up graph nodes
source_nodes = [0]
student_nodes = list(range(1, num_students+1))
subject_nodes = list(range(num_students+1, num_subjects+num_students+1))
drain_nodes = [num_students+num_subjects+1]

# setting up the min cost flow edges
start_nodes = [int(c) for c in (source_nodes*num_students + [i for i in student_nodes for _ in subject_nodes] + subject_nodes)]
end_nodes   = [int(c) for c in (student_nodes + subject_nodes*num_students + drain_nodes*num_subjects)]
capacities  = [int(c) for c in ([1]*num_students + [1]*num_students*num_subjects + demand)]
unit_costs  = [int(c) for c in ([0.]*num_students + list((-student_scores_raw*np.array(weight)*10).flatten()) + [0.]*num_subjects)]
assert len(start_nodes) == len(end_nodes) == len(capacities) == len(unit_costs)

# setting up the min cost flow demands
supplies = [sum(demand)] + [0]*(num_students + num_subjects) + [-sum(demand)]

# initialize the min cost flow problem instance
min_cost_flow = pywrapgraph.SimpleMinCostFlow()
for i in range(0, len(start_nodes)):
  min_cost_flow.AddArcWithCapacityAndUnitCost(start_nodes[i], end_nodes[i], capacities[i], unit_costs[i])
for i in range(0, len(supplies)):
  min_cost_flow.SetNodeSupply(i, supplies[i])

# solve the problem
t_solver_start = time.time()
if min_cost_flow.Solve() == min_cost_flow.OPTIMAL:
  print('Best Value:', -min_cost_flow.OptimalCost()/10)
  print('Solver time:', str(time.time()-t_solver_start)+'s')
  print('Total Runtime until solution:', str(time.time()-t_start)+'s')
  
  #extracting the solution
  solution = []
  for i in range(min_cost_flow.NumArcs()):
    if min_cost_flow.Flow(i) > 0 and min_cost_flow.Tail(i) in student_nodes:
      student_id = min_cost_flow.Tail(i)-1
      subject_id = min_cost_flow.Head(i)-1-num_students
      solution.append([student_id, subjects[subject_id], student_scores_raw[student_id, subject_id]])
  assert(len(solution) == sum(demand))

  solution = pd.DataFrame(solution, columns = ['student_id', 'subject', 'score'])
  print(solution.head())
else:
  print('There was an issue with the min cost flow input.')

print('Total Runtime:', str(time.time()-t_start)+'s')

在上面的代码中,用下面的列表理解(也不是每次迭代都使用列表查找)替换解决方案提取的for循环,可以显著提高运行时。但是出于可读性的原因,我也将这个旧的解决方案留在这里。这是新的:

  solution = [[min_cost_flow.Tail(i)-1, 
               subjects[min_cost_flow.Head(i)-1-num_students], 
               student_scores_raw[min_cost_flow.Tail(i)-1, min_cost_flow.Head(i)-1-num_students]
               ]
              for i in range(min_cost_flow.NumArcs())
              if (min_cost_flow.Flow(i) > 0 and 
                  min_cost_flow.Tail(i) <= num_students and 
                  min_cost_flow.Tail(i)>0)
              ]

下面的输出给出了新的更快实现的运行时

输出

Best Value: 1675250.7
Solver time: 0.542395830154419s
Total Runtime until solution: 1.4248979091644287s
   student_id    subject  score
0           3    ENGLISH     99
1           5       MATH     98
2          17  COMPUTERS    100
3          22  COMPUTERS    100
4          33    ENGLISH    100
Total Runtime: 1.752336025238037s

请指出我可能犯的任何错误

我希望这有帮助

我想你已经接近了。这是一个相当标准的整数线性规划(ILP)分配问题。这会有点慢,因为问题的结构

你没有在你的帖子中说设置的故障是什么&;解决时间很短。我看到你在读一个文件并使用熊猫。我认为熊猫在优化问题上会很快变得笨重,但这只是个人喜好

我使用cbc解算器在pyomo中对您的问题进行了编码,我很确定这与pulp用于比较的解算器相同。(见下文)。我认为你有两个约束和一个双索引二进制决策变量

如果我把它减少到10万名学生(没有松弛…只是一对一配对),它会在14秒内解决,以便进行比较。我的设置是一个5年的iMac,带有大量ram

与池中的10万名学生一起运行,在调用解算器之前,它将在大约25分钟内以10秒的“设置”时间解算。所以我不确定为什么你的编码要花2小时。如果你能分解你的求解时间,那会有帮助。其余的应该是微不足道的。我没有在输出中做太多的改动,但OBJ函数值980K似乎是合理的

其他想法:

如果您可以正确配置解算器选项,并将mip间距设置为0.05左右,那么如果您可以接受稍微非最佳的解决方案,应该可以加快速度。我只在像古罗比这样的付费解算器的解算器选项上有过不错的运气。我没有太多的运气与使用免费解决方案,YMMV

import pyomo.environ as pyo
from random import randint
from time import time

# start setup clock
tic = time()
# exam types
subjects = ['Math', 'English', 'Computers', 'History', 'Physics']

# make set of students...
num_students = 100_000
students = [f'student_{s}' for s in range(num_students)]

# make 100K fake scores in "flat" format
student_scores = { (student, subj) : randint(0,100) 
                        for student in students
                        for subj in subjects}

assignments = { 'Math': 4000, 'English': 3000, 'Computers': 2000, 'History': 750, 'Physics': 250}
weights = {'Math': 1.9, 'English': 1.7, 'Computers': 1.5, 'History': 1.3, 'Physics': 1.1}

# Set up model
m = pyo.ConcreteModel('exam assignments')

# Sets
m.subjects = pyo.Set(initialize=subjects)
m.students = pyo.Set(initialize=students)

# Parameters
m.assignments = pyo.Param(m.subjects, initialize=assignments)
m.weights =     pyo.Param(m.subjects, initialize=weights)
m.scores =      pyo.Param(m.students, m.subjects, initialize=student_scores)

# Variables
m.x = pyo.Var(m.students, m.subjects, within=pyo.Binary)  # binary selection of pairing student to test

# Objective
m.OBJ = pyo.Objective(expr=sum(m.scores[student, subject] * m.x[student, subject] 
                for student in m.students
                for subject in m.subjects), sense=pyo.maximize)

### Constraints ###
# fill all assignments
def fill_assignments(m, subject):
    return sum(m.x[student, subject] for student in m.students) == assignments[subject]
m.C1 = pyo.Constraint(m.subjects, rule=fill_assignments)

# use each student at most 1 time
def limit_student(m, student):
    return sum(m.x[student, subject] for subject in m.subjects) <= 1
m.C2 = pyo.Constraint(m.students, rule=limit_student)

toc = time()
print (f'setup time: {toc-tic:0.3f}')
tic = toc

# solve it..
solver = pyo.SolverFactory('cbc')
solution = solver.solve(m)
print(solution)
toc = time()
print (f'solve time: {toc-tic:0.3f}')

输出

setup time: 10.835

Problem: 
- Name: unknown
  Lower bound: -989790.0
  Upper bound: -989790.0
  Number of objectives: 1
  Number of constraints: 100005
  Number of variables: 500000
  Number of binary variables: 500000
  Number of integer variables: 500000
  Number of nonzeros: 495094
  Sense: maximize
Solver: 
- Status: ok
  User time: -1.0
  System time: 1521.55
  Wallclock time: 1533.36
  Termination condition: optimal
  Termination message: Model was solved to optimality (subject to tolerances), and an optimal solution is available.
  Statistics: 
    Branch and bound: 
      Number of bounded subproblems: 0
      Number of created subproblems: 0
    Black box: 
      Number of iterations: 0
  Error rc: 0
  Time: 1533.8383190631866
Solution: 
- number of solutions: 0
  number of solutions displayed: 0

solve time: 1550.528

相关问题 更多 >