我有一个熊猫数据框,其中包含三个ID之间的现有关系:manager_id
,employee_id
,project_id
。通过更改哪些经理来管理哪些员工,我需要找出每个项目中经理人数最少的情况。
manager_id
可以有多个employee_ids
employee_id
都有一个manager_id
employee_id
可以有一个或多个project_ids
project_id
可以有一个或多个employee_id
唯一的进一步条件是,每位经理最多只能雇用特定数量的员工(比目前多10%)。
我最初的想法是在所有项目和经理之间采用笛卡尔积,以找到所有可能的组合;将结果限制在适合最大值的情况下;然后找到行数最少的方案。试图通过以下方式实现这一目标:
import pandas as pd
from itertools import permutations
relationship_df = pd.DataFrame({'manager_id': [1,1,2,2,2,2],
'employee_id': [1,2,3,4,5,6],
'project_id': [1,2,1,2,None,None],}
)
# Create a dictionary with all the projects and number of employees working on them
project_df = relationship_df.groupby('project_id')['employee_id'].count()
project_df = project_df.sort_values(ascending = False)
project_dict = project_df.to_dict()
# Create a dictionary with all the managers and number of employees working for them
manager_df = relationship_df.groupby('manager_id')['employee_id'].count()
manager_df = manager_df.sort_values(ascending = False)
manager_dict = manager_df.to_dict()
# Create all the possible permutation orders for the projects:
project_perms = list(permutations(project_dict))
# For each possible permutation, assign the project to the manager until their team is full:
for perm_num, perm in enumerate(project_perms):
for manager, team_size in manager_dict.items():
new_teams = {}
for project in perm:
project_size = project_dict[project]
if sum(new_teams.values()) <= 1.1 * team_size:
new_teams[project] = project_size
是否有更好的方法来实现此目标或应该使用的特定程序包/算法?